Skip to content

Commit

Permalink
refactor(transaction): rename List to Find in transaction handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Feb 24, 2025
1 parent 83ceca6 commit 0931158
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/handler/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (h *transactionHandler) GetTransactions(c *gin.Context) {
}

// Fetch transactions
transactions, total, err := h.onchainbtcprocessedtransaction.List(h.db, filter)
transactions, total, err := h.onchainbtcprocessedtransaction.Find(h.db, filter)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch transactions"})
return
Expand Down
13 changes: 13 additions & 0 deletions internal/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ func (o *IcyOracle) GetCirculatedICY() (*model.Web3BigInt, error) {
sum = sum.Add(balance)
}

totalSwapIcy, err := o.store.OnchainIcySwapTransaction.SumTotalIcyAmount(o.db)
if err != nil {
o.logger.Error("[GetCirculatedICY][SumTotalIcyAmount]", map[string]string{
"error": err.Error(),
})
return nil, err
}

sum = sum.Add(&model.Web3BigInt{
Value: totalSwapIcy.String(),
Decimal: 18,
})

return totalSupply.Sub(sum), nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/store/onchainbtcprocessedtransaction/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ type IStore interface {
// Get all pending BTC processed transactions
GetPendingTransactions(tx *gorm.DB) ([]model.OnchainBtcProcessedTransaction, error)

List(db *gorm.DB, filter ListFilter) ([]*model.OnchainBtcProcessedTransaction, int64, error)
Find(db *gorm.DB, filter ListFilter) ([]*model.OnchainBtcProcessedTransaction, int64, error)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *store) GetPendingTransactions(tx *gorm.DB) ([]model.OnchainBtcProcessed
return pendingTxs, err
}

func (s *store) List(db *gorm.DB, filter ListFilter) ([]*model.OnchainBtcProcessedTransaction, int64, error) {
func (s *store) Find(db *gorm.DB, filter ListFilter) ([]*model.OnchainBtcProcessedTransaction, int64, error) {
var transactions []*model.OnchainBtcProcessedTransaction
var total int64

Expand Down
3 changes: 3 additions & 0 deletions internal/store/onchainicyswaptransaction/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package onchainicyswaptransaction

import (
"math/big"

"gorm.io/gorm"

"github.com/dwarvesf/icy-backend/internal/model"
Expand All @@ -10,4 +12,5 @@ type Store interface {
Create(db *gorm.DB, transaction *model.OnchainIcySwapTransaction) (*model.OnchainIcySwapTransaction, error)
GetByTransactionHash(db *gorm.DB, hash string) (*model.OnchainIcySwapTransaction, error)
GetLatestTransaction(db *gorm.DB) (*model.OnchainIcySwapTransaction, error)
SumTotalIcyAmount(db *gorm.DB) (*big.Int, error)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package onchainicyswaptransaction

import (
"fmt"
"math/big"

"gorm.io/gorm"

"github.com/dwarvesf/icy-backend/internal/model"
Expand Down Expand Up @@ -34,3 +37,27 @@ func (s *store) GetLatestTransaction(db *gorm.DB) (*model.OnchainIcySwapTransact
}
return &transaction, nil
}

// SumTotalIcyAmount calculates the total ICY amount of all OnchainIcySwapTransactions in the database
func (s *store) SumTotalIcyAmount(db *gorm.DB) (*big.Int, error) {
var result struct {
TotalAmount string
}

// Use SQL to sum the icy_amount column
err := db.Model(&model.OnchainIcySwapTransaction{}).
Select("SUM(icy_amount::numeric) as total_amount").
Scan(&result).Error

if err != nil {
return nil, fmt.Errorf("failed to calculate total ICY amount: %v", err)
}

// Convert the sum to big.Int
total, ok := new(big.Int).SetString(result.TotalAmount, 10)
if !ok {
return nil, fmt.Errorf("invalid total ICY amount format: %s", result.TotalAmount)
}

return total, nil
}
7 changes: 4 additions & 3 deletions internal/telemetry/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ func (t *Telemetry) ProcessSwapRequests() error {
_, err = t.baseRpc.Swap(icyAmount, req.BTCAddress, satAmount)
if err != nil {
t.logger.Error("[ProcessSwapRequests][Swap]", map[string]string{
"error": err.Error(),
"icy_amount": req.ICYAmount,
"btc_address": req.BTCAddress,
"error": err.Error(),
"icy_amount": req.ICYAmount,
"btc_address": req.BTCAddress,
"latest_price": latestPrice.Value,
})
continue
}
Expand Down

0 comments on commit 0931158

Please sign in to comment.