wallet/pkg/rest/tron_v1_withdraw.go
Anton Nesterov b19bf34e5f
[init]
2024-08-31 16:54:43 +02:00

225 lines
5.7 KiB
Go

package rest
import (
"net/http"
store "custodial/pkg/store"
trc "custodial/pkg/tron"
gin "github.com/gin-gonic/gin"
)
type WithdrawTronRequest struct {
AccountID string `json:"account_id"`
Amount string `json:"amount"`
To string `json:"to"`
}
type WithdrawTronResponse struct {
Status string `json:"status"`
TXID string `json:"txid"`
}
type WidtdrawUSDTByContract struct {
AccountID string `json:"account_id"`
Amount string `json:"amount"`
To string `json:"to"`
ComissionAmount string `json:"comission_amount"`
ContractAddress string `json:"contract_address"`
}
// @BasePath /api/v1
//
// TronWithdrawUsdtByContractV1 godoc
//
// @Summary Withdraw USDT by commision contract
// @Schemes
// @Description Withdraw usdt funds from tron account using approved comission contract
// @Tags Tron
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body WidtdrawUSDTByContract true "Account Info"
// @Success 200 {object} WithdrawTronResponse
// @Failure 400 {object} ErrorResponse
// @Router /tron/withdraw-usdt-by-contract [post]
func TronWithdrawUsdtByContractV1(c *gin.Context) {
var req WidtdrawUSDTByContract
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
storedAccount, err := store.GetAccount(req.AccountID, store.Tron)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
spenderAccount, err := trc.GetSpender()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
txid, err := spenderAccount.WithdrawUSDTByCommissionContract(req.ContractAddress, storedAccount.GetAddress(), req.To, req.Amount, req.ComissionAmount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"txid": txid,
"status": "ok",
})
}
// @BasePath /api/v1
//
// TronWithdrawUsdtV1 godoc
//
// @Summary Withdraw USDT by spender
// @Schemes
// @Description Withdraw usdt funds from tron account using approved spender
// @Tags Tron
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body WithdrawUSDTBySpenderRequest true "Account Info"
// @Success 200 {object} WithdrawSpenderResponse
// @Failure 400 {object} ErrorResponse
// @Router /tron/withdraw-usdt-by-spender [post]
func TronWithdrawUsdtBySpenderV1(c *gin.Context) {
var req WithdrawUSDTBySpenderRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
storedAccount, err := store.GetAccount(req.AccountID, store.Tron)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
spenderAccount, err := trc.GetSpender()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
txid, err := spenderAccount.WithdrawUSDTBySpender(storedAccount.GetAddress(), req.To, req.Amount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
comission := ""
if req.ComissionAmount != "" && req.ComissionAmount != "0" {
txidComission, err := spenderAccount.WithdrawUSDTBySpender(
storedAccount.GetAddress(),
spenderAccount.Address(),
req.ComissionAmount,
)
if err != nil {
comission = err.Error()
} else {
comission = txidComission
}
}
c.JSON(http.StatusOK, gin.H{
"txid_withdrawal": txid,
"txid_commission": comission,
"status": "ok",
})
}
// @BasePath /api/v1
//
// TronWithdrawUsdtV1 godoc
//
// @Summary Withdraw USDT
// @Schemes
// @Description Withdraw usdt funds from tron account
// @Tags Tron
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body WithdrawTronRequest true "Account Info"
// @Success 200 {object} WithdrawTronResponse
// @Failure 400 {object} ErrorResponse
// @Router /tron/withdraw-usdt [post]
func TronWithdrawUsdtV1(c *gin.Context) {
var req WithdrawTronRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
storedAccount, err := store.GetAccount(req.AccountID, store.Tron)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hdIndex := storedAccount.GetHDIndex()
account, err := trc.DeriveAccount(hdIndex)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
txid, err := account.WithdrawUSDT(req.To, req.Amount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"})
}
// @BasePath /api/v1
//
// TronWithdrawUsdtV1 godoc
//
// @Summary Withdraw TRX
// @Schemes
// @Description Withdraw TRON funds from tron account
// @Tags Tron
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body WithdrawTronRequest true "Account Info"
// @Success 200 {object} WithdrawTronResponse
// @Failure 400 {object} ErrorResponse
// @Router /tron/withdraw-trx [post]
func TronWithdrawTrxV1(c *gin.Context) {
var req WithdrawTronRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
storedAccount, err := store.GetAccount(req.AccountID, store.Tron)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hdIndex := storedAccount.GetHDIndex()
account, err := trc.DeriveAccount(hdIndex)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
txid, err := account.WithdrawTRX(req.To, req.Amount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"})
}