-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathinput_test.go
159 lines (123 loc) · 6.01 KB
/
input_test.go
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// nolint: forcetypeassert, dupl
package abiutil_test
import (
"github.com/brianvoe/gofakeit/v6"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/core/testsuite"
"github.com/synapsecns/sanguine/ethergo/parser/abiutil"
"github.com/synapsecns/sanguine/ethergo/parser/abiutil/internal"
"gotest.tools/assert"
"math/big"
)
func (a *AbiSuite) TestUnpackDataToInterface() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
paramA := new(big.Int).SetUint64(gofakeit.Uint64())
paramB := common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64()))
paramC := new(big.Int).SetUint64(gofakeit.Uint64())
paramD := common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64()))
paramE := []byte(gofakeit.Paragraph(1, 10, 1, " "))
paramF := gofakeit.Bool()
tx, err := a.testContract.DoSomethingManyTypes(auth.TransactOpts, paramA, paramB, paramC, paramD, paramE, paramF)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
receipt, err := a.backend.TransactionReceipt(a.GetTestContext(), tx.Hash())
a.Require().NoError(err)
filterNum := receipt.BlockNumber.Uint64()
filteredEvent, err := a.testContract.FilterSomethingHappenedManyTypes(&bind.FilterOpts{
Context: a.GetTestContext(),
Start: filterNum,
End: &filterNum,
})
a.Require().NoError(err)
// unpack into interface
var originalEvent internal.TestSignatureSomethingHappenedManyTypes
err = abiutil.UnpackInputDataToInterface(&originalEvent, tx.Data(), a.metadata)
a.Require().NoError(err)
// get the first event
filteredEvent.Next()
assert.DeepEqual(a.T(), originalEvent.A, filteredEvent.Event.A, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), originalEvent.B, filteredEvent.Event.B)
assert.DeepEqual(a.T(), originalEvent.C, filteredEvent.Event.C, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), common.Bytes2Hex(originalEvent.D[:]), common.Bytes2Hex(filteredEvent.Event.D[:]))
assert.DeepEqual(a.T(), originalEvent.E, filteredEvent.Event.E)
assert.DeepEqual(a.T(), originalEvent.F, filteredEvent.Event.F)
}
func (a *AbiSuite) TestDoSomethingNoParamsUnpack() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
tx, err := a.testContract.DoSomethingWithoutParams(auth.TransactOpts)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
out, err := abiutil.UnpackInputData(tx.Data(), a.metadata)
a.Require().NoError(err)
a.Require().Len(out, 0)
}
func (a *AbiSuite) TestDoSomethingUnpack() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
paramA := new(big.Int).SetUint64(gofakeit.Uint64())
paramB := new(big.Int).SetUint64(gofakeit.Uint64())
tx, err := a.testContract.DoSomething(auth.TransactOpts, paramA, paramB)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
out, err := abiutil.UnpackInputData(tx.Data(), a.metadata)
a.Require().NoError(err)
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out1 := *abi.ConvertType(out[1], new(*big.Int)).(**big.Int)
assert.DeepEqual(a.T(), out0, paramA, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), out1, paramB, testsuite.BigIntComparer())
}
func (a *AbiSuite) TestDoSomethingOverloadUnpack() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
paramA := new(big.Int).SetUint64(gofakeit.Uint64())
paramB := new(big.Int).SetUint64(gofakeit.Uint64())
tx, err := a.testContract.DoSomethingOverload(auth.TransactOpts, paramA, paramB)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
out, err := abiutil.UnpackInputData(tx.Data(), a.metadata)
a.Require().NoError(err)
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out1 := *abi.ConvertType(out[1], new(*big.Int)).(**big.Int)
assert.DeepEqual(a.T(), out0, paramA, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), out1, paramB, testsuite.BigIntComparer())
}
func (a *AbiSuite) TestDoSomethingOverload0Unpack() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
paramA := new(big.Int).SetUint64(gofakeit.Uint64())
paramB := common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64()))
tx, err := a.testContract.DoSomethingOverload0(auth.TransactOpts, paramA, paramB)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
out, err := abiutil.UnpackInputData(tx.Data(), a.metadata)
a.Require().NoError(err)
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out1 := *abi.ConvertType(out[1], new(common.Address)).(*common.Address)
assert.DeepEqual(a.T(), out0, paramA, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), out1, paramB)
}
func (a *AbiSuite) TestDoSomethingManyTypeUnpack() {
auth := a.backend.GetTxContext(a.GetTestContext(), nil)
paramA := new(big.Int).SetUint64(gofakeit.Uint64())
paramB := common.BigToAddress(new(big.Int).SetUint64(gofakeit.Uint64()))
paramC := new(big.Int).SetUint64(gofakeit.Uint64())
paramD := common.BigToHash(new(big.Int).SetUint64(gofakeit.Uint64()))
paramE := []byte(gofakeit.Paragraph(1, 10, 1, " "))
paramF := gofakeit.Bool()
tx, err := a.testContract.DoSomethingManyTypes(auth.TransactOpts, paramA, paramB, paramC, paramD, paramE, paramF)
a.Require().NoError(err)
a.backend.WaitForConfirmation(a.GetTestContext(), tx)
out, err := abiutil.UnpackInputData(tx.Data(), a.metadata)
a.Require().NoError(err)
out0 := *abi.ConvertType(out[0], new(*big.Int)).(**big.Int)
out1 := *abi.ConvertType(out[1], new(common.Address)).(*common.Address)
out2 := *abi.ConvertType(out[2], new(*big.Int)).(**big.Int)
out3 := *abi.ConvertType(out[3], new([32]byte)).(*[32]byte)
out4 := *abi.ConvertType(out[4], new([]byte)).(*[]byte)
out5 := *abi.ConvertType(out[5], new(bool)).(*bool)
assert.DeepEqual(a.T(), out0, paramA, testsuite.BigIntComparer())
assert.DeepEqual(a.T(), out1, paramB)
assert.DeepEqual(a.T(), out2, paramC, testsuite.BigIntComparer())
assert.Equal(a.T(), common.Bytes2Hex(out3[:]), common.Bytes2Hex(paramD.Bytes()))
assert.DeepEqual(a.T(), out4, paramE)
assert.DeepEqual(a.T(), out5, paramF)
}