Skip to content

Commit

Permalink
Added more ways to filter via transactions and assets
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Dec 10, 2024
1 parent 9d689fe commit 7badab2
Show file tree
Hide file tree
Showing 18 changed files with 1,180 additions and 897 deletions.
859 changes: 9 additions & 850 deletions README.md

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions api/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func GetActionsByActionName(db *sql.DB) gin.HandlerFunc {

// Get total count of actions for this name
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM actions WHERE LOWER(action_name) = $1`, actionName).Scan(&totalCount)
err := db.QueryRow(`SELECT COUNT(*) FROM actions WHERE action_name ILIKE $1`, actionName).Scan(&totalCount)
if err != nil {
log.Printf("Error counting actions by name: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count actions"})
Expand All @@ -142,21 +142,20 @@ func GetActionsByActionName(db *sql.DB) gin.HandlerFunc {
func GetActionsByUser(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
user := c.Param("user")
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Normalize the user identifier by removing "0x" prefix if present
user = strings.TrimPrefix(user, "0x")

limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of user's actions
var totalCount int
err := db.QueryRow(`
SELECT COUNT(*)
FROM actions
INNER JOIN transactions ON actions.tx_hash = transactions.tx_hash
WHERE transactions.sponsor = $1
`, user).Scan(&totalCount)
WHERE transactions.sponsor ILIKE $1
`, "%"+user+"%").Scan(&totalCount)
if err != nil {
log.Printf("Error fetching actions: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count actions for user"})
Expand Down
47 changes: 39 additions & 8 deletions api/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,46 @@ import (
"database/sql"
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/nuklai/nuklaivm-external-subscriber/models"
)

// GetAllAssets retrieves all assets
// GetAllAssets retrieves all assets with optional filters
func GetAllAssets(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
assetType := c.Query("type")
user := c.Query("user")
assetAddress := c.Query("asset_address")
name := c.Query("name")
symbol := c.Query("symbol")
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of assets
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM assets`).Scan(&totalCount)
// Normalize user to search with and without "0x" prefix
if user != "" {
user = strings.TrimPrefix(user, "0x")
}
if assetAddress != "" {
assetAddress = strings.TrimPrefix(assetAddress, "0x")
}

// Get total count with filters
totalCount, err := models.CountFilteredAssets(db, assetType, user, assetAddress, name, symbol)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count assets"})
return
}

// Fetch paginated actions
assets, err := models.FetchAllAssets(db, limit, offset)
// Fetch filtered assets with pagination
assets, err := models.FetchFilteredAssets(db, assetType, user, assetAddress, name, symbol, limit, offset)
if err != nil {
log.Printf("Error fetching assets: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve assets"})
return
}

// Return response with counter
c.JSON(http.StatusOK, gin.H{
"counter": totalCount,
"items": assets,
Expand Down Expand Up @@ -80,9 +92,11 @@ func GetAssetsByUser(db *sql.DB) gin.HandlerFunc {
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

user = strings.TrimPrefix(user, "0x")

// Get total count of assets created by user
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM assets WHERE asset_creator = $1`, user).Scan(&totalCount)
err := db.QueryRow(`SELECT COUNT(*) FROM assets WHERE asset_creator ILIKE $1`, "%"+user+"%").Scan(&totalCount)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count assets for user"})
return
Expand All @@ -103,3 +117,20 @@ func GetAssetsByUser(db *sql.DB) gin.HandlerFunc {
})
}
}

// GetAssetByAddress retrieves a specific asset by its asset address
func GetAssetByAddress(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
assetAddress := c.Param("asset_address")

assetAddress = strings.TrimPrefix(assetAddress, "0x")

asset, err := models.FetchAssetByAddress(db, assetAddress)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Asset not found"})
return
}

c.JSON(http.StatusOK, asset)
}
}
29 changes: 18 additions & 11 deletions api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,37 @@ import (
"github.com/nuklai/nuklaivm-external-subscriber/models"
)

// GetAllTransactions retrieves all transactions with pagination and total count
// GetAllTransactions retrieves all transactions with pagination and supports additional filters
func GetAllTransactions(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
txHash := c.Query("tx_hash")
blockHash := c.Query("block_hash")
actionType := c.Query("action_type")
actionName := strings.ToLower(c.Query("action_name"))
user := c.Query("user")
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of transactions
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM transactions`).Scan(&totalCount)
// Normalize user to search with and without "0x" prefix
if user != "" {
user = strings.TrimPrefix(user, "0x")
}

// Get total count with filters
totalCount, err := models.CountFilteredTransactions(db, txHash, blockHash, actionType, actionName, user)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count transactions"})
return
}

// Fetch paginated transactions
transactions, err := models.FetchAllTransactions(db, limit, offset)
// Fetch filtered transactions with pagination
transactions, err := models.FetchFilteredTransactions(db, txHash, blockHash, actionType, actionName, user, limit, offset)
if err != nil {
log.Printf("Error fetching transactions: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve transactions"})
return
}

// Return response with counter
c.JSON(http.StatusOK, gin.H{
"counter": totalCount,
"items": transactions,
Expand Down Expand Up @@ -79,16 +87,15 @@ func GetTransactionsByBlock(db *sql.DB) gin.HandlerFunc {
func GetTransactionsByUser(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
user := c.Param("user")
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Normalize the user identifier by removing "0x" prefix if present
user = strings.TrimPrefix(user, "0x")

limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of user's transactions
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM transactions WHERE sponsor = $1`, user).Scan(&totalCount)
err := db.QueryRow(`SELECT COUNT(*) FROM transactions WHERE sponsor ILIKE $1`, "%"+user+"%").Scan(&totalCount)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count transactions for user"})
return
Expand Down
4 changes: 2 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func CreateSchema(db *sql.DB) error {
CREATE TABLE IF NOT EXISTS assets (
id SERIAL PRIMARY KEY,
asset_id TEXT NOT NULL UNIQUE,
asset_address TEXT NOT NULL UNIQUE,
asset_type_id SMALLINT NOT NULL,
asset_type TEXT NOT NULL,
asset_creator TEXT NOT NULL,
Expand Down Expand Up @@ -109,7 +109,7 @@ func CreateSchema(db *sql.DB) error {
CREATE INDEX IF NOT EXISTS idx_assets_creator ON assets(asset_creator);
CREATE INDEX IF NOT EXISTS idx_assets_type ON assets(asset_type_id);
CREATE INDEX IF NOT EXISTS idx_asset_id ON assets(asset_id);
CREATE INDEX IF NOT EXISTS idx_asset_address ON assets(asset_address);
`

_, err := db.Exec(schema)
Expand Down
Loading

0 comments on commit 7badab2

Please sign in to comment.