|
| 1 | +package bind_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + |
| 8 | + ethereum "github.com/ethereum/go-ethereum" |
| 9 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 10 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | +) |
| 13 | + |
| 14 | +type mockCaller struct { |
| 15 | + codeAtBlockNumber *big.Int |
| 16 | + callContractBlockNumber *big.Int |
| 17 | +} |
| 18 | + |
| 19 | +func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) { |
| 20 | + mc.codeAtBlockNumber = blockNumber |
| 21 | + return []byte{1, 2, 3}, nil |
| 22 | +} |
| 23 | + |
| 24 | +func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { |
| 25 | + mc.callContractBlockNumber = blockNumber |
| 26 | + return nil, nil |
| 27 | +} |
| 28 | + |
| 29 | +func TestPassingBlockNumber(t *testing.T) { |
| 30 | + |
| 31 | + mc := &mockCaller{} |
| 32 | + |
| 33 | + bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{ |
| 34 | + Methods: map[string]abi.Method{ |
| 35 | + "something": { |
| 36 | + Name: "something", |
| 37 | + Outputs: abi.Arguments{}, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, mc, nil, nil) |
| 41 | + var ret string |
| 42 | + |
| 43 | + blockNumber := big.NewInt(42) |
| 44 | + |
| 45 | + bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something") |
| 46 | + |
| 47 | + if mc.callContractBlockNumber != blockNumber { |
| 48 | + t.Fatalf("CallContract() was not passed the block number") |
| 49 | + } |
| 50 | + |
| 51 | + if mc.codeAtBlockNumber != blockNumber { |
| 52 | + t.Fatalf("CodeAt() was not passed the block number") |
| 53 | + } |
| 54 | + |
| 55 | + bc.Call(&bind.CallOpts{}, &ret, "something") |
| 56 | + |
| 57 | + if mc.callContractBlockNumber != nil { |
| 58 | + t.Fatalf("CallContract() was passed a block number when it should not have been") |
| 59 | + } |
| 60 | + |
| 61 | + if mc.codeAtBlockNumber != nil { |
| 62 | + t.Fatalf("CodeAt() was passed a block number when it should not have been") |
| 63 | + } |
| 64 | +} |
0 commit comments