Skip to content

Commit

Permalink
[api] support "input" field in web3 request params (#3971)
Browse files Browse the repository at this point in the history
* support input/data field in web3 request

* add test

---------

Co-authored-by: CoderZhi <[email protected]>
  • Loading branch information
envestcc and CoderZhi authored Nov 20, 2023
1 parent 6a8672d commit 453a2b9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion api/web3server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,12 @@ func parseCallObject(in *gjson.Result) (address.Address, string, uint64, *big.In
}
}

data = common.FromHex(in.Get("params.0.data").String())
input := in.Get("params.0.input")
if input.Exists() {
data = common.FromHex(input.String())
} else {
data = common.FromHex(in.Get("params.0.data").String())
}
return from, to, gasLimit, value, data, nil
}

Expand Down
74 changes: 74 additions & 0 deletions api/web3server_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package api

import (
"math/big"
"testing"

"github.com/iotexproject/iotex-address/address"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)

func TestParseCallObject(t *testing.T) {
require := require.New(t)

testData := []struct {
name string
input string

from string
to string
gasLimit uint64
value *big.Int
data []byte
err error
}{
{
name: "legacy",
input: `{"params":[{
"from": "",
"to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5",
"gas": "0x4e20",
"gasPrice": "0xe8d4a51000",
"value": "0x1",
"data": "0x6d4ce63c"
},
1]}`,
from: address.ZeroAddress,
to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090",
gasLimit: 20000,
value: new(big.Int).SetInt64(1),
data: []byte{0x6d, 0x4c, 0xe6, 0x3c},
},
{
name: "input instead of data",
input: `{"params":[{
"from": "",
"to": "0x7c13866F9253DEf79e20034eDD011e1d69E67fe5",
"gas": "0x4e20",
"gasPrice": "0xe8d4a51000",
"value": "0x1",
"input": "0x6d4ce63c"
},
1]}`,
from: address.ZeroAddress,
to: "io10sfcvmuj2000083qqd8d6qg7r457vll9gly090",
gasLimit: 20000,
value: new(big.Int).SetInt64(1),
data: []byte{0x6d, 0x4c, 0xe6, 0x3c},
},
}

for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
in := gjson.Parse(test.input)
from, to, gasLimit, value, data, err := parseCallObject(&in)
require.Equal(test.from, from.String())
require.Equal(test.to, to)
require.Equal(test.gasLimit, gasLimit)
require.Equal(test.value, value)
require.Equal(test.data, data)
require.Equal(test.err, err)
})
}
}

0 comments on commit 453a2b9

Please sign in to comment.