Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parse oneinch rfq trades #28

Merged
merged 11 commits into from
Mar 12, 2024
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ cmd/*/*
!cmd/*/*.go
!cmd/*/migrations
*/cmd/*/*
!*/cmd/*/*.go
!*/cmd/*/*.go
.idea
.env.example
.env
7 changes: 6 additions & 1 deletion cmd/tradelogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"github.com/KyberNetwork/tradelogs/pkg/parser"
"github.com/KyberNetwork/tradelogs/pkg/parser/oneinch"
"log"
"os"

Expand All @@ -13,7 +15,6 @@ import (
tradelogs "github.com/KyberNetwork/tradelogs/internal/server/tradelogs"
"github.com/KyberNetwork/tradelogs/internal/worker"
"github.com/KyberNetwork/tradelogs/pkg/evmlistenerclient"
"github.com/KyberNetwork/tradelogs/pkg/parser"
"github.com/KyberNetwork/tradelogs/pkg/parser/hashflow"
hashflowv3 "github.com/KyberNetwork/tradelogs/pkg/parser/hashflow_v3"
"github.com/KyberNetwork/tradelogs/pkg/parser/kyberswap"
Expand All @@ -39,6 +40,7 @@ func main() {
app.Flags = append(app.Flags, libapp.EvmListenerFlags()...)
app.Flags = append(app.Flags, libapp.HTTPServerFlags()...)
app.Flags = append(app.Flags, libapp.BigqueryFlags()...)
app.Flags = append(app.Flags, libapp.RPCNodeFlags()...)

if err := app.Run(os.Args); err != nil {
log.Panic(err)
Expand Down Expand Up @@ -72,6 +74,7 @@ func run(c *cli.Context) error {
l.Errorw("Error while init listener service")
return err
}

w, err := worker.New(l, s, listener,
kyberswap.MustNewParser(),
zxotc.MustNewParser(),
Expand All @@ -98,7 +101,9 @@ func run(c *cli.Context) error {
"native": native.MustNewParser(),
"kyberswaprfq": kyberswaprfq.MustNewParser(),
"hashflowv3": hashflowv3.MustNewParser(),
"1inch": oneinch.MustNewParser(c.String(libapp.RPCUrlFlagName)),
}

backfillWorker, err := bigquery.NewWorker(libapp.BigqueryProjectIDFFromCli(c), s, parserMap)
if err != nil {
l.Errorw("Error while init backfillWorker")
Expand Down
21 changes: 21 additions & 0 deletions internal/app/rpcnode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app

import "github.com/urfave/cli"

const (
RPCUrlFlagName = "rpc-url"
)

var (
RPCUrlFlag = &cli.StringFlag{
Name: RPCUrlFlagName,
EnvVar: "RPC_URL",
Usage: "RPC node url",
}
)

func RPCNodeFlags() []cli.Flag {
return []cli.Flag{
RPCUrlFlag,
}
}
12 changes: 12 additions & 0 deletions internal/types/contract_call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package types

type ContractCallParam struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Type string `json:"type"`
}
type ContractCall struct {
ContractType string `json:"contract_type,omitempty"`
Name string `json:"name"`
Params []ContractCallParam `json:"params"`
}
39 changes: 39 additions & 0 deletions internal/types/trace_call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"github.com/ethereum/go-ethereum/common"
ethereumTypes "github.com/ethereum/go-ethereum/core/types"
)

type TraceCallResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result CallFrame `json:"result"`
}

type CallLog struct {
Address common.Address `json:"address"`
Topics []common.Hash `json:"topics"`
Data string `json:"data"`
}

func (l CallLog) ToEthereumLog() ethereumTypes.Log {
return ethereumTypes.Log{
Address: l.Address,
Topics: l.Topics,
Data: common.Hex2Bytes(l.Data),
}
}

type CallFrame struct {
From string `json:"from"`
Gas string `json:"gas"`
GasUsed string `json:"gasUsed"`
To string `json:"to"`
Input string `json:"input"`
Output string `json:"output"`
Calls []CallFrame `json:"calls"`
Value string `json:"value"`
Type string `json:"type"`
Logs []CallLog `json:"logs"`
}
2 changes: 1 addition & 1 deletion internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/KyberNetwork/tradelogs/pkg/convert"

"github.com/KyberNetwork/tradelogs/pkg/storage"
"github.com/KyberNetwork/tradelogs/pkg/evmlistenerclient"
"github.com/KyberNetwork/tradelogs/pkg/parser"
"github.com/KyberNetwork/tradelogs/pkg/storage"
"go.uber.org/zap"
)

Expand Down
53 changes: 53 additions & 0 deletions pkg/decoder/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package decoder

import (
"encoding/hex"
"github.com/KyberNetwork/tradelogs/internal/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"strings"
)

func Decode(abiStr string, input string) (*types.ContractCall, error) {
ABI, err := abi.JSON(strings.NewReader(abiStr))
tiennampham23 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

inputBytes := common.FromHex(input)
method, err := ABI.MethodById(inputBytes)
if err != nil {
return nil, err
}

bytes, err := hex.DecodeString(input[10:])
if err != nil {
return nil, err
}

inputs, err := method.Inputs.Unpack(bytes)
if err != nil {
return nil, err
}

nonIndexedArgs := method.Inputs.NonIndexed()

contractCall := &types.ContractCall{
Name: method.Name,
}

for i, input := range inputs {
arg := nonIndexedArgs[i]
param := types.ContractCallParam{
Name: arg.Name,
Value: input,
Type: arg.Type.String(),
}
contractCall.Params = append(contractCall.Params, param)
}

return contractCall, nil
}
Loading
Loading