wallet/pkg/rest/account_v1_balance.go
Anton Nesterov f1bc53ce2a
[init]
2024-08-31 17:00:14 +02:00

154 lines
4.2 KiB
Go

package rest
import (
"net/http"
eth "custodial/pkg/eth"
store "custodial/pkg/store"
trc "custodial/pkg/tron"
gin "github.com/gin-gonic/gin"
)
type AccountBalanceRequest struct {
AccountID string `json:"account_id"`
}
type AccountHDBalanceRequest struct {
Index int `json:"index"`
}
type AccountBalanceResponse struct {
AccountID string `json:"account_id"`
Label string `json:"label"`
Tron trc.TronAccountBalance
Ethereum eth.EthAccountBalance
TronSpender string `json:"tron_spender"`
EthSpender string `json:"eth_spender"`
TronHdIndex int `json:"tron_hd_index"`
EthHdIndex int `json:"eth_hd_index"`
EthAllowance string `json:"eth_allowance"`
TronAllowance string `json:"tron_allowance"`
}
type AccountHDBalanceResponse struct {
Tron trc.TronAccountBalance
Ethereum eth.EthAccountBalance
EthAllowance string `json:"eth_allowance"`
TronAllowance string `json:"tron_allowance"`
}
// @BasePath /api/v1
//
// AccountBalance godoc
//
// @Summary Get account balance
// @Schemes
// @Description Get account balance for all currencies
// @Tags Account
// @Accept json
// @Security ApiKeyAuth
// @Produce json
// @Param message body AccountBalanceRequest true "Account Info"
// @Success 200 {object} AccountBalanceResponse
// @Failure 400 {object} ErrorResponse
// @Router /account/balance [post]
func AccountBalanceV1(c *gin.Context) {
var req AccountBalanceRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
dbEthAccount, err := store.GetAccount(req.AccountID, store.Ethereum)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
dbTronAccount, err := store.GetAccount(req.AccountID, store.Tron)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
tronBalance, err := trc.TronBalance(dbTronAccount.GetAddress())
tronSpender, _ := trc.GetSpender()
tronAllowance, _ := trc.TronAllowance(dbTronAccount.GetAddress(), tronSpender.Address())
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ethBalance, err := eth.EthBalance(dbEthAccount.GetAddress())
ethSpender, _ := eth.GetSpender()
ethAllowance, _ := eth.EthAllowance(dbEthAccount.GetAddress(), ethSpender.Address().String())
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"account_id": req.AccountID,
"label": dbEthAccount.GetLabel(),
"tron": tronBalance,
"ethereum": ethBalance,
"tron_spender": dbTronAccount.GetSpender(),
"eth_spender": dbEthAccount.GetSpender(),
"tron_hd_index": dbTronAccount.GetHDIndex(),
"eth_hd_index": dbEthAccount.GetHDIndex(),
"eth_allowance": ethAllowance,
"tron_allowance": tronAllowance,
})
}
// @BasePath /api/v1
//
// AccountBalance godoc
//
// @Summary Get account balance
// @Schemes
// @Description Get account balance for all currencies
// @Tags Account
// @Accept json
// @Security ApiKeyAuth
// @Produce json
// @Param message body AccountHDBalanceRequest true "Account Info"
// @Success 200 {object} AccountHDBalanceResponse
// @Failure 400 {object} ErrorResponse
// @Router /account/hd/balance [post]
func AccountHDBalanceV1(c *gin.Context) {
var req AccountHDBalanceRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
keys := GetPrivKeys(req.Index)
tronBalance, err := trc.TronBalance(keys.TRONAddress)
tronSpender, _ := trc.GetSpender()
tronAllowance, _ := trc.TronAllowance(keys.TRONAddress, tronSpender.Address())
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ethBalance, err := eth.EthBalance(keys.ETHAddress)
ethSpender, _ := eth.GetSpender()
ethAllowance, _ := eth.EthAllowance(keys.ETHAddress, ethSpender.Address().String())
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"tron": tronBalance,
"ethereum": ethBalance,
"eth_allowance": ethAllowance,
"tron_allowance": tronAllowance,
})
}