package rest import ( "net/http" store "custodial/pkg/store" trc "custodial/pkg/tron" gin "github.com/gin-gonic/gin" ) type TRXAprroveUSDTRequest struct { AccountID string `json:"account_id"` } type TRXAprroveContractRequest struct { AccountID string `json:"account_id"` ConractAddress string `json:"contract_address"` } type TRXApproveUSDTResponse struct { Status string `json:"status"` TXID string `json:"txid"` } // @BasePath /api/v1 // // TronApproveUsdtV1 godoc // // @Summary Approve USDT Spender // @Schemes // @Description Approve USDT spender for account // @Tags Tron // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body TRXAprroveUSDTRequest true "Account Info" // @Success 200 {object} TRXApproveUSDTResponse // @Failure 400 {object} ErrorResponse // @Router /tron/approve-usdt-spender [post] func TronApproveUsdtSpenderV1(c *gin.Context) { var req TRXAprroveUSDTRequest 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 } // if storedAccount.GetSpender() != "" { // c.JSON(http.StatusBadRequest, gin.H{"error": "Spender already approved"}) // return // } trcSpender, _ := trc.GetSpender() approveAmount := "1000000000000" hdIndex := storedAccount.GetHDIndex() account, err := trc.DeriveAccount(hdIndex) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } balance, err := trc.TronBalance(account.Address()) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } if balance.TRXRaw < 1000000 { c.JSON(http.StatusBadRequest, gin.H{"error": "Not enough TRX to pay for transaction"}) return } txid, err := account.ApproveUSDTSpender(trcSpender.Address(), approveAmount) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } storedAccount.(*store.TronAccount).Spender = trcSpender.Address() store.UpdateAccount(storedAccount) c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"}) } // @BasePath /api/v1 // // TronApproveContractV1 godoc // // @Summary Approve USDT Spender Contract // @Schemes // @Description Approve USDT spender for contract // @Tags Tron // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body TRXAprroveContractRequest true "Account Info" // @Success 200 {object} TRXApproveUSDTResponse // @Failure 400 {object} ErrorResponse // @Router /tron/approve-usdt-contract [post] func TronApproveUsdtContractV1(c *gin.Context) { var req TRXAprroveContractRequest 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 } approveAmount := "1000000000000" hdIndex := storedAccount.GetHDIndex() account, err := trc.DeriveAccount(hdIndex) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } balance, err := trc.TronBalance(account.Address()) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } if balance.TRXRaw < 1000000 { c.JSON(http.StatusBadRequest, gin.H{"error": "Not enough TRX to pay for transaction"}) return } txid, err := account.ApproveUSDTSpender(req.ConractAddress, approveAmount) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } storedAccount.(*store.TronAccount).Spender = req.ConractAddress store.UpdateAccount(storedAccount) c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"}) }