diff --git a/pkg/decoder/decoder.go b/pkg/decoder/decoder.go index 27e4914..41f2077 100644 --- a/pkg/decoder/decoder.go +++ b/pkg/decoder/decoder.go @@ -31,7 +31,9 @@ func Decode(ABI *abi.ABI, input string) (*tradingTypes.ContractCall, error) { if err != nil { return nil, err } - + if len(input) < 10 { + return nil, fmt.Errorf("input data is too short") + } bytes, err := hex.DecodeString(input[10:]) if err != nil { return nil, err 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..3cf74cb 100644 --- a/pkg/parser/hashflow/hashflow_test.go +++ b/pkg/parser/hashflow/hashflow_test.go @@ -3,7 +3,6 @@ package hashflow import ( "context" "encoding/json" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" "testing" @@ -14,6 +13,9 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/require" + + tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types" + tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ) const rpcURL = "" @@ -84,7 +86,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, uint64(time.Now().Unix())) + parse, err := p.ParseWithCallFrame(tradelogstype.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..59cfdea 100644 --- a/pkg/parser/hashflow_v3/hashflowv3_test.go +++ b/pkg/parser/hashflow_v3/hashflowv3_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + tradelogstypes "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(tradelogstypes.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..d82a329 100644 --- a/pkg/parser/hashflow_v3/parser.go +++ b/pkg/parser/hashflow_v3/parser.go @@ -6,11 +6,9 @@ import ( "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/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" @@ -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..ec66391 100644 --- a/pkg/parser/kyberswap/parser.go +++ b/pkg/parser/kyberswap/parser.go @@ -3,12 +3,11 @@ package kyberswap import ( "strings" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - ethereumTypes "github.com/ethereum/go-ethereum/core/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" ) @@ -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..ef2a1ab 100644 --- a/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go +++ b/pkg/parser/kyberswap_rfq/kyberswap_rfq_test.go @@ -3,13 +3,16 @@ package kyberswaprfq import ( "context" "encoding/json" + "strings" + "testing" + "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/require" - "strings" - "testing" + + tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types" + tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ) const rpcURL = "" @@ -55,7 +58,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogstype.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..b90b8e4 100644 --- a/pkg/parser/kyberswap_rfq/parser.go +++ b/pkg/parser/kyberswap_rfq/parser.go @@ -7,11 +7,9 @@ import ( "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/KyberNetwork/tradelogs/pkg/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" @@ -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..895d79d 100644 --- a/pkg/parser/oneinch/oneinch_test.go +++ b/pkg/parser/oneinch/oneinch_test.go @@ -8,16 +8,17 @@ import ( "testing" "time" - "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" - - "github.com/KyberNetwork/tradelogs/pkg/rpcnode" - "github.com/KyberNetwork/tradelogs/pkg/tracecall" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/require" + + "github.com/KyberNetwork/tradelogs/pkg/rpcnode" + "github.com/KyberNetwork/tradelogs/pkg/storage" + "github.com/KyberNetwork/tradelogs/pkg/tracecall" + tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types" + tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ) const rpcURL = "" @@ -155,7 +156,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogstype.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..f647ba6 100644 --- a/pkg/parser/oneinch/parser.go +++ b/pkg/parser/oneinch/parser.go @@ -4,8 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/KyberNetwork/tradelogs/pkg/types" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" @@ -14,6 +12,7 @@ import ( "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" @@ -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..b84717d --- /dev/null +++ b/pkg/parser/oneinchv6/makertraits.go @@ -0,0 +1,150 @@ +package oneinchv6 + +import ( + "bytes" + "errors" + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" +) + +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..6078bad --- /dev/null +++ b/pkg/parser/oneinchv6/makertraits_test.go @@ -0,0 +1,56 @@ +package oneinchv6 + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common/math" +) + +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..d9d9a53 100644 --- a/pkg/parser/paraswap/paraswap_test.go +++ b/pkg/parser/paraswap/paraswap_test.go @@ -3,8 +3,6 @@ package paraswap import ( "context" "encoding/json" - "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "math/big" "strings" "testing" @@ -15,6 +13,10 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/require" + + "github.com/KyberNetwork/tradelogs/pkg/storage" + tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types" + tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ) const rpcURL = "" @@ -69,7 +71,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(tradelogstype.ConvertCallFrame(&callFrame)) require.NoError(t, err) require.NotNil(t, rfqOrderParam) t.Log(rfqOrderParam) @@ -117,7 +119,7 @@ func TestParseWithCallFrame(t *testing.T) { continue } - parse, err := p.ParseWithCallFrame(&callFrame, *eventLog, 0) + parse, err := p.ParseWithCallFrame(tradelogstype.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..e275450 100644 --- a/pkg/parser/paraswap/parser.go +++ b/pkg/parser/paraswap/parser.go @@ -6,11 +6,9 @@ import ( "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/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" @@ -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..3d9380b 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/KyberNetwork/tradelogs/pkg/storage" - tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" + "github.com/KyberNetwork/tradelogs/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..b7e90c9 100644 --- a/pkg/parser/zxotc/parser.go +++ b/pkg/parser/zxotc/parser.go @@ -6,11 +6,9 @@ import ( "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/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" @@ -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..660daef 100644 --- a/pkg/parser/zxotc/zxotc_test.go +++ b/pkg/parser/zxotc/zxotc_test.go @@ -3,7 +3,6 @@ package zxotc import ( "context" "encoding/json" - "github.com/KyberNetwork/tradelogs/pkg/storage" "math/big" "strings" "testing" @@ -15,6 +14,8 @@ import ( "github.com/ethereum/go-ethereum/ethclient" "github.com/stretchr/testify/require" + "github.com/KyberNetwork/tradelogs/pkg/storage" + tradelogstype "github.com/KyberNetwork/tradelogs/pkg/types" tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" ) @@ -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(tradelogstype.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(tradelogstype.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) {