Merge pull request #33 from grassrootseconomics/sohail/archived-tokens

feat: add support for archived tokens
This commit is contained in:
Mohamed Sohail 2022-07-13 16:34:23 +03:00 committed by GitHub
commit c490f28a96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 0 deletions

View File

@ -37,6 +37,7 @@ func InitAdminApi(e *echo.Echo, db *pgxpool.Pool, queries goyesql.Queries, metaC
g.GET("/address-2-phone/:address", handleAddress2Phone)
g.GET("/latest-transactions/:phone", handleLatestTransactions)
g.GET("/latest-transactions-by-token/:phone/:token", handleLatestTransactionsByToken)
g.GET("/latest-transactions-by-archived-token/:phone/:token", handleLatestTransactionsByArchivedToken)
}
func newApi(db *pgxpool.Pool, queries goyesql.Queries, metaClient *meta.CicMeta, jwtKey string) *api {

View File

@ -94,3 +94,39 @@ func handleLatestTransactionsByToken(c echo.Context) error {
return c.JSON(http.StatusOK, data)
}
func handleLatestTransactionsByArchivedToken(c echo.Context) error {
var (
api = c.Get("api").(*api)
phone = c.Param("phone")
token = c.Param("token")
pg = pagination.GetPagination(c.QueryParams())
data []userTransactionRes
rows pgx.Rows
err error
)
if pg.FirstPage {
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-archived-token"], phone, token, pg.PerPage)
if err != nil {
return err
}
} else if pg.Next {
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-archived-token-next"], phone, token, pg.Cursor, pg.PerPage)
if err != nil {
return err
}
} else {
rows, err = api.db.Query(context.Background(), api.q["account-latest-transactions-by-archived-token-previous"], phone, token, pg.Cursor, pg.PerPage)
if err != nil {
return err
}
}
if err := pgxscan.ScanAll(&data, rows); err != nil {
return err
}
return c.JSON(http.StatusOK, data)
}

View File

@ -0,0 +1 @@
ALTER TABLE tokens ALTER COLUMN token_name TYPE VARCHAR(32), ALTER COLUMN token_name SET NOT NULL;

View File

@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS archived_tokens (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
token_address VARCHAR(40) NOT NULL UNIQUE,
token_decimals INT NOT NULL,
token_name VARCHAR(16) NOT NULL,
token_symbol VARCHAR(10) NOT NULL
);
---- create above / drop below ----
DROP TABLE IF EXISTS archived_tokens;

View File

@ -79,4 +79,36 @@ SELECT * FROM (
INNER JOIN users ON ((transactions.sender_address = users.blockchain_address) OR (transactions.recipient_address = users.blockchain_address))
INNER JOIN tokens ON transactions.token_address = tokens.token_address
WHERE users.phone_number = $1 AND tokens.token_symbol = $2 AND transactions.id > $3 ORDER BY transactions.id ASC LIMIT $4
) AS previous_page ORDER BY id DESC;
-- name: account-latest-transactions-by-archived-token
-- Returns the first page of a users latest transactions, filter by token
SELECT transactions.id, transactions.date_block, transactions.tx_hash, archived_tokens.token_symbol, transactions.sender_address, transactions.recipient_address, transactions.tx_value, transactions.success,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.sender_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.sender_address) as sender_identifier,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.recipient_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.recipient_address) as recipient_identifier
FROM transactions
INNER JOIN users ON ((transactions.sender_address = users.blockchain_address) OR (transactions.recipient_address = users.blockchain_address))
INNER JOIN archived_tokens ON transactions.token_address = archived_tokens.token_address
WHERE users.phone_number = $1 AND archived_tokens.token_address = $2 ORDER BY transactions.id DESC LIMIT $3;
-- name: account-latest-transactions-by-archived-token-next
-- Returns the next page based on a cursor, filter by token
SELECT transactions.id, transactions.date_block, transactions.tx_hash, archived_tokens.token_symbol, transactions.sender_address, transactions.recipient_address, transactions.tx_value, transactions.success,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.sender_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.sender_address) as sender_identifier,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.recipient_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.recipient_address) as recipient_identifier
FROM transactions
INNER JOIN users ON ((transactions.sender_address = users.blockchain_address) OR (transactions.recipient_address = users.blockchain_address))
INNER JOIN archived_tokens ON transactions.token_address = archived_tokens.token_address
WHERE users.phone_number = $1 AND archived_tokens.token_address = $2 AND transactions.id < $3 ORDER BY transactions.id DESC LIMIT $4;
-- name: account-latest-transactions-by-archived-token-previous
-- Returns the previous page based on cursor, filter by token
SELECT * FROM (
SELECT transactions.id, transactions.date_block, transactions.tx_hash, archived_tokens.token_symbol, transactions.sender_address, transactions.recipient_address, transactions.tx_value, transactions.success,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.sender_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.sender_address) as sender_identifier,
(SELECT phone_number FROM users WHERE blockchain_address = transactions.recipient_address UNION ALL SELECT address_description FROM sys_accounts WHERE sys_address = transactions.recipient_address) as recipient_identifier
FROM transactions
INNER JOIN users ON ((transactions.sender_address = users.blockchain_address) OR (transactions.recipient_address = users.blockchain_address))
INNER JOIN archived_tokens ON transactions.token_address = archived_tokens.token_address
WHERE users.phone_number = $1 AND archived_tokens.token_address = $2 AND transactions.id > $3 ORDER BY transactions.id ASC LIMIT $4
) AS previous_page ORDER BY id DESC;