Skip to content

Commit

Permalink
refactor(handler): update logging for CreateSwapRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
lmquang committed Feb 21, 2025
1 parent 75f2252 commit 157d1ec
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
23 changes: 13 additions & 10 deletions internal/handler/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func New(
swapRequestStore: swaprequest.New(),
}
}

func (h *handler) GenerateSignature(c *gin.Context) {
var req GenerateSignatureRequest
if err := c.ShouldBindJSON(&req); err != nil {
Expand Down Expand Up @@ -108,9 +109,11 @@ func (h *handler) GenerateSignature(c *gin.Context) {
}

c.JSON(http.StatusOK, view.CreateResponse[any](map[string]interface{}{
"signature": signature,
"nonce": nonce,
"deadline": deadline,
"signature": signature,
"nonce": nonce,
"deadline": deadline,
"icy_amount": icyAmount.Value,
"btc_amount": btcAmount.Value,
}, nil, nil, "signature generated successfully"))
}

Expand All @@ -129,7 +132,7 @@ func (h *handler) GenerateSignature(c *gin.Context) {
func (h *handler) CreateSwapRequest(c *gin.Context) {
var req SwapRequest
if err := c.ShouldBindJSON(&req); err != nil {
h.logger.Error("[TriggerSwap][ShouldBindJSON]", map[string]string{
h.logger.Error("[CreateSwapRequest][ShouldBindJSON]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, err, req, "invalid request"))
Expand All @@ -139,7 +142,7 @@ func (h *handler) CreateSwapRequest(c *gin.Context) {
// validate req
err := validator.New().Struct(req)
if err != nil {
h.logger.Error("[TriggerSwap][Validator]", map[string]string{
h.logger.Error("[CreateSwapRequest][Validator]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, err, req, "invalid request"))
Expand All @@ -148,7 +151,7 @@ func (h *handler) CreateSwapRequest(c *gin.Context) {

icyAmountFloat, err := strconv.ParseFloat(req.ICYAmount, 64)
if err != nil {
h.logger.Error("[TriggerSwap][ParseFloat]", map[string]string{
h.logger.Error("[CreateSwapRequest][ParseFloat]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, err, req, "invalid ICY amount"))
Expand All @@ -162,15 +165,15 @@ func (h *handler) CreateSwapRequest(c *gin.Context) {
// Check if the ICY transaction has already been exisiting
existingTx, err := h.btcProcessedTxStore.GetByIcyTransactionHash(h.db, req.IcyTx)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
h.logger.Error("[TriggerSwap][CheckICYTransaction]", map[string]string{
h.logger.Error("[CreateSwapRequest][CheckICYTransaction]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, err, nil, "failed to check ICY transaction"))
return
}

if existingTx != nil {
h.logger.Error("[TriggerSwap][DuplicateICYTransaction]", map[string]string{
h.logger.Error("[CreateSwapRequest][DuplicateICYTransaction]", map[string]string{
"tx_hash": req.IcyTx,
})
c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, fmt.Errorf("transaction already processed"), nil, "transaction has already been used for a swap"))
Expand All @@ -196,7 +199,7 @@ func (h *handler) CreateSwapRequest(c *gin.Context) {
_, err = h.swapRequestStore.Create(tx, swapRequest)
if err != nil {
tx.Rollback()
h.logger.Error("[TriggerSwap][CreateSwapRequest]", map[string]string{
h.logger.Error("[CreateSwapRequest][Create]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, err, nil, "failed to create swap request"))
Expand All @@ -205,7 +208,7 @@ func (h *handler) CreateSwapRequest(c *gin.Context) {

// Commit the transaction
if err := tx.Commit().Error; err != nil {
h.logger.Error("[TriggerSwap][CommitTransaction]", map[string]string{
h.logger.Error("[CreateSwapRequest][CommitTransaction]", map[string]string{
"error": err.Error(),
})
c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, err, nil, "failed to commit transaction"))
Expand Down
12 changes: 1 addition & 11 deletions internal/telemetry/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package telemetry
import (
"errors"
"fmt"
"math"
"math/big"
"strconv"

"github.com/dwarvesf/icy-backend/internal/consts"
"github.com/dwarvesf/icy-backend/internal/model"
Expand Down Expand Up @@ -48,16 +46,8 @@ func (t *Telemetry) ProcessSwapRequests() error {
continue
}

icyAmountFloat, err := strconv.ParseFloat(req.ICYAmount, 64)
if err != nil {
t.logger.Error("[ProcessSwapRequests][ParseFloat]", map[string]string{
"error": err.Error(),
})
continue
}

icyAmount := &model.Web3BigInt{
Value: fmt.Sprintf("%.0f", icyAmountFloat*math.Pow(10, 18)),
Value: req.ICYAmount,
Decimal: 18,
}

Expand Down

0 comments on commit 157d1ec

Please sign in to comment.