Skip to content

Commit

Permalink
add txorigin to tradelogs v1 and solver table to tradelogs v2
Browse files Browse the repository at this point in the history
  • Loading branch information
iostream1308 committed Dec 30, 2024
1 parent 553ac00 commit 70be5e1
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cmd/tradelogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func run(c *cli.Context) error {
l.Infow("dial rpc node successfully", "node", url[:10]+"***"+url[len(url)-10:])
ethClients[i] = client
}

traceCalls := tracecall.NewCache(rpcnode.NewClient(l, ethClients...))
rpcNodeClient := rpcnode.NewClient(l, ethClients...)
traceCalls := tracecall.NewCache(rpcNodeClient)

parsers := []parser.Parser{kyberswap.MustNewParser(),
zxotc.MustNewParser(),
Expand All @@ -131,7 +131,7 @@ func run(c *cli.Context) error {
}

tradeLogChan := make(chan storage.TradeLog, 1000)
w, err := worker.New(l, s, listener, priceFiller, tradeLogChan, parsers)
w, err := worker.New(l, s, listener, priceFiller, tradeLogChan, parsers, rpcNodeClient)
if err != nil {
l.Errorw("Error while init worker")
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE tradelogs
ADD COLUMN tx_origin TEXT DEFAULT NULL;
35 changes: 22 additions & 13 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/KyberNetwork/tradelogs/pkg/evmlistenerclient"
"github.com/KyberNetwork/tradelogs/pkg/parser"
"github.com/KyberNetwork/tradelogs/pkg/pricefiller"
"github.com/KyberNetwork/tradelogs/pkg/rpcnode"
"github.com/KyberNetwork/tradelogs/pkg/storage"
"github.com/KyberNetwork/tradinglib/pkg/metrics"
ethTypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -24,24 +25,26 @@ const (
)

type Worker struct {
listener *evmlistenerclient.Client
l *zap.SugaredLogger
s *storage.Storage
p []parser.Parser
priceFiller *pricefiller.PriceFiller
tradeLogChan chan storage.TradeLog
listener *evmlistenerclient.Client
l *zap.SugaredLogger
s *storage.Storage
p []parser.Parser
priceFiller *pricefiller.PriceFiller
tradeLogChan chan storage.TradeLog
rpcNodeClient *rpcnode.Client
}

func New(l *zap.SugaredLogger, s *storage.Storage, listener *evmlistenerclient.Client,
priceFiller *pricefiller.PriceFiller, tradeLogChan chan storage.TradeLog,
parsers []parser.Parser) (*Worker, error) {
parsers []parser.Parser, rpcNodeClient *rpcnode.Client) (*Worker, error) {
return &Worker{
listener: listener,
l: l,
s: s,
p: parsers,
priceFiller: priceFiller,
tradeLogChan: tradeLogChan,
listener: listener,
l: l,
s: s,
p: parsers,
priceFiller: priceFiller,
tradeLogChan: tradeLogChan,
rpcNodeClient: rpcNodeClient,
}, nil
}

Expand Down Expand Up @@ -132,6 +135,12 @@ func (w *Worker) processMessages(m []evmlistenerclient.Message) error {
}
continue
}
txOrigin, err := w.rpcNodeClient.GetTxOriginByTxHash(context.Background(), order.TxHash)
if err != nil {
w.l.Errorw("error when get tx origin", "txHash", order.TxHash, "err", err)
continue
}
order.TxOrigin = strings.ToLower(txOrigin.Hex())
insertOrders = append(insertOrders, order)
}
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/rpcnode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package rpcnode

import (
"context"
"fmt"

"github.com/KyberNetwork/tradelogs/pkg/types"
"github.com/ethereum/go-ethereum/common"
ethereum_types "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -40,3 +43,22 @@ func (c *Client) FetchTraceCall(ctx context.Context, txHash string) (types.CallF
}
return result, err
}

func (c *Client) GetTxOriginByTxHash(ctx context.Context, txHash string) (common.Address, error) {
for _, ethClient := range c.ethClient {
tx, _, err := ethClient.TransactionByHash(ctx, common.HexToHash(txHash))
if err != nil {
return common.Address{}, err
}
fmt.Println(tx.Type())
sender, err := ethereum_types.Sender(ethereum_types.NewCancunSigner(tx.ChainId()), tx)
if err != nil {
return common.Address{}, err
}
if sender != (common.Address{}) {
return sender, nil
}

}
return common.Address{}, fmt.Errorf("failed to get sender")
}
23 changes: 23 additions & 0 deletions pkg/rpcnode/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package rpcnode

import (
"context"
"testing"

"github.com/ethereum/go-ethereum/ethclient"
)

func TestGetSender(t *testing.T) {
t.Skip("need rpc url")
rpcURL := ""
ethClient, err := ethclient.Dial(rpcURL)
if err != nil {
panic(err)
}
rpcnode := NewClient(nil, ethClient)
from, err := rpcnode.GetTxOriginByTxHash(context.Background(), "0x9570e65ab98a007f69e485a8c90ec7256cffedb9d7983cf8d2f0a566ea3c46a3")
if err != nil {
t.Log(err)
}
t.Log(from)
}
2 changes: 2 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s *Storage) Insert(orders []TradeLog) error {
log_index=excluded.log_index,
timestamp=excluded.timestamp,
event_hash=excluded.event_hash,
tx_origin=excluded.tx_origin,
maker_traits=excluded.maker_traits,
maker_token_price=excluded.maker_token_price,
taker_token_price=excluded.taker_token_price,
Expand Down Expand Up @@ -153,6 +154,7 @@ func tradelogsColumns() []string {
"log_index",
"timestamp",
"event_hash",
"tx_origin",
"maker_traits",
"maker_token_price",
"taker_token_price",
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TradeLog 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"`
TxOrigin string `db:"tx_origin" json:"tx_origin,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 @@ -69,6 +70,7 @@ func (o *TradeLog) Serialize() []interface{} {
o.LogIndex,
o.Timestamp,
o.EventHash,
o.TxOrigin,
o.MakerTraits,
o.MakerTokenPrice,
o.TakerTokenPrice,
Expand Down
75 changes: 75 additions & 0 deletions v2/cmd/migrations/00014_add_cow_solvers_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
CREATE TABLE IF NOT EXISTS solver (
address TEXT NOT NULL,
solver_name TEXT NOT NULL,
PRIMARY KEY(address)
);

INSERT INTO solver (address, solver_name) VALUES
('0x9dfc9bb0fff2dc96728d2bb94eacee6ba3592351', 'cow-Rizzolver'),
('0x2854c9a92cd1dc65bddf45afe397d9d75d4718c8', 'cow-Rizzolver'),
('0x4fc4a61a3b99a1ad4a61b03f3752ca12b4a17646', 'cow-Rizzolver'),
('0x34717040928d7fd8154d4612f3228eff14521023', 'cow-Laita'),
('0xd0ba1b1782fbde45edab392428f60e14827d08ec', 'cow-Laita'),
('0x95480d3f27658e73b2785d30beb0c847d78294c7', 'cow-Fractal'),
('0x755bae1cd46c9c27a3230aef0ce923bda13d29f7', 'cow-Fractal'),
('0x2a2883ade8ce179265f12fc7b48a4b50b092f1fd', 'cow-Fractal'),
('0xab11302cb4f7c417e527a4d39c22aa9f04edb07d', 'cow-Fractal'),
('0x4339889fd9dfca20a423fba011e9dff1c856caeb', 'cow-GlueX_Protocol'),
('0x08924194cedd747dc85025b2b30a98d3ae1ce21d', 'cow-GlueX_Protocol'),
('0xbada5552a3e5e2fb57db982e29257821a2cf192d', 'cow-Project_Blanc'),
('0xc7899ff6a3ac2ff59261bd960a8c880df06e1041', 'cow-Barter'),
('0xbf54079c9bc879ae4dd6bc79bce11d3988fd9c2b', 'cow-Barter'),
('0x5f7a6aeec3d3e80558278632954da4b730996f83', 'cow-Barter'),
('0xb9332b6301e5983272c30dd5b48a4e3b1664511b', 'cow-Baseline'),
('0x01246d541e732d7f15d164331711edff217e4665', 'cow-Baseline'),
('0x00806daa2cfe49715ea05243ff259deb195760fc', 'cow-Quasilabs'),
('0x9b7e7f21d98f21c0354035798c40e9040e25787f', 'cow-Quasilabs'),
('0x68005a99946447be44d594832e1b164f6c034e63', 'cow-Quasilabs'),
('0x8f70a86c1309d8b1f5befc58948e7386fd495875', 'cow-Tsolver'),
('0xa432cea087311d7cd07925d70f799ee94e7893a4', 'cow-Tsolver'),
('0xc10d4dfda62227d9ec23ab0010e2942e48338a60', 'cow-Apollo'),
('0xa08b00576aee8d8dd960e08298fac9fd7c756e36', 'cow-Apollo'),
('0x0ddcb0769a3591230caa80f85469240b71442089', 'cow-Seasolver'),
('0xac6cc8e2f0232b5b213503257c861235f4ed42c1', 'cow-Seasolver'),
('0xe3067c7c27c1038de4e8ad95a83b927d23dfbd99', 'cow-PLM'),
('0x27ac9ed6b768d232855473600a7880a974b71780', 'cow-PLM'),
('0x28b1bd44996105b5c14c4de41093226ff78a4eb1', 'cow-0x'),
('0xcc73072b53697911ff394ae01d3de59c9900b0b0', 'cow-0x'),
('0x6f799f4bf6c1c56fb8d890e9e0fff2934b0de157', 'cow-Gnosis_1inch'),
('0x8616dcdfcecbde13ccd89eac358dc5abda79ec31', 'cow-Gnosis_1inch'),
('0x2224eaacc7c2dbf85d5355bab9e9271e01d30b55', 'cow-Sector_Finance'),
('0xc9f2e6ea1637e499406986ac50ddc92401ce1f58', 'cow-PropellerSwap'),
('0x3bf7c0e1728fb64bb0973b1e2a342f1c7eb1bb8e', 'cow-PropellerSwap'),
('0x9528e8c42f7e109baded964e2d927fd5b6ca71e9', 'cow-Odos'),
('0xf13eaf9093a210ebdaba581f5448ffa545ee2e65', 'cow-Odos'),
('0x0ab21031124af2165586fbb495d93725a372c227', 'cow-Enso'),
('0xeada531bc5361c9287088d857512e0ca812eaa68', 'cow-Enso'),
('0xa883710b6dbf008a1cc25722c54583e35884a209', 'cow-Horadrim'),
('0x2c3a1c33d96c9dca1c34eb234b1e65f79deae60e', 'cow-Horadrim'),
('0x8a75ee64b4a40f679ad98bcc38312702971e07b7', 'cow-OneBit_Quant'),
('0xe2823b5bd541ba8bff3d4bf55396567440772eab', 'cow-OneBit_Quant'),
('0xd50ecb485dcf5d97266122dfed979587dd8923ac', 'cow-Gnosis_BalancerSOR'),
('0x849bbdf910465913272a8262dda44279a82c5c76', 'cow-Gnosis_BalancerSOR'),
('0xc74b656bd2ebe313d26d1ac02bcf95b137d1c857', 'cow-Gnosis_0x'),
('0xa697c60706210a5ec6f9ccce364c507e604d9462', 'cow-Gnosis_0x'),
('0xdd2e786980cd58acc5f64807b354c981f4094936', 'cow-Gnosis_ParaSwap'),
('0x460aac34690189911ccb49d36e55ab548cf8a9a1', 'cow-Gnosis_ParaSwap'),
('0xff662eedb413273b6727e1f59755607ca6876044', 'cow-OpenOcean_Aggregator'),
('0x9902f0b57b6b8b2fa7339cd3efe0710cf63c86d6', 'cow-OpenOcean_Aggregator'),
('0xbeaf89aec78a2990be29e2a317feed6b75bc78cd', 'cow-Quasimodo'),
('0xaac451d13cf8d6915f859f4c7bc26da2df10eca6', 'cow-Quasimodo'),
('0x8646ee3c5e82b495be8f9fe2f2f213701eed0edc', 'cow-Seasolver_v2'),
('0x94aef67903bfe8bf65193a78074c887ba901d043', 'cow-Seasolver_v2'),
('0x16c473448e770ff647c69cbe19e28528877fba1b', 'cow-Copium_Capital'),
('0x008300082c3000009e63680088f8c7f4d3ff2e87', 'cow-Copium_Capital'),
('0x8e8c00ad7011aabea0e06e984cfa7194cf8b16b0', 'cow-Copium_Capital'),
('0xa5559c2e1302c5ce82582a6b1e4aec562c2fbcf4', 'cow-Project_Blanc'),
('0xbada55babee5d2b7f3b551f9da846838760e068c', 'cow-Project_Blanc'),
('0xd1508a211d98bb81195dc1f9533edcdf68adf036', 'cow-Furucombo'),
('0x279fb872beaf64e94890376725c423c0820eda97', 'cow-Furucombo'),
('0x6bf97afe2d2c790999cded2a8523009eb8a0823f', 'cow-Portus'),
('0x5131590ca2e9d3edc182581352b289dcae83430c', 'cow-Portus'),
('0x1a7a08423ab83e7939bf1df95952347d425e0a0a', 'cow-Naive'),
('0x854490ef1d402d4f6fce05abefe1c676eb0dcd74', 'cow-ApeOut_1Inch'),
('0x047a2fbe8aef590d4eb8942426a24970af301875', 'cow-Laertes'),
('0xdf4296e752fd08b2d7db4101b98a48b74e59fd56', 'cow-Legacy');
35 changes: 34 additions & 1 deletion v2/internal/server/tradelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func (s *TradeLogs) register() {
s.r.GET("/tradelogs", s.getTradeLogs)
s.r.GET("/tokens", s.getTokens)
s.r.GET("/makers", s.getMakerName)
s.r.POST("/add_makers", s.addMakerName)
s.r.POST("/makers", s.addMakerName)
s.r.GET("/solvers", s.getSolvers)
s.r.POST("/solvers", s.addSolvers)
}

func (s *TradeLogs) getTradeLogs(c *gin.Context) {
Expand Down Expand Up @@ -152,3 +154,34 @@ func (s *TradeLogs) addMakerName(c *gin.Context) {
"data": queries,
})
}

func (s *TradeLogs) getSolvers(c *gin.Context) {
data, err := s.dashStorage.GetSolvers()
if err != nil {
responseErr(c, http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": data,
})
}

func (s *TradeLogs) addSolvers(c *gin.Context) {
var queries []dashboardTypes.Solver

if err := c.ShouldBindJSON(&queries); err != nil {
responseErr(c, http.StatusBadRequest, err)
return
}

if err := s.dashStorage.InsertSolver(queries); err != nil {
responseErr(c, http.StatusInternalServerError, err)
return
}

c.JSON(http.StatusOK, gin.H{
"success": true,
"data": queries,
})
}
47 changes: 47 additions & 0 deletions v2/pkg/storage/dashboard/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
tokenTable = "token"
makerNameTable = "maker_name"
solverTable = "solver"
)

type Storage struct {
Expand Down Expand Up @@ -85,6 +86,34 @@ func (s *Storage) InsertMakerName(makerName []types.MakerName) error {
return nil
}

func (s *Storage) InsertSolver(solvers []types.Solver) error {
if len(solvers) == 0 {
return nil
}

b := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Insert(solverTable).Columns(
types.SolverColumns()...,
)
for _, solver := range solvers {
b = b.Values(
solver.SerializeSolver()...,
)
}
q, p, err := b.Suffix(`ON CONFLICT (address) DO UPDATE
SET
solver_name=excluded.solver_name
`).ToSql()
if err != nil {
s.l.Errorw("Error build insert", "error", err)
return err
}
if _, err := s.db.Exec(q, p...); err != nil {
s.l.Errorw("Error exec insert", "sql", q, "arg", p, "error", err)
return err
}
return nil
}

// ----------------------------get----------------------------
func (s *Storage) GetTokens(query types.TokenQuery) ([]types.Token, error) {
builder := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
Expand Down Expand Up @@ -135,3 +164,21 @@ func (s *Storage) GetMakerName() ([]types.MakerName, error) {

return makerName, nil
}

func (s *Storage) GetSolvers() ([]types.Solver, error) {
builder := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).
Select(types.SolverColumns()...).
From(solverTable)

q, p, err := builder.ToSql()
if err != nil {
return nil, err
}

var solvers []types.Solver
if err := s.db.Select(&solvers, q, p...); err != nil {
return nil, err
}

return solvers, nil
}
22 changes: 22 additions & 0 deletions v2/pkg/storage/dashboard/types/solver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package types

import "strings"

type Solver struct {
Address string `db:"address" json:"address"`
SolverName string `db:"solver_name" json:"solver_name"`
}

func (o *Solver) SerializeSolver() []interface{} {
return []interface{}{
strings.ToLower(o.Address),
o.SolverName,
}
}

func SolverColumns() []string {
return []string{
"address",
"solver_name",
}
}

0 comments on commit 70be5e1

Please sign in to comment.