@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"encoding/hex"
20
20
"encoding/json"
21
+ "errors"
21
22
"fmt"
22
23
"math"
23
24
"math/big"
@@ -1330,6 +1331,26 @@ func TestConstructContractCallData(t *testing.T) {
1330
1331
},
1331
1332
expectedResult : "cf9d137c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000deaddeaddeaddeaddeaddeaddeaddeaddead0000000000000000000000000000b0935a466e6fa8fda8143c7f4a8c149ca56d06fe" ,
1332
1333
},
1334
+ "nil args" : {
1335
+ methodSig : "deposit()" ,
1336
+ methodArgs : nil ,
1337
+ expectedResult : "d0e30db0" ,
1338
+ },
1339
+ "list of non string args" : {
1340
+ methodSig : "register(string,address,bool)" ,
1341
+ methodArgs : []interface {}{"bool abc" , "0x0000000000000000000000000000000000000000" , "true" },
1342
+ expectedResult : "60d7a2780000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000008626f6f6c20616263000000000000000000000000000000000000000000000000" ,
1343
+ },
1344
+ "method sig is an empty string and args is a list of interface" : {
1345
+ methodSig : "" ,
1346
+ methodArgs : []interface {}{"0xabcde12345" },
1347
+ expectedResult : "abcde12345" ,
1348
+ },
1349
+ "method sig is NO-METHOD-SIG and args is a list of interface" : {
1350
+ methodSig : NoMethodSig ,
1351
+ methodArgs : []interface {}{"0xaabbcc112233" },
1352
+ expectedResult : "aabbcc112233" ,
1353
+ },
1333
1354
"invalid bytes format" : {
1334
1355
methodSig : "deploy(bytes32,address)" ,
1335
1356
methodArgs : []string {
@@ -1377,3 +1398,63 @@ func TestConstructContractCallData(t *testing.T) {
1377
1398
})
1378
1399
}
1379
1400
}
1401
+
1402
+ func TestConstruction_preprocessArgs (t * testing.T ) {
1403
+ tests := map [string ]struct {
1404
+ methodSig string
1405
+ methodArgs interface {}
1406
+
1407
+ expectedResponse interface {}
1408
+ expectedError error
1409
+ }{
1410
+ "happy path: method sig is function name" : {
1411
+ methodSig : "withdraw(address,uint256,uint32,bytes)" ,
1412
+ methodArgs : []interface {}{
1413
+ "0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22" ,
1414
+ "32941055343948244352" ,
1415
+ "0" ,
1416
+ "0x" },
1417
+ expectedResponse : []interface {}{
1418
+ "0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22" ,
1419
+ "32941055343948244352" ,
1420
+ "0" ,
1421
+ "0x" },
1422
+ },
1423
+ "happy path: method sig is empty and args is nil" : {
1424
+ methodSig : "" ,
1425
+ methodArgs : nil ,
1426
+ expectedResponse : nil ,
1427
+ },
1428
+ "happy path: method sig is NO-METHOD-SIG and args is a single string" : {
1429
+ methodSig : NoMethodSig ,
1430
+ methodArgs : "0x12345" ,
1431
+ expectedResponse : "0x12345" ,
1432
+ },
1433
+ "happy path: method sig is empty and args is a list of interface" : {
1434
+ methodSig : "" ,
1435
+ methodArgs : []interface {}{"0xabcde" },
1436
+ expectedResponse : "0xabcde" ,
1437
+ },
1438
+ "happy path: method sig is NO-METHOD-SIG and args is a list of strings" : {
1439
+ methodSig : NoMethodSig ,
1440
+ methodArgs : []string {"0x1a2b3c" },
1441
+ expectedResponse : "0x1a2b3c" ,
1442
+ },
1443
+ "unhappy path: args is a list of interface and cannot be converted to strings" : {
1444
+ methodSig : "" ,
1445
+ methodArgs : []interface {}{34567 },
1446
+ expectedError : errors .New ("failed to convert method arg \" int\" to string" ),
1447
+ },
1448
+ }
1449
+
1450
+ for name , test := range tests {
1451
+ t .Run (name , func (t * testing.T ) {
1452
+ argsReturned , err := preprocessArgs (test .methodSig , test .methodArgs )
1453
+ if err != nil {
1454
+ assert .EqualError (t , err , test .expectedError .Error ())
1455
+ } else {
1456
+ assert .Equal (t , test .expectedResponse , argsReturned )
1457
+ }
1458
+ })
1459
+ }
1460
+ }
0 commit comments