fix: fourbyte integration no arg

This commit is contained in:
Anthony Laibe 2023-07-10 11:35:11 +02:00 committed by Anthony Laibe
parent e9d8f4cab2
commit bc92df79d5
3 changed files with 81 additions and 71 deletions

View File

@ -32,15 +32,16 @@ type SignatureList struct {
}
type Client struct {
client *http.Client
Client *http.Client
URL string
}
func NewClient() *Client {
return &Client{client: &http.Client{Timeout: time.Minute}}
return &Client{Client: &http.Client{Timeout: time.Minute}, URL: "https://www.4byte.directory"}
}
func (c *Client) DoQuery(url string) (*http.Response, error) {
resp, err := c.client.Get(url)
resp, err := c.Client.Get(url)
if err != nil {
return nil, err
@ -53,7 +54,7 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
return nil, errors.New("input is badly formatted")
}
methodSigData := data[2:10]
url := fmt.Sprintf("https://www.4byte.directory/api/v1/signatures/?hex_signature=%s", methodSigData)
url := fmt.Sprintf("%s/api/v1/signatures/?hex_signature=%s", c.URL, methodSigData)
resp, err := c.DoQuery(url)
if err != nil {
return nil, err
@ -79,7 +80,8 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
id := fmt.Sprintf("0x%x", signature.ID)
name := strings.Split(signature.Text, "(")[0]
rs := rgx.FindStringSubmatch(signature.Text)
inputsMapString := make(map[string]string)
if len(rs[1]) > 0 {
inputs := make([]string, 0)
rawInputs := strings.Split(rs[1], ",")
for index, typ := range rawInputs {
@ -88,7 +90,6 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
}
inputs = append(inputs, fmt.Sprintf("{\"name\":\"%d\",\"type\":\"%s\"}", index, typ))
}
functionABI := fmt.Sprintf("[{\"constant\":true,\"inputs\":[%s],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\", \"name\": \"%s\"}], ", strings.Join(inputs, ","), name)
contractABI, err := abi.JSON(strings.NewReader(functionABI))
if err != nil {
@ -97,13 +98,13 @@ 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)
for key, value := range inputsMap {
inputsMapString[key] = fmt.Sprintf("%v", value)
}
}
return &thirdparty.DataParsed{
Name: name,

View File

@ -1,27 +1,34 @@
package fourbyte
// TODO: use mocked data if it make sense, instead of testing the servers UP status
//
// func TestRun(t *testing.T) {
// client := NewClient()
// res, err := client.Run("0x40e8d703000000000000000000000000670dca62b3418bddd08cbc69cb4490a5a3382a9f0000000000000000000000000000000000000000000000000000000000000064")
// require.Nil(t, err)
// require.Equal(t, res.Signature, "processDepositQueue(address,uint256)")
// require.Equal(t, res.Name, "processDepositQueue")
// require.Equal(t, res.ID, "0xf94d2")
// require.Equal(t, res.Inputs, map[string]string{
// "0": "0x3030303030303030303030303637306463613632",
// "1": "44417128579249187980157595307322491418158007948522794164811090501355597543782",
// })
import (
"net/http"
"net/http/httptest"
"testing"
// 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",
// })
// }
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
_, err := w.Write([]byte("{\"count\":1,\"next\":null,\"previous\":null,\"results\":[{\"id\":145,\"created_at\":\"2016-07-09T03:58:28.234977Z\",\"text_signature\":\"transfer(address,uint256)\",\"hex_signature\":\"0xa9059cbb\",\"bytes_signature\":\"0xa9059cbb\"}]}"))
if err != nil {
return
}
}))
defer srv.Close()
fb := NewClient()
fb.Client = srv.Client()
fb.URL = srv.URL
res, err := fb.Run("0xa9059cbb000000000000000000000000e0e40d81121d41a7d85d8d2462b475074f9df5ec0000000000000000000000000000000000000000000000000000000077359400")
require.Nil(t, err)
require.Equal(t, res.Signature, "transfer(address,uint256)")
require.Equal(t, res.ID, "0x91")
require.Equal(t, res.Name, "transfer")
require.Equal(t, res.Inputs, map[string]string{
"0": "0x3030303030303030303030306530653430643831",
"1": "22252012820881184517742036120632151212095838186768864961872069019727748752739",
})
}

View File

@ -69,7 +69,9 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
id := fmt.Sprintf("0x%s", methodSigData)
name := strings.Split(signature, "(")[0]
rs := rgx.FindStringSubmatch(signature)
inputsMapString := make(map[string]string)
if len(rs[1]) > 0 {
inputs := make([]string, 0)
rawInputs := strings.Split(rs[1], ",")
for index, typ := range rawInputs {
@ -89,10 +91,10 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil {
return nil, err
}
inputsMapString := make(map[string]string)
for key, value := range inputsMap {
inputsMapString[key] = fmt.Sprintf("%v", value)
}
}
return &thirdparty.DataParsed{
Name: name,