wallet/pkg/rest/eth_v1_transaction.go

63 lines
1.3 KiB
Go
Raw Normal View History

2024-08-31 14:46:20 +00:00
package rest
import (
"net/http"
eth "custodial/pkg/eth"
gin "github.com/gin-gonic/gin"
)
type EthTransactionInfoRequest struct {
TxID string `json:"tx_id"`
}
type EthTransactionInfoResponse struct {
Id string `json:"id"`
Status string `json:"status"`
}
// @BasePath /api/v1
//
// AddressBalance godoc
//
// @Summary Get transaction info
// @Schemes
// @Description Get transaction info
// @Tags Ethereum
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body EthTransactionInfoRequest true "Tx Info"
// @Success 200 {object} EthTransactionInfoResponse
// @Failure 400 {object} ErrorResponse
// @Router /eth/tx-info [post]
func EthTransactionInfoV1(c *gin.Context) {
var req EthTransactionInfoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tx, err := eth.GetTransactionByHash(req.TxID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
status := "pending"
if tx == nil {
status = "pending"
} else if tx.Status == 1 {
status = "success"
} else if tx.Status == 0 {
status = "failed"
}
res := EthTransactionInfoResponse{
Id: req.TxID,
Status: status,
}
c.JSON(http.StatusOK, res)
}