Skip to content

Commit

Permalink
feat: support bytes[] as the contract call args (#130)
Browse files Browse the repository at this point in the history
* Handle bytes[]

* Add test case
  • Loading branch information
yfl92 authored Sep 27, 2024
1 parent 5b4708c commit 5f86092
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 additions & 0 deletions services/construction/contract_call_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package construction

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -176,6 +177,24 @@ func encodeMethodArgsStrings(methodID []byte, methodSig string, methodArgs []str
copy(value[:], bytes)
argData = value
}
// Note: we must handle "bytes[]" before "bytes" because they both share the same prefix
case v == "bytes[]":
{
var bytesArgs []string
if err := json.Unmarshal([]byte(methodArgs[i]), &bytesArgs); err != nil {
log.Fatal(err)
}

value := make([][]byte, len(bytesArgs))
for j, bytesArg := range bytesArgs {
bytes, err := hexutil.Decode(bytesArg)
if err != nil {
log.Fatal(err)
}
value[j] = bytes
}
argData = value
}
case strings.HasPrefix(v, "bytes"):
{
// No fixed size set as it would make it an "array" instead
Expand Down
6 changes: 6 additions & 0 deletions services/construction/contract_call_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func TestConstruction_ContractCallData(t *testing.T) {
methodArgs: []interface{}{"0xabcde12345"},
expectedResponse: "0xabcde12345",
},
// https://sepolia.basescan.org/tx/0x61bca9ade0522b70524ebe3e9b3064572cd22e05cdd67d650ad8657dc0f8a1f4
"happy path: bytes array and uint256 ": {
methodSig: "createAccount(bytes[],uint256)",
methodArgs: []interface{}{"[\"0x000000000000000000000000911a81c0a2cd632fd4c45461541bf8973d11870a\",\"0x0000000000000000000000006ecb18183838265968039955f1e8829480db5329\",\"0x0000000000000000000000000bfc799df7e440b7c88cc2454f12c58f8a29d986\"]", "0"},
expectedResponse: "0x3ffba36f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000911a81c0a2cd632fd4c45461541bf8973d11870a00000000000000000000000000000000000000000000000000000000000000200000000000000000000000006ecb18183838265968039955f1e8829480db532900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000bfc799df7e440b7c88cc2454f12c58f8a29d986",
},
"happy path: method sig is NO-METHOD-SIG and args is a list of interface": {
methodSig: NoMethodSig,
methodArgs: []interface{}{"0xaabbcc112233"},
Expand Down

0 comments on commit 5f86092

Please sign in to comment.