Skip to content

Commit

Permalink
Merge pull request #77 from KyberNetwork/update_interface
Browse files Browse the repository at this point in the history
Fix some issues
  • Loading branch information
ngocthanh1389 authored Oct 4, 2024
2 parents bedc184 + 033382b commit c8db598
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 55 deletions.
3 changes: 1 addition & 2 deletions cmd/tradelogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -116,7 +115,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)),
Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/bebop/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,7 @@ func getAggregateOrderInfo(order AggregateOrder) *big.Int {
}
return quoteTakerAmount
}

func (p *Parser) Address() string {
return parser.AddrBebop
}
4 changes: 4 additions & 0 deletions pkg/parser/hashflow_v3/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
4 changes: 4 additions & 0 deletions pkg/parser/kyberswap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/parser/kyberswap_rfq/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions pkg/parser/oneinchv6/oneinchv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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)
Expand Down
80 changes: 41 additions & 39 deletions pkg/parser/oneinchv6/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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,
}
}

Expand All @@ -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(),
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -248,17 +247,20 @@ 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 {
return strings.EqualFold(log.Address.String(), parser.Addr1InchV6) &&
len(log.Topics) > 0 &&
strings.EqualFold(log.Topics[0].String(), p.eventHash)
}

func (p *Parser) Address() string {
return parser.Addr1InchV6
}
4 changes: 4 additions & 0 deletions pkg/parser/pancakeswap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,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
}
4 changes: 4 additions & 0 deletions pkg/parser/paraswap/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 13 additions & 11 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -52,4 +53,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
}
4 changes: 4 additions & 0 deletions pkg/parser/uniswapx/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/parser/zxotc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/parser/zxrfqv3/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,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 ""
}

0 comments on commit c8db598

Please sign in to comment.