github.com/codingfuture/orig-energi3@v0.8.4/accounts/abi/bind/base_test.go (about) 1 // Copyright 2019 The Energi Core Authors 2 // Copyright 2018 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package bind_test 19 20 import ( 21 "context" 22 "math/big" 23 "testing" 24 25 ethereum "github.com/ethereum/go-ethereum" 26 "github.com/ethereum/go-ethereum/accounts/abi" 27 "github.com/ethereum/go-ethereum/accounts/abi/bind" 28 "github.com/ethereum/go-ethereum/common" 29 ) 30 31 type mockCaller struct { 32 codeAtBlockNumber *big.Int 33 callContractBlockNumber *big.Int 34 } 35 36 func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) { 37 mc.codeAtBlockNumber = blockNumber 38 return []byte{1, 2, 3}, nil 39 } 40 41 func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { 42 mc.callContractBlockNumber = blockNumber 43 return nil, nil 44 } 45 46 func TestPassingBlockNumber(t *testing.T) { 47 48 mc := &mockCaller{} 49 50 bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{ 51 Methods: map[string]abi.Method{ 52 "something": { 53 Name: "something", 54 Outputs: abi.Arguments{}, 55 }, 56 }, 57 }, mc, nil, nil) 58 var ret string 59 60 blockNumber := big.NewInt(42) 61 62 bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something") 63 64 if mc.callContractBlockNumber != blockNumber { 65 t.Fatalf("CallContract() was not passed the block number") 66 } 67 68 if mc.codeAtBlockNumber != blockNumber { 69 t.Fatalf("CodeAt() was not passed the block number") 70 } 71 72 bc.Call(&bind.CallOpts{}, &ret, "something") 73 74 if mc.callContractBlockNumber != nil { 75 t.Fatalf("CallContract() was passed a block number when it should not have been") 76 } 77 78 if mc.codeAtBlockNumber != nil { 79 t.Fatalf("CodeAt() was passed a block number when it should not have been") 80 } 81 }