2023-02-15 08:05:43 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
|
|
|
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
2023-02-15 08:05:43 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2023-04-14 09:56:21 +02:00
|
|
|
// HandleSignTransfer godoc
|
2023-05-16 14:20:01 +02:00
|
|
|
//
|
2023-04-14 09:56:21 +02:00
|
|
|
// @Summary Sign and dispatch transfer request.
|
|
|
|
// @Description Sign and dispatch a transfer request.
|
|
|
|
// @Tags network
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param signTransferRequest body object{from=string,to=string,voucherAddress=string,amount=uint64} true "Sign Transfer Request"
|
|
|
|
// @Success 200 {object} OkResp
|
|
|
|
// @Failure 400 {object} ErrResp
|
|
|
|
// @Failure 500 {object} ErrResp
|
|
|
|
// @Router /sign/transfer [post]
|
2023-03-28 08:39:44 +02:00
|
|
|
func HandleSignTransfer(cu *custodial.Custodial) func(echo.Context) error {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
req struct {
|
|
|
|
From string `json:"from" validate:"required,eth_addr_checksum"`
|
|
|
|
To string `json:"to" validate:"required,eth_addr_checksum"`
|
|
|
|
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr_checksum"`
|
2023-07-10 08:32:05 +02:00
|
|
|
Amount uint64 `json:"amount" validate:"gt=0"`
|
2023-03-28 08:39:44 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := c.Bind(&req); err != nil {
|
|
|
|
return NewBadRequestError(ErrInvalidJSON)
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
if err := c.Validate(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
accountActive, gasLock, err := cu.Store.GetAccountStatus(c.Request().Context(), req.From)
|
2023-03-28 08:39:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
if !accountActive {
|
|
|
|
return c.JSON(http.StatusForbidden, ErrResp{
|
|
|
|
Ok: false,
|
|
|
|
Message: "Account pending activation. Try again later.",
|
|
|
|
})
|
|
|
|
}
|
2023-03-08 07:59:10 +01:00
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
if gasLock {
|
2023-03-28 08:39:44 +02:00
|
|
|
return c.JSON(http.StatusForbidden, ErrResp{
|
|
|
|
Ok: false,
|
2023-05-16 14:20:01 +02:00
|
|
|
Message: "Gas lock. Gas balance unavailable. Try again later.",
|
2023-03-28 08:39:44 +02:00
|
|
|
})
|
|
|
|
}
|
2023-03-08 07:49:09 +01:00
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
trackingId := uuid.NewString()
|
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
taskPayload, err := json.Marshal(task.TransferPayload{
|
|
|
|
TrackingId: trackingId,
|
|
|
|
From: req.From,
|
|
|
|
To: req.To,
|
|
|
|
VoucherAddress: req.VoucherAddress,
|
|
|
|
Amount: req.Amount,
|
2023-03-08 07:49:09 +01:00
|
|
|
})
|
2023-03-28 08:39:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-08 07:49:09 +01:00
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
c.Request().Context(),
|
|
|
|
tasker.SignTransferTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Id: trackingId,
|
|
|
|
Payload: taskPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-03-28 08:39:44 +02:00
|
|
|
return c.JSON(http.StatusOK, OkResp{
|
|
|
|
Ok: true,
|
|
|
|
Result: H{
|
|
|
|
"trackingId": trackingId,
|
|
|
|
},
|
|
|
|
})
|
2023-03-08 07:49:09 +01:00
|
|
|
}
|
2023-02-15 08:05:43 +01:00
|
|
|
}
|