Skip to content

Commit

Permalink
add type column for 1inch tradelogs (#102)
Browse files Browse the repository at this point in the history
* add type column for 1inch tradelogs

* store maker traits as json object

* fix unit test
  • Loading branch information
linhnt3400 authored Dec 2, 2024
1 parent 6387b00 commit 33be191
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
6 changes: 6 additions & 0 deletions v2/cmd/migrations/00007_add_type_column_1inch.up.sql
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 14 additions & 1 deletion v2/pkg/parser/oneinchv6/order_rfq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion v2/pkg/storage/tradelogs/oneinch_v6/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -179,6 +183,7 @@ func tradeLogSerialize(o *storageTypes.TradeLog) []interface{} {
o.MakerUsdAmount,
o.TakerUsdAmount,
o.MakerTraits,
o.Type,
}
}

Expand Down Expand Up @@ -207,5 +212,6 @@ func tradeLogColumns() []string {
"maker_usd_amount",
"taker_usd_amount",
"maker_traits",
"type",
}
}
2 changes: 2 additions & 0 deletions v2/pkg/storage/tradelogs/oneinch_v6/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestSimple(t *testing.T) {
Timestamp: 1671614015000,
MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f",
InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
MakerTraits: []byte(`{}`),
},
},
Success: true,
Expand All @@ -89,6 +90,7 @@ func TestSimple(t *testing.T) {
TxHash: "0x0e0ec48f90f388a31c637a61ac769b9d0facebff207cb6dc8cf4fc2dacefa55f",
LogIndex: 202,
Timestamp: 1671614111000,
MakerTraits: []byte(`{}`),
},
},
Success: true,
Expand Down
7 changes: 7 additions & 0 deletions v2/pkg/storage/tradelogs/types/oneinch_v6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

type OneInchType string

const (
RFQType OneInchType = "RFQ"
)
58 changes: 31 additions & 27 deletions v2/pkg/storage/tradelogs/types/trade_log.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 33be191

Please sign in to comment.