bound_test.go 3.9 KB
Newer Older
1 2 3 4 5 6 7 8
package batching

import (
	"math/big"
	"testing"

	"github.com/ethereum-optimism/optimism/op-bindings/bindings"
	"github.com/ethereum/go-ethereum/common"
9
	"github.com/ethereum/go-ethereum/core/types"
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
	"github.com/stretchr/testify/require"
)

func TestDecodeCall(t *testing.T) {
	method := "approve"
	spender := common.Address{0xbb, 0xee}
	amount := big.NewInt(4242)
	testAbi, err := bindings.ERC20MetaData.GetAbi()
	require.NoError(t, err)
	validData, err := testAbi.Pack(method, spender, amount)
	require.NoError(t, err)

	contract := NewBoundContract(testAbi, common.Address{0xaa})
	t.Run("TooShort", func(t *testing.T) {
		_, _, err := contract.DecodeCall([]byte{1, 2, 3})
		require.ErrorIs(t, err, ErrUnknownMethod)
	})

	t.Run("UnknownMethodId", func(t *testing.T) {
		_, _, err := contract.DecodeCall([]byte{1, 2, 3, 4})
		require.ErrorIs(t, err, ErrUnknownMethod)
	})

	t.Run("MissingArgs", func(t *testing.T) {
		// Truncate to just the 4 byte method selector
		_, _, err = contract.DecodeCall(validData[:4])
		require.ErrorIs(t, err, ErrInvalidCall)

		// Truncate to partial args
		_, _, err = contract.DecodeCall(validData[:6])
		require.ErrorIs(t, err, ErrInvalidCall)

		// Truncate to first arg but missing second
		_, _, err = contract.DecodeCall(validData[:24])
		require.ErrorIs(t, err, ErrInvalidCall)
	})

	t.Run("ValidCall", func(t *testing.T) {
		name, args, err := contract.DecodeCall(validData)
		require.NoError(t, err)
		require.Equal(t, name, method)
		require.Equal(t, spender, args.GetAddress(0))
		require.Zero(t, amount.Cmp(args.GetBigInt(1)))
	})
}
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

func TestDecodeEvent(t *testing.T) {
	testAbi, err := bindings.ERC20MetaData.GetAbi()
	require.NoError(t, err)

	// event Transfer(address indexed from, address indexed to, uint256 amount);
	event := testAbi.Events["Transfer"]

	contract := NewBoundContract(testAbi, common.Address{0xaa})
	t.Run("NoTopics", func(t *testing.T) {
		log := &types.Log{}
		_, _, err := contract.DecodeEvent(log)
		require.ErrorIs(t, err, ErrUnknownEvent)
	})

	t.Run("UnknownEvent", func(t *testing.T) {
		log := &types.Log{
			Topics: []common.Hash{{0xaa}},
		}
		_, _, err := contract.DecodeEvent(log)
		require.ErrorIs(t, err, ErrUnknownEvent)
	})

	t.Run("InvalidTopics", func(t *testing.T) {
		amount := big.NewInt(828274)
		data, err := event.Inputs.NonIndexed().Pack(amount)
		require.NoError(t, err)
		log := &types.Log{
			Topics: []common.Hash{
				event.ID,
				common.BytesToHash(common.Address{0xaa}.Bytes()),
				// Missing topic for to indexed value
			},
			Data: data,
		}
		_, _, err = contract.DecodeEvent(log)
		require.ErrorIs(t, err, ErrInvalidEvent)
	})

	t.Run("MissingData", func(t *testing.T) {
		log := &types.Log{
			Topics: []common.Hash{
				event.ID,
				common.BytesToHash(common.Address{0xaa}.Bytes()),
				common.BytesToHash(common.Address{0xbb}.Bytes()),
			},
		}
		_, _, err := contract.DecodeEvent(log)
		require.ErrorIs(t, err, ErrInvalidEvent)
	})

	t.Run("InvalidData", func(t *testing.T) {
		log := &types.Log{
			Topics: []common.Hash{
				event.ID,
				common.BytesToHash(common.Address{0xaa}.Bytes()),
				common.BytesToHash(common.Address{0xbb}.Bytes()),
			},
			Data: []byte{0xbb, 0xcc},
		}
		_, _, err := contract.DecodeEvent(log)
		require.ErrorIs(t, err, ErrInvalidEvent)
	})

	t.Run("ValidEvent", func(t *testing.T) {
		amount := big.NewInt(828274)
		data, err := event.Inputs.NonIndexed().Pack(amount)
		require.NoError(t, err)
		log := &types.Log{
			Topics: []common.Hash{
				event.ID,
				common.BytesToHash(common.Address{0xaa}.Bytes()),
				common.BytesToHash(common.Address{0xbb}.Bytes()),
			},
			Data: data,
		}
		name, result, err := contract.DecodeEvent(log)
		require.NoError(t, err)
		require.Equal(t, name, event.Name)
		require.Equal(t, common.Address{0xaa}, result.GetAddress(0))
		require.Equal(t, common.Address{0xbb}, result.GetAddress(1))
		require.Zerof(t, amount.Cmp(result.GetBigInt(2)), "expected %v but got %v", amount, result.GetBigInt(2))
	})
}