From 1f52e2dc5c3aaeab87386bb26ebb0cd4d6f454e8 Mon Sep 17 00:00:00 2001 From: robcxyz Date: Tue, 14 Feb 2023 17:34:21 +0530 Subject: [PATCH] chore: add block param to transactions/token-transfers/address/{address} --- src/api/rest/transactions.go | 6 ++++- src/crud/token_transfer.go | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/api/rest/transactions.go b/src/api/rest/transactions.go index 8d7fc6e..63815e1 100644 --- a/src/api/rest/transactions.go +++ b/src/api/rest/transactions.go @@ -711,6 +711,8 @@ func handlerGetTokenTransfers(c *fiber.Ctx) error { // @Produce json // @Param limit query int false "amount of records" // @Param skip query int false "skip to a record" +// @Param start_block_number query int false "find by block number range" +// @Param end_block_number query int false "find by block number range" // @Param address path string true "find by address" // @Router /api/v1/transactions/token-transfers/address/{address} [get] // @Success 200 {object} []models.TokenTransfer @@ -746,10 +748,12 @@ func handlerGetTokenTransfersAddress(c *fiber.Ctx) error { } // Get Transactions - tokenTransfers, err := crud.GetTokenTransferCrud().SelectManyByAddress( + tokenTransfers, err := crud.GetTokenTransferCrud().SelectManyByAddressBlockRange( params.Limit, params.Skip, address, + params.StartBlockNumber, + params.EndBlockNumber, ) if err != nil { c.Status(500) diff --git a/src/crud/token_transfer.go b/src/crud/token_transfer.go index a146029..82f23fb 100644 --- a/src/crud/token_transfer.go +++ b/src/crud/token_transfer.go @@ -1,6 +1,7 @@ package crud import ( + "strconv" "sync" "go.uber.org/zap" @@ -163,6 +164,56 @@ func (m *TokenTransferCrud) SelectManyByAddress( return tokenTransfers, db.Error } +func (m *TokenTransferCrud) SelectManyByAddressBlockRange( + limit int, + skip int, + address string, + startBlockNumber int, + endBlockNumber int, +) (*[]models.TokenTransfer, error) { + db := m.db + + // Set table + db = db.Model(&[]models.TokenTransfer{}) + + blockWhereCondition := "" + if startBlockNumber != 0 || endBlockNumber != 0 { + blockWhereCondition = blockWhereCondition + "and" + } + + // start block number + if startBlockNumber != 0 { + blockWhereCondition = blockWhereCondition + " > " + strconv.Itoa(startBlockNumber) + } + + // end block number + if endBlockNumber != 0 { + blockWhereCondition = blockWhereCondition + " > " + strconv.Itoa(startBlockNumber) + } + + // Latest transactions first + db = db.Order("block_number desc") + + // Address + db = db.Where(`(transaction_hash, log_index) + IN ( + SELECT + transaction_hash, log_index + FROM + token_transfer_by_addresses + WHERE + address = ? ? + ORDER BY block_number desc + LIMIT ? + OFFSET ? + )`, address, blockWhereCondition, limit, skip) + + tokenTransfers := &[]models.TokenTransfer{} + db = db.Find(tokenTransfers) + + return tokenTransfers, db.Error +} + // SelectManyByTokenContracAddress - select from token_transfers table by token contract address // Returns: models, error (if present) func (m *TokenTransferCrud) SelectManyByTokenContractAddress(