Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Txs by action #26

Merged
merged 6 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions api/controller/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,13 @@ func (c *dashboardController) Tokens(ctx *gin.Context) {
// @Failure 500 {object} httputil.InternalServerError
// @Param pool query string false "Pool address"
// @Param token query string false "Token address"
// @Param type query string false "Transaction type, empty value is for all types" Enums(swap, add, remove)
// @Router /dashboard/txs [get]
func (c *dashboardController) Txs(ctx *gin.Context) {

pool := dashboardService.Addr(ctx.Query("pool"))
token := dashboardService.Addr(ctx.Query("token"))
txType := c.txTypeToServiceTxType(TxType(ctx.Query("type")))

if len(pool) > 0 && len(token) > 0 {
httputil.NewError(ctx, http.StatusBadRequest, errors.New("invalid query, must choose one of (pool or token, not both)"))
Expand All @@ -429,11 +431,11 @@ func (c *dashboardController) Txs(ctx *gin.Context) {
var txs dashboardService.Txs
var err error
if len(token) > 0 {
txs, err = c.Dashboard.TxsOfToken(token)
txs, err = c.Dashboard.TxsOfToken(txType, token)
} else if len(pool) > 0 {
txs, err = c.Dashboard.Txs(pool)
txs, err = c.Dashboard.Txs(txType, pool)
} else {
txs, err = c.Dashboard.Txs()
txs, err = c.Dashboard.Txs(txType)
}
if err != nil {
c.logger.Warn(err)
Expand Down
13 changes: 12 additions & 1 deletion api/controller/dashboard/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
"time"
)

type TxType string

const (
TX_TYPE_SWAP TxType = "swap"
TX_TYPE_ADD TxType = "add"
TX_TYPE_REMOVE TxType = "remove"
TX_TYPE_ALL TxType = ""
)

type RecentRes struct {
Volume string `json:"volume"`
VolumeChangeRate float32 `json:"volumeChangeRate"`
Expand Down Expand Up @@ -53,7 +62,9 @@ type TokenRes struct {

type TxsRes []TxRes
type TxRes struct {
Action string `json:"action"`
Action string `json:"action"`
ActionDisplay string `json:"actionDisplay"`

Address string `json:"address"`
Hash string `json:"hash"`
TotalValue string `json:"totalValue"`
Expand Down
112 changes: 81 additions & 31 deletions api/controller/dashboard/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"strings"
"time"

dashboardService "github.com/dezswap/dezswap-api/api/service/dashboard"
ds "github.com/dezswap/dezswap-api/api/service/dashboard"
"github.com/pkg/errors"
)

type mapper struct{}

func (m *mapper) tokenToRes(token dashboardService.Token) TokenRes {
func (m *mapper) tokenToRes(token ds.Token) TokenRes {
return TokenRes{
Address: string(token.Addr),
Price: token.Price,
Expand All @@ -27,7 +27,7 @@ func (m *mapper) tokenToRes(token dashboardService.Token) TokenRes {
}
}

func (m *mapper) tokensToRes(tokens dashboardService.Tokens) TokensRes {
func (m *mapper) tokensToRes(tokens ds.Tokens) TokensRes {
res := make(TokensRes, len(tokens))
for i, t := range tokens {
res[i] = m.tokenToRes(t)
Expand All @@ -36,7 +36,7 @@ func (m *mapper) tokensToRes(tokens dashboardService.Tokens) TokensRes {
return res
}

func (m *mapper) recentToRes(recent dashboardService.Recent) RecentRes {
func (m *mapper) recentToRes(recent ds.Recent) RecentRes {
return RecentRes{
Volume: recent.Volume,
VolumeChangeRate: recent.VolumeChangeRate,
Expand All @@ -47,7 +47,7 @@ func (m *mapper) recentToRes(recent dashboardService.Recent) RecentRes {
}
}

func (m *mapper) statisticToRes(statistic dashboardService.Statistic) StatisticRes {
func (m *mapper) statisticToRes(statistic ds.Statistic) StatisticRes {
res := make(StatisticRes, len(statistic))
for i, s := range statistic {
res[i] = StatisticResItem{
Expand All @@ -60,40 +60,64 @@ func (m *mapper) statisticToRes(statistic dashboardService.Statistic) StatisticR
return res
}

func (m *mapper) txsToRes(txs dashboardService.Txs) TxsRes {
func (m *mapper) txsToRes(txs ds.Txs) TxsRes {
actionConverter := func(action string) string {
jhlee-young marked this conversation as resolved.
Show resolved Hide resolved
actionStr := m.serviceTxTypeToTxTypeString(action)
actionStr = strings.ReplaceAll(actionStr, "_", " ")
return fmt.Sprintf("%s%s", strings.ToUpper(actionStr[0:1]), actionStr[1:])
}

type asset struct {
Asset string
Amount string
Symbol string
}
type assets [2]asset

actionDisplayConverter := func(action ds.TxType, orderedAssets [2]asset) string {
switch action {
case "swap":
return "Swap"
case "provide":
return "Add"
case "withdraw":
return "Remove"
case ds.TX_TYPE_SWAP:
return fmt.Sprintf("%s %s for %s", actionConverter(string(action)), orderedAssets[0].Symbol, orderedAssets[1].Symbol)
default:
str := strings.ReplaceAll(action, "_", " ")
return fmt.Sprintf("%s%s", strings.ToUpper(str[0:1]), str[1:])
return fmt.Sprintf("%s %s and %s", actionConverter(string(action)), orderedAssets[0].Symbol, orderedAssets[1].Symbol)
}
}

orderAssets := func(action ds.TxType, unorderedAssets assets) assets {
switch action {
case ds.TX_TYPE_SWAP:
if strings.Contains(unorderedAssets[0].Amount, "-") {
return [2]asset{unorderedAssets[1], unorderedAssets[0]}
}
}
return unorderedAssets
}

res := make(TxsRes, len(txs))
for i, tx := range txs {
targetAssets := assets{
{Asset: tx.Asset0, Amount: tx.Asset0Amount, Symbol: tx.Asset0Symbol},
{Asset: tx.Asset1, Amount: tx.Asset1Amount, Symbol: tx.Asset1Symbol},
}
targetAssets = orderAssets(ds.TxType(tx.Action), targetAssets)
res[i] = TxRes{
Action: fmt.Sprintf("%s %s and %s", actionConverter(tx.Action), tx.Asset0Symbol, tx.Asset1Symbol),
Hash: tx.Hash,
Address: tx.Address,
Asset0: tx.Asset0,
Asset0Amount: tx.Asset0Amount,
Asset1: tx.Asset1,
Asset1Amount: tx.Asset1Amount,
TotalValue: tx.TotalValue,
Account: tx.Sender,
Timestamp: tx.Timestamp,
Action: m.serviceTxTypeToTxTypeString(tx.Action),
ActionDisplay: actionDisplayConverter(ds.TxType(tx.Action), targetAssets),
Hash: tx.Hash,
Address: tx.Address,
Asset0: targetAssets[0].Asset,
Asset0Amount: strings.ReplaceAll(targetAssets[0].Amount, "-", ""),
Asset1: targetAssets[1].Asset,
Asset1Amount: strings.ReplaceAll(targetAssets[1].Amount, "-", ""),
TotalValue: tx.TotalValue,
Account: tx.Sender,
Timestamp: tx.Timestamp,
}
}
return res
}

func (m *mapper) poolsToRes(pools dashboardService.Pools) PoolsRes {
func (m *mapper) poolsToRes(pools ds.Pools) PoolsRes {
res := make(PoolsRes, len(pools))

for i, p := range pools {
Expand All @@ -109,7 +133,7 @@ func (m *mapper) poolsToRes(pools dashboardService.Pools) PoolsRes {
return res
}

func (m *mapper) poolDetailToRes(pool dashboardService.PoolDetail) PoolDetailRes {
func (m *mapper) poolDetailToRes(pool ds.PoolDetail) PoolDetailRes {
res := PoolDetailRes{}

res.Recent = m.recentToRes(pool.Recent)
Expand All @@ -118,7 +142,7 @@ func (m *mapper) poolDetailToRes(pool dashboardService.PoolDetail) PoolDetailRes
return res
}

func (m *mapper) volumesToChartRes(volumes dashboardService.Volumes) ChartRes {
func (m *mapper) volumesToChartRes(volumes ds.Volumes) ChartRes {
res := make(ChartRes, len(volumes))

for i, v := range volumes {
Expand All @@ -130,7 +154,7 @@ func (m *mapper) volumesToChartRes(volumes dashboardService.Volumes) ChartRes {
return res
}

func (m *mapper) tvlsToChartRes(tvls dashboardService.Tvls) ChartRes {
func (m *mapper) tvlsToChartRes(tvls ds.Tvls) ChartRes {
res := make(ChartRes, len(tvls))

for i, v := range tvls {
Expand All @@ -142,7 +166,7 @@ func (m *mapper) tvlsToChartRes(tvls dashboardService.Tvls) ChartRes {
return res
}

func (m *mapper) aprsToChartRes(aprs dashboardService.Aprs) ChartRes {
func (m *mapper) aprsToChartRes(aprs ds.Aprs) ChartRes {
res := make(ChartRes, len(aprs))

for i, v := range aprs {
Expand All @@ -154,7 +178,7 @@ func (m *mapper) aprsToChartRes(aprs dashboardService.Aprs) ChartRes {
return res
}

func (m *mapper) feesToChartRes(aprs dashboardService.Fees) ChartRes {
func (m *mapper) feesToChartRes(aprs ds.Fees) ChartRes {
res := make(ChartRes, len(aprs))

for i, v := range aprs {
Expand All @@ -166,7 +190,7 @@ func (m *mapper) feesToChartRes(aprs dashboardService.Fees) ChartRes {
return res
}

func (m *mapper) tokenChartToChartRes(chart dashboardService.TokenChart) (ChartRes, error) {
func (m *mapper) tokenChartToChartRes(chart ds.TokenChart) (ChartRes, error) {
res := make(ChartRes, len(chart))

for i, v := range chart {
Expand All @@ -180,3 +204,29 @@ func (m *mapper) tokenChartToChartRes(chart dashboardService.TokenChart) (ChartR

return res, nil
}

func (m *mapper) txTypeToServiceTxType(ty TxType) ds.TxType {
switch ty {
case TX_TYPE_SWAP:
return ds.TX_TYPE_SWAP
case TX_TYPE_ADD:
return ds.TX_TYPE_PROVIDE
case TX_TYPE_REMOVE:
return ds.TX_TYPE_WITHDRAW
}
return ds.TX_TYPE_ALL
}

func (m *mapper) serviceTxTypeToTxTypeString(ty string) string {
switch ds.TxType(ty) {
case ds.TX_TYPE_SWAP:
return string(TX_TYPE_SWAP)
case ds.TX_TYPE_PROVIDE:
return string(TX_TYPE_ADD)
case ds.TX_TYPE_WITHDRAW:
return string(TX_TYPE_REMOVE)
}

// return the tx type as is
return ty
}
14 changes: 14 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ const docTemplate = `{
"description": "Token address",
"name": "token",
"in": "query"
},
{
"enum": [
"swap",
"add",
"remove"
],
"type": "string",
"description": "Transaction type, empty value is for all types",
"name": "type",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -1509,6 +1520,9 @@ const docTemplate = `{
"action": {
"type": "string"
},
"actionDisplay": {
"type": "string"
},
"address": {
"type": "string"
},
Expand Down
14 changes: 14 additions & 0 deletions api/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,17 @@
"description": "Token address",
"name": "token",
"in": "query"
},
{
"enum": [
"swap",
"add",
"remove"
],
"type": "string",
"description": "Transaction type, empty value is for all types",
"name": "type",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -1498,6 +1509,9 @@
"action": {
"type": "string"
},
"actionDisplay": {
"type": "string"
},
"address": {
"type": "string"
},
Expand Down
10 changes: 10 additions & 0 deletions api/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ definitions:
type: string
action:
type: string
actionDisplay:
type: string
address:
type: string
asset0:
Expand Down Expand Up @@ -787,6 +789,14 @@ paths:
in: query
name: token
type: string
- description: Transaction type, empty value is for all types
enum:
- swap
- add
- remove
in: query
name: type
type: string
produces:
- application/json
responses:
Expand Down
13 changes: 10 additions & 3 deletions api/service/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (d *dashboard) PoolDetail(addr Addr) (PoolDetail, error) {
if err != nil {
return detail, errors.Wrap(err, "dashboard.PoolDetail")
}
detail.Txs, err = d.Txs(addr)
detail.Txs, err = d.Txs(TX_TYPE_ALL, addr)
if err != nil {
return detail, errors.Wrap(err, "dashboard.PoolDetail")
}
Expand Down Expand Up @@ -1035,9 +1035,12 @@ func (d *dashboard) TvlsOf(addr Addr, duration Duration) ([]Tvl, error) {
}

// Txs implements Dashboard.
func (d *dashboard) Txs(addr ...Addr) (Txs, error) {
func (d *dashboard) Txs(txType TxType, addr ...Addr) (Txs, error) {
m := parser.ParsedTx{}
subQuery := d.DB.Model(&m).Select("*").Where("chain_id = ?", d.chainId).Order("timestamp DESC").Limit(100)
if txType != TX_TYPE_ALL {
subQuery = subQuery.Where("type = ?", string(txType))
}
if len(addr) > 0 {
subQuery = subQuery.Where("contract = ?", string(addr[0]))
}
Expand Down Expand Up @@ -1070,9 +1073,13 @@ func (d *dashboard) Txs(addr ...Addr) (Txs, error) {
}

// TxsOfToken implements Dashboard.
func (d *dashboard) TxsOfToken(addr Addr) (Txs, error) {
func (d *dashboard) TxsOfToken(txType TxType, addr Addr) (Txs, error) {
m := parser.ParsedTx{}
subQuery := d.DB.Model(&m).Select("*").Where("chain_id = ?", d.chainId).Order("timestamp DESC").Limit(100)

if txType != TX_TYPE_ALL {
subQuery = subQuery.Where("type = ?", string(txType))
}
if len(addr) > 0 {
subQuery = subQuery.Where("asset0 = ? OR asset1 = ?", string(addr), string(addr))
}
Expand Down
Loading