123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- // Copyright 2015 The go-ethereum Authors
- // 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 (
- "context"
- "errors"
- "fmt"
- "math/big"
- "github.com/ethereum/go-ethereum"
- "github.com/ethereum/go-ethereum/accounts/abi"
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/core"
- "github.com/ethereum/go-ethereum/core/types"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/event"
- )
- // SignerFn is a signer function callback when a contract requires a method to
- // sign the transaction before submission.
- type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
- // Quorum
- //
- // Additional arguments in order to support transaction privacy
- type PrivateTxArgs struct {
- PrivateFor []string `json:"privateFor"`
- }
- // CallOpts is the collection of options to fine tune a contract call request.
- type CallOpts struct {
- 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
- Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
- }
- // TransactOpts is the collection of authorization data required to create a
- // valid Ethereum transaction.
- type TransactOpts struct {
- 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)
- 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)
- GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
- Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
- NoSend bool // Do all transact steps but do not send the transaction
- // Quorum
- PrivateFrom string // The public key of the Tessera/Constellation identity to send this tx from.
- PrivateFor []string // The public keys of the Tessera/Constellation identities this tx is intended for.
- IsUsingPrivacyPrecompile bool
- }
- // 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)
- }
- // 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
- filterer ContractFilterer // Event filtering to interact with the blockchain
- }
- // NewBoundContract creates a low level contract interface through which calls
- // and transactions may be made through.
- func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor, filterer ContractFilterer) *BoundContract {
- return &BoundContract{
- address: address,
- abi: abi,
- caller: caller,
- transactor: transactor,
- filterer: filterer,
- }
- }
- // 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
- c := NewBoundContract(common.Address{}, abi, backend, backend, backend)
- 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
- }
- c.address = crypto.CreateAddress(opts.From, tx.Nonce())
- return c.address, tx, c, nil
- }
- // 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.
- func (c *BoundContract) Call(opts *CallOpts, results *[]interface{}, method string, params ...interface{}) error {
- // Don't crash on a lazy user
- if opts == nil {
- opts = new(CallOpts)
- }
- if results == nil {
- results = new([]interface{})
- }
- // Pack the input, call and unpack the results
- input, err := c.abi.Pack(method, params...)
- if err != nil {
- return err
- }
- var (
- msg = ethereum.CallMsg{From: opts.From, To: &c.address, Data: input}
- 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)
- if err == nil && len(output) == 0 {
- // 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
- }
- }
- } else {
- output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber)
- 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 = c.caller.CodeAt(ctx, c.address, opts.BlockNumber); err != nil {
- return err
- } else if len(code) == 0 {
- return ErrNoCode
- }
- }
- }
- if len(*results) == 0 {
- res, err := c.abi.Unpack(method, output)
- *results = res
- return err
- }
- res := *results
- return c.abi.UnpackIntoInterface(res[0], method, output)
- }
- // Transact invokes the (paid) contract method with params as input values.
- func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
- // Otherwise pack up the parameters and invoke the contract
- input, err := c.abi.Pack(method, params...)
- if err != nil {
- return nil, err
- }
- // todo(rjl493456442) check the method is payable or not,
- // reject invalid transaction at the first place
- return c.transact(opts, &c.address, input)
- }
- // RawTransact initiates a transaction with the given raw calldata as the input.
- // It's usually used to initiate transactions for invoking **Fallback** function.
- func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) {
- // todo(rjl493456442) check the method is payable or not,
- // reject invalid transaction at the first place
- return c.transact(opts, &c.address, calldata)
- }
- // 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) {
- // todo(rjl493456442) check the payable fallback or receive is defined
- // or not, reject invalid transaction at the first place
- return c.transact(opts, &c.address, 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) {
- var err error
- // Ensure a valid value field and resolve the account nonce
- value := opts.Value
- if value == nil {
- value = new(big.Int)
- }
- var nonce uint64
- if opts.Nonce == nil {
- nonce, err = c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From)
- if err != nil {
- return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
- }
- } else {
- nonce = opts.Nonce.Uint64()
- }
- // Figure out the gas allowance and gas price values
- gasPrice := opts.GasPrice
- if gasPrice == nil {
- gasPrice, err = c.transactor.SuggestGasPrice(ensureContext(opts.Context))
- if err != nil {
- return nil, fmt.Errorf("failed to suggest gas price: %v", err)
- }
- }
- gasLimit := opts.GasLimit
- if gasLimit == 0 {
- // Gas estimation cannot succeed without code for method invocations
- if contract != nil {
- if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil {
- return nil, err
- } else if len(code) == 0 {
- return nil, ErrNoCode
- }
- }
- // If the contract surely has code (or code is not needed), estimate the transaction
- msg := ethereum.CallMsg{From: opts.From, To: contract, GasPrice: gasPrice, Value: value, Data: input}
- gasLimit, err = c.transactor.EstimateGas(ensureContext(opts.Context), msg)
- if err != nil {
- return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
- }
- }
- // Create the transaction, sign it and schedule it for execution
- var rawTx *types.Transaction
- if contract == nil {
- rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input)
- } else {
- rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input)
- }
- // Quorum
- // If this transaction is private, we need to substitute the data payload
- // with the hash of the transaction from tessera/constellation.
- if opts.PrivateFor != nil {
- var payload []byte
- hash, err := c.transactor.PreparePrivateTransaction(rawTx.Data(), opts.PrivateFrom)
- if err != nil {
- return nil, err
- }
- payload = hash.Bytes()
- rawTx = c.createPrivateTransaction(rawTx, payload)
- if opts.IsUsingPrivacyPrecompile {
- rawTx, _ = c.createMarkerTx(opts, rawTx, PrivateTxArgs{PrivateFor: opts.PrivateFor})
- opts.PrivateFor = nil
- }
- }
- // Choose signer to sign transaction
- if opts.Signer == nil {
- return nil, errors.New("no signer to authorize the transaction with")
- }
- signedTx, err := opts.Signer(opts.From, rawTx)
- if err != nil {
- return nil, err
- }
- if opts.NoSend {
- return signedTx, nil
- }
- if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx, PrivateTxArgs{PrivateFor: opts.PrivateFor}); err != nil {
- return nil, err
- }
- return signedTx, nil
- }
- // 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
- query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
- topics, err := abi.MakeTopics(query...)
- 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
- }
- sub, err := event.NewSubscription(func(quit <-chan struct{}) error {
- for _, log := range buff {
- select {
- case logs <- log:
- case <-quit:
- return nil
- }
- }
- return nil
- }), nil
- if err != nil {
- return nil, nil, err
- }
- 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
- query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
- topics, err := abi.MakeTopics(query...)
- 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 {
- if len(log.Data) > 0 {
- if err := c.abi.UnpackIntoInterface(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)
- }
- }
- return abi.ParseTopics(out, indexed, log.Topics[1:])
- }
- // UnpackLogIntoMap unpacks a retrieved log into the provided map.
- func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
- 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)
- }
- }
- return abi.ParseTopicsIntoMap(out, indexed, log.Topics[1:])
- }
- // Quorum
- // createPrivateTransaction replaces the payload of private transaction to the hash from Tessera/Constellation
- func (c *BoundContract) createPrivateTransaction(tx *types.Transaction, payload []byte) *types.Transaction {
- var privateTx *types.Transaction
- if tx.To() == nil {
- privateTx = types.NewContractCreation(tx.Nonce(), tx.Value(), tx.Gas(), tx.GasPrice(), payload)
- } else {
- privateTx = types.NewTransaction(tx.Nonce(), c.address, tx.Value(), tx.Gas(), tx.GasPrice(), payload)
- }
- privateTx.SetPrivate()
- return privateTx
- }
- // (Quorum) createMarkerTx creates a new public privacy marker transaction for the given private tx, distributing tx to the specified privateFor recipients
- func (c *BoundContract) createMarkerTx(opts *TransactOpts, tx *types.Transaction, args PrivateTxArgs) (*types.Transaction, error) {
- // Choose signer to sign transaction
- if opts.Signer == nil {
- return nil, errors.New("no signer to authorize the transaction with")
- }
- signedTx, err := opts.Signer(opts.From, tx)
- if err != nil {
- return nil, err
- }
- hash, err := c.transactor.DistributeTransaction(ensureContext(opts.Context), signedTx, args)
- if err != nil {
- return nil, err
- }
- // Note: using isHomestead and isEIP2028 set to true, which may give a slightly higher gas value (but avoids making an API call to get the block number)
- intrinsicGas, err := core.IntrinsicGas(common.FromHex(hash), tx.AccessList(), false, true, true)
- if err != nil {
- return nil, err
- }
- return types.NewTransaction(signedTx.Nonce(), common.QuorumPrivacyPrecompileContractAddress(), tx.Value(), intrinsicGas, tx.GasPrice(), common.FromHex(hash)), nil
- }
- // ensureContext is a helper method to ensure a context is not nil, even if the
- // user specified it as such.
- func ensureContext(ctx context.Context) context.Context {
- if ctx == nil {
- return context.TODO()
- }
- return ctx
- }
|