Skip to content

Commit

Permalink
update ver5
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocthanh1389 committed Mar 26, 2024
1 parent 406c449 commit 926b5e5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
69 changes: 55 additions & 14 deletions internal/server/backfill/dune_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

const (
limit = 100
retry = 5
limit = 100
retry = 5
OneInchV5EventHash = "0xc3b639f02b125bfa160e50739b8c44eb2d1b6908e2b6d5925c6d770f2ca78127"
OneInchV6EventHash = "0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"
)

type DuneWorker struct {
Expand Down Expand Up @@ -128,7 +130,7 @@ func DuneLogToETHLog(log dune.DuneLog) types.Log {
}
}

func (d *DuneWorker) backfillOneInch(l *zap.SugaredLogger, queryID int64, eventHash string) error {
func (d *DuneWorker) backfillOneInch(l *zap.SugaredLogger, queryID int64, version string) error {
var progress uint64 = 0
errCount := 0
for {
Expand All @@ -147,7 +149,7 @@ func (d *DuneWorker) backfillOneInch(l *zap.SugaredLogger, queryID int64, eventH

l.Infow("collected data", "progress", progress, "len", len(logs), "total", rowCount)
for _, l := range logs {
parse, err := OneInchDuneLogToTrade(l, eventHash)
parse, err := OneInchDuneLogToTrade(l, version)
if err != nil {
return err
}
Expand All @@ -164,28 +166,67 @@ func (d *DuneWorker) backfillOneInch(l *zap.SugaredLogger, queryID int64, eventH
return nil
}

type OneInchOrder struct {
type OneInchOrderV6 struct {
Maker storage.BigInt `json:"maker"`
MakerAsset storage.BigInt `json:"makerAsset"`
TakerAsset storage.BigInt `json:"takerAsset"`
}

func OneInchDuneLogToTrade(l dune.OneInchDuneLog, eventHash string) (storage.TradeLog, error) {
type OneInchOrderV5 struct {
Maker string `json:"maker"`
MakerAsset string `json:"makerAsset"`
TakerAsset string `json:"takerAsset"`
}

func OneInchDuneLogToTrade(l dune.OneInchDuneLog, version string) (storage.TradeLog, error) {
ts, err := time.Parse("2006-01-02 15:04:05.999 UTC", l.BlockTime)
if err != nil {
return storage.TradeLog{}, err
}
order := OneInchOrder{}
if err := json.Unmarshal([]byte(l.Order), &order); err != nil {
return storage.TradeLog{}, err
makerTokenAmount, takerTokenAmount, maker, makerAsset, takerAsset, eventHash := "", "", "", "", "", ""
switch version {
case "v5":
order := OneInchOrderV5{}
if err := json.Unmarshal([]byte(l.Order), &order); err != nil {
return storage.TradeLog{}, err
}
maker = order.Maker
makerAsset = order.MakerAsset
takerAsset = order.TakerAsset
if err := json.Unmarshal(l.Output0, &makerTokenAmount); err != nil {
return storage.TradeLog{}, err
}
if err := json.Unmarshal(l.Output1, &takerTokenAmount); err != nil {
return storage.TradeLog{}, err
}
eventHash = OneInchV5EventHash
default:
order := OneInchOrderV6{}
if err := json.Unmarshal([]byte(l.Order), &order); err != nil {
return storage.TradeLog{}, err
}
maker = order.Maker.Hex()
makerAsset = order.MakerAsset.Hex()
takerAsset = order.TakerAsset.Hex()
var tmp storage.BigInt
if err := json.Unmarshal(l.Output0, &tmp); err != nil {
return storage.TradeLog{}, err
}
makerTokenAmount = tmp.String()
if err := json.Unmarshal(l.Output1, &tmp); err != nil {
return storage.TradeLog{}, err
}
takerTokenAmount = tmp.String()
eventHash = OneInchV6EventHash
}

return storage.TradeLog{
OrderHash: l.Output2,
Maker: order.Maker.Hex(),
MakerToken: order.MakerAsset.Hex(),
TakerToken: order.TakerAsset.Hex(),
MakerTokenAmount: l.Output0.String(),
TakerTokenAmount: l.Output1.String(),
Maker: maker,
MakerToken: makerAsset,
TakerToken: takerAsset,
MakerTokenAmount: makerTokenAmount,
TakerTokenAmount: takerTokenAmount,
ContractAddress: l.ContractAddress,
BlockNumber: l.BlockNumber,
TxHash: l.TxHash,
Expand Down
6 changes: 1 addition & 5 deletions internal/server/backfill/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"go.uber.org/zap"
)

const (
OneInchV6EventHash = "0xfec331350fce78ba658e082a71da20ac9f8d798a99b3c79681c8440cbfe77e07"
)

// Server to serve the service.
type Server struct {
l *zap.SugaredLogger
Expand Down Expand Up @@ -170,7 +166,7 @@ func (s *Server) backfillOneinch(c *gin.Context) {
return
}

if err := s.dune.backfillOneInch(s.l.With("reqID", xid.New().String()), params.QueryID, OneInchV6EventHash); err != nil {
if err := s.dune.backfillOneInch(s.l.With("reqID", xid.New().String()), params.QueryID, params.Version); err != nil {
responseErr(c, err)
return
}
Expand Down
3 changes: 2 additions & 1 deletion internal/server/backfill/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package server

type BackFillOneInchRequest struct {
QueryID int64 `json:"query_id" binding:"required"`
QueryID int64 `json:"query_id" binding:"required"`
Version string `json:"version"`
}
20 changes: 9 additions & 11 deletions pkg/dune/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"net/http"
"time"

"github.com/KyberNetwork/tradelogs/pkg/storage"
)

type Client struct {
Expand Down Expand Up @@ -168,13 +166,13 @@ func (c *Client) GetLastestExecuteResult(queryID int64, limit, offset uint64, ou
}

type OneInchDuneLog struct {
ContractAddress string `json:"contract_address"`
EventIndex uint64 `json:"evt_index"`
TxHash string `json:"call_tx_hash"`
BlockTime string `json:"call_block_time"`
BlockNumber uint64 `json:"call_block_number"`
Order string `json:"order"`
Output0 storage.BigInt `json:"output_0"`
Output1 storage.BigInt `json:"output_1"`
Output2 string `json:"output_2"`
ContractAddress string `json:"contract_address"`
EventIndex uint64 `json:"evt_index"`
TxHash string `json:"call_tx_hash"`
BlockTime string `json:"call_block_time"`
BlockNumber uint64 `json:"call_block_number"`
Order string `json:"order"`
Output0 json.RawMessage `json:"output_0"`
Output1 json.RawMessage `json:"output_1"`
Output2 string `json:"output_2"`
}

0 comments on commit 926b5e5

Please sign in to comment.