aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/bind/base.go
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/abi/bind/base.go')
-rw-r--r--accounts/abi/bind/base.go75
1 files changed, 48 insertions, 27 deletions
diff --git a/accounts/abi/bind/base.go b/accounts/abi/bind/base.go
index 80948d3f1..965f51e85 100644
--- a/accounts/abi/bind/base.go
+++ b/accounts/abi/bind/base.go
@@ -20,8 +20,8 @@ import (
"errors"
"fmt"
"math/big"
- "sync/atomic"
+ "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
@@ -62,9 +62,6 @@ type BoundContract struct {
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
-
- latestHasCode uint32 // Cached verification that the latest state contains code for this contract
- pendingHasCode uint32 // Cached verification that the pending state contains code for this contract
}
// NewBoundContract creates a low level contract interface through which calls
@@ -105,25 +102,42 @@ func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string,
if opts == nil {
opts = new(CallOpts)
}
- // Make sure we have a contract to operate on, and bail out otherwise
- if (opts.Pending && atomic.LoadUint32(&c.pendingHasCode) == 0) || (!opts.Pending && atomic.LoadUint32(&c.latestHasCode) == 0) {
- if code, err := c.caller.HasCode(opts.Context, c.address, opts.Pending); err != nil {
- return err
- } else if !code {
- return ErrNoCode
- }
- if opts.Pending {
- atomic.StoreUint32(&c.pendingHasCode, 1)
- } else {
- atomic.StoreUint32(&c.latestHasCode, 1)
- }
- }
// Pack the input, call and unpack the results
input, err := c.abi.Pack(method, params...)
if err != nil {
return err
}
- output, err := c.caller.ContractCall(opts.Context, c.address, input, opts.Pending)
+ var (
+ msg = ethereum.CallMsg{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, nil)
+ if err == nil && 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, nil); err != nil {
+ return err
+ } else if len(code) == 0 {
+ return ErrNoCode
+ }
+ }
+ }
if err != nil {
return err
}
@@ -158,7 +172,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
}
nonce := uint64(0)
if opts.Nonce == nil {
- nonce, err = c.transactor.PendingAccountNonce(opts.Context, opts.From)
+ nonce, err = c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From)
if err != nil {
return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
}
@@ -168,7 +182,7 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
// Figure out the gas allowance and gas price values
gasPrice := opts.GasPrice
if gasPrice == nil {
- gasPrice, err = c.transactor.SuggestGasPrice(opts.Context)
+ gasPrice, err = c.transactor.SuggestGasPrice(ensureContext(opts.Context))
if err != nil {
return nil, fmt.Errorf("failed to suggest gas price: %v", err)
}
@@ -176,18 +190,18 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
gasLimit := opts.GasLimit
if gasLimit == nil {
// Gas estimation cannot succeed without code for method invocations
- if contract != nil && atomic.LoadUint32(&c.pendingHasCode) == 0 {
- if code, err := c.transactor.HasCode(opts.Context, c.address, true); err != nil {
+ if contract != nil {
+ if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil {
return nil, err
- } else if !code {
+ } else if len(code) == 0 {
return nil, ErrNoCode
}
- atomic.StoreUint32(&c.pendingHasCode, 1)
}
// If the contract surely has code (or code is not needed), estimate the transaction
- gasLimit, err = c.transactor.EstimateGasLimit(opts.Context, opts.From, contract, value, input)
+ msg := ethereum.CallMsg{From: opts.From, To: contract, Value: value, Data: input}
+ gasLimit, err = c.transactor.EstimateGas(ensureContext(opts.Context), msg)
if err != nil {
- return nil, fmt.Errorf("failed to exstimate gas needed: %v", err)
+ return nil, fmt.Errorf("failed to estimate gas needed: %v", err)
}
}
// Create the transaction, sign it and schedule it for execution
@@ -204,8 +218,15 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
if err != nil {
return nil, err
}
- if err := c.transactor.SendTransaction(opts.Context, signedTx); err != nil {
+ if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil {
return nil, err
}
return signedTx, nil
}
+
+func ensureContext(ctx context.Context) context.Context {
+ if ctx == nil {
+ return context.TODO()
+ }
+ return ctx
+}