Skip to content

Commit

Permalink
TRD-573 add message sender and interact contract
Browse files Browse the repository at this point in the history
  • Loading branch information
linhnt3400 committed Sep 12, 2024
1 parent 432e8ee commit 02656de
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 25 deletions.
2 changes: 1 addition & 1 deletion v2/cmd/parse_log/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions v2/cmd/parse_log/migrations/00001_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 19 additions & 11 deletions v2/pkg/handler/trade_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion v2/pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion v2/pkg/parser/zxotc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions v2/pkg/storage/tradelogs/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func TestSimple(t *testing.T) {
LogIndex: 198,
Timestamp: 1671614015000,
State: "new",
MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f",
InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions v2/pkg/storage/tradelogs/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"contract_address": "0x617Dee16B86534a5d792A4d7A62FB491B544111E",
"block_number": 16232109,
"tx_hash": "0xb61c3c802df945e215d6894a2bc3765e1175ebecdd7148dfc7aa5c9f599b9c13",
"message_sender":"0x50f77c5640e07c304432af44fb4034cd51e36f6f",
"interact_contract":"0xdef1c0ded9bec7f1a1670819833240f027b25eff",
"log_index": 198,
"timestamp": 1671614015000
},
Expand Down
20 changes: 20 additions & 0 deletions v2/pkg/storage/tradelogs/types/trade_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand All @@ -79,10 +83,26 @@ func CommonTradeLogColumns() []string {
"log_index",
"timestamp",
"event_hash",
"message_sender",
"interact_contract",
"maker_token_price",
"taker_token_price",
"maker_usd_amount",
"taker_usd_amount",
"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
}
}
24 changes: 13 additions & 11 deletions v2/pkg/storage/tradelogs/types/trade_log_query.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 2 additions & 0 deletions v2/pkg/storage/tradelogs/zxotc/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func TestSimple(t *testing.T) {
LogIndex: 198,
Timestamp: 1671614015000,
State: "new",
MessageSender: "0x50f77c5640e07c304432af44fb4034cd51e36f6f",
InteractContract: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
},
},
},
Expand Down

0 comments on commit 02656de

Please sign in to comment.