From 29af99790d8215b09b66eccec9af57ba77afa977 Mon Sep 17 00:00:00 2001 From: "minhthanh.vu" Date: Thu, 3 Oct 2024 14:40:32 +0700 Subject: [PATCH 1/3] get address parser --- cmd/tradelogs/main.go | 2 +- pkg/parser/bebop/parser.go | 4 ++++ pkg/parser/hashflow_v3/parser.go | 4 ++++ pkg/parser/kyberswap/parser.go | 4 ++++ pkg/parser/kyberswap_rfq/parser.go | 4 ++++ pkg/parser/oneinchv6/parser.go | 4 ++++ pkg/parser/pancakeswap/parser.go | 4 ++++ pkg/parser/paraswap/parser.go | 4 ++++ pkg/parser/parser.go | 1 + pkg/parser/uniswapx/parser.go | 4 ++++ pkg/parser/zxotc/parser.go | 4 ++++ pkg/parser/zxrfqv3/parser.go | 4 ++++ 12 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cmd/tradelogs/main.go b/cmd/tradelogs/main.go index 3d19bab..0e06481 100644 --- a/cmd/tradelogs/main.go +++ b/cmd/tradelogs/main.go @@ -116,7 +116,7 @@ func run(c *cli.Context) error { kyberswaprfq.MustNewParser(), hashflowv3.MustNewParser(), oneinchv6.MustNewParser(traceCalls), - uniswapxv1.MustNewParser(traceCalls), + // uniswapxv1.MustNewParser(traceCalls), uniswapx.MustNewParser(traceCalls), bebop.MustNewParser(traceCalls), zxrfqv3.MustNewParserWithDeployer(traceCalls, ethClients[0], common.HexToAddress(parser.Deployer0xV3)), diff --git a/pkg/parser/bebop/parser.go b/pkg/parser/bebop/parser.go index 7a03a46..c4af956 100644 --- a/pkg/parser/bebop/parser.go +++ b/pkg/parser/bebop/parser.go @@ -432,3 +432,7 @@ func getAggregateOrderInfo(order AggregateOrder) *big.Int { } return quoteTakerAmount } + +func (p *Parser) Address() string { + return parser.AddrBebop +} diff --git a/pkg/parser/hashflow_v3/parser.go b/pkg/parser/hashflow_v3/parser.go index d82a329..3463836 100644 --- a/pkg/parser/hashflow_v3/parser.go +++ b/pkg/parser/hashflow_v3/parser.go @@ -131,3 +131,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { return len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return "" +} diff --git a/pkg/parser/kyberswap/parser.go b/pkg/parser/kyberswap/parser.go index 630feba..98aadb6 100644 --- a/pkg/parser/kyberswap/parser.go +++ b/pkg/parser/kyberswap/parser.go @@ -89,3 +89,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.AddrKyberswap +} diff --git a/pkg/parser/kyberswap_rfq/parser.go b/pkg/parser/kyberswap_rfq/parser.go index b90b8e4..3cfc94c 100644 --- a/pkg/parser/kyberswap_rfq/parser.go +++ b/pkg/parser/kyberswap_rfq/parser.go @@ -134,3 +134,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.AddrKyberswapRFQ +} diff --git a/pkg/parser/oneinchv6/parser.go b/pkg/parser/oneinchv6/parser.go index e755b3c..ee48e97 100644 --- a/pkg/parser/oneinchv6/parser.go +++ b/pkg/parser/oneinchv6/parser.go @@ -262,3 +262,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.Addr1InchV6 +} diff --git a/pkg/parser/pancakeswap/parser.go b/pkg/parser/pancakeswap/parser.go index b5eea82..717f2cd 100644 --- a/pkg/parser/pancakeswap/parser.go +++ b/pkg/parser/pancakeswap/parser.go @@ -291,3 +291,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.AddrPancakewap +} diff --git a/pkg/parser/paraswap/parser.go b/pkg/parser/paraswap/parser.go index e275450..6bcdcbc 100644 --- a/pkg/parser/paraswap/parser.go +++ b/pkg/parser/paraswap/parser.go @@ -132,3 +132,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.AddrParaswap +} diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index bf81dc3..ab9c984 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -52,4 +52,5 @@ type Parser interface { UseTraceCall() bool ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) LogFromExchange(log ethereumTypes.Log) bool + Address() string } diff --git a/pkg/parser/uniswapx/parser.go b/pkg/parser/uniswapx/parser.go index 2db9a79..994237c 100644 --- a/pkg/parser/uniswapx/parser.go +++ b/pkg/parser/uniswapx/parser.go @@ -344,3 +344,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.AddrUniswapX +} diff --git a/pkg/parser/zxotc/parser.go b/pkg/parser/zxotc/parser.go index b7e90c9..cda925c 100644 --- a/pkg/parser/zxotc/parser.go +++ b/pkg/parser/zxotc/parser.go @@ -131,3 +131,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { len(log.Topics) > 0 && strings.EqualFold(log.Topics[0].String(), p.eventHash) } + +func (p *Parser) Address() string { + return parser.Addr0x +} diff --git a/pkg/parser/zxrfqv3/parser.go b/pkg/parser/zxrfqv3/parser.go index 740577a..7cb6cab 100644 --- a/pkg/parser/zxrfqv3/parser.go +++ b/pkg/parser/zxrfqv3/parser.go @@ -356,3 +356,7 @@ func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { return p.contractABIs.containAddress(log.Address) && len(log.Topics) == 0 } + +func (p *Parser) Address() string { + return "" +} From ea39614f3bda5e85b3f5b2fb0056e44bfd8ead9a Mon Sep 17 00:00:00 2001 From: "minhthanh.vu" Date: Thu, 3 Oct 2024 15:07:58 +0700 Subject: [PATCH 2/3] add paraswap taker --- cmd/tradelogs/main.go | 1 - pkg/parser/parser.go | 23 ++++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/tradelogs/main.go b/cmd/tradelogs/main.go index 0e06481..fecbd56 100644 --- a/cmd/tradelogs/main.go +++ b/cmd/tradelogs/main.go @@ -15,7 +15,6 @@ import ( "github.com/KyberNetwork/tradelogs/pkg/parser/oneinchv6" "github.com/KyberNetwork/tradelogs/pkg/parser/pancakeswap" "github.com/KyberNetwork/tradelogs/pkg/parser/uniswapx" - uniswapxv1 "github.com/KyberNetwork/tradelogs/pkg/parser/uniswapx_v1" "github.com/KyberNetwork/tradelogs/pkg/parser/zxrfqv3" "github.com/KyberNetwork/tradelogs/pkg/pricefiller" "github.com/KyberNetwork/tradelogs/pkg/rpcnode" diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index ab9c984..86b8d97 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -27,17 +27,18 @@ const ( ExUniswapXV1 = "uniswapxV1" ExPancackeSwap = "pancakeswap" - Addr1InchV6 = "0x111111125421cA6dc452d289314280a0f8842A65" - AddrBebop = "0xbbbbbBB520d69a9775E85b458C58c648259FAD5F" - AddrHashflowV3 = "0x24b9d98FABF4DA1F69eE10775F240AE3dA6856fd" - AddrKyberswap = "0x6131B5fae19EA4f9D964eAc0408E4408b66337b5" - AddrKyberswapRFQ = "0x7A819Fa46734a49D0112796f9377E024c350FB26" - AddrParaswap = "0xe92b586627ccA7a83dC919cc7127196d70f55a06" - AddrUniswapX = "0x00000011F84B9aa48e5f8aA8B9897600006289Be" - AddrUniswapXV1 = "0x6000da47483062A0D734Ba3dc7576Ce6A0B645C4" - Addr0x = "0xDef1C0ded9bec7F1a1670819833240f027b25EfF" - Deployer0xV3 = "0x00000000000004533Fe15556B1E086BB1A72cEae" - AddrPancakewap = "0x35db01D1425685789dCc9228d47C7A5C049388d8" + Addr1InchV6 = "0x111111125421cA6dc452d289314280a0f8842A65" + AddrBebop = "0xbbbbbBB520d69a9775E85b458C58c648259FAD5F" + AddrHashflowV3 = "0x24b9d98FABF4DA1F69eE10775F240AE3dA6856fd" + AddrKyberswap = "0x6131B5fae19EA4f9D964eAc0408E4408b66337b5" + AddrKyberswapRFQ = "0x7A819Fa46734a49D0112796f9377E024c350FB26" + AddrParaswap = "0xe92b586627ccA7a83dC919cc7127196d70f55a06" + AddrUniswapX = "0x00000011F84B9aa48e5f8aA8B9897600006289Be" + AddrUniswapXV1 = "0x6000da47483062A0D734Ba3dc7576Ce6A0B645C4" + Addr0x = "0xDef1C0ded9bec7F1a1670819833240f027b25EfF" + Deployer0xV3 = "0x00000000000004533Fe15556B1E086BB1A72cEae" + AddrPancakewap = "0x35db01D1425685789dCc9228d47C7A5C049388d8" + AddrParaswapTaker = "0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57" ) var ( From 033382bfb7cbc1506842d49b345687d70c9a307a Mon Sep 17 00:00:00 2001 From: "minhthanh.vu" Date: Fri, 4 Oct 2024 14:00:08 +0700 Subject: [PATCH 3/3] check match 1inchv6 trade by remain amount --- pkg/parser/oneinchv6/oneinchv6_test.go | 6 +- pkg/parser/oneinchv6/parser.go | 76 +++++++++++++------------- 2 files changed, 40 insertions(+), 42 deletions(-) diff --git a/pkg/parser/oneinchv6/oneinchv6_test.go b/pkg/parser/oneinchv6/oneinchv6_test.go index 6fcdeaa..9590587 100644 --- a/pkg/parser/oneinchv6/oneinchv6_test.go +++ b/pkg/parser/oneinchv6/oneinchv6_test.go @@ -32,8 +32,8 @@ func TestFetchEvent(t *testing.T) { require.NoError(t, err) logs, err := client.FilterLogs(context.Background(), ethereum.FilterQuery{ BlockHash: nil, - FromBlock: big.NewInt(19517464), - ToBlock: big.NewInt(19517464), + FromBlock: big.NewInt(20890481), + ToBlock: big.NewInt(20890481), Addresses: nil, Topics: [][]common.Hash{ { @@ -49,7 +49,7 @@ func TestFetchEvent(t *testing.T) { func TestParseEvent(t *testing.T) { t.Skip("Need to add the rpc url that enables the trace call JSON-RPC") - eventRaw := `[{"address":"0x111111125421ca6dc452d289314280a0f8842a65","topics":["0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"],"data":"0x2c680fc902966ed935ddf9dc75550a32f1b2d1fba7dcaa7e4ab3b541734acddb00000000000000000000000000000000000000000000021246e3d52a827b39b7","blockNumber":"0x129d018","transactionHash":"0x4fa77df92aa4726ddbf54a41f72ca78a57035793c50e86643a25a058a27e35f4","transactionIndex":"0x6","blockHash":"0xa8eaa16d7ca8d934343bfe5fbb14bfeab272143f65fbd51f9caecf67754cfd29","logIndex":"0x53","removed":false},{"address":"0x111111125421ca6dc452d289314280a0f8842a65","topics":["0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"],"data":"0x2c680fc902966ed935ddf9dc75550a32f1b2d1fba7dcaa7e4ab3b541734acddb00000000000000000000000000000000000000000000020c219b06bffa614286","blockNumber":"0x129d018","transactionHash":"0x4fa77df92aa4726ddbf54a41f72ca78a57035793c50e86643a25a058a27e35f4","transactionIndex":"0x6","blockHash":"0xa8eaa16d7ca8d934343bfe5fbb14bfeab272143f65fbd51f9caecf67754cfd29","logIndex":"0x60","removed":false},{"address":"0x111111125421ca6dc452d289314280a0f8842a65","topics":["0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"],"data":"0xe0f3e2f9d0dbc1d72d355d2f80328b4e432d316c91a011d2fa26b56b7a39c371000000000000000000000000000000000000000000000000e1394e6d5fa7bd71","blockNumber":"0x129d018","transactionHash":"0x4fa77df92aa4726ddbf54a41f72ca78a57035793c50e86643a25a058a27e35f4","transactionIndex":"0x6","blockHash":"0xa8eaa16d7ca8d934343bfe5fbb14bfeab272143f65fbd51f9caecf67754cfd29","logIndex":"0x6a","removed":false}]` + eventRaw := `[{"address":"0x111111125421ca6dc452d289314280a0f8842a65","topics":["0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"],"data":"0xb81725973077ecc6db7dd73510be46152917f31ab7d02210aace851edc0f3fbb000000000000000000000000000000000000000000000000a5953ec9be11bd73","blockNumber":"0x13ec371","transactionHash":"0x09b63a10295261192885b38af317ba138ad7fe5828ce25524e7f0f053e250458","transactionIndex":"0x7","blockHash":"0x291e59d96ccc8b661842cfd9c4dcce1d11ed30cd4cf2b8034bff2a8d7a595785","logIndex":"0x3e","removed":false}]` events := []types.Log{} err := json.Unmarshal([]byte(eventRaw), &events) require.NoError(t, err) diff --git a/pkg/parser/oneinchv6/parser.go b/pkg/parser/oneinchv6/parser.go index ee48e97..83935ba 100644 --- a/pkg/parser/oneinchv6/parser.go +++ b/pkg/parser/oneinchv6/parser.go @@ -16,7 +16,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/ethereum/go-ethereum/common/lru" ethereumTypes "github.com/ethereum/go-ethereum/core/types" ) @@ -38,11 +37,10 @@ func init() { } type Parser struct { - abi *abi.ABI - ps *Oneinchv6Filterer - eventHash string - traceCalls *tracecall.Cache - orderHashCount lru.BasicLRU[string, int] + abi *abi.ABI + ps *Oneinchv6Filterer + eventHash string + traceCalls *tracecall.Cache } func MustNewParser(cache *tracecall.Cache) *Parser { @@ -60,11 +58,10 @@ func MustNewParser(cache *tracecall.Cache) *Parser { } return &Parser{ - ps: ps, - abi: ab, - eventHash: event.ID.String(), - traceCalls: cache, - orderHashCount: lru.NewBasicLRU[string, int](100), + ps: ps, + abi: ab, + eventHash: event.ID.String(), + traceCalls: cache, } } @@ -75,25 +72,25 @@ func (p *Parser) Topics() []string { } func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - order, err := p.buildOrderByLog(log) + order, remain, err := p.buildOrderByLog(log) if err != nil { return storage.TradeLog{}, err } order.Timestamp = blockTime * 1000 - order, err = p.detectOneInchRfqTrade(order) + order, err = p.detectOneInchRfqTrade(order, remain) if err != nil { return order, err } return order, nil } -func (p *Parser) buildOrderByLog(log ethereumTypes.Log) (storage.TradeLog, error) { +func (p *Parser) buildOrderByLog(log ethereumTypes.Log) (storage.TradeLog, *big.Int, error) { if len(log.Topics) > 0 && log.Topics[0].Hex() != p.eventHash { - return storage.TradeLog{}, parser.ErrInvalidTopic + return storage.TradeLog{}, nil, parser.ErrInvalidTopic } e, err := p.ps.ParseOrderFilled(log) if err != nil { - return storage.TradeLog{}, fmt.Errorf("error when parse log %w", err) + return storage.TradeLog{}, nil, fmt.Errorf("error when parse log %w", err) } order := storage.TradeLog{ OrderHash: common.Hash(e.OrderHash).String(), @@ -103,7 +100,7 @@ func (p *Parser) buildOrderByLog(log ethereumTypes.Log) (storage.TradeLog, error LogIndex: uint64(e.Raw.Index), EventHash: p.eventHash, } - return order, nil + return order, e.RemainingAmount, nil } func (p *Parser) ParseFromInternalCall(order storage.TradeLog, internalCall types.CallFrame) (storage.TradeLog, error) { @@ -126,7 +123,6 @@ func (p *Parser) ParseFromInternalCall(order storage.TradeLog, internalCall type if err != nil { return order, fmt.Errorf("error when decode input %w", err) } - order, err = ToTradeLog(order, contractCall) if err != nil { return order, fmt.Errorf("error when parse contract call to order %w", err) @@ -136,7 +132,7 @@ func (p *Parser) ParseFromInternalCall(order storage.TradeLog, internalCall type return order, nil } -func (p *Parser) detectOneInchRfqTrade(order storage.TradeLog) (storage.TradeLog, error) { +func (p *Parser) detectOneInchRfqTrade(order storage.TradeLog, remain *big.Int) (storage.TradeLog, error) { var ( traceCall types.CallFrame err error @@ -147,26 +143,28 @@ func (p *Parser) detectOneInchRfqTrade(order storage.TradeLog) (storage.TradeLog return order, fmt.Errorf("error when get tracecall %w", err) } - count := 0 - order, err = p.recursiveDetectOneInchRFQTrades(order, traceCall, &count) + order, err = p.recursiveDetectOneInchRFQTrades(order, traceCall, remain) if err != nil { traceData, _ := json.Marshal(traceCall) - return order, fmt.Errorf("error when parse tracecall %s %d %w", string(traceData), count, err) + return order, fmt.Errorf("error when parse tracecall %s %s %w", string(traceData), remain.String(), err) } return order, nil } -func (p *Parser) recursiveDetectOneInchRFQTrades(tradeLog storage.TradeLog, traceCall types.CallFrame, count *int) (storage.TradeLog, error) { +func (p *Parser) recursiveDetectOneInchRFQTrades(tradeLog storage.TradeLog, traceCall types.CallFrame, remain *big.Int) (storage.TradeLog, error) { var ( err error ) - if p.isOneInchRFQTrades(tradeLog.TxHash, tradeLog.OrderHash, traceCall, count) { - return p.ParseFromInternalCall(tradeLog, traceCall) + if p.isOneInchRFQTrades(tradeLog.OrderHash, traceCall, remain) { + trade, err := p.ParseFromInternalCall(tradeLog, traceCall) + if err == nil { + return trade, err + } } for _, subCall := range traceCall.Calls { - tradeLog, err = p.recursiveDetectOneInchRFQTrades(tradeLog, subCall, count) + tradeLog, err = p.recursiveDetectOneInchRFQTrades(tradeLog, subCall, remain) if err == nil { return tradeLog, nil } @@ -175,7 +173,7 @@ func (p *Parser) recursiveDetectOneInchRFQTrades(tradeLog storage.TradeLog, trac return tradeLog, parser.ErrNotFoundTrade } -func (p *Parser) isOneInchRFQTrades(txHash, orderHash string, traceCall types.CallFrame, count *int) bool { +func (p *Parser) isOneInchRFQTrades(orderHash string, traceCall types.CallFrame, remain *big.Int) bool { for _, eventLog := range traceCall.Logs { if len(eventLog.Topics) == 0 { continue @@ -185,23 +183,24 @@ func (p *Parser) isOneInchRFQTrades(txHash, orderHash string, traceCall types.Ca continue } - _, _, orderHashFromOutput, err := p.decodeOutput(traceCall.Output) + data, err := hexutil.Decode(eventLog.Data) if err != nil { return false } - - if orderHash != orderHashFromOutput { + e, err := p.ps.ParseOrderFilled(ethereumTypes.Log{ + Address: eventLog.Address, + Topics: eventLog.Topics, + Data: data, + }) + if err != nil { return false } - last, ok := p.orderHashCount.Get(fmt.Sprintf("%s-%s", txHash, orderHash)) - if !ok { - last = 0 + if orderHash != common.Hash(e.OrderHash).String() { + return false } - if last != *count { - *count += 1 + if remain.Cmp(e.RemainingAmount) != 0 { return false } - p.orderHashCount.Add(fmt.Sprintf("%s-%s", txHash, orderHash), last+1) return true } return false @@ -248,13 +247,12 @@ func (p *Parser) UseTraceCall() bool { } func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - order, err := p.buildOrderByLog(log) + order, remain, err := p.buildOrderByLog(log) if err != nil { return storage.TradeLog{}, err } order.Timestamp = blockTime * 1000 - count := 0 - return p.recursiveDetectOneInchRFQTrades(order, callFrame, &count) + return p.recursiveDetectOneInchRFQTrades(order, callFrame, remain) } func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool {