Sohail/latest token transactions (#21)

* add: latest-token-transactions table

* fix: remove token symbol from query

* fix: move latest tx to dashboard domain
This commit is contained in:
Mohamed Sohail 2022-05-26 09:35:39 +03:00 committed by GitHub
parent 3f24894700
commit 861af1761d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 8 deletions

View File

@ -28,4 +28,5 @@ func InitDashboardApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries) {
g.GET("/transactions-count", handleTransactionsCount) g.GET("/transactions-count", handleTransactionsCount)
g.GET("/token-transactions-count/:address", handleTokenTransactionsCount) g.GET("/token-transactions-count/:address", handleTokenTransactionsCount)
g.GET("/token-volume/:address", handleTokenVolume) g.GET("/token-volume/:address", handleTokenVolume)
g.GET("/latest-token-transactions/:address", handleLatestTokenTransactions)
} }

View File

@ -1,6 +1,7 @@
package dashboard package dashboard
import ( import (
"cic-dw/pkg/date_range"
"context" "context"
"net/http" "net/http"
"time" "time"
@ -21,7 +22,7 @@ func handleNewRegistrations(c echo.Context) error {
data []lineChartRes data []lineChartRes
) )
from, to := parseDateRange(c.QueryParams()) from, to := date_range.ParseDateRange(c.QueryParams())
rows, err := api.db.Query(context.Background(), api.q["new-user-registrations"], from, to) rows, err := api.db.Query(context.Background(), api.q["new-user-registrations"], from, to)
if err != nil { if err != nil {
@ -42,7 +43,7 @@ func handleTransactionsCount(c echo.Context) error {
data []lineChartRes data []lineChartRes
) )
from, to := parseDateRange(c.QueryParams()) from, to := date_range.ParseDateRange(c.QueryParams())
rows, err := api.db.Query(context.Background(), api.q["transactions-count"], from, to) rows, err := api.db.Query(context.Background(), api.q["transactions-count"], from, to)
if err != nil { if err != nil {
@ -64,7 +65,7 @@ func handleTokenTransactionsCount(c echo.Context) error {
data []lineChartRes data []lineChartRes
) )
from, to := parseDateRange(c.QueryParams()) from, to := date_range.ParseDateRange(c.QueryParams())
rows, err := api.db.Query(context.Background(), api.q["token-transactions-count"], from, to, token) rows, err := api.db.Query(context.Background(), api.q["token-transactions-count"], from, to, token)
if err != nil { if err != nil {
@ -86,7 +87,7 @@ func handleTokenVolume(c echo.Context) error {
data []lineChartRes data []lineChartRes
) )
from, to := parseDateRange(c.QueryParams()) from, to := date_range.ParseDateRange(c.QueryParams())
rows, err := api.db.Query(context.Background(), api.q["token-volume"], from, to, token) rows, err := api.db.Query(context.Background(), api.q["token-volume"], from, to, token)
if err != nil { if err != nil {

View File

@ -0,0 +1,45 @@
package dashboard
import (
"context"
"net/http"
"time"
"github.com/georgysavva/scany/pgxscan"
"github.com/labstack/echo/v4"
)
type tokenTransactionsRes struct {
Id int64 `db:"id" json:"id"`
Block int64 `db:"block_number" json:"block"`
Date time.Time `db:"date_block" json:"time"`
TxHash string `db:"tx_hash" json:"tx_hash"`
From string `db:"sender_address" json:"from"`
To string `db:"recipient_address" json:"to"`
Value int64 `db:"tx_value" json:"tx_value"`
Success bool `db:"success" json:"success"`
}
func handleLatestTokenTransactions(c echo.Context) error {
var (
api = c.Get("api").(*api)
token = c.Param("address")
data []tokenTransactionsRes
)
rows, err := api.db.Query(context.Background(), api.q["latest-token-transactions"], token)
if err != nil {
return err
}
if err := pgxscan.ScanAll(&data, rows); err != nil {
return err
}
if len(data) < 1 {
data = []tokenTransactionsRes{}
}
return c.JSON(http.StatusOK, data)
}

View File

@ -1,11 +1,12 @@
package dashboard package date_range
import ( import (
"github.com/golang-module/carbon/v2"
"net/url" "net/url"
"github.com/golang-module/carbon/v2"
) )
func parseDateRange(q url.Values) (string, string) { func ParseDateRange(q url.Values) (string, string) {
var from, to string var from, to string
qFrom := q.Get("from") qFrom := q.Get("from")

View File

@ -69,3 +69,8 @@ AND transactions.success = true
GROUP BY date_range.day GROUP BY date_range.day
ORDER BY date_range.day ORDER BY date_range.day
LIMIT 730; LIMIT 730;
--name: latest-token-transactions
-- Returns latest token transactions, with curosr forward query and limit
SELECT id, block_number, date_block, tx_hash, sender_address, recipient_address, tx_value, success FROM transactions
WHERE token_address = $1 AND date_block > TIMESTAMP 'yesterday' ORDER BY id DESC;

View File

@ -45,4 +45,4 @@ SELECT COUNT(*) FROM transactions
WHERE token_address = $1 WHERE token_address = $1
AND transactions.sender_address NOT IN (SELECT sys_address FROM exclude) AND transactions.sender_address NOT IN (SELECT sys_address FROM exclude)
AND transactions.recipient_address NOT IN (SELECT sys_address FROM exclude) AND transactions.recipient_address NOT IN (SELECT sys_address FROM exclude)
AND transactions.success = true; AND transactions.success = true;