refactor: use echo ctx only for cancellation/propagation

This commit is contained in:
Mohamed Sohail 2023-03-28 06:39:44 +00:00
parent ef5bd1860f
commit 5f5678e25b
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
5 changed files with 173 additions and 178 deletions

View File

@ -29,12 +29,6 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
} }
server.HTTPErrorHandler = customHTTPErrorHandler server.HTTPErrorHandler = customHTTPErrorHandler
server.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("cu", custodialContainer)
return next(c)
}
})
server.Use(middleware.Recover()) server.Use(middleware.Recover())
server.Use(middleware.BodyLimit("1M")) server.Use(middleware.BodyLimit("1M"))
server.Use(middleware.ContextTimeout(contextTimeout)) server.Use(middleware.ContextTimeout(contextTimeout))
@ -48,10 +42,10 @@ func initApiServer(custodialContainer *custodial.Custodial) *echo.Echo {
apiRoute := server.Group("/api", systemGlobalLock) apiRoute := server.Group("/api", systemGlobalLock)
apiRoute.POST("/account/create", api.HandleAccountCreate) apiRoute.POST("/account/create", api.HandleAccountCreate(custodialContainer))
apiRoute.GET("/account/status/:address", api.HandleNetworkAccountStatus) apiRoute.GET("/account/status/:address", api.HandleNetworkAccountStatus(custodialContainer))
apiRoute.POST("/sign/transfer", api.HandleSignTransfer) apiRoute.POST("/sign/transfer", api.HandleSignTransfer(custodialContainer))
apiRoute.GET("/track/:trackingId", api.HandleTrackTx) apiRoute.GET("/track/:trackingId", api.HandleTrackTx(custodialContainer))
return server return server
} }

View File

@ -15,49 +15,47 @@ import (
// CreateAccountHandler route. // CreateAccountHandler route.
// POST: /api/account/create // POST: /api/account/create
// Returns the public key. // Returns the public key.
func HandleAccountCreate(c echo.Context) error { func HandleAccountCreate(cu *custodial.Custodial) func(echo.Context) error {
var ( return func(c echo.Context) error {
cu = c.Get("cu").(*custodial.Custodial) generatedKeyPair, err := keypair.Generate()
) if err != nil {
return err
}
generatedKeyPair, err := keypair.Generate() id, err := cu.Keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
if err != nil { if err != nil {
return err return err
}
trackingId := uuid.NewString()
taskPayload, err := json.Marshal(task.AccountPayload{
PublicKey: generatedKeyPair.Public,
TrackingId: trackingId,
})
if err != nil {
return err
}
_, err = cu.TaskerClient.CreateTask(
c.Request().Context(),
tasker.AccountPrepareTask,
tasker.DefaultPriority,
&tasker.Task{
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"publicKey": generatedKeyPair.Public,
"custodialId": id,
"trackingId": trackingId,
},
})
} }
id, err := cu.Keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
if err != nil {
return err
}
trackingId := uuid.NewString()
taskPayload, err := json.Marshal(task.AccountPayload{
PublicKey: generatedKeyPair.Public,
TrackingId: trackingId,
})
if err != nil {
return err
}
_, err = cu.TaskerClient.CreateTask(
c.Request().Context(),
tasker.AccountPrepareTask,
tasker.DefaultPriority,
&tasker.Task{
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"publicKey": generatedKeyPair.Public,
"custodialId": id,
"trackingId": trackingId,
},
})
} }

View File

@ -11,41 +11,42 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
func HandleNetworkAccountStatus(c echo.Context) error { func HandleNetworkAccountStatus(cu *custodial.Custodial) func(echo.Context) error {
var ( return func(c echo.Context) error {
cu = c.Get("cu").(*custodial.Custodial) var (
accountStatusRequest struct { accountStatusRequest struct {
Address string `param:"address" validate:"required,eth_addr_checksum"` Address string `param:"address" validate:"required,eth_addr_checksum"`
}
networkBalance big.Int
networkNonce uint64
)
if err := c.Bind(&accountStatusRequest); err != nil {
return NewBadRequestError(ErrInvalidJSON)
} }
networkBalance big.Int
networkNonce uint64
)
if err := c.Bind(&accountStatusRequest); err != nil { if err := c.Validate(accountStatusRequest); err != nil {
return NewBadRequestError(ErrInvalidJSON) return err
}
if err := cu.CeloProvider.Client.CallCtx(
c.Request().Context(),
eth.Nonce(w3.A(accountStatusRequest.Address), nil).Returns(&networkNonce),
eth.Balance(w3.A(accountStatusRequest.Address), nil).Returns(&networkBalance),
); err != nil {
return err
}
if networkNonce > 0 {
networkNonce--
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"balance": fmt.Sprintf("%s CELO", w3.FromWei(&networkBalance, 18)),
"nonce": networkNonce,
},
})
} }
if err := c.Validate(accountStatusRequest); err != nil {
return err
}
if err := cu.CeloProvider.Client.CallCtx(
c.Request().Context(),
eth.Nonce(w3.A(accountStatusRequest.Address), nil).Returns(&networkNonce),
eth.Balance(w3.A(accountStatusRequest.Address), nil).Returns(&networkBalance),
); err != nil {
return err
}
if networkNonce > 0 {
networkNonce--
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"balance": fmt.Sprintf("%s CELO", w3.FromWei(&networkBalance, 18)),
"nonce": networkNonce,
},
})
} }

View File

@ -21,78 +21,79 @@ import (
// amount -> int (6 d.p. precision) // amount -> int (6 d.p. precision)
// e.g. 1000000 = 1 VOUCHER // e.g. 1000000 = 1 VOUCHER
// Returns the task id. // Returns the task id.
func HandleSignTransfer(c echo.Context) error { func HandleSignTransfer(cu *custodial.Custodial) func(echo.Context) error {
var ( return func(c echo.Context) error {
cu = c.Get("cu").(*custodial.Custodial) var (
req struct { req struct {
From string `json:"from" validate:"required,eth_addr_checksum"` From string `json:"from" validate:"required,eth_addr_checksum"`
To string `json:"to" validate:"required,eth_addr_checksum"` To string `json:"to" validate:"required,eth_addr_checksum"`
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr_checksum"` VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr_checksum"`
Amount uint64 `json:"amount" validate:"required"` Amount uint64 `json:"amount" validate:"required"`
}
)
if err := c.Bind(&req); err != nil {
return NewBadRequestError(ErrInvalidJSON)
} }
)
if err := c.Bind(&req); err != nil { if err := c.Validate(req); err != nil {
return NewBadRequestError(ErrInvalidJSON) return err
} }
if err := c.Validate(req); err != nil { accountActive, gasQuota, err := cu.PgStore.GetAccountStatusByAddress(c.Request().Context(), req.From)
return err if err != nil {
} return err
}
accountActive, gasQuota, err := cu.PgStore.GetAccountStatusByAddress(c.Request().Context(), req.From) if !accountActive {
if err != nil { return c.JSON(http.StatusForbidden, ErrResp{
return err Ok: false,
} Message: "Account pending activation. Try again later.",
})
}
if !accountActive { if gasQuota < 1 {
return c.JSON(http.StatusForbidden, ErrResp{ return c.JSON(http.StatusForbidden, ErrResp{
Ok: false, Ok: false,
Message: "Account pending activation. Try again later.", Message: "Out of gas, refill pending. Try again later.",
})
}
trackingId := uuid.NewString()
taskPayload, err := json.Marshal(task.TransferPayload{
TrackingId: trackingId,
From: req.From,
To: req.To,
VoucherAddress: req.VoucherAddress,
Amount: req.Amount,
})
if err != nil {
return err
}
_, err = cu.TaskerClient.CreateTask(
c.Request().Context(),
tasker.SignTransferTask,
tasker.HighPriority,
&tasker.Task{
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return err
}
err = cu.PgStore.DecrGasQuota(c.Request().Context(), req.From)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"trackingId": trackingId,
},
}) })
} }
if gasQuota < 1 {
return c.JSON(http.StatusForbidden, ErrResp{
Ok: false,
Message: "Out of gas, refill pending. Try again later.",
})
}
trackingId := uuid.NewString()
taskPayload, err := json.Marshal(task.TransferPayload{
TrackingId: trackingId,
From: req.From,
To: req.To,
VoucherAddress: req.VoucherAddress,
Amount: req.Amount,
})
if err != nil {
return err
}
_, err = cu.TaskerClient.CreateTask(
c.Request().Context(),
tasker.SignTransferTask,
tasker.HighPriority,
&tasker.Task{
Id: trackingId,
Payload: taskPayload,
},
)
if err != nil {
return err
}
err = cu.PgStore.DecrGasQuota(c.Request().Context(), req.From)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"trackingId": trackingId,
},
})
} }

View File

@ -12,31 +12,32 @@ import (
// Route param: // Route param:
// trackingId -> tracking UUID // trackingId -> tracking UUID
// Returns array of tx status. // Returns array of tx status.
func HandleTrackTx(c echo.Context) error { func HandleTrackTx(cu *custodial.Custodial) func(echo.Context) error {
var ( return func(c echo.Context) error {
cu = c.Get("cu").(*custodial.Custodial) var (
txStatusRequest struct { txStatusRequest struct {
TrackingId string `param:"trackingId" validate:"required,uuid"` TrackingId string `param:"trackingId" validate:"required,uuid"`
}
)
if err := c.Bind(&txStatusRequest); err != nil {
return NewBadRequestError(err)
} }
)
if err := c.Bind(&txStatusRequest); err != nil { if err := c.Validate(txStatusRequest); err != nil {
return NewBadRequestError(err) return err
}
txs, err := cu.PgStore.GetTxStatusByTrackingId(c.Request().Context(), txStatusRequest.TrackingId)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"transactions": txs,
},
})
} }
if err := c.Validate(txStatusRequest); err != nil {
return err
}
txs, err := cu.PgStore.GetTxStatusByTrackingId(c.Request().Context(), txStatusRequest.TrackingId)
if err != nil {
return err
}
return c.JSON(http.StatusOK, OkResp{
Ok: true,
Result: H{
"transactions": txs,
},
})
} }