Skip to content

Commit

Permalink
TRD-558 Fallback for Fetching Trace Call (#68)
Browse files Browse the repository at this point in the history
* TRD-558 fallback for fetching trace call

* TRD-558 update client

* TRD-558 update rpc flag

* TRD-558 add log for rpc client
  • Loading branch information
linhnt3400 authored Sep 9, 2024
1 parent 6e4898b commit 7ec5b7a
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 31 deletions.
21 changes: 16 additions & 5 deletions cmd/tradelogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,22 @@ func run(c *cli.Context) error {
ResponseHeaderTimeout: time.Second * 30,
},
}
ethClient, err := ethclient.Dial(c.String(libapp.RPCUrlFlagName))
if err != nil {
panic(err)

rpcURL := c.StringSlice(libapp.RPCUrlFlagName)
if len(rpcURL) == 0 {
return fmt.Errorf("rpc url is empty")
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))

ethClients := make([]*ethclient.Client, len(rpcURL))
for i, url := range rpcURL {
client, err := ethclient.Dial(url)
if err != nil {
panic(err)
}
ethClients[i] = client
}

traceCalls := tracecall.NewCache(rpcnode.NewClient(l, ethClients...))

parsers := []parser.Parser{kyberswap.MustNewParser(),
zxotc.MustNewParser(),
Expand All @@ -107,7 +118,7 @@ func run(c *cli.Context) error {
uniswapxv1.MustNewParser(traceCalls),
uniswapx.MustNewParser(traceCalls),
bebop.MustNewParser(traceCalls),
zxrfqv3.MustNewParserWithDeployer(traceCalls, ethClient, common.HexToAddress(parser.Deployer0xV3)),
zxrfqv3.MustNewParserWithDeployer(traceCalls, ethClients[0], common.HexToAddress(parser.Deployer0xV3)),
}

binanceClient := binance.NewClient(c.String(pricefiller.BinanceAPIKeyFlag.Name), c.String(pricefiller.BinanceSecretKeyFlag.Name))
Expand Down
4 changes: 2 additions & 2 deletions internal/app/rpcnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const (
)

var (
RPCUrlFlag = &cli.StringFlag{
RPCUrlFlag = &cli.StringSliceFlag{
Name: RPCUrlFlagName,
EnvVar: "RPC_URL",
Usage: "RPC node url",
Value: "https://ethereum.kyberengineering.io/trading-tokyo",
Value: &cli.StringSlice{"https://ethereum.kyberengineering.io/trading-tokyo"},
}
)

Expand Down
9 changes: 5 additions & 4 deletions pkg/parser/bebop/bebop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/test-go/testify/require"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -24,7 +25,7 @@ func TestFetchEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
require.Equal(t, p.abi.Events[TradeEvent].ID, common.HexToHash("0xadd7095becdaa725f0f33243630938c861b0bba83dfd217d4055701aa768ec2e"))
client, err := ethclient.Dial(rpcURL)
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestParseAggregateOrderEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(events, uint64(time.Now().Unix()))
require.NoError(t, err)
Expand All @@ -79,7 +80,7 @@ func TestParseMultiOrderEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(events, uint64(time.Now().Unix()))
require.NoError(t, err)
Expand All @@ -102,7 +103,7 @@ func TestParseSingleOrderEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(events, uint64(time.Now().Unix()))
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions pkg/parser/oneinch/oneinch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/KyberNetwork/tradelogs/pkg/tracecall"
tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types"
tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -29,7 +30,7 @@ func TestFetchEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
require.Equal(t, p.abi.Events[FilledEvent].ID, common.HexToHash("0xc3b639f02b125bfa160e50739b8c44eb2d1b6908e2b6d5925c6d770f2ca78127"))
client, err := ethclient.Dial(rpcURL)
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestParseEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(event, uint64(time.Now().Unix()))
require.NoError(t, err)
Expand All @@ -83,7 +84,7 @@ func TestParseOneinchTradeLog(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
client, err := ethclient.Dial(rpcURL)
require.NoError(t, err)
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestParseWithCallFrame(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
client, err := ethclient.Dial(rpcURL)
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/parser/oneinchv6/oneinchv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -24,7 +25,7 @@ func TestFetchEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
require.Equal(t, p.abi.Events[FilledEvent].ID, common.HexToHash("0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"))
client, err := ethclient.Dial(rpcURL)
Expand Down Expand Up @@ -56,7 +57,7 @@ func TestParseEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
for _, event := range events {
log, err := p.Parse(event, uint64(time.Now().Unix()))
Expand Down
5 changes: 3 additions & 2 deletions pkg/parser/uniswapx/uniswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -24,7 +25,7 @@ func TestFetchEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
require.Equal(t, p.abi.Events[FilledEvent].ID, common.HexToHash("0x78ad7ec0e9f89e74012afa58738b6b661c024cb0fd185ee2f616c0a28924bd66"))
logs, err := ethClient.FilterLogs(context.Background(), ethereum.FilterQuery{
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestParseEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(event, 1713889895)
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/parser/uniswapx_v1/uniswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -24,7 +25,7 @@ func TestFetchEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
require.Equal(t, p.abi.Events[FilledEvent].ID, common.HexToHash("0x78ad7ec0e9f89e74012afa58738b6b661c024cb0fd185ee2f616c0a28924bd66"))
logs, err := ethClient.FilterLogs(context.Background(), ethereum.FilterQuery{
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestParseEvent(t *testing.T) {
if err != nil {
panic(err)
}
traceCalls := tracecall.NewCache(rpcnode.NewClient(ethClient))
traceCalls := tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
p := MustNewParser(traceCalls)
log, err := p.Parse(event, 1713889895)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/parser/zxrfqv3/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

const rpcURL = ""
Expand All @@ -25,7 +26,7 @@ func newParserTest(t *testing.T, contractABI ContractABI, needRpc bool) *Parser
if err != nil {
panic(err)
}
cache = tracecall.NewCache(rpcnode.NewClient(ethClient))
cache = tracecall.NewCache(rpcnode.NewClient(zap.S(), ethClient))
}
return MustNewParser(cache, contractABI)
}
Expand Down
31 changes: 22 additions & 9 deletions pkg/rpcnode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,38 @@ import (

"github.com/KyberNetwork/tradelogs/pkg/types"
"github.com/ethereum/go-ethereum/ethclient"
"go.uber.org/zap"
)

type Client struct {
ethClient *ethclient.Client
l *zap.SugaredLogger
ethClient []*ethclient.Client
}

func NewClient(ethClient *ethclient.Client) *Client {
func NewClient(l *zap.SugaredLogger, ethClient ...*ethclient.Client) *Client {
return &Client{
l: l,
ethClient: ethClient,
}
}

func (c *Client) FetchTraceCall(ctx context.Context, txHash string) (types.CallFrame, error) {
var result types.CallFrame
err := c.ethClient.Client().CallContext(ctx, &result, "debug_traceTransaction", txHash, map[string]interface{}{
"tracer": "callTracer",
"tracerConfig": map[string]interface{}{
"withLog": true,
},
})
var (
result types.CallFrame
err error
)
for i, ethClient := range c.ethClient {
err = ethClient.Client().CallContext(ctx, &result, "debug_traceTransaction", txHash, map[string]interface{}{
"tracer": "callTracer",
"tracerConfig": map[string]interface{}{
"withLog": true,
},
})
if err != nil {
c.l.Errorw("fetch trace call failed", "error", err, "txHash", txHash, "clientID", i)
continue
}
return result, nil
}
return result, err
}

0 comments on commit 7ec5b7a

Please sign in to comment.