From 02656defa1751dfc65f8b9ecc9a6fafcc1823cdf Mon Sep 17 00:00:00 2001 From: linhnt Date: Thu, 12 Sep 2024 15:41:48 +0700 Subject: [PATCH] TRD-573 add message sender and interact contract --- v2/cmd/parse_log/main.go | 2 +- v2/cmd/parse_log/migrations/00001_init.up.sql | 2 ++ v2/pkg/handler/trade_logs.go | 30 ++++++++++++------- v2/pkg/parser/parser.go | 2 +- v2/pkg/parser/zxotc/parser.go | 6 +++- v2/pkg/storage/tradelogs/manager_test.go | 2 ++ v2/pkg/storage/tradelogs/test.json | 2 ++ v2/pkg/storage/tradelogs/types/trade_log.go | 20 +++++++++++++ .../tradelogs/types/trade_log_query.go | 24 ++++++++------- .../storage/tradelogs/zxotc/storage_test.go | 2 ++ 10 files changed, 67 insertions(+), 25 deletions(-) diff --git a/v2/cmd/parse_log/main.go b/v2/cmd/parse_log/main.go index 0ea2aae..7f7ce37 100644 --- a/v2/cmd/parse_log/main.go +++ b/v2/cmd/parse_log/main.go @@ -78,7 +78,7 @@ func run(c *cli.Context) error { for i, url := range rpcURL { client, err := ethclient.Dial(url) if err != nil { - panic(err) + panic(fmt.Errorf("cannot dial eth client: %w", err)) } ethClients[i] = client } diff --git a/v2/cmd/parse_log/migrations/00001_init.up.sql b/v2/cmd/parse_log/migrations/00001_init.up.sql index 0203848..4de574b 100644 --- a/v2/cmd/parse_log/migrations/00001_init.up.sql +++ b/v2/cmd/parse_log/migrations/00001_init.up.sql @@ -15,6 +15,8 @@ create table tradelogs_zerox tx_hash text not null, timestamp bigint not null, event_hash text default ''::text not null, + message_sender text default ''::text not null, + interact_contract text default ''::text not null, maker_token_price double precision default 0 not null, taker_token_price double precision default 0 not null, maker_usd_amount double precision default 0 not null, diff --git a/v2/pkg/handler/trade_logs.go b/v2/pkg/handler/trade_logs.go index 7bc1549..79207bc 100644 --- a/v2/pkg/handler/trade_logs.go +++ b/v2/pkg/handler/trade_logs.go @@ -30,11 +30,13 @@ type TradeLogHandler struct { } type logMetadata struct { - blockNumber uint64 - blockHash string - txHash string - txIndex int - timestamp uint64 + blockNumber uint64 + blockHash string + txHash string + txIndex int + timestamp uint64 + messageSender string + interactContract string } func NewTradeLogHandler(l *zap.SugaredLogger, rpc *rpcnode.Client, storage *tradelogs.Manager, parsers []parser.Parser, @@ -59,11 +61,13 @@ func (h *TradeLogHandler) ProcessBlock(blockHash string, blockNumber uint64, tim for i, call := range calls { logIndexStart = assignLogIndexes(&call.CallFrame, logIndexStart) metadata := logMetadata{ - blockNumber: blockNumber, - blockHash: blockHash, - txHash: call.TxHash, - txIndex: i, - timestamp: timestamp, + blockNumber: blockNumber, + blockHash: blockHash, + txHash: call.TxHash, + txIndex: i, + timestamp: timestamp, + messageSender: call.CallFrame.From, + interactContract: call.CallFrame.To, } tradeLogs := h.processCallFrame(call.CallFrame, metadata) @@ -136,7 +140,11 @@ func (h *TradeLogHandler) processCallFrame(call types.CallFrame, metadata logMet } // parse trade log - tradeLogs, err := p.ParseWithCallFrame(call, ethLog, metadata.timestamp) + tradeLogs, err := p.ParseWithCallFrame( + call, ethLog, metadata.timestamp, + storageTypes.WithMessageSender(metadata.messageSender), + storageTypes.WithInteractContract(metadata.interactContract), + ) if err != nil { h.l.Errorw("error when parse log", "log", ethLog, "err", err, "parser", p.Exchange()) continue diff --git a/v2/pkg/parser/parser.go b/v2/pkg/parser/parser.go index 9f41170..a5579da 100644 --- a/v2/pkg/parser/parser.go +++ b/v2/pkg/parser/parser.go @@ -48,6 +48,6 @@ type Parser interface { Topics() []string Exchange() string UseTraceCall() bool - ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) ([]storageTypes.TradeLog, error) + ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64, options ...storageTypes.Option) ([]storageTypes.TradeLog, error) LogFromExchange(log ethereumTypes.Log) bool } diff --git a/v2/pkg/parser/zxotc/parser.go b/v2/pkg/parser/zxotc/parser.go index 5cbcc9c..f0f96e3 100644 --- a/v2/pkg/parser/zxotc/parser.go +++ b/v2/pkg/parser/zxotc/parser.go @@ -96,7 +96,7 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) ([]storageTypes.TradeLog, error) { +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64, options ...storageTypes.Option) ([]storageTypes.TradeLog, error) { orderRfq, err := p.getRFQOrderParams(callFrame) if err != nil { return nil, err @@ -109,6 +109,10 @@ func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes tradeLog.Expiry = orderRfq.GetExpiry() + for _, option := range options { + option(tradeLog.CommonTradeLog) + } + return []storageTypes.TradeLog{tradeLog}, nil } diff --git a/v2/pkg/storage/tradelogs/manager_test.go b/v2/pkg/storage/tradelogs/manager_test.go index a9a4dea..f34ad75 100644 --- a/v2/pkg/storage/tradelogs/manager_test.go +++ b/v2/pkg/storage/tradelogs/manager_test.go @@ -75,6 +75,8 @@ func TestSimple(t *testing.T) { LogIndex: 198, Timestamp: 1671614015000, State: "new", + MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f", + InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff", }, }, }, diff --git a/v2/pkg/storage/tradelogs/test.json b/v2/pkg/storage/tradelogs/test.json index be14b5a..d81c132 100644 --- a/v2/pkg/storage/tradelogs/test.json +++ b/v2/pkg/storage/tradelogs/test.json @@ -10,6 +10,8 @@ "contract_address": "0x617Dee16B86534a5d792A4d7A62FB491B544111E", "block_number": 16232109, "tx_hash": "0xb61c3c802df945e215d6894a2bc3765e1175ebecdd7148dfc7aa5c9f599b9c13", + "message_sender":"0x50f77c5640e07c304432af44fb4034cd51e36f6f", + "interact_contract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff", "log_index": 198, "timestamp": 1671614015000 }, diff --git a/v2/pkg/storage/tradelogs/types/trade_log.go b/v2/pkg/storage/tradelogs/types/trade_log.go index 20ae502..9bbbb20 100644 --- a/v2/pkg/storage/tradelogs/types/trade_log.go +++ b/v2/pkg/storage/tradelogs/types/trade_log.go @@ -21,6 +21,8 @@ type CommonTradeLog struct { LogIndex uint64 `db:"log_index" json:"log_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"` + 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"` @@ -56,6 +58,8 @@ func (o *CommonTradeLog) Serialize() []interface{} { o.LogIndex, o.Timestamp, o.EventHash, + o.MessageSender, + o.InteractContract, o.MakerTokenPrice, o.TakerTokenPrice, o.MakerUsdAmount, @@ -79,6 +83,8 @@ func CommonTradeLogColumns() []string { "log_index", "timestamp", "event_hash", + "message_sender", + "interact_contract", "maker_token_price", "taker_token_price", "maker_usd_amount", @@ -86,3 +92,17 @@ func CommonTradeLogColumns() []string { "state", } } + +type Option func(c *CommonTradeLog) + +func WithMessageSender(msgSender string) Option { + return func(c *CommonTradeLog) { + c.MessageSender = msgSender + } +} + +func WithInteractContract(interactContract string) Option { + return func(c *CommonTradeLog) { + c.InteractContract = interactContract + } +} diff --git a/v2/pkg/storage/tradelogs/types/trade_log_query.go b/v2/pkg/storage/tradelogs/types/trade_log_query.go index 8a0ad2d..28ba622 100644 --- a/v2/pkg/storage/tradelogs/types/trade_log_query.go +++ b/v2/pkg/storage/tradelogs/types/trade_log_query.go @@ -1,15 +1,17 @@ package types type TradeLogsQuery struct { - FromTime uint64 `form:"from_time" json:"from_time,omitempty" binding:"required"` - ToTime uint64 `form:"to_time" json:"to_time,omitempty" binding:"required"` - ContractAddress string `form:"contract_address" json:"contract_address,omitempty"` - Maker string `form:"maker" json:"maker,omitempty"` - Taker string `form:"taker" json:"taker,omitempty"` - MakerToken string `form:"maker_token" json:"maker_token,omitempty"` - TakerToken string `form:"taker_token" json:"taker_token,omitempty"` - OrderHash string `form:"order_hash" json:"order_hash,omitempty"` - EventHash string `form:"event_hash" json:"event_hash,omitempty"` - State string `form:"state" json:"state,omitempty"` - Limit uint64 `form:"limit" json:"limit,omitempty"` + FromTime uint64 `form:"from_time" json:"from_time,omitempty" binding:"required"` + ToTime uint64 `form:"to_time" json:"to_time,omitempty" binding:"required"` + ContractAddress string `form:"contract_address" json:"contract_address,omitempty"` + Maker string `form:"maker" json:"maker,omitempty"` + Taker string `form:"taker" json:"taker,omitempty"` + MakerToken string `form:"maker_token" json:"maker_token,omitempty"` + TakerToken string `form:"taker_token" json:"taker_token,omitempty"` + OrderHash string `form:"order_hash" json:"order_hash,omitempty"` + EventHash string `form:"event_hash" json:"event_hash,omitempty"` + MessageSender string `form:"message_sender" json:"message_sender,omitempty"` + InteractContract string `form:"interact_contract" json:"interact_contract,omitempty"` + State string `form:"state" json:"state,omitempty"` + Limit uint64 `form:"limit" json:"limit,omitempty"` } diff --git a/v2/pkg/storage/tradelogs/zxotc/storage_test.go b/v2/pkg/storage/tradelogs/zxotc/storage_test.go index 9edcdaf..5aac7c2 100644 --- a/v2/pkg/storage/tradelogs/zxotc/storage_test.go +++ b/v2/pkg/storage/tradelogs/zxotc/storage_test.go @@ -75,6 +75,8 @@ func TestSimple(t *testing.T) { LogIndex: 198, Timestamp: 1671614015000, State: "new", + MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f", + InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff", }, }, },