fix: parse input data with bytes

This commit is contained in:
Anthony Laibe 2023-06-19 12:04:29 +02:00 committed by Anthony Laibe
parent fee033fadb
commit 8ef3c52f4d
2 changed files with 16 additions and 8 deletions

View File

@ -53,7 +53,6 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
return nil, errors.New("input is badly formatted")
}
methodSigData := data[2:10]
fmt.Println(methodSigData)
url := fmt.Sprintf("https://www.4byte.directory/api/v1/signatures/?hex_signature=%s", methodSigData)
resp, err := c.DoQuery(url)
if err != nil {
@ -70,7 +69,6 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
if err != nil {
return nil, err
}
if signatures.Count == 0 {
return nil, err
}
@ -83,7 +81,11 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
rs := rgx.FindStringSubmatch(signature.Text)
inputs := make([]string, 0)
for index, typ := range strings.Split(rs[1], ",") {
rawInputs := strings.Split(rs[1], ",")
for index, typ := range rawInputs {
if index == len(rawInputs)-1 && typ == "bytes" {
continue
}
inputs = append(inputs, fmt.Sprintf("{\"name\":\"%d\",\"type\":\"%s\"}", index, typ))
}
@ -95,6 +97,7 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
method := contractABI.Methods[name]
inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil {
fmt.Println(err)
continue
}
inputsMapString := make(map[string]string)

View File

@ -18,9 +18,14 @@ func TestRun(t *testing.T) {
"1": "44417128579249187980157595307322491418158007948522794164811090501355597543782",
})
_, err = client.Run("0x70a08231000")
require.NotNil(t, err)
_, err = client.Run("0x70a082310")
require.NotNil(t, err)
res, err = client.Run("0xb88d4fde000000000000000000000000af1857535160973f42c34cb20f5ed7cd29099ad9000000000000000000000000baa4b7858c3277da9cb9cdadf405f2017afea19a00000000000000000000000000000000000000000000000000000000000000cb00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000360c6ebe")
require.Nil(t, err)
require.Equal(t, res.Signature, "safeTransferFrom(address,address,uint256,bytes)")
require.Equal(t, res.Name, "safeTransferFrom")
require.Equal(t, res.ID, "0x73e0")
require.Equal(t, res.Inputs, map[string]string{
"0": "0x3030303030303030303030306166313835373533",
"1": "0x3463623230663565643763643239303939616439",
"2": "21796157974083048550319244236929488537086114760591164995666220774418197985333",
})
}