aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/bind/base_test.go
blob: ea3035638ec698c53f00c1d2a387c3db179aee0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package bind_test

import (
    "context"
    "math/big"
    "testing"

    ethereum "github.com/dexon-foundation/dexon"
    "github.com/dexon-foundation/dexon/accounts/abi"
    "github.com/dexon-foundation/dexon/accounts/abi/bind"
    "github.com/dexon-foundation/dexon/common"
)

type mockCaller struct {
    codeAtBlockNumber       *big.Int
    callContractBlockNumber *big.Int
}

func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
    mc.codeAtBlockNumber = blockNumber
    return []byte{1, 2, 3}, nil
}

func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
    mc.callContractBlockNumber = blockNumber
    return nil, nil
}

func TestPassingBlockNumber(t *testing.T) {

    mc := &mockCaller{}

    bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{
        Methods: map[string]abi.Method{
            "something": {
                Name:    "something",
                Outputs: abi.Arguments{},
            },
        },
    }, mc, nil, nil)
    var ret string

    blockNumber := big.NewInt(42)

    bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, &ret, "something")

    if mc.callContractBlockNumber != blockNumber {
        t.Fatalf("CallContract() was not passed the block number")
    }

    if mc.codeAtBlockNumber != blockNumber {
        t.Fatalf("CodeAt() was not passed the block number")
    }

    bc.Call(&bind.CallOpts{}, &ret, "something")

    if mc.callContractBlockNumber != nil {
        t.Fatalf("CallContract() was passed a block number when it should not have been")
    }

    if mc.codeAtBlockNumber != nil {
        t.Fatalf("CodeAt() was passed a block number when it should not have been")
    }
}