diff --git a/v2/cmd/migrations/00007_add_type_column_1inch.up.sql b/v2/cmd/migrations/00007_add_type_column_1inch.up.sql new file mode 100644 index 0000000..d89a6f9 --- /dev/null +++ b/v2/cmd/migrations/00007_add_type_column_1inch.up.sql @@ -0,0 +1,6 @@ +alter table tradelogs_oneinch_v6 add column type text default ''::text not null; + +alter table tradelogs_oneinch_v6 + alter column maker_traits drop default, + alter column maker_traits type json using maker_traits::json, + alter column maker_traits set default '{}'::json; \ No newline at end of file diff --git a/v2/pkg/parser/oneinchv6/order_rfq.go b/v2/pkg/parser/oneinchv6/order_rfq.go index bc0047a..3ed74c4 100644 --- a/v2/pkg/parser/oneinchv6/order_rfq.go +++ b/v2/pkg/parser/oneinchv6/order_rfq.go @@ -11,6 +11,7 @@ import ( tradingTypes "github.com/KyberNetwork/tradinglib/pkg/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" + "k8s.io/apimachinery/pkg/util/sets" ) const ( @@ -19,10 +20,15 @@ const ( argsParamName = "args" ) +var fillContractOrderNameSet = sets.NewString("fillContractOrder", "fillContractOrderArgs") + func ToTradeLog(tradeLog storageTypes.TradeLog, contractCall *tradingTypes.ContractCall) (storageTypes.TradeLog, error) { if contractCall == nil { return tradeLog, errors.New("contract call is empty") } + if fillContractOrderNameSet.Has(contractCall.Name) { + tradeLog.Type = storageTypes.RFQType + } for _, param := range contractCall.Params { if param.Name != paramName { continue @@ -44,11 +50,18 @@ func ToTradeLog(tradeLog storageTypes.TradeLog, contractCall *tradingTypes.Contr tradeLog.MakerTokenOriginAmount = rfqOrder.MakingAmount.String() tradeLog.TakerTokenOriginAmount = rfqOrder.TakingAmount.String() - tradeLog.MakerTraits = rfqOrder.MakerTraits.String() makerTraitsOption, err := DecodeMarkerTraits(math.PaddedBigBytes(rfqOrder.MakerTraits, 32)) if err != nil { return tradeLog, err } + tradeLog.MakerTraits, err = json.Marshal(makerTraitsOption) + if err != nil { + return tradeLog, err + } + + if !makerTraitsOption.NoPartialFills && !makerTraitsOption.MultipleFills { + tradeLog.Type = storageTypes.RFQType + } tradeLog.Expiry = uint64(makerTraitsOption.Expiration) // if maker is Permit2WitnessProxy diff --git a/v2/pkg/storage/tradelogs/oneinch_v6/storage.go b/v2/pkg/storage/tradelogs/oneinch_v6/storage.go index 2abf951..8fa47c5 100644 --- a/v2/pkg/storage/tradelogs/oneinch_v6/storage.go +++ b/v2/pkg/storage/tradelogs/oneinch_v6/storage.go @@ -68,7 +68,8 @@ func (s *Storage) Insert(orders []storageTypes.TradeLog) error { taker_token_price=excluded.taker_token_price, maker_usd_amount=excluded.maker_usd_amount, taker_usd_amount=excluded.taker_usd_amount, - maker_traits=excluded.maker_traits + maker_traits=excluded.maker_traits, + type=excluded.type `).ToSql() if err != nil { s.l.Errorw("Error build insert", "error", err) @@ -155,6 +156,9 @@ func (s *Storage) Delete(blocks []uint64) error { } func tradeLogSerialize(o *storageTypes.TradeLog) []interface{} { + if o.MakerTraits == nil { + o.MakerTraits = []byte(`{}`) + } return []interface{}{ o.OrderHash, strings.ToLower(o.Maker), @@ -179,6 +183,7 @@ func tradeLogSerialize(o *storageTypes.TradeLog) []interface{} { o.MakerUsdAmount, o.TakerUsdAmount, o.MakerTraits, + o.Type, } } @@ -207,5 +212,6 @@ func tradeLogColumns() []string { "maker_usd_amount", "taker_usd_amount", "maker_traits", + "type", } } diff --git a/v2/pkg/storage/tradelogs/oneinch_v6/storage_test.go b/v2/pkg/storage/tradelogs/oneinch_v6/storage_test.go index 818c519..d6321c9 100644 --- a/v2/pkg/storage/tradelogs/oneinch_v6/storage_test.go +++ b/v2/pkg/storage/tradelogs/oneinch_v6/storage_test.go @@ -65,6 +65,7 @@ func TestSimple(t *testing.T) { Timestamp: 1671614015000, MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f", InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff", + MakerTraits: []byte(`{}`), }, }, Success: true, @@ -89,6 +90,7 @@ func TestSimple(t *testing.T) { TxHash: "0x0e0ec48f90f388a31c637a61ac769b9d0facebff207cb6dc8cf4fc2dacefa55f", LogIndex: 202, Timestamp: 1671614111000, + MakerTraits: []byte(`{}`), }, }, Success: true, diff --git a/v2/pkg/storage/tradelogs/types/oneinch_v6.go b/v2/pkg/storage/tradelogs/types/oneinch_v6.go new file mode 100644 index 0000000..64073f1 --- /dev/null +++ b/v2/pkg/storage/tradelogs/types/oneinch_v6.go @@ -0,0 +1,7 @@ +package types + +type OneInchType string + +const ( + RFQType OneInchType = "RFQ" +) diff --git a/v2/pkg/storage/tradelogs/types/trade_log.go b/v2/pkg/storage/tradelogs/types/trade_log.go index 673655f..1de1296 100644 --- a/v2/pkg/storage/tradelogs/types/trade_log.go +++ b/v2/pkg/storage/tradelogs/types/trade_log.go @@ -1,34 +1,38 @@ package types -import "strings" +import ( + "encoding/json" + "strings" +) type TradeLog struct { - Exchange string `json:"exchange"` - OrderHash string `db:"order_hash" json:"order_hash,omitempty"` - Maker string `db:"maker" json:"maker,omitempty"` - Taker string `db:"taker" json:"taker,omitempty"` - MakerToken string `db:"maker_token" json:"maker_token,omitempty"` - TakerToken string `db:"taker_token" json:"taker_token,omitempty"` - MakerTokenAmount string `db:"maker_token_amount" json:"maker_token_amount,omitempty"` - TakerTokenAmount string `db:"taker_token_amount" json:"taker_token_amount,omitempty"` - MakerTokenOriginAmount string `db:"maker_token_origin_amount" json:"maker_token_origin_amount,omitempty"` - TakerTokenOriginAmount string `db:"taker_token_origin_amount" json:"taker_token_origin_amount,omitempty"` - ContractAddress string `db:"contract_address" json:"contract_address,omitempty"` - BlockNumber uint64 `db:"block_number" json:"block_number,omitempty"` - TxHash string `db:"tx_hash" json:"tx_hash,omitempty"` - LogIndex uint64 `db:"log_index" json:"log_index,omitempty"` - TradeIndex uint64 `db:"trade_index" json:"trade_index,omitempty"` - Timestamp uint64 `db:"timestamp" json:"timestamp,omitempty"` - EventHash string `db:"event_hash" json:"event_hash,omitempty"` - MessageSender string `db:"message_sender" json:"message_sender,omitempty"` - TxOrigin string `db:"tx_origin" json:"tx_origin,omitempty"` - InteractContract string `db:"interact_contract" json:"interact_contract,omitempty"` - MakerTraits string `db:"maker_traits" json:"maker_traits,omitempty"` - Expiry uint64 `db:"expiration_date" json:"expiration_date"` - MakerTokenPrice *float64 `db:"maker_token_price" json:"maker_token_price"` - TakerTokenPrice *float64 `db:"taker_token_price" json:"taker_token_price"` - MakerUsdAmount *float64 `db:"maker_usd_amount" json:"maker_usd_amount"` - TakerUsdAmount *float64 `db:"taker_usd_amount" json:"taker_usd_amount"` + Exchange string `json:"exchange"` + OrderHash string `db:"order_hash" json:"order_hash,omitempty"` + Maker string `db:"maker" json:"maker,omitempty"` + Taker string `db:"taker" json:"taker,omitempty"` + MakerToken string `db:"maker_token" json:"maker_token,omitempty"` + TakerToken string `db:"taker_token" json:"taker_token,omitempty"` + MakerTokenAmount string `db:"maker_token_amount" json:"maker_token_amount,omitempty"` + TakerTokenAmount string `db:"taker_token_amount" json:"taker_token_amount,omitempty"` + MakerTokenOriginAmount string `db:"maker_token_origin_amount" json:"maker_token_origin_amount,omitempty"` + TakerTokenOriginAmount string `db:"taker_token_origin_amount" json:"taker_token_origin_amount,omitempty"` + ContractAddress string `db:"contract_address" json:"contract_address,omitempty"` + BlockNumber uint64 `db:"block_number" json:"block_number,omitempty"` + TxHash string `db:"tx_hash" json:"tx_hash,omitempty"` + LogIndex uint64 `db:"log_index" json:"log_index,omitempty"` + TradeIndex uint64 `db:"trade_index" json:"trade_index,omitempty"` + Timestamp uint64 `db:"timestamp" json:"timestamp,omitempty"` + EventHash string `db:"event_hash" json:"event_hash,omitempty"` + MessageSender string `db:"message_sender" json:"message_sender,omitempty"` + TxOrigin string `db:"tx_origin" json:"tx_origin,omitempty"` + InteractContract string `db:"interact_contract" json:"interact_contract,omitempty"` + MakerTraits json.RawMessage `db:"maker_traits" json:"maker_traits,omitempty"` + Expiry uint64 `db:"expiration_date" json:"expiration_date"` + MakerTokenPrice *float64 `db:"maker_token_price" json:"maker_token_price"` + TakerTokenPrice *float64 `db:"taker_token_price" json:"taker_token_price"` + MakerUsdAmount *float64 `db:"maker_usd_amount" json:"maker_usd_amount"` + TakerUsdAmount *float64 `db:"taker_usd_amount" json:"taker_usd_amount"` + Type OneInchType `db:"type" json:"type"` } // CommonTradeLogSerialize used for exchanges only storing fields in common trade logs,