package rest import ( "net/http" eth "custodial/pkg/eth" store "custodial/pkg/store" gin "github.com/gin-gonic/gin" ) type ETHApproveUSDTRequest struct { AccountID string `json:"account_id"` } type ETHApproveContractRequest struct { AccountID string `json:"account_id"` ConractAddress string `json:"contract_address"` } type ETHApproveResponse struct { Status string `json:"status"` TXID string `json:"txid"` } // @BasePath /api/v1 // // EthApproveUsdtV1 godoc // // @Summary Approve USDT spender // @Schemes // @Description Approve USDT spender address for account // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body ETHApproveUSDTRequest true "Account Info" // @Success 200 {object} ETHApproveResponse // @Failure 400 {object} ErrorResponse // @Router /eth/approve-usdt-spender [post] func EthApproveUsdtSpenderV1(c *gin.Context) { var req ETHApproveUSDTRequest 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 } if storedAccount.GetSpender() != "" { c.JSON(http.StatusBadRequest, gin.H{"error": "Spender already approved"}) return } ethSpender, _ := eth.GetSpender() approveAmount := "1000000000000" hdIndex := storedAccount.GetHDIndex() account, err := eth.DeriveAccount(hdIndex) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } balance, err := eth.EthBalance(account.Address().String()) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } if balance.ETHRaw < 1000000 { c.JSON(http.StatusBadRequest, gin.H{"error": "Not enough ETH to pay for transaction"}) return } txid, err := account.ApproveUSDTSpender(ethSpender.Address().String(), approveAmount) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } storedAccount.(*store.EthereumAccount).Spender = ethSpender.Address().String() store.UpdateAccount(storedAccount) c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"}) } // @BasePath /api/v1 // // EthApproveUsdtV1 godoc // // @Summary Approve USDT spender contract // @Schemes // @Description Approve USDT spender contract address for account // @Tags Ethereum // @Accept json // @Produce json // @Security ApiKeyAuth // @Param message body ETHApproveContractRequest true "Account Info" // @Success 200 {object} ETHApproveResponse // @Failure 400 {object} ErrorResponse // @Router /eth/approve-usdt-contract [post] func EthApproveUsdtContractV1(c *gin.Context) { var req ETHApproveContractRequest 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 } approveAmount := "1000000000000" hdIndex := storedAccount.GetHDIndex() account, err := eth.DeriveAccount(hdIndex) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } balance, err := eth.EthBalance(account.Address().String()) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } if balance.ETHRaw < 1000000000000 { c.JSON(http.StatusBadRequest, gin.H{"error": "Not enough ETH 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.EthereumAccount).Spender = req.ConractAddress store.UpdateAccount(storedAccount) c.JSON(http.StatusOK, gin.H{"txid": txid, "status": "ok"}) }