|
| 1 | +package script |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/accounts/abi" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func die[O any](value O, err error) O { |
| 16 | + if err != nil { |
| 17 | + panic(err) |
| 18 | + } |
| 19 | + |
| 20 | + return value |
| 21 | +} |
| 22 | + |
| 23 | +func TestMatchTypes(t *testing.T) { |
| 24 | + type matchTypesTest struct { |
| 25 | + abiType abi.Type |
| 26 | + goType reflect.Type |
| 27 | + err string |
| 28 | + } |
| 29 | + |
| 30 | + type StructWithPrimitiveFields struct { |
| 31 | + AddressField common.Address |
| 32 | + BoolField bool |
| 33 | + UintField *big.Int |
| 34 | + } |
| 35 | + |
| 36 | + type StructWithPrimitiveFieldsWrapper struct { |
| 37 | + Nested StructWithPrimitiveFields |
| 38 | + } |
| 39 | + |
| 40 | + structWithPrimitiveFieldsMarshalling := []abi.ArgumentMarshaling{{Name: "addressField", Type: "address"}, {Name: "boolField", Type: "bool"}, {Name: "uintField", Type: "uint256"}} |
| 41 | + |
| 42 | + matchTypesTests := []matchTypesTest{ |
| 43 | + { |
| 44 | + abiType: die(abi.NewType("uint256", "", []abi.ArgumentMarshaling{})), |
| 45 | + goType: reflect.TypeOf(new(big.Int)), |
| 46 | + err: ``, |
| 47 | + }, |
| 48 | + { |
| 49 | + abiType: die(abi.NewType("uint128", "", []abi.ArgumentMarshaling{})), |
| 50 | + goType: reflect.TypeOf(new(big.Int)), |
| 51 | + err: ``, |
| 52 | + }, |
| 53 | + { |
| 54 | + abiType: die(abi.NewType("uint64", "", []abi.ArgumentMarshaling{})), |
| 55 | + goType: reflect.TypeOf(*new(uint64)), |
| 56 | + err: ``, |
| 57 | + }, |
| 58 | + { |
| 59 | + abiType: die(abi.NewType("uint8", "", []abi.ArgumentMarshaling{})), |
| 60 | + goType: reflect.TypeOf(*new(uint8)), |
| 61 | + err: ``, |
| 62 | + }, |
| 63 | + { |
| 64 | + abiType: die(abi.NewType("string", "", []abi.ArgumentMarshaling{})), |
| 65 | + goType: reflect.TypeOf(*new(string)), |
| 66 | + err: ``, |
| 67 | + }, |
| 68 | + { |
| 69 | + abiType: die(abi.NewType("bool", "", []abi.ArgumentMarshaling{})), |
| 70 | + goType: reflect.TypeOf(*new(bool)), |
| 71 | + err: ``, |
| 72 | + }, |
| 73 | + { |
| 74 | + abiType: die(abi.NewType("bytes", "", []abi.ArgumentMarshaling{})), |
| 75 | + goType: reflect.TypeOf(*new([]byte)), |
| 76 | + err: ``, |
| 77 | + }, |
| 78 | + { |
| 79 | + abiType: die(abi.NewType("bytes", "", []abi.ArgumentMarshaling{})), |
| 80 | + goType: reflect.TypeOf(*new([32]byte)), |
| 81 | + err: `ABI type bytes (represented by []uint8) is not assignable to Go type [32]uint8`, |
| 82 | + }, |
| 83 | + { |
| 84 | + abiType: die(abi.NewType("bytes32", "", []abi.ArgumentMarshaling{})), |
| 85 | + goType: reflect.TypeOf(*new([32]byte)), |
| 86 | + err: ``, |
| 87 | + }, |
| 88 | + { |
| 89 | + abiType: die(abi.NewType("bytes32", "", []abi.ArgumentMarshaling{})), |
| 90 | + goType: reflect.TypeOf(*new([]byte)), |
| 91 | + err: `ABI type bytes32 (represented by [32]uint8) is not assignable to Go type []uint8`, |
| 92 | + }, |
| 93 | + { |
| 94 | + abiType: die(abi.NewType("bytes32", "", []abi.ArgumentMarshaling{})), |
| 95 | + goType: reflect.TypeOf(*new([64]byte)), |
| 96 | + err: `ABI type bytes32 (represented by [32]uint8) is not assignable to Go type [64]uint8`, |
| 97 | + }, |
| 98 | + { |
| 99 | + abiType: die(abi.NewType("address", "", []abi.ArgumentMarshaling{})), |
| 100 | + goType: reflect.TypeOf(*new(common.Address)), |
| 101 | + err: ``, |
| 102 | + }, |
| 103 | + { |
| 104 | + abiType: die(abi.NewType("address", "", []abi.ArgumentMarshaling{})), |
| 105 | + goType: reflect.TypeOf(*new([]byte)), |
| 106 | + err: `ABI type address (represented by common.Address) is not assignable to Go type []uint8`, |
| 107 | + }, |
| 108 | + { |
| 109 | + abiType: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{})), |
| 110 | + goType: reflect.TypeOf(*new(struct{})), |
| 111 | + err: ``, |
| 112 | + }, |
| 113 | + { |
| 114 | + abiType: die(abi.NewType("tuple", "", structWithPrimitiveFieldsMarshalling)), |
| 115 | + goType: reflect.TypeOf(*new(StructWithPrimitiveFields)), |
| 116 | + err: ``, |
| 117 | + }, |
| 118 | + { |
| 119 | + abiType: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{{Name: "boolField", Type: "bool"}, {Name: "addressField", Type: "address"}, {Name: "uintField", Type: "uint256"}})), |
| 120 | + goType: reflect.TypeOf(*new(StructWithPrimitiveFields)), |
| 121 | + err: `ABI type (bool,address,uint256) (represented by struct { BoolField bool "json:\"boolField\""; AddressField common.Address "json:\"addressField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type script.StructWithPrimitiveFields: ABI field name BoolField at index 0 does not match Go field name AddressField. Please make sure to match the Go structs with Solidity structs`, |
| 122 | + }, |
| 123 | + { |
| 124 | + abiType: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{{Name: "addressField", Type: "bool"}, {Name: "boolField", Type: "bool"}, {Name: "uintField", Type: "uint256"}})), |
| 125 | + goType: reflect.TypeOf(*new(StructWithPrimitiveFields)), |
| 126 | + err: `ABI type (bool,bool,uint256) (represented by struct { AddressField bool "json:\"addressField\""; BoolField bool "json:\"boolField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type script.StructWithPrimitiveFields: ABI field AddressField does not match Go field AddressField: ABI type bool (represented by bool) is not assignable to Go type common.Address`, |
| 127 | + }, |
| 128 | + { |
| 129 | + abiType: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{{Name: "nested", Type: "tuple", Components: structWithPrimitiveFieldsMarshalling}})), |
| 130 | + goType: reflect.TypeOf(*new(StructWithPrimitiveFieldsWrapper)), |
| 131 | + err: ``, |
| 132 | + }, |
| 133 | + { |
| 134 | + abiType: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{{Name: "nested", Type: "tuple", Components: []abi.ArgumentMarshaling{{Name: "addressField", Type: "bool"}, {Name: "boolField", Type: "bool"}, {Name: "uintField", Type: "uint256"}}}})), |
| 135 | + goType: reflect.TypeOf(*new(StructWithPrimitiveFieldsWrapper)), |
| 136 | + err: `ABI type ((bool,bool,uint256)) (represented by struct { Nested struct { AddressField bool "json:\"addressField\""; BoolField bool "json:\"boolField\""; UintField *big.Int "json:\"uintField\"" } "json:\"nested\"" }) is not assignable to Go type script.StructWithPrimitiveFieldsWrapper: ABI field Nested does not match Go field Nested: ABI type (bool,bool,uint256) (represented by struct { AddressField bool "json:\"addressField\""; BoolField bool "json:\"boolField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type script.StructWithPrimitiveFields: ABI field AddressField does not match Go field AddressField: ABI type bool (represented by bool) is not assignable to Go type common.Address`, |
| 137 | + }, |
| 138 | + { |
| 139 | + abiType: die(abi.NewType("tuple[]", "", []abi.ArgumentMarshaling{})), |
| 140 | + goType: reflect.TypeOf(*new([]struct{})), |
| 141 | + err: ``, |
| 142 | + }, |
| 143 | + { |
| 144 | + abiType: die(abi.NewType("tuple[]", "", structWithPrimitiveFieldsMarshalling)), |
| 145 | + goType: reflect.TypeOf(*new([]StructWithPrimitiveFields)), |
| 146 | + err: ``, |
| 147 | + }, |
| 148 | + { |
| 149 | + abiType: die(abi.NewType("tuple[]", "", []abi.ArgumentMarshaling{{Name: "boolField", Type: "bool"}, {Name: "addressField", Type: "address"}, {Name: "uintField", Type: "uint256"}})), |
| 150 | + goType: reflect.TypeOf(*new([]StructWithPrimitiveFields)), |
| 151 | + err: `ABI type (bool,address,uint256)[] (represented by []struct { BoolField bool "json:\"boolField\""; AddressField common.Address "json:\"addressField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type []script.StructWithPrimitiveFields: ABI type (bool,address,uint256) (represented by struct { BoolField bool "json:\"boolField\""; AddressField common.Address "json:\"addressField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type script.StructWithPrimitiveFields: ABI field name BoolField at index 0 does not match Go field name AddressField. Please make sure to match the Go structs with Solidity structs`, |
| 152 | + }, |
| 153 | + { |
| 154 | + abiType: die(abi.NewType("tuple[2]", "", structWithPrimitiveFieldsMarshalling)), |
| 155 | + goType: reflect.TypeOf(*new([3]StructWithPrimitiveFields)), |
| 156 | + err: `ABI type (address,bool,uint256)[2] (represented by [2]struct { AddressField common.Address "json:\"addressField\""; BoolField bool "json:\"boolField\""; UintField *big.Int "json:\"uintField\"" }) is not assignable to Go type [3]script.StructWithPrimitiveFields: expected an array of length 2, got length 3`, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, test := range matchTypesTests { |
| 161 | + t.Run(fmt.Sprintf("%s <-> %s", test.abiType, test.goType), func(t *testing.T) { |
| 162 | + err := matchTypes(test.abiType, test.goType) |
| 163 | + |
| 164 | + if test.err == "" { |
| 165 | + require.NoError(t, err) |
| 166 | + } else { |
| 167 | + require.EqualError(t, err, test.err) |
| 168 | + } |
| 169 | + }) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func TestMatchArguments(t *testing.T) { |
| 174 | + type matchArgumentsTest struct { |
| 175 | + abiArguments abi.Arguments |
| 176 | + goTypes []reflect.Type |
| 177 | + err string |
| 178 | + } |
| 179 | + |
| 180 | + matchArgumentsTests := []matchArgumentsTest{ |
| 181 | + { |
| 182 | + abiArguments: abi.Arguments{ |
| 183 | + { |
| 184 | + Name: "", |
| 185 | + Type: die(abi.NewType("uint256", "", []abi.ArgumentMarshaling{})), |
| 186 | + }, |
| 187 | + }, |
| 188 | + goTypes: []reflect.Type{reflect.TypeOf(new(big.Int))}, |
| 189 | + }, |
| 190 | + { |
| 191 | + abiArguments: abi.Arguments{ |
| 192 | + { |
| 193 | + Name: "", |
| 194 | + Type: die(abi.NewType("uint256[]", "", []abi.ArgumentMarshaling{})), |
| 195 | + }, |
| 196 | + }, |
| 197 | + goTypes: []reflect.Type{reflect.TypeOf(*new([]*big.Int))}, |
| 198 | + }, |
| 199 | + { |
| 200 | + abiArguments: abi.Arguments{ |
| 201 | + { |
| 202 | + Name: "", |
| 203 | + Type: die(abi.NewType("uint256[]", "", []abi.ArgumentMarshaling{})), |
| 204 | + }, |
| 205 | + }, |
| 206 | + goTypes: []reflect.Type{}, |
| 207 | + err: `ABI arguments don't match Go types: ABI has 1 arguments, Go has 0`, |
| 208 | + }, |
| 209 | + { |
| 210 | + abiArguments: abi.Arguments{}, |
| 211 | + goTypes: []reflect.Type{reflect.TypeOf(*new([]*big.Int))}, |
| 212 | + err: `ABI arguments don't match Go types: ABI has 0 arguments, Go has 1`, |
| 213 | + }, |
| 214 | + { |
| 215 | + abiArguments: abi.Arguments{ |
| 216 | + { |
| 217 | + Name: "", |
| 218 | + Type: die(abi.NewType("uint256[2]", "", []abi.ArgumentMarshaling{})), |
| 219 | + }, |
| 220 | + }, |
| 221 | + goTypes: []reflect.Type{reflect.TypeOf(*new([]*big.Int))}, |
| 222 | + err: `ABI argument at index 0 doesn't match Go type: ABI type uint256[2] (represented by [2]*big.Int) is not assignable to Go type []*big.Int`, |
| 223 | + }, |
| 224 | + { |
| 225 | + abiArguments: abi.Arguments{ |
| 226 | + { |
| 227 | + Name: "", |
| 228 | + Type: die(abi.NewType("tuple", "", []abi.ArgumentMarshaling{{Name: "field", Type: "address"}})), |
| 229 | + }, |
| 230 | + }, |
| 231 | + goTypes: []reflect.Type{reflect.TypeOf(*new(struct{ Field *big.Int }))}, |
| 232 | + err: `ABI argument at index 0 doesn't match Go type: ABI type (address) (represented by struct { Field common.Address "json:\"field\"" }) is not assignable to Go type struct { Field *big.Int }: ABI field Field does not match Go field Field: ABI type address (represented by common.Address) is not assignable to Go type *big.Int`, |
| 233 | + }, |
| 234 | + } |
| 235 | + |
| 236 | + for _, test := range matchArgumentsTests { |
| 237 | + t.Run(fmt.Sprintf("%v <-> %v", test.abiArguments, test.goTypes), func(t *testing.T) { |
| 238 | + err := matchArguments(test.abiArguments, test.goTypes...) |
| 239 | + |
| 240 | + if test.err == "" { |
| 241 | + require.NoError(t, err) |
| 242 | + } else { |
| 243 | + require.EqualError(t, err, test.err) |
| 244 | + } |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
0 commit comments