2016-11-09 02:01:56 +01:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2016-03-16 12:48:33 +02:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package bind
|
|
|
|
|
|
|
|
import (
|
2017-03-22 18:20:33 +01:00
|
|
|
"context"
|
2016-03-16 12:48:33 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2021-07-27 16:22:21 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2016-03-16 12:48:33 +02:00
|
|
|
|
2016-08-22 14:01:28 +02:00
|
|
|
"github.com/ethereum/go-ethereum"
|
2016-03-16 12:48:33 +02:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2016-03-17 19:27:37 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-01-05 12:39:24 +02:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2016-03-16 12:48:33 +02:00
|
|
|
)
|
|
|
|
|
2022-08-25 04:59:50 -04:00
|
|
|
const basefeeWiggleMultiplier = 2
|
|
|
|
|
2023-03-31 02:35:29 -04:00
|
|
|
var (
|
|
|
|
errNoEventSignature = errors.New("no event signature")
|
|
|
|
errEventSignatureMismatch = errors.New("event signature mismatch")
|
|
|
|
)
|
|
|
|
|
2016-03-16 12:48:33 +02:00
|
|
|
// SignerFn is a signer function callback when a contract requires a method to
|
|
|
|
// sign the transaction before submission.
|
2020-12-08 14:44:56 +01:00
|
|
|
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
|
2016-03-16 12:48:33 +02:00
|
|
|
|
2016-03-17 19:27:37 +02:00
|
|
|
// CallOpts is the collection of options to fine tune a contract call request.
|
|
|
|
type CallOpts struct {
|
2019-01-03 11:54:24 +00:00
|
|
|
Pending bool // Whether to operate on the pending state or the last known one
|
|
|
|
From common.Address // Optional the sender address, otherwise the first account is used
|
|
|
|
BlockNumber *big.Int // Optional the block number on which the call should be performed
|
2023-10-17 22:34:01 +10:00
|
|
|
BlockHash common.Hash // Optional the block hash on which the call should be performed
|
2019-01-03 11:54:24 +00:00
|
|
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransactOpts is the collection of authorization data required to create a
|
|
|
|
// valid Ethereum transaction.
|
|
|
|
type TransactOpts struct {
|
2016-03-21 14:34:49 +02:00
|
|
|
From common.Address // Ethereum account to send the transaction from
|
|
|
|
Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
|
|
|
|
Signer SignerFn // Method to use for signing the transaction (mandatory)
|
2016-03-16 12:48:33 +02:00
|
|
|
|
2024-08-02 02:09:04 +08:00
|
|
|
Value *big.Int // Funds to transfer along the transaction (nil = 0 = no funds)
|
|
|
|
GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
|
|
|
|
GasFeeCap *big.Int // Gas fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
|
|
|
GasTipCap *big.Int // Gas priority fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
|
|
|
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
|
|
|
|
AccessList types.AccessList // Access list to set for the transaction execution (nil = no access list)
|
2015-12-16 04:26:23 +01:00
|
|
|
|
|
|
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
2021-03-19 06:56:10 -04:00
|
|
|
|
|
|
|
NoSend bool // Do all transact steps but do not send the transaction
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 12:39:24 +02:00
|
|
|
// FilterOpts is the collection of options to fine tune filtering for events
|
|
|
|
// within a bound contract.
|
|
|
|
type FilterOpts struct {
|
|
|
|
Start uint64 // Start of the queried range
|
|
|
|
End *uint64 // End of the range (nil = latest)
|
|
|
|
|
|
|
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchOpts is the collection of options to fine tune subscribing for events
|
|
|
|
// within a bound contract.
|
|
|
|
type WatchOpts struct {
|
|
|
|
Start *uint64 // Start of the queried range (nil = latest)
|
|
|
|
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:22:21 +02:00
|
|
|
// MetaData collects all metadata for a bound contract.
|
|
|
|
type MetaData struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
Sigs map[string]string
|
|
|
|
Bin string
|
|
|
|
ABI string
|
|
|
|
ab *abi.ABI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MetaData) GetAbi() (*abi.ABI, error) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if m.ab != nil {
|
|
|
|
return m.ab, nil
|
|
|
|
}
|
|
|
|
if parsed, err := abi.JSON(strings.NewReader(m.ABI)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
m.ab = &parsed
|
|
|
|
}
|
|
|
|
return m.ab, nil
|
|
|
|
}
|
|
|
|
|
2016-03-16 12:48:33 +02:00
|
|
|
// BoundContract is the base wrapper object that reflects a contract on the
|
|
|
|
// Ethereum network. It contains a collection of methods that are used by the
|
|
|
|
// higher level contract bindings to operate.
|
|
|
|
type BoundContract struct {
|
|
|
|
address common.Address // Deployment address of the contract on the Ethereum blockchain
|
|
|
|
abi abi.ABI // Reflect based ABI to access the correct Ethereum methods
|
|
|
|
caller ContractCaller // Read interface to interact with the blockchain
|
|
|
|
transactor ContractTransactor // Write interface to interact with the blockchain
|
2018-01-05 12:39:24 +02:00
|
|
|
filterer ContractFilterer // Event filtering to interact with the blockchain
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBoundContract creates a low level contract interface through which calls
|
|
|
|
// and transactions may be made through.
|
2018-01-05 12:39:24 +02:00
|
|
|
func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor, filterer ContractFilterer) *BoundContract {
|
2016-03-16 12:48:33 +02:00
|
|
|
return &BoundContract{
|
|
|
|
address: address,
|
|
|
|
abi: abi,
|
|
|
|
caller: caller,
|
|
|
|
transactor: transactor,
|
2018-01-05 12:39:24 +02:00
|
|
|
filterer: filterer,
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 19:27:37 +02:00
|
|
|
// DeployContract deploys a contract onto the Ethereum blockchain and binds the
|
|
|
|
// deployment address with a Go wrapper.
|
|
|
|
func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) {
|
|
|
|
// Otherwise try to deploy the contract
|
2018-01-05 12:39:24 +02:00
|
|
|
c := NewBoundContract(common.Address{}, abi, backend, backend, backend)
|
2016-03-17 19:27:37 +02:00
|
|
|
|
|
|
|
input, err := c.abi.Pack("", params...)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, nil, nil, err
|
|
|
|
}
|
|
|
|
tx, err := c.transact(opts, nil, append(bytecode, input...))
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, nil, nil, err
|
|
|
|
}
|
2016-03-21 14:34:49 +02:00
|
|
|
c.address = crypto.CreateAddress(opts.From, tx.Nonce())
|
2016-03-17 19:27:37 +02:00
|
|
|
return c.address, tx, c, nil
|
|
|
|
}
|
|
|
|
|
2016-03-16 12:48:33 +02:00
|
|
|
// Call invokes the (constant) contract method with params as input values and
|
|
|
|
// sets the output to result. The result type might be a single field for simple
|
|
|
|
// returns, a slice of interfaces for anonymous returns and a struct for named
|
|
|
|
// returns.
|
2020-09-28 14:10:26 +02:00
|
|
|
func (c *BoundContract) Call(opts *CallOpts, results *[]interface{}, method string, params ...interface{}) error {
|
2016-03-17 19:27:37 +02:00
|
|
|
// Don't crash on a lazy user
|
|
|
|
if opts == nil {
|
|
|
|
opts = new(CallOpts)
|
|
|
|
}
|
2020-09-28 14:10:26 +02:00
|
|
|
if results == nil {
|
|
|
|
results = new([]interface{})
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
// Pack the input, call and unpack the results
|
2016-03-16 12:48:33 +02:00
|
|
|
input, err := c.abi.Pack(method, params...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-22 14:01:28 +02:00
|
|
|
var (
|
2017-03-16 12:24:22 +01:00
|
|
|
msg = ethereum.CallMsg{From: opts.From, To: &c.address, Data: input}
|
2016-08-22 14:01:28 +02:00
|
|
|
ctx = ensureContext(opts.Context)
|
|
|
|
code []byte
|
|
|
|
output []byte
|
|
|
|
)
|
|
|
|
if opts.Pending {
|
|
|
|
pb, ok := c.caller.(PendingContractCaller)
|
|
|
|
if !ok {
|
|
|
|
return ErrNoPendingState
|
|
|
|
}
|
|
|
|
output, err = pb.PendingCallContract(ctx, msg)
|
2022-04-12 17:36:29 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(output) == 0 {
|
2016-08-22 14:01:28 +02:00
|
|
|
// Make sure we have a contract to operate on, and bail out otherwise.
|
|
|
|
if code, err = pb.PendingCodeAt(ctx, c.address); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(code) == 0 {
|
|
|
|
return ErrNoCode
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 22:34:01 +10:00
|
|
|
} else if opts.BlockHash != (common.Hash{}) {
|
|
|
|
bh, ok := c.caller.(BlockHashContractCaller)
|
|
|
|
if !ok {
|
|
|
|
return ErrNoBlockHashState
|
|
|
|
}
|
|
|
|
output, err = bh.CallContractAtHash(ctx, msg, opts.BlockHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(output) == 0 {
|
|
|
|
// Make sure we have a contract to operate on, and bail out otherwise.
|
|
|
|
if code, err = bh.CodeAtHash(ctx, c.address, opts.BlockHash); err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(code) == 0 {
|
|
|
|
return ErrNoCode
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 14:01:28 +02:00
|
|
|
} else {
|
2019-01-03 11:54:24 +00:00
|
|
|
output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber)
|
2020-10-27 17:22:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(output) == 0 {
|
2016-08-22 14:01:28 +02:00
|
|
|
// Make sure we have a contract to operate on, and bail out otherwise.
|
2019-01-03 11:54:24 +00:00
|
|
|
if code, err = c.caller.CodeAt(ctx, c.address, opts.BlockNumber); err != nil {
|
2016-08-22 14:01:28 +02:00
|
|
|
return err
|
|
|
|
} else if len(code) == 0 {
|
|
|
|
return ErrNoCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 14:10:26 +02:00
|
|
|
|
|
|
|
if len(*results) == 0 {
|
|
|
|
res, err := c.abi.Unpack(method, output)
|
|
|
|
*results = res
|
2016-03-16 12:48:33 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-09-28 14:10:26 +02:00
|
|
|
res := *results
|
|
|
|
return c.abi.UnpackIntoInterface(res[0], method, output)
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:02:43 +03:00
|
|
|
// Transact invokes the (paid) contract method with params as input values.
|
2016-03-17 19:27:37 +02:00
|
|
|
func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
|
|
|
|
// Otherwise pack up the parameters and invoke the contract
|
2016-03-16 12:48:33 +02:00
|
|
|
input, err := c.abi.Pack(method, params...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-14 00:33:46 +02:00
|
|
|
// todo(rjl493456442) check whether the method is payable or not,
|
2020-04-15 15:23:58 +08:00
|
|
|
// reject invalid transaction at the first place
|
2016-03-17 19:27:37 +02:00
|
|
|
return c.transact(opts, &c.address, input)
|
|
|
|
}
|
|
|
|
|
2020-04-15 15:23:58 +08:00
|
|
|
// RawTransact initiates a transaction with the given raw calldata as the input.
|
2020-09-20 11:43:57 +03:00
|
|
|
// It's usually used to initiate transactions for invoking **Fallback** function.
|
2020-04-15 15:23:58 +08:00
|
|
|
func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) {
|
2023-12-14 00:33:46 +02:00
|
|
|
// todo(rjl493456442) check whether the method is payable or not,
|
2020-04-15 15:23:58 +08:00
|
|
|
// reject invalid transaction at the first place
|
|
|
|
return c.transact(opts, &c.address, calldata)
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:02:43 +03:00
|
|
|
// Transfer initiates a plain transaction to move funds to the contract, calling
|
|
|
|
// its default method if one is available.
|
|
|
|
func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) {
|
2020-04-15 15:23:58 +08:00
|
|
|
// todo(rjl493456442) check the payable fallback or receive is defined
|
|
|
|
// or not, reject invalid transaction at the first place
|
2016-04-01 13:02:43 +03:00
|
|
|
return c.transact(opts, &c.address, nil)
|
|
|
|
}
|
|
|
|
|
2021-10-13 22:59:11 +02:00
|
|
|
func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Address, input []byte, head *types.Header) (*types.Transaction, error) {
|
|
|
|
// Normalize value
|
2016-03-16 12:48:33 +02:00
|
|
|
value := opts.Value
|
|
|
|
if value == nil {
|
|
|
|
value = new(big.Int)
|
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
// Estimate TipCap
|
|
|
|
gasTipCap := opts.GasTipCap
|
|
|
|
if gasTipCap == nil {
|
|
|
|
tip, err := c.transactor.SuggestGasTipCap(ensureContext(opts.Context))
|
2016-03-16 12:48:33 +02:00
|
|
|
if err != nil {
|
2021-10-13 22:59:11 +02:00
|
|
|
return nil, err
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
gasTipCap = tip
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
// Estimate FeeCap
|
|
|
|
gasFeeCap := opts.GasFeeCap
|
|
|
|
if gasFeeCap == nil {
|
|
|
|
gasFeeCap = new(big.Int).Add(
|
|
|
|
gasTipCap,
|
2022-08-25 04:59:50 -04:00
|
|
|
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)),
|
2021-10-13 22:59:11 +02:00
|
|
|
)
|
2021-06-15 13:56:14 +03:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
if gasFeeCap.Cmp(gasTipCap) < 0 {
|
|
|
|
return nil, fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap)
|
|
|
|
}
|
|
|
|
// Estimate GasLimit
|
|
|
|
gasLimit := opts.GasLimit
|
|
|
|
if opts.GasLimit == 0 {
|
|
|
|
var err error
|
|
|
|
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// create the transaction
|
|
|
|
nonce, err := c.getNonce(opts)
|
2021-06-15 13:56:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
baseTx := &types.DynamicFeeTx{
|
2024-08-02 02:09:04 +08:00
|
|
|
To: contract,
|
|
|
|
Nonce: nonce,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
Gas: gasLimit,
|
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
AccessList: opts.AccessList,
|
2021-10-13 22:59:11 +02:00
|
|
|
}
|
|
|
|
return types.NewTx(baseTx), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
|
2024-08-02 02:09:04 +08:00
|
|
|
if opts.GasFeeCap != nil || opts.GasTipCap != nil || opts.AccessList != nil {
|
|
|
|
return nil, errors.New("maxFeePerGas or maxPriorityFeePerGas or accessList specified but london is not active yet")
|
2021-10-13 22:59:11 +02:00
|
|
|
}
|
|
|
|
// Normalize value
|
|
|
|
value := opts.Value
|
|
|
|
if value == nil {
|
|
|
|
value = new(big.Int)
|
|
|
|
}
|
|
|
|
// Estimate GasPrice
|
|
|
|
gasPrice := opts.GasPrice
|
|
|
|
if gasPrice == nil {
|
|
|
|
price, err := c.transactor.SuggestGasPrice(ensureContext(opts.Context))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
gasPrice = price
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
// Estimate GasLimit
|
2016-03-16 12:48:33 +02:00
|
|
|
gasLimit := opts.GasLimit
|
2021-10-13 22:59:11 +02:00
|
|
|
if opts.GasLimit == 0 {
|
|
|
|
var err error
|
|
|
|
gasLimit, err = c.estimateGasLimit(opts, contract, input, gasPrice, nil, nil, value)
|
2016-03-16 12:48:33 +02:00
|
|
|
if err != nil {
|
2021-10-13 22:59:11 +02:00
|
|
|
return nil, err
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
// create the transaction
|
|
|
|
nonce, err := c.getNonce(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
baseTx := &types.LegacyTx{
|
|
|
|
To: contract,
|
|
|
|
Nonce: nonce,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
Gas: gasLimit,
|
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
return types.NewTx(baseTx), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value *big.Int) (uint64, error) {
|
|
|
|
if contract != nil {
|
|
|
|
// Gas estimation cannot succeed without code for method invocations.
|
|
|
|
if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if len(code) == 0 {
|
|
|
|
return 0, ErrNoCode
|
2021-06-15 13:56:14 +03:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
}
|
|
|
|
msg := ethereum.CallMsg{
|
|
|
|
From: opts.From,
|
|
|
|
To: contract,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
Value: value,
|
|
|
|
Data: input,
|
|
|
|
}
|
|
|
|
return c.transactor.EstimateGas(ensureContext(opts.Context), msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BoundContract) getNonce(opts *TransactOpts) (uint64, error) {
|
|
|
|
if opts.Nonce == nil {
|
|
|
|
return c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From)
|
2016-03-17 19:27:37 +02:00
|
|
|
} else {
|
2021-10-13 22:59:11 +02:00
|
|
|
return opts.Nonce.Uint64(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// transact executes an actual transaction invocation, first deriving any missing
|
|
|
|
// authorization fields, and then scheduling the transaction for execution.
|
|
|
|
func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
|
|
|
|
if opts.GasPrice != nil && (opts.GasFeeCap != nil || opts.GasTipCap != nil) {
|
|
|
|
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
|
|
|
}
|
|
|
|
// Create the transaction
|
|
|
|
var (
|
|
|
|
rawTx *types.Transaction
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if opts.GasPrice != nil {
|
|
|
|
rawTx, err = c.createLegacyTx(opts, contract, input)
|
2022-11-05 00:33:34 +03:30
|
|
|
} else if opts.GasFeeCap != nil && opts.GasTipCap != nil {
|
|
|
|
rawTx, err = c.createDynamicTx(opts, contract, input, nil)
|
2021-10-13 22:59:11 +02:00
|
|
|
} else {
|
|
|
|
// Only query for basefee if gasPrice not specified
|
2021-10-21 16:40:35 +07:00
|
|
|
if head, errHead := c.transactor.HeaderByNumber(ensureContext(opts.Context), nil); errHead != nil {
|
2021-10-13 22:59:11 +02:00
|
|
|
return nil, errHead
|
|
|
|
} else if head.BaseFee != nil {
|
|
|
|
rawTx, err = c.createDynamicTx(opts, contract, input, head)
|
|
|
|
} else {
|
|
|
|
// Chain is not London ready -> use legacy transaction
|
|
|
|
rawTx, err = c.createLegacyTx(opts, contract, input)
|
2021-06-15 13:56:14 +03:00
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
2021-10-13 22:59:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Sign the transaction and schedule it for execution
|
2016-03-16 12:48:33 +02:00
|
|
|
if opts.Signer == nil {
|
|
|
|
return nil, errors.New("no signer to authorize the transaction with")
|
|
|
|
}
|
2020-12-08 14:44:56 +01:00
|
|
|
signedTx, err := opts.Signer(opts.From, rawTx)
|
2016-03-16 12:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-19 06:56:10 -04:00
|
|
|
if opts.NoSend {
|
|
|
|
return signedTx, nil
|
|
|
|
}
|
2016-08-22 14:01:28 +02:00
|
|
|
if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
|
2016-03-16 12:48:33 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return signedTx, nil
|
|
|
|
}
|
2016-08-22 14:01:28 +02:00
|
|
|
|
2018-01-05 12:39:24 +02:00
|
|
|
// FilterLogs filters contract logs for past blocks, returning the necessary
|
|
|
|
// channels to construct a strongly typed bound iterator on top of them.
|
|
|
|
func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
|
|
|
|
// Don't crash on a lazy user
|
|
|
|
if opts == nil {
|
|
|
|
opts = new(FilterOpts)
|
|
|
|
}
|
|
|
|
// Append the event selector to the query parameters and construct the topic set
|
2020-04-20 09:01:04 +02:00
|
|
|
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
|
2018-01-05 12:39:24 +02:00
|
|
|
|
2020-05-12 12:21:40 +02:00
|
|
|
topics, err := abi.MakeTopics(query...)
|
2018-01-05 12:39:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Start the background filtering
|
|
|
|
logs := make(chan types.Log, 128)
|
|
|
|
|
|
|
|
config := ethereum.FilterQuery{
|
|
|
|
Addresses: []common.Address{c.address},
|
|
|
|
Topics: topics,
|
|
|
|
FromBlock: new(big.Int).SetUint64(opts.Start),
|
|
|
|
}
|
|
|
|
if opts.End != nil {
|
|
|
|
config.ToBlock = new(big.Int).SetUint64(*opts.End)
|
|
|
|
}
|
|
|
|
/* TODO(karalabe): Replace the rest of the method below with this when supported
|
|
|
|
sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs)
|
|
|
|
*/
|
|
|
|
buff, err := c.filterer.FilterLogs(ensureContext(opts.Context), config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-03-15 17:46:22 +08:00
|
|
|
sub := event.NewSubscription(func(quit <-chan struct{}) error {
|
2018-01-05 12:39:24 +02:00
|
|
|
for _, log := range buff {
|
|
|
|
select {
|
|
|
|
case logs <- log:
|
|
|
|
case <-quit:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2024-03-15 17:46:22 +08:00
|
|
|
})
|
2018-01-05 12:39:24 +02:00
|
|
|
|
|
|
|
return logs, sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchLogs filters subscribes to contract logs for future blocks, returning a
|
|
|
|
// subscription object that can be used to tear down the watcher.
|
|
|
|
func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
|
|
|
|
// Don't crash on a lazy user
|
|
|
|
if opts == nil {
|
|
|
|
opts = new(WatchOpts)
|
|
|
|
}
|
|
|
|
// Append the event selector to the query parameters and construct the topic set
|
2020-04-20 09:01:04 +02:00
|
|
|
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
|
2018-01-05 12:39:24 +02:00
|
|
|
|
2020-05-12 12:21:40 +02:00
|
|
|
topics, err := abi.MakeTopics(query...)
|
2018-01-05 12:39:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Start the background filtering
|
|
|
|
logs := make(chan types.Log, 128)
|
|
|
|
|
|
|
|
config := ethereum.FilterQuery{
|
|
|
|
Addresses: []common.Address{c.address},
|
|
|
|
Topics: topics,
|
|
|
|
}
|
|
|
|
if opts.Start != nil {
|
|
|
|
config.FromBlock = new(big.Int).SetUint64(*opts.Start)
|
|
|
|
}
|
|
|
|
sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return logs, sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackLog unpacks a retrieved log into the provided output structure.
|
|
|
|
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
|
2023-03-31 02:35:29 -04:00
|
|
|
// Anonymous events are not supported.
|
|
|
|
if len(log.Topics) == 0 {
|
|
|
|
return errNoEventSignature
|
|
|
|
}
|
2021-09-28 18:56:03 +08:00
|
|
|
if log.Topics[0] != c.abi.Events[event].ID {
|
2023-03-31 02:35:29 -04:00
|
|
|
return errEventSignatureMismatch
|
2021-09-28 18:56:03 +08:00
|
|
|
}
|
2018-01-05 12:39:24 +02:00
|
|
|
if len(log.Data) > 0 {
|
2020-09-28 14:10:26 +02:00
|
|
|
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
|
2018-01-05 12:39:24 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var indexed abi.Arguments
|
|
|
|
for _, arg := range c.abi.Events[event].Inputs {
|
|
|
|
if arg.Indexed {
|
|
|
|
indexed = append(indexed, arg)
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 12:21:40 +02:00
|
|
|
return abi.ParseTopics(out, indexed, log.Topics[1:])
|
2018-01-05 12:39:24 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 08:42:59 -05:00
|
|
|
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
|
|
|
|
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
|
2023-03-31 02:35:29 -04:00
|
|
|
// Anonymous events are not supported.
|
|
|
|
if len(log.Topics) == 0 {
|
|
|
|
return errNoEventSignature
|
|
|
|
}
|
2021-09-28 18:56:03 +08:00
|
|
|
if log.Topics[0] != c.abi.Events[event].ID {
|
2023-03-31 02:35:29 -04:00
|
|
|
return errEventSignatureMismatch
|
2021-09-28 18:56:03 +08:00
|
|
|
}
|
2019-04-01 08:42:59 -05:00
|
|
|
if len(log.Data) > 0 {
|
|
|
|
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var indexed abi.Arguments
|
|
|
|
for _, arg := range c.abi.Events[event].Inputs {
|
|
|
|
if arg.Indexed {
|
|
|
|
indexed = append(indexed, arg)
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 12:21:40 +02:00
|
|
|
return abi.ParseTopicsIntoMap(out, indexed, log.Topics[1:])
|
2019-04-01 08:42:59 -05:00
|
|
|
}
|
|
|
|
|
2018-01-05 12:39:24 +02:00
|
|
|
// ensureContext is a helper method to ensure a context is not nil, even if the
|
|
|
|
// user specified it as such.
|
2016-08-22 14:01:28 +02:00
|
|
|
func ensureContext(ctx context.Context) context.Context {
|
|
|
|
if ctx == nil {
|
2021-06-22 11:06:34 +02:00
|
|
|
return context.Background()
|
2016-08-22 14:01:28 +02:00
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|