Skip to content

Commit

Permalink
Updated the way to retrieve all transactions with counter
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Dec 2, 2024
1 parent 9e7bf6a commit 49f89ef
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 88 deletions.
184 changes: 101 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,18 @@ The REST API is available at `http://localhost:8080`.
- **Output**:

```bash
{
"ID": 832,
"BlockHeight": 416,
"BlockHash": "2b38U36vP4esbZbjAXu54pPALaJ32kaf32tDU1qC6Ek97aRtH1",
"ParentBlock": "2AHQ7qbehvuYJ2sDB8Wgwz5PMyJx2BejHhJZa292KmbXw3Te9R",
"StateRoot": "6jGeT1cD6SopVAcc13yyJ8VMLFdiPh6upL3BAoBv1HGPUUuUZ",
"Timestamp": "2024-12-02T10:22:50Z",
"UnitPrices": "(Bandwidth=100, Compute=100, Storage(Read)=100, Storage(Allocate)=100, Storage(Write)=100)"
}
{
"BlockHeight": 73,
"BlockHash": "p5jkYp2TfK1nH7WzXmeDypNKmtnMoRpNLadYCDNYoRyrTvxZS",
"ParentBlockHash": "6bMS7Gi5u4SRfHUrboQcjhvpEjMNaNpFU4s7EAzdqi5Vqcyrj",
"StateRoot": "oz2McvCZbS8gfp9sFjZfAFKawZW8TdrGjwwETfPpcEro26mTh",
"BlockSize": 307,
"TxCount": 1,
"TotalFee": 48500,
"AvgTxSize": 307,
"UniqueParticipants": 1,
"Timestamp": "2024-12-02T11:47:09Z"
}
```

- **Get All Blocks**
Expand All @@ -194,26 +197,35 @@ The REST API is available at `http://localhost:8080`.
- **Output**:

```bash
[
{
"ID": 1988,
"BlockHeight": 994,
"BlockHash": "iRex7gTFqNQKfhMn3Voqex2vLaJXfqQ5xg3xhxqpuc9Rn6LUy",
"ParentBlock": "NSsynENZfcbXges2JuFqSgj6SDGMTq7jCgFqXnjqj9i74YAwa",
"StateRoot": "taSygsQcSb8y1EMwwEXkUvM9Dz5SFDkf3Rz6YFtcwAxQbZmUk",
"Timestamp": "2024-12-02T10:30:11Z",
"UnitPrices": "(Bandwidth=100, Compute=100, Storage(Read)=100, Storage(Allocate)=100, Storage(Write)=100)"
},
{
"ID": 1986,
"BlockHeight": 993,
"BlockHash": "NSsynENZfcbXges2JuFqSgj6SDGMTq7jCgFqXnjqj9i74YAwa",
"ParentBlock": "ZNwNGUx9DL4hoSx76wwS2RAUnMj773bGg51bd42oaqgWdxiMg",
"StateRoot": "2RizGvY7zGDgNkiXWVvuEhVkGAtydGoB3irSzgFq1qUV41ysLP",
"Timestamp": "2024-12-02T10:30:10Z",
"UnitPrices": "(Bandwidth=100, Compute=100, Storage(Read)=100, Storage(Allocate)=100, Storage(Write)=100)"
}
]
{
"counter": 73,
"items": [
{
"BlockHeight": 73,
"BlockHash": "p5jkYp2TfK1nH7WzXmeDypNKmtnMoRpNLadYCDNYoRyrTvxZS",
"ParentBlockHash": "6bMS7Gi5u4SRfHUrboQcjhvpEjMNaNpFU4s7EAzdqi5Vqcyrj",
"StateRoot": "oz2McvCZbS8gfp9sFjZfAFKawZW8TdrGjwwETfPpcEro26mTh",
"BlockSize": 307,
"TxCount": 1,
"TotalFee": 48500,
"AvgTxSize": 307,
"UniqueParticipants": 1,
"Timestamp": "2024-12-02T11:47:09Z"
},
{
"BlockHeight": 72,
"BlockHash": "6bMS7Gi5u4SRfHUrboQcjhvpEjMNaNpFU4s7EAzdqi5Vqcyrj",
"ParentBlockHash": "fStF9MQQwV7UKQK73Pp4reKBttRGuC1X4sQRoaiB1VTTT1eG3",
"StateRoot": "2RuPbB1GejDpXG9pMxmmfUuP3XfLKrjmyNUXKbq8LSm6dALtL7",
"BlockSize": 84,
"TxCount": 0,
"TotalFee": 0,
"AvgTxSize": 0,
"UniqueParticipants": 0,
"Timestamp": "2024-12-02T11:48:12Z"
}
]
}
```

#### Transaction Endpoints
Expand Down Expand Up @@ -251,36 +263,39 @@ The REST API is available at `http://localhost:8080`.
- **Output**:

```bash
[
{
"ID": 4,
"TxHash": "57KRbZ34ypofg839mYuiwRAsN2XdzsoLW7UXoTpdYGHLrMJNX",
"BlockHash": "CZP3tAWjARyoyNV18qD9QPfiarNZUbSxqPSEP1rF7ESoBJRWU",
"Sponsor": "00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9",
"MaxFee": 53000,
"Success": true,
"Fee": 48500,
"Outputs": {
"receiver_balance": 105000000000,
"sender_balance": 852999894999903000
},
"Timestamp": "2024-12-02T10:32:22Z"
},
{
"ID": 2,
"TxHash": "WPfzKZZAeug9wakxdzpQyp2qzJ27Kbi8BEfEY3KQDAKfiBbDp",
"BlockHash": "2b38U36vP4esbZbjAXu54pPALaJ32kaf32tDU1qC6Ek97aRtH1",
"Sponsor": "00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9",
"MaxFee": 53000,
"Success": true,
"Fee": 48500,
"Outputs": {
"receiver_balance": 100000000000,
"sender_balance": 852999899999951500
{
"counter": 2,
"items": [
{
"ID": 4,
"TxHash": "2dikBvg99xuS1zvabXHKTu2MGC43Sh89mseCgbRXtRxrYomo4",
"BlockHash": "2LrudVbwePsi8GmNWg4qitSEWyBMeSFycaWu3vKaAw6NrVRJ9K",
"Sponsor": "00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9",
"MaxFee": 53000,
"Success": true,
"Fee": 48500,
"Outputs": {
"receiver_balance": 15000000000,
"sender_balance": 852999984999903000
},
"Timestamp": "2024-12-02T11:49:59Z"
},
"Timestamp": "2024-12-02T10:22:50Z"
}
]
{
"ID": 2,
"TxHash": "68UpaXgNCxiT9xBq4Ja8vb7QKmQhLvWguj5z4Czq86hXZPtb1",
"BlockHash": "p5jkYp2TfK1nH7WzXmeDypNKmtnMoRpNLadYCDNYoRyrTvxZS",
"Sponsor": "00c4cb545f748a28770042f893784ce85b107389004d6a0e0d6d7518eeae1292d9",
"MaxFee": 53000,
"Success": true,
"Fee": 48500,
"Outputs": {
"receiver_balance": 10000000000,
"sender_balance": 852999989999951500
},
"Timestamp": "2024-12-02T11:47:09Z"
}
]
}
```

- **Get Transactions by Block**
Expand Down Expand Up @@ -345,32 +360,35 @@ The REST API is available at `http://localhost:8080`.
- **Output**:

```bash
[
{
"ID": 4,
"TxHash": "57KRbZ34ypofg839mYuiwRAsN2XdzsoLW7UXoTpdYGHLrMJNX",
"ActionType": 0,
"ActionDetails": {
"AssetAddress": "0x00cf77495ce1bdbf11e5e45463fad5a862cb6cc0a20e00e658c4ac3355dcdc64bb",
"Memo": "",
"To": "0x00f570339dce77fb2694edac17c9e6f36c0945959813b99b0b1a18849a7d622237",
"Value": 5000000000
},
"Timestamp": "2024-12-02T10:32:22Z"
},
{
"ID": 2,
"TxHash": "WPfzKZZAeug9wakxdzpQyp2qzJ27Kbi8BEfEY3KQDAKfiBbDp",
"ActionType": 0,
"ActionDetails": {
"AssetAddress": "0x00cf77495ce1bdbf11e5e45463fad5a862cb6cc0a20e00e658c4ac3355dcdc64bb",
"Memo": "",
"To": "0x00f570339dce77fb2694edac17c9e6f36c0945959813b99b0b1a18849a7d622237",
"Value": 100000000000
{
"counter": 2,
"items": [
{
"ID": 4,
"TxHash": "2dikBvg99xuS1zvabXHKTu2MGC43Sh89mseCgbRXtRxrYomo4",
"ActionType": 0,
"ActionDetails": {
"asset_address": "0x00cf77495ce1bdbf11e5e45463fad5a862cb6cc0a20e00e658c4ac3355dcdc64bb",
"memo": "",
"to": "0x00f570339dce77fb2694edac17c9e6f36c0945959813b99b0b1a18849a7d622237",
"value": 5000000000
},
"Timestamp": "2024-12-02T11:49:59Z"
},
"Timestamp": "2024-12-02T10:22:50Z"
}
]
{
"ID": 2,
"TxHash": "68UpaXgNCxiT9xBq4Ja8vb7QKmQhLvWguj5z4Czq86hXZPtb1",
"ActionType": 0,
"ActionDetails": {
"asset_address": "0x00cf77495ce1bdbf11e5e45463fad5a862cb6cc0a20e00e658c4ac3355dcdc64bb",
"memo": "",
"to": "0x00f570339dce77fb2694edac17c9e6f36c0945959813b99b0b1a18849a7d622237",
"value": 10000000000
},
"Timestamp": "2024-12-02T11:47:09Z"
}
]
}
```

- **Get Actions by Block**
Expand Down
17 changes: 15 additions & 2 deletions api/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,32 @@ import (
"github.com/nuklai/nuklaivm-external-subscriber/models"
)

// GetAllActions retrieves all actions with pagination
// GetAllActions retrieves all actions with pagination and total count
func GetAllActions(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of actions
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM actions`).Scan(&totalCount)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count actions"})
return
}

// Fetch paginated actions
actions, err := models.FetchAllActions(db, limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve actions"})
return
}

c.JSON(http.StatusOK, actions)
// Return response with counter
c.JSON(http.StatusOK, gin.H{
"counter": totalCount,
"items": actions,
})
}
}

Expand Down
17 changes: 15 additions & 2 deletions api/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,32 @@ import (
"github.com/gin-gonic/gin"
)

// GetAllBlocks retrieves all blocks with pagination
// GetAllBlocks retrieves all blocks with pagination and total count
func GetAllBlocks(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
limit := c.DefaultQuery("limit", "10")
offset := c.DefaultQuery("offset", "0")

// Get total count of blocks
var totalCount int
err := db.QueryRow(`SELECT COUNT(*) FROM blocks`).Scan(&totalCount)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to count blocks"})
return
}

// Fetch paginated blocks
blocks, err := models.FetchAllBlocks(db, limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve blocks"})
return
}

c.JSON(http.StatusOK, blocks)
// Return response with counter
c.JSON(http.StatusOK, gin.H{
"counter": totalCount,
"items": blocks,
})
}
}

Expand Down
16 changes: 15 additions & 1 deletion api/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,32 @@ import (
)

// GetAllTransactions retrieves all transactions with pagination
// GetAllTransactions retrieves all transactions with pagination and total count
func GetAllTransactions(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
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)
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)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve transactions"})
return
}

c.JSON(http.StatusOK, transactions)
// Return response with counter
c.JSON(http.StatusOK, gin.H{
"counter": totalCount,
"items": transactions,
})
}
}

Expand Down

0 comments on commit 49f89ef

Please sign in to comment.