|
1 | 1 | package abi |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "reflect" |
6 | 5 | "testing" |
7 | 6 |
|
8 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
9 | 9 | ) |
10 | 10 |
|
11 | 11 | func TestAbi(t *testing.T) { |
@@ -106,14 +106,53 @@ func TestAbi(t *testing.T) { |
106 | 106 | } |
107 | 107 |
|
108 | 108 | if !reflect.DeepEqual(abi, c.Output) { |
109 | | - fmt.Println(reflect.DeepEqual(abi.Methods["balanceOf"].Outputs, c.Output.Methods["balanceOf"].Outputs)) |
110 | 109 | t.Fatal("bad") |
111 | 110 | } |
112 | | - |
113 | 111 | }) |
114 | 112 | } |
115 | 113 | } |
116 | 114 |
|
| 115 | +func TestAbi_InternalType(t *testing.T) { |
| 116 | + const abiStr = `[ |
| 117 | + { |
| 118 | + "inputs": [ |
| 119 | + { |
| 120 | + "components": [ |
| 121 | + { |
| 122 | + "internalType": "address", |
| 123 | + "type": "address" |
| 124 | + }, |
| 125 | + { |
| 126 | + "internalType": "uint256[4]", |
| 127 | + "type": "uint256[4]" |
| 128 | + } |
| 129 | + ], |
| 130 | + "internalType": "struct X", |
| 131 | + "name": "newSet", |
| 132 | + "type": "tuple[]" |
| 133 | + }, |
| 134 | + { |
| 135 | + "internalType": "custom_address", |
| 136 | + "name": "_to", |
| 137 | + "type": "address" |
| 138 | + } |
| 139 | + ], |
| 140 | + "outputs": [], |
| 141 | + "name": "transfer", |
| 142 | + "type": "function" |
| 143 | + } |
| 144 | + ]` |
| 145 | + |
| 146 | + abi, err := NewABI(abiStr) |
| 147 | + require.NoError(t, err) |
| 148 | + |
| 149 | + typ := abi.GetMethod("transfer").Inputs |
| 150 | + require.Equal(t, typ.tuple[0].Elem.InternalType(), "struct X") |
| 151 | + require.Equal(t, typ.tuple[0].Elem.elem.tuple[0].Elem.InternalType(), "address") |
| 152 | + require.Equal(t, typ.tuple[0].Elem.elem.tuple[1].Elem.InternalType(), "uint256[4]") |
| 153 | + require.Equal(t, typ.tuple[1].Elem.InternalType(), "custom_address") |
| 154 | +} |
| 155 | + |
117 | 156 | func TestAbi_Polymorphism(t *testing.T) { |
118 | 157 | // This ABI contains 2 "transfer" functions (polymorphism) |
119 | 158 | const polymorphicABI = `[ |
@@ -208,49 +247,49 @@ func TestAbi_HumanReadable(t *testing.T) { |
208 | 247 | Inputs: MustNewType("tuple(string symbol, string name)"), |
209 | 248 | }, |
210 | 249 | Methods: map[string]*Method{ |
211 | | - "transferFrom": &Method{ |
| 250 | + "transferFrom": { |
212 | 251 | Name: "transferFrom", |
213 | 252 | Inputs: MustNewType("tuple(address from, address to, uint256 value)"), |
214 | 253 | Outputs: MustNewType("tuple()"), |
215 | 254 | }, |
216 | | - "balanceOf": &Method{ |
| 255 | + "balanceOf": { |
217 | 256 | Name: "balanceOf", |
218 | 257 | Inputs: MustNewType("tuple(address owner)"), |
219 | 258 | Outputs: MustNewType("tuple(uint256 balance)"), |
220 | 259 | }, |
221 | | - "balanceOf0": &Method{ |
| 260 | + "balanceOf0": { |
222 | 261 | Name: "balanceOf", |
223 | 262 | Inputs: MustNewType("tuple()"), |
224 | 263 | Outputs: MustNewType("tuple()"), |
225 | 264 | }, |
226 | | - "addPerson": &Method{ |
| 265 | + "addPerson": { |
227 | 266 | Name: "addPerson", |
228 | 267 | Inputs: MustNewType("tuple(tuple(string name, uint16 age) person)"), |
229 | 268 | Outputs: MustNewType("tuple()"), |
230 | 269 | }, |
231 | | - "addPeople": &Method{ |
| 270 | + "addPeople": { |
232 | 271 | Name: "addPeople", |
233 | 272 | Inputs: MustNewType("tuple(tuple(string name, uint16 age)[] person)"), |
234 | 273 | Outputs: MustNewType("tuple()"), |
235 | 274 | }, |
236 | | - "getPerson": &Method{ |
| 275 | + "getPerson": { |
237 | 276 | Name: "getPerson", |
238 | 277 | Inputs: MustNewType("tuple(uint256 id)"), |
239 | 278 | Outputs: MustNewType("tuple(tuple(string name, uint16 age))"), |
240 | 279 | }, |
241 | 280 | }, |
242 | 281 | Events: map[string]*Event{ |
243 | | - "Transfer": &Event{ |
| 282 | + "Transfer": { |
244 | 283 | Name: "Transfer", |
245 | 284 | Inputs: MustNewType("tuple(address indexed from, address indexed to, address value)"), |
246 | 285 | }, |
247 | | - "PersonAdded": &Event{ |
| 286 | + "PersonAdded": { |
248 | 287 | Name: "PersonAdded", |
249 | 288 | Inputs: MustNewType("tuple(uint256 indexed id, tuple(string name, uint16 age) person)"), |
250 | 289 | }, |
251 | 290 | }, |
252 | 291 | Errors: map[string]*Error{ |
253 | | - "InsufficientBalance": &Error{ |
| 292 | + "InsufficientBalance": { |
254 | 293 | Name: "InsufficientBalance", |
255 | 294 | Inputs: MustNewType("tuple(address owner, uint256 balance)"), |
256 | 295 | }, |
|
0 commit comments