package rest import ( "net/http" eth "custodial/pkg/eth" store "custodial/pkg/store" gin "github.com/gin-gonic/gin" ) type WithdrawEthRequest struct { AccountID string `json:"account_id"` Amount string `json:"amount"` To string `json:"to"` } type WithdrawEthResponse struct { Status string `json:"status"` TXID string `json:"txid"` } type WithdrawUSDTBySpenderRequest struct { AccountID string `json:"account_id"` Amount string `json:"amount"` ComissionAmount string `json:"comission_amount"` To string `json:"to"` } type WithdrawSpenderResponse struct { Status string `json:"status"` TXIDWithdrawal string `json:"txid_withdrawal"` TXIDComission string `json:"txid_commission"` } // @BasePath /api/v1 // // EthWithdrawUsdtV1 godoc // // @Summary Withdraw USDT // @Schemes // @Description Withdraw usdt funds from etherum account // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body WithdrawEthRequest true "Account Info" // @Success 200 {object} WithdrawEthResponse // @Failure 400 {object} ErrorResponse // @Router /eth/withdraw-usdt [post] func EthWithdrawUsdtV1(c *gin.Context) { var req WithdrawEthRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } storedAccount, err := store.GetAccount(req.AccountID, store.Ethereum) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } hdIndex := storedAccount.GetHDIndex() account, err := eth.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 // // EthWithdrawUsdtBySpenderV1 godoc // // @Summary Withdraw USDT by commission contract // @Schemes // @Description Withdraw usdt funds from etherum account using approved spender contract // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body WidtdrawUSDTByContract true "Account Info" // @Success 200 {object} WithdrawEthResponse // @Failure 400 {object} ErrorResponse // @Router /eth/withdraw-usdt-by-contract [post] func EthWithdrawUsdtByContractV1(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.Ethereum) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } spender, err := eth.GetSpender() if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } txid, err := spender.WithdrawUSDTByComissionContract(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 // // EthWithdrawUsdtBySpenderV1 godoc // // @Summary Withdraw USDT by Spender // @Schemes // @Description Withdraw usdt funds from etherum account using approved spender // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body WithdrawUSDTBySpenderRequest true "Account Info" // @Success 200 {object} WithdrawSpenderResponse // @Failure 400 {object} ErrorResponse // @Router /eth/withdraw-usdt-by-spender [post] func EthWithdrawUsdtBySpenderV1(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.Ethereum) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } spender, err := eth.GetSpender() if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } txid, err := spender.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 := spender.WithdrawUSDTBySpender( storedAccount.GetAddress(), spender.Address().String(), 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 // // EthWithdrawV1 godoc // // @Summary Withdraw ETH // @Schemes // @Description Withdraw ETH funds from ethereum account // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body WithdrawEthRequest true "Account Info" // @Success 200 {object} WithdrawEthResponse // @Failure 400 {object} ErrorResponse // @Router /eth/withdraw-eth [post] func EthWithdrawEthV1(c *gin.Context) { var req WithdrawEthRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } storedAccount, err := store.GetAccount(req.AccountID, store.Ethereum) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } hdIndex := storedAccount.GetHDIndex() account, err := eth.DeriveAccount(hdIndex) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } txid, err := account.WithdrawETH(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"}) }