wallet/pkg/rest/eth_v1_recharge.go
Anton Nesterov 5efcf35dd6
[init]
2024-08-31 16:59:34 +02:00

64 lines
1.5 KiB
Go

package rest
import (
"net/http"
eth "custodial/pkg/eth"
store "custodial/pkg/store"
gin "github.com/gin-gonic/gin"
)
type RechargeETHRequest struct {
AccountID string `json:"account_id"`
Amount string `json:"amount"`
}
type RechargeETHResponse struct {
Status string `json:"status"`
TXID string `json:"txid"`
}
// @BasePath /api/v1
//
// EthRechargeEthBySpenderV1 godoc
//
// @Summary Add ETH from spender account
// @Schemes
// @Description Add ETH funds to the account from the spender
// @Tags Ethereum
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param message body RechargeETHRequest true "Account Info"
// @Success 200 {object} RechargeETHResponse
// @Failure 400 {object} ErrorResponse
// @Router /eth/recharge-eth-by-spender [post]
func EthRechargeEthBySpenderV1(c *gin.Context) {
var req RechargeETHRequest
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.WithdrawETH(storedAccount.GetAddress(), 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"})
}