Skip to content

Commit

Permalink
add type to origin table
Browse files Browse the repository at this point in the history
  • Loading branch information
iostream1308 committed Dec 31, 2024
1 parent abfaf92 commit de97b4c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions v2/cmd/migrations/00016_add_column_txorigin.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE txorigin ADD COLUMN type TEXT;
UPDATE txorigin SET type = 'cow';
7 changes: 6 additions & 1 deletion v2/internal/server/tradelogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions v2/pkg/storage/dashboard/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -165,10 +166,19 @@ 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
}
builder = builder.Where(squirrel.Eq{tag: strings.ToLower(v.Field(i).String())})
}

q, p, err := builder.ToSql()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions v2/pkg/storage/dashboard/types/tx_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ 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,
}
}

func TxOriginColumns() []string {
return []string{
"address",
"name",
"type",
}
}

0 comments on commit de97b4c

Please sign in to comment.