From 8939bb96ab33e8d4faaab1f1a8d5c9b5e82ef79b Mon Sep 17 00:00:00 2001 From: hai pham Date: Tue, 27 Aug 2024 11:02:37 +0700 Subject: [PATCH] feat: support parse expiry of bebop, oneinchv6, uniswapx and update parse with call frame --- pkg/parser/bebop/parser.go | 7 +- pkg/parser/hashflow/hashflow_test.go | 3 +- pkg/parser/hashflow/parser.go | 27 +--- pkg/parser/hashflow_v3/hashflowv3_test.go | 3 +- pkg/parser/hashflow_v3/parser.go | 28 ++-- pkg/parser/kyberswap/parser.go | 5 +- .../kyberswap_rfq/kyberswap_rfq_test.go | 3 +- pkg/parser/kyberswap_rfq/parser.go | 28 ++-- pkg/parser/native/parser.go | 5 +- pkg/parser/okx/parser.go | 4 +- pkg/parser/oneinch/oneinch_test.go | 3 +- pkg/parser/oneinch/parser.go | 8 +- pkg/parser/oneinchv6/makertraits.go | 150 ++++++++++++++++++ pkg/parser/oneinchv6/makertraits_test.go | 55 +++++++ pkg/parser/oneinchv6/order_rfq.go | 10 +- pkg/parser/oneinchv6/parser.go | 11 +- pkg/parser/paraswap/paraswap_test.go | 5 +- pkg/parser/paraswap/parser.go | 28 ++-- pkg/parser/parser.go | 4 +- pkg/parser/uniswapx/parser.go | 15 +- pkg/parser/uniswapx/uniswap_test.go | 1 + pkg/parser/uniswapx_v1/parser.go | 16 +- pkg/parser/uniswapx_v1/uniswap_test.go | 1 + pkg/parser/zxotc/parser.go | 27 +--- pkg/parser/zxotc/zxotc_test.go | 5 +- pkg/parser/zxrfqv3/parser.go | 5 +- 26 files changed, 300 insertions(+), 157 deletions(-) create mode 100644 pkg/parser/oneinchv6/makertraits.go create mode 100644 pkg/parser/oneinchv6/makertraits_test.go diff --git a/pkg/parser/bebop/parser.go b/pkg/parser/bebop/parser.go index 7b288b4..7a03a46 100644 --- a/pkg/parser/bebop/parser.go +++ b/pkg/parser/bebop/parser.go @@ -267,15 +267,12 @@ func (p *Parser) buildOrderByLog(log ethereumTypes.Log, blockTime uint64) (stora return order, nil } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { order, err := p.buildOrderByLog(log, blockTime) if err != nil { return storage.TradeLog{}, err } - return p.searchTradeLog(order, types.ConvertCallFrame(callFrame)) + return p.searchTradeLog(order, callFrame) } func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { diff --git a/pkg/parser/hashflow/hashflow_test.go b/pkg/parser/hashflow/hashflow_test.go index 8a1eccd..5d68730 100644 --- a/pkg/parser/hashflow/hashflow_test.go +++ b/pkg/parser/hashflow/hashflow_test.go @@ -3,6 +3,7 @@ package hashflow import ( "context" "encoding/json" + tradelogs_type "github.com/KyberNetwork/tradelogs/pkg/types" tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" @@ -84,7 +85,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, uint64(time.Now().Unix())) + parse, err := p.ParseWithCallFrame(tradelogs_type.ConvertCallFrame(&callFrame), *eventLog, uint64(time.Now().Unix())) require.NoError(t, err) t.Log(parse) } diff --git a/pkg/parser/hashflow/parser.go b/pkg/parser/hashflow/parser.go index c6bee99..3d46e6d 100644 --- a/pkg/parser/hashflow/parser.go +++ b/pkg/parser/hashflow/parser.go @@ -4,11 +4,9 @@ import ( "encoding/json" "errors" "github.com/KyberNetwork/tradelogs/pkg/decoder" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" ethereumTypes "github.com/ethereum/go-ethereum/core/types" @@ -79,10 +77,7 @@ func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) (storage.TradeLo return res, nil } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.Parse(log, blockTime) if err != nil { return storage.TradeLog{}, err @@ -103,19 +98,13 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) getRFQOrderParams(callFrame *tradingTypes.CallFrame) (*OrderRFQ, error) { - var ( - err error - ) - contractCall := callFrame.ContractCall +func (p *Parser) getRFQOrderParams(callFrame types.CallFrame) (*OrderRFQ, error) { + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, err + } if contractCall == nil { - contractCall, err = decoder.Decode(p.abi, hexutil.Encode(callFrame.Input)) - if err != nil { - return nil, err - } - if contractCall == nil { - return nil, errors.New("missing contract_call") - } + return nil, errors.New("missing contract_call") } for _, param := range contractCall.Params { if param.Name != paramName { diff --git a/pkg/parser/hashflow_v3/hashflowv3_test.go b/pkg/parser/hashflow_v3/hashflowv3_test.go index 3cc4504..68fcf89 100644 --- a/pkg/parser/hashflow_v3/hashflowv3_test.go +++ b/pkg/parser/hashflow_v3/hashflowv3_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + tradelogs_types "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -84,7 +85,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, uint64(time.Now().Unix())) + parse, err := p.ParseWithCallFrame(tradelogs_types.ConvertCallFrame(&callFrame), *eventLog, uint64(time.Now().Unix())) require.NoError(t, err) t.Log(parse) } diff --git a/pkg/parser/hashflow_v3/parser.go b/pkg/parser/hashflow_v3/parser.go index 764e7b1..207f79b 100644 --- a/pkg/parser/hashflow_v3/parser.go +++ b/pkg/parser/hashflow_v3/parser.go @@ -3,12 +3,10 @@ package hashflowv3 import ( "encoding/json" "errors" + "github.com/KyberNetwork/tradelogs/pkg/types" "strings" "github.com/KyberNetwork/tradelogs/pkg/decoder" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/ethereum/go-ethereum/accounts/abi" @@ -87,10 +85,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.Parse(log, blockTime) if err != nil { return storage.TradeLog{}, err @@ -103,20 +98,15 @@ func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ether return tradeLog, nil } -func (p *Parser) getRFQOrderParams(callFrame *tradingTypes.CallFrame) (*OrderRFQ, error) { - var ( - err error - ) - contractCall := callFrame.ContractCall +func (p *Parser) getRFQOrderParams(callFrame types.CallFrame) (*OrderRFQ, error) { + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, err + } if contractCall == nil { - contractCall, err = decoder.Decode(p.abi, hexutil.Encode(callFrame.Input)) - if err != nil { - return nil, err - } - if contractCall == nil { - return nil, errors.New("missing contract_call") - } + return nil, errors.New("missing contract_call") } + for _, param := range contractCall.Params { if param.Name != paramName { continue diff --git a/pkg/parser/kyberswap/parser.go b/pkg/parser/kyberswap/parser.go index b2ca417..bdf2dfe 100644 --- a/pkg/parser/kyberswap/parser.go +++ b/pkg/parser/kyberswap/parser.go @@ -1,10 +1,9 @@ package kyberswap import ( + "github.com/KyberNetwork/tradelogs/pkg/types" "strings" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - ethereumTypes "github.com/ethereum/go-ethereum/core/types" "github.com/KyberNetwork/tradelogs/pkg/parser" @@ -81,7 +80,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(_ *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { +func (p *Parser) ParseWithCallFrame(_ types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { return p.Parse(log, blockTime) } diff --git a/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go b/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go index a120d05..a009d6d 100644 --- a/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go +++ b/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/KyberNetwork/tradelogs/pkg/storage" + tradelogs_type "github.com/KyberNetwork/tradelogs/pkg/types" tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" @@ -55,7 +56,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogs_type.ConvertCallFrame(&callFrame), *eventLog, 0) require.NoError(t, err) t.Log(parse) require.Equal(t, expectedTradelog, parse) diff --git a/pkg/parser/kyberswap_rfq/parser.go b/pkg/parser/kyberswap_rfq/parser.go index 1532df6..7d68227 100644 --- a/pkg/parser/kyberswap_rfq/parser.go +++ b/pkg/parser/kyberswap_rfq/parser.go @@ -4,12 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "github.com/KyberNetwork/tradelogs/pkg/types" "strings" "github.com/KyberNetwork/tradelogs/pkg/decoder" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/ethereum/go-ethereum/accounts/abi" @@ -89,10 +87,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.Parse(log, blockTime) if err != nil { return storage.TradeLog{}, err @@ -105,20 +100,15 @@ func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ether return tradeLog, nil } -func (p *Parser) getRFQOrderParams(callFrame *tradingTypes.CallFrame) (*OrderRFQ, error) { - var ( - err error - ) - contractCall := callFrame.ContractCall +func (p *Parser) getRFQOrderParams(callFrame types.CallFrame) (*OrderRFQ, error) { + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, err + } if contractCall == nil { - contractCall, err = decoder.Decode(p.abi, hexutil.Encode(callFrame.Input)) - if err != nil { - return nil, err - } - if contractCall == nil { - return nil, errors.New("missing contract_call") - } + return nil, errors.New("missing contract_call") } + for _, param := range contractCall.Params { if param.Name != paramName { continue diff --git a/pkg/parser/native/parser.go b/pkg/parser/native/parser.go index 793dd53..c9bb903 100644 --- a/pkg/parser/native/parser.go +++ b/pkg/parser/native/parser.go @@ -2,10 +2,9 @@ package native import ( "errors" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -84,6 +83,6 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(_ *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { +func (p *Parser) ParseWithCallFrame(_ types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { return p.Parse(log, blockTime) } diff --git a/pkg/parser/okx/parser.go b/pkg/parser/okx/parser.go index 735f364..c4d9943 100644 --- a/pkg/parser/okx/parser.go +++ b/pkg/parser/okx/parser.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" ethereumTypes "github.com/ethereum/go-ethereum/core/types" @@ -69,6 +69,6 @@ func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) (storage.TradeLo return res, nil } -func (p *Parser) ParseWithCallFrame(_ *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { +func (p *Parser) ParseWithCallFrame(_ types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { return p.Parse(log, blockTime) } diff --git a/pkg/parser/oneinch/oneinch_test.go b/pkg/parser/oneinch/oneinch_test.go index 3dfa124..f19ad2a 100644 --- a/pkg/parser/oneinch/oneinch_test.go +++ b/pkg/parser/oneinch/oneinch_test.go @@ -3,6 +3,7 @@ package oneinch import ( "context" "encoding/json" + tradelogs_type "github.com/KyberNetwork/tradelogs/pkg/types" "math/big" "strings" "testing" @@ -155,7 +156,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogs_type.ConvertCallFrame(&callFrame), *eventLog, 0) require.NoError(t, err) require.Equal(t, expectedTradeLog, parse) } diff --git a/pkg/parser/oneinch/parser.go b/pkg/parser/oneinch/parser.go index 889325d..691c856 100644 --- a/pkg/parser/oneinch/parser.go +++ b/pkg/parser/oneinch/parser.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" @@ -233,15 +232,12 @@ func (p *Parser) buildOrderByLog(log ethereumTypes.Log, blockTime uint64) (stora return order, nil } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.buildOrderByLog(log, blockTime) if err != nil { return storage.TradeLog{}, err } - tradeLog, err = p.recursiveDetectOneInchRFQTrades(tradeLog, types.ConvertCallFrame(callFrame)) + tradeLog, err = p.recursiveDetectOneInchRFQTrades(tradeLog, callFrame) if err != nil { return tradeLog, err } diff --git a/pkg/parser/oneinchv6/makertraits.go b/pkg/parser/oneinchv6/makertraits.go new file mode 100644 index 0000000..0743035 --- /dev/null +++ b/pkg/parser/oneinchv6/makertraits.go @@ -0,0 +1,150 @@ +package oneinchv6 + +import ( + "bytes" + "errors" + "github.com/ethereum/go-ethereum/common/math" + "math/big" + + "github.com/ethereum/go-ethereum/common" +) + +type MakerTraitsOption struct { + NoPartialFills bool + MultipleFills bool + PreInteractionCall bool + PostInteractionCall bool + CheckEpochManager bool + HasExtension bool + UsePermit2 bool + UnwrapWETH bool + + AllowedSender common.Address + Expiration int64 + NonceOrEpoch int64 + Series int64 +} + +const ( + noPartialFillsMask = 7 + multipleFillsMask = 6 + preInteractionMask = 4 + postInteractionMask = 3 + checkEpochMask = 2 + hasExtensionMask = 1 + usePermit2Mask = 0 + + unwrapWETHMask = 7 +) + +var fiveBytes = [5]byte{} + +func BuildMakerTraits(values MakerTraitsOption) *big.Int { + buff := new(bytes.Buffer) + { + var lb uint8 + if values.NoPartialFills { + lb = lb | (0xff & 1 << noPartialFillsMask) + } + if values.MultipleFills { + lb = lb | (1 << multipleFillsMask) + } + if values.PreInteractionCall { + lb = lb | (0xff & 1 << preInteractionMask) + } + if values.PostInteractionCall { + lb = lb | (0xff & 1 << postInteractionMask) + } + if values.CheckEpochManager { + lb = lb | (0xff & 1 << checkEpochMask) + } + if values.HasExtension { + lb = lb | (0xff & 1 << hasExtensionMask) + } + if values.UsePermit2 { + lb = lb | (0xff & 1 << usePermit2Mask) + } + buff.WriteByte(lb) + } + { + var unwrapETH uint8 + if values.UnwrapWETH { + unwrapETH = 1 << unwrapWETHMask + } + _ = buff.WriteByte(unwrapETH) + } + _, _ = buff.Write(fiveBytes[:]) + _, _ = buff.Write(math.PaddedBigBytes(big.NewInt(values.Series), 5)) + _, _ = buff.Write(math.PaddedBigBytes(big.NewInt(values.NonceOrEpoch), 5)) + _, _ = buff.Write(math.PaddedBigBytes(big.NewInt(values.Expiration), 5)) + _, _ = buff.Write(values.AllowedSender[10:]) + + return big.NewInt(0).SetBytes(buff.Bytes()) +} + +func DecodeMarkerTraits(data []byte) (MakerTraitsOption, error) { + res := MakerTraitsOption{} + if len(data) != common.HashLength { + return MakerTraitsOption{}, errors.New("invalid data length") + } + buff := bytes.NewBuffer(data) + { + b, _ := buff.ReadByte() + if b>>noPartialFillsMask != 0 { + res.NoPartialFills = true + } + if (b>>multipleFillsMask)&1 != 0 { + res.MultipleFills = true + } + if (b>>preInteractionMask)&1 != 0 { + res.PreInteractionCall = true + } + if (b>>postInteractionMask)&1 != 0 { + res.PostInteractionCall = true + } + if (b>>checkEpochMask)&1 != 0 { + res.CheckEpochManager = true + } + if (b>>hasExtensionMask)&1 != 0 { + res.HasExtension = true + } + if (b & 1) != 0 { + res.UsePermit2 = true + } + } + { + b, _ := buff.ReadByte() + if b>>unwrapWETHMask != 0 { + res.UnwrapWETH = true + } + } + var temp [5]byte + if _, err := buff.Read(temp[:]); err != nil { + return MakerTraitsOption{}, err + } + _, err := buff.Read(temp[:]) + if err != nil { + return MakerTraitsOption{}, err + } + series := big.NewInt(0).SetBytes(temp[:]) + res.Series = series.Int64() + _, err = buff.Read(temp[:]) + if err != nil { + return MakerTraitsOption{}, err + } + nonce := big.NewInt(0).SetBytes(temp[:]) + res.NonceOrEpoch = nonce.Int64() + _, err = buff.Read(temp[:]) + if err != nil { + return MakerTraitsOption{}, err + } + expire := big.NewInt(0).SetBytes(temp[:]) + res.Expiration = expire.Int64() + var addr common.Address + _, err = buff.Read(addr[10:]) + if err != nil { + return MakerTraitsOption{}, err + } + res.AllowedSender = addr + return res, nil +} diff --git a/pkg/parser/oneinchv6/makertraits_test.go b/pkg/parser/oneinchv6/makertraits_test.go new file mode 100644 index 0000000..1cca94e --- /dev/null +++ b/pkg/parser/oneinchv6/makertraits_test.go @@ -0,0 +1,55 @@ +package oneinchv6 + +import ( + "github.com/ethereum/go-ethereum/common/math" + "math/big" + "testing" +) + +func TestDecodeTraits(t *testing.T) { + type args struct { + MakerTraits string + HasError bool + ExpectedMakerTraits MakerTraitsOption + } + testCases := []args{ + { + MakerTraits: "74438847047041592770632009233243045888", + HasError: false, + ExpectedMakerTraits: MakerTraitsOption{ + Expiration: int64(1719942657), + }, + }, + { + MakerTraits: "13289623586853032246658166462032491053056", + HasError: false, + ExpectedMakerTraits: MakerTraitsOption{ + Expiration: int64(1724667808), + }, + }, + { + MakerTraits: "", + HasError: true, + }, + } + for _, tt := range testCases { + bb, ok := new(big.Int).SetString(tt.MakerTraits, 10) + if !ok { + if tt.HasError { + continue + } + t.Error("big.Int SetString failed") + } + got, err := DecodeMarkerTraits(math.PaddedBigBytes(bb, 32)) + if tt.HasError { + if err == nil { + t.Errorf("DecodeTraits() error = %v, wantErr %v", err, tt.HasError) + return + } + } + if got.Expiration != tt.ExpectedMakerTraits.Expiration { + t.Errorf("DecodeTraits() = %v, want %v", got.Expiration, tt.ExpectedMakerTraits.Expiration) + } + + } +} diff --git a/pkg/parser/oneinchv6/order_rfq.go b/pkg/parser/oneinchv6/order_rfq.go index bbc2258..ef3c211 100644 --- a/pkg/parser/oneinchv6/order_rfq.go +++ b/pkg/parser/oneinchv6/order_rfq.go @@ -3,11 +3,10 @@ package oneinchv6 import ( "encoding/json" "errors" - + "github.com/KyberNetwork/tradelogs/pkg/storage" tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "github.com/ethereum/go-ethereum/common" - - "github.com/KyberNetwork/tradelogs/pkg/storage" + "github.com/ethereum/go-ethereum/common/math" ) const ( @@ -36,6 +35,11 @@ func ToTradeLog(tradeLog storage.TradeLog, contractCall *tradingTypes.ContractCa tradeLog.TakerToken = common.BigToAddress(rfqOrder.TakerAsset).String() tradeLog.Maker = common.BigToAddress(rfqOrder.Maker).String() tradeLog.MakerTraits = rfqOrder.MakerTraits.String() + makerTraitsOption, err := DecodeMarkerTraits(math.PaddedBigBytes(rfqOrder.MakerTraits, 32)) + if err != nil { + return tradeLog, err + } + tradeLog.Expiry = uint64(makerTraitsOption.Expiration) } return tradeLog, nil diff --git a/pkg/parser/oneinchv6/parser.go b/pkg/parser/oneinchv6/parser.go index 6f8bb7f..c04e47d 100644 --- a/pkg/parser/oneinchv6/parser.go +++ b/pkg/parser/oneinchv6/parser.go @@ -7,14 +7,12 @@ import ( "math/big" "strings" - "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/KyberNetwork/tradelogs/pkg/abitypes" "github.com/KyberNetwork/tradelogs/pkg/decoder" "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/KyberNetwork/tradelogs/pkg/tracecall" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -248,17 +246,14 @@ func (p *Parser) UseTraceCall() bool { return true } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { order, err := p.buildOrderByLog(log) if err != nil { return storage.TradeLog{}, err } order.Timestamp = blockTime * 1000 count := 0 - return p.recursiveDetectOneInchRFQTrades(order, types.ConvertCallFrame(callFrame), &count) + return p.recursiveDetectOneInchRFQTrades(order, callFrame, &count) } func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool { diff --git a/pkg/parser/paraswap/paraswap_test.go b/pkg/parser/paraswap/paraswap_test.go index 6aaa3cf..351a020 100644 --- a/pkg/parser/paraswap/paraswap_test.go +++ b/pkg/parser/paraswap/paraswap_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/KyberNetwork/tradelogs/pkg/storage" + tradelogs_type "github.com/KyberNetwork/tradelogs/pkg/types" tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" @@ -69,7 +70,7 @@ func TestGetExpiry(t *testing.T) { p := MustNewParser() err := json.Unmarshal([]byte(rawData), &callFrame) require.NoError(t, err) - rfqOrderParam, err := p.getRFQOrderParams(&callFrame) + rfqOrderParam, err := p.getRFQOrderParams(tradelogs_type.ConvertCallFrame(&callFrame)) require.NoError(t, err) require.NotNil(t, rfqOrderParam) t.Log(rfqOrderParam) @@ -117,7 +118,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogs_type.ConvertCallFrame(&callFrame), *eventLog, 0) require.NoError(t, err) t.Log(parse) require.Equal(t, expectedTradeLog, parse) diff --git a/pkg/parser/paraswap/parser.go b/pkg/parser/paraswap/parser.go index ad19c2f..8f9538e 100644 --- a/pkg/parser/paraswap/parser.go +++ b/pkg/parser/paraswap/parser.go @@ -3,12 +3,10 @@ package paraswap import ( "encoding/json" "errors" + "github.com/KyberNetwork/tradelogs/pkg/types" "strings" "github.com/KyberNetwork/tradelogs/pkg/decoder" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/ethereum/go-ethereum/accounts/abi" @@ -87,10 +85,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.Parse(log, blockTime) if err != nil { return storage.TradeLog{}, err @@ -103,20 +98,15 @@ func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ether return tradeLog, nil } -func (p *Parser) getRFQOrderParams(callFrame *tradingTypes.CallFrame) (*OrderRFQ, error) { - var ( - err error - ) - contractCall := callFrame.ContractCall +func (p *Parser) getRFQOrderParams(callFrame types.CallFrame) (*OrderRFQ, error) { + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, err + } if contractCall == nil { - contractCall, err = decoder.Decode(p.abi, hexutil.Encode(callFrame.Input)) - if err != nil { - return nil, err - } - if contractCall == nil { - return nil, errors.New("missing contract_call") - } + return nil, errors.New("missing contract_call") } + for _, param := range contractCall.Params { if param.Name != paramName { continue diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index d1032eb..8ad3ceb 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -2,9 +2,9 @@ package parser import ( "errors" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ethereumTypes "github.com/ethereum/go-ethereum/core/types" ) @@ -48,6 +48,6 @@ type Parser interface { Topics() []string Exchange() string UseTraceCall() bool - ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) + ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) LogFromExchange(log ethereumTypes.Log) bool } diff --git a/pkg/parser/uniswapx/parser.go b/pkg/parser/uniswapx/parser.go index 10962e3..d63d650 100644 --- a/pkg/parser/uniswapx/parser.go +++ b/pkg/parser/uniswapx/parser.go @@ -2,18 +2,15 @@ package uniswapx import ( "encoding/json" - "errors" "fmt" "math/big" "strings" - "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/KyberNetwork/tradelogs/pkg/decoder" "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/KyberNetwork/tradelogs/pkg/tracecall" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" ethereumTypes "github.com/ethereum/go-ethereum/core/types" @@ -268,6 +265,9 @@ func (p *Parser) updateOrder(internal storage.TradeLog, parsed []interface{}) (s big.NewInt(int64(internal.Timestamp/1000)))) } internal.MakerTokenAmount = makerAmount.String() + if order.Info.Deadline != nil { + internal.Expiry = order.Info.Deadline.Uint64() + } return internal, nil } @@ -325,15 +325,12 @@ func (p *Parser) UseTraceCall() bool { return true } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { order, err := p.buildOrderByLog(log, blockTime) if err != nil { return storage.TradeLog{}, err } - order, err = p.recursiveDetectRFQTrades(order, types.ConvertCallFrame(callFrame)) + order, err = p.recursiveDetectRFQTrades(order, callFrame) if err != nil { return order, err } diff --git a/pkg/parser/uniswapx/uniswap_test.go b/pkg/parser/uniswapx/uniswap_test.go index 67aa724..390bdda 100644 --- a/pkg/parser/uniswapx/uniswap_test.go +++ b/pkg/parser/uniswapx/uniswap_test.go @@ -64,5 +64,6 @@ func TestParseEvent(t *testing.T) { require.Equal(t, log.Taker, "0x250A94C03b9b57C93CC5549760D59d6eAcfB136d") require.Equal(t, log.MakerTokenAmount, "42282994361466557") require.Equal(t, log.TakerTokenAmount, "146889265") + require.Equal(t, log.Expiry, uint64(1713890185)) t.Log(log) } diff --git a/pkg/parser/uniswapx_v1/parser.go b/pkg/parser/uniswapx_v1/parser.go index b2650ac..720e0fa 100644 --- a/pkg/parser/uniswapx_v1/parser.go +++ b/pkg/parser/uniswapx_v1/parser.go @@ -2,18 +2,15 @@ package uniswapxv1 import ( "encoding/json" - "errors" "fmt" "math/big" "strings" - "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/KyberNetwork/tradelogs/pkg/decoder" "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/KyberNetwork/tradelogs/pkg/tracecall" + "github.com/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" ethereumTypes "github.com/ethereum/go-ethereum/core/types" @@ -237,7 +234,9 @@ func (p *Parser) updateOrder(internal storage.TradeLog, parsed []interface{}) (s big.NewInt(int64(internal.Timestamp/1000)))) } internal.MakerTokenAmount = makerAmount.String() - + if order.Info.Deadline != nil { + internal.Expiry = order.Info.Deadline.Uint64() + } return internal, nil } @@ -275,15 +274,12 @@ func (p *Parser) UseTraceCall() bool { return true } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { order, err := p.buildOrderByLog(log, blockTime) if err != nil { return storage.TradeLog{}, err } - order, err = p.recursiveDetectRFQTrades(order, types.ConvertCallFrame(callFrame)) + order, err = p.recursiveDetectRFQTrades(order, callFrame) if err != nil { return order, err } diff --git a/pkg/parser/uniswapx_v1/uniswap_test.go b/pkg/parser/uniswapx_v1/uniswap_test.go index 5367241..4d939bd 100644 --- a/pkg/parser/uniswapx_v1/uniswap_test.go +++ b/pkg/parser/uniswapx_v1/uniswap_test.go @@ -66,5 +66,6 @@ func TestParseEvent(t *testing.T) { require.Equal(t, log.MakerToken, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") require.Equal(t, log.TakerTokenAmount, "30000000000000000") require.Equal(t, log.TakerToken, "0xEE16bd5e21cd5D27D0EAfdabbCCA0A438e97E46C") + require.Equal(t, log.Expiry, uint64(1754699687)) t.Log(log) } diff --git a/pkg/parser/zxotc/parser.go b/pkg/parser/zxotc/parser.go index c048403..e2d4360 100644 --- a/pkg/parser/zxotc/parser.go +++ b/pkg/parser/zxotc/parser.go @@ -3,12 +3,10 @@ package zxotc import ( "encoding/json" "errors" + "github.com/KyberNetwork/tradelogs/pkg/types" "strings" "github.com/KyberNetwork/tradelogs/pkg/decoder" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - "github.com/ethereum/go-ethereum/common/hexutil" - "github.com/KyberNetwork/tradelogs/pkg/parser" "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/ethereum/go-ethereum/accounts/abi" @@ -87,10 +85,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { - if callFrame == nil { - return storage.TradeLog{}, errors.New("missing call frame") - } +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.Parse(log, blockTime) if err != nil { return storage.TradeLog{}, err @@ -103,19 +98,13 @@ func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ether return tradeLog, nil } -func (p *Parser) getRFQOrderParams(callFrame *tradingTypes.CallFrame) (*OrderRFQ, error) { - var ( - err error - ) - contractCall := callFrame.ContractCall +func (p *Parser) getRFQOrderParams(callFrame types.CallFrame) (*OrderRFQ, error) { + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, err + } if contractCall == nil { - contractCall, err = decoder.Decode(p.abi, hexutil.Encode(callFrame.Input)) - if err != nil { - return nil, err - } - if contractCall == nil { - return nil, errors.New("missing contract_call") - } + return nil, errors.New("missing contract_call") } for _, param := range contractCall.Params { if param.Name != paramName { diff --git a/pkg/parser/zxotc/zxotc_test.go b/pkg/parser/zxotc/zxotc_test.go index ab70b22..8f2f7a8 100644 --- a/pkg/parser/zxotc/zxotc_test.go +++ b/pkg/parser/zxotc/zxotc_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/KyberNetwork/tradelogs/pkg/storage" + tradelogs_type "github.com/KyberNetwork/tradelogs/pkg/types" "math/big" "strings" "testing" @@ -92,7 +93,7 @@ func TestGetExpiry(t *testing.T) { p := MustNewParser() err := json.Unmarshal([]byte(rawData), &callFrame) require.NoError(t, err) - rfqOrderParam, err := p.getRFQOrderParams(&callFrame) + rfqOrderParam, err := p.getRFQOrderParams(tradelogs_type.ConvertCallFrame(&callFrame)) require.NoError(t, err) require.NotNil(t, rfqOrderParam) require.Equal(t, uint64(1719460132), rfqOrderParam.GetExpiry()) @@ -139,7 +140,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogs_type.ConvertCallFrame(&callFrame), *eventLog, 0) require.NoError(t, err) t.Log(parse) require.Equal(t, expectedTradeLog, parse) diff --git a/pkg/parser/zxrfqv3/parser.go b/pkg/parser/zxrfqv3/parser.go index e4b6d39..740577a 100644 --- a/pkg/parser/zxrfqv3/parser.go +++ b/pkg/parser/zxrfqv3/parser.go @@ -13,7 +13,6 @@ import ( "github.com/KyberNetwork/tradelogs/pkg/storage" "github.com/KyberNetwork/tradelogs/pkg/tracecall" "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" @@ -168,12 +167,12 @@ func (p *Parser) buildOrderByLog(log ethereumTypes.Log, blockTime uint64) (stora return tradeLog, nil } -func (p *Parser) ParseWithCallFrame(callFrame *tradingTypes.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) (storage.TradeLog, error) { tradeLog, err := p.buildOrderByLog(log, blockTime) if err != nil { return storage.TradeLog{}, err } - return p.recursiveDetectRFQTrades(tradeLog, types.ConvertCallFrame(callFrame), log) + return p.recursiveDetectRFQTrades(tradeLog, callFrame, log) } func getOrderHash(data []byte) (string, error) {