From 8e66fcec6f4d61deb07facd76954d4776c6fbfe2 Mon Sep 17 00:00:00 2001 From: iostream1308 <51293334+iostream1308@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:34:25 +0700 Subject: [PATCH] add type to origin table (#116) * add type to origin table * add operation vt * fix index timestamp --- .../00016_add_column_txorigin.up.sql | 49 +++++++++++++++++++ v2/internal/server/tradelogs.go | 7 ++- v2/pkg/storage/dashboard/storage.go | 18 ++++++- v2/pkg/storage/dashboard/types/tx_origin.go | 8 +++ 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 v2/cmd/migrations/00016_add_column_txorigin.up.sql diff --git a/v2/cmd/migrations/00016_add_column_txorigin.up.sql b/v2/cmd/migrations/00016_add_column_txorigin.up.sql new file mode 100644 index 0000000..07520e0 --- /dev/null +++ b/v2/cmd/migrations/00016_add_column_txorigin.up.sql @@ -0,0 +1,49 @@ +ALTER TABLE txorigin ADD COLUMN type TEXT; +UPDATE txorigin SET type = 'cow'; + +INSERT INTO txorigin (address, name, type) VALUES +('0x421925abb2e3311571f18e545e37fb983318f274', 'ktt-operator-00', 'vt'), +('0xd2e018c16dc6a4ce18e7880bed5b1b3b64fc35c7', 'ktt-operator-01', 'vt'), +('0x86ee24a1effa6bdf75a311c9a3b73ca0b0ec3207', 'ktt-operator-10', 'vt'), +('0x866bdd48566d984f94c55f5ff8988208b3aa0008', 'ktt-operator-11', 'vt'), +('0x869f1a409aa364ed2d7f1a0327fe8844d547f909', 'ktt-operator-12', 'vt'), +('0x8686ca74753f976ff7ead5a692096f2cd1388c00', 'ktt-operator-02', 'vt'), +('0x8686301a430112d76fef8331821aef42c8c48686', 'ktt-operator-03', 'vt,uniswapx,pcsx'), +('0x8686e88c147fadc829bfef8e2d853e3d91ff8101', 'ktt-operator-04', 'uniswapx,pcsx'), +('0x868601d3693003cfd2f6b8ddd6945569ce5d7602', 'ktt-operator-05', '1inch-resolver'), +('0x8686b3d43415e3d4bfbf686450a2799b9249fd03', 'ktt-operator-06', '1inch-resolver'), +('0x86b1980c889ad981837a5e8b587ae82100fe8e04', 'ktt-operator-07', 'vt'), +('0x860d6604b813376645b996278464e9953f884705', 'ktt-operator-08', 'vt'), +('0x86e81996242d1dce7fc5f9b9d060bbd3410fc206', 'ktt-operator-09', 'vt'); + +DROP INDEX zerox_timestamp_idx; +DROP INDEX kyberswap_timestamp_idx; +DROP INDEX kyberswap_rfq_timestamp_idx; +DROP INDEX paraswap_timestamp_idx; +DROP INDEX hashflow_v3_timestamp_idx; +DROP INDEX oneinch_v6_timestamp_idx; +DROP INDEX uniswapx_timestamp_idx; +DROP INDEX bebop_timestamp_idx; +DROP INDEX zerox_v3_timestamp_idx; +DROP INDEX pancakeswap_timestamp_idx; + +create index zerox_timestamp_idx + on tradelogs_zerox (timestamp DESC); +create index kyberswap_timestamp_idx + on tradelogs_kyberswap (timestamp DESC); +create index kyberswap_rfq_timestamp_idx + on tradelogs_kyberswap_rfq (timestamp DESC); +create index paraswap_timestamp_idx + on tradelogs_paraswap (timestamp DESC); +create index hashflow_v3_timestamp_idx + on tradelogs_hashflow_v3 (timestamp DESC); +create index oneinch_v6_timestamp_idx + on tradelogs_oneinch_v6 (timestamp DESC); +create index uniswapx_timestamp_idx + on tradelogs_uniswapx (timestamp DESC); +create index bebop_timestamp_idx + on tradelogs_bebop (timestamp DESC); +create index zerox_v3_timestamp_idx + on tradelogs_zerox_v3 (timestamp DESC); +create index pancakeswap_timestamp_idx + on tradelogs_pancakeswap (timestamp DESC); diff --git a/v2/internal/server/tradelogs.go b/v2/internal/server/tradelogs.go index dfcb3d6..26762c9 100644 --- a/v2/internal/server/tradelogs.go +++ b/v2/internal/server/tradelogs.go @@ -156,7 +156,12 @@ func (s *TradeLogs) addMakerName(c *gin.Context) { } func (s *TradeLogs) getTxOrigin(c *gin.Context) { - data, err := s.dashStorage.GetTxOrigin() + var queries dashboardTypes.TxOriginQuery + if err := c.ShouldBind(&queries); err != nil { + responseErr(c, http.StatusBadRequest, err) + return + } + data, err := s.dashStorage.GetTxOrigin(queries) if err != nil { responseErr(c, http.StatusInternalServerError, err) return diff --git a/v2/pkg/storage/dashboard/storage.go b/v2/pkg/storage/dashboard/storage.go index 8e6aa8f..ee2813f 100644 --- a/v2/pkg/storage/dashboard/storage.go +++ b/v2/pkg/storage/dashboard/storage.go @@ -101,7 +101,8 @@ func (s *Storage) InsertTxOrigin(txOrigins []types.TxOrigin) error { } q, p, err := b.Suffix(`ON CONFLICT (address) DO UPDATE SET - name=excluded.name + name=excluded.name, + type=excluded.type `).ToSql() if err != nil { s.l.Errorw("Error build insert", "error", err) @@ -165,10 +166,23 @@ func (s *Storage) GetMakerName() ([]types.MakerName, error) { return makerName, nil } -func (s *Storage) GetTxOrigin() ([]types.TxOrigin, error) { +func (s *Storage) GetTxOrigin(query types.TxOriginQuery) ([]types.TxOrigin, error) { builder := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar). Select(types.TxOriginColumns()...). From(txOriginTable) + v := reflect.ValueOf(query) + fields := v.Type() + for i := 0; i < v.NumField(); i++ { + tag := string(fields.Field(i).Tag.Get("form")) + if v.Field(i).IsZero() { + continue + } + if tag == "type" { + builder = builder.Where(squirrel.Like{tag: "%" + strings.ToLower(v.Field(i).String()) + "%"}) + continue + } + builder = builder.Where(squirrel.Eq{tag: strings.ToLower(v.Field(i).String())}) + } q, p, err := builder.ToSql() if err != nil { diff --git a/v2/pkg/storage/dashboard/types/tx_origin.go b/v2/pkg/storage/dashboard/types/tx_origin.go index c9ef680..2c7f316 100644 --- a/v2/pkg/storage/dashboard/types/tx_origin.go +++ b/v2/pkg/storage/dashboard/types/tx_origin.go @@ -5,12 +5,19 @@ import "strings" type TxOrigin struct { Address string `db:"address" json:"address"` Name string `db:"name" json:"name"` + Type string `db:"type" json:"type"` +} + +type TxOriginQuery struct { + Type string `form:"type" json:"type,omitempty"` + Address string `form:"address" json:"address,omitempty"` } func (o *TxOrigin) SerializeTxOrigin() []interface{} { return []interface{}{ strings.ToLower(o.Address), o.Name, + o.Type, } } @@ -18,5 +25,6 @@ func TxOriginColumns() []string { return []string{ "address", "name", + "type", } }