github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/accounts/abi/bind/base_test.go (about)

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