mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2026-05-18 18:35:21 +02:00
wip: refactor taskers
This commit is contained in:
@@ -1,22 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// CreateAccountHandler route.
|
||||
// POST: /api/account/create.
|
||||
// Returns the public key and tasker account prep receipt.
|
||||
// POST: /api/account/create
|
||||
// JSON Body:
|
||||
// trackingId -> Unique string
|
||||
// Returns the public key.
|
||||
func CreateAccountHandler(
|
||||
taskerClient *tasker.TaskerClient,
|
||||
keystore keystore.Keystore,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
var accountRequest struct {
|
||||
TrackingId string `json:"trackingId" validate:"required"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&accountRequest); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
if err := c.Validate(accountRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
generatedKeyPair, err := keypair.Generate()
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
@@ -33,24 +52,38 @@ func CreateAccountHandler(
|
||||
})
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(task.AccountPayload{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
TrackingId: accountRequest.TrackingId,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.PrepareAccountTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Id: accountRequest.TrackingId,
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
Result: H{
|
||||
"publicKey": generatedKeyPair.Public,
|
||||
"keyId": id,
|
||||
"publicKey": generatedKeyPair.Public,
|
||||
"custodialId": id,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// AccountStatusHandler route.
|
||||
// GET: /api/account/status.
|
||||
// Check if an account is ready to be used.
|
||||
// Returns the status as a bool.
|
||||
func AccountStatusHandler() func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
71
internal/api/sign.go
Normal file
71
internal/api/sign.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// SignTxHandler route.
|
||||
// POST: /api/sign/transfer
|
||||
// JSON Body:
|
||||
// trackingId -> Unique string
|
||||
// from -> ETH address
|
||||
// to -> ETH address
|
||||
// voucherAddress -> ETH address
|
||||
// amount -> int (6 d.p. precision)
|
||||
// e.g. 1000000 = 1 VOUCHER
|
||||
// Returns the task id.
|
||||
func SignTransferHandler(
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
var transferRequest struct {
|
||||
TrackingId string `json:"trackingId" validate:"required"`
|
||||
From string `json:"from" validate:"required,eth_address"`
|
||||
To string `json:"to" validate:"required,eth_addr"`
|
||||
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
|
||||
Amount int64 `json:"amount" validate:"required,numeric"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&transferRequest); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
if err := c.Validate(transferRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(transferRequest)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TransferTokenTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Id: transferRequest.TrackingId,
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Code: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -31,10 +31,12 @@ func NewPostgresKeytore(o Opts) Keystore {
|
||||
|
||||
// WriteKeyPair inserts a keypair into the db and returns the linked id.
|
||||
func (ks *PostgresKeystore) WriteKeyPair(ctx context.Context, keypair keypair.Key) (uint, error) {
|
||||
var id uint
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := ks.db.QueryRow(ctx, ks.queries.WriteKeyPair, keypair.Public, keypair.Private).Scan(&id); err != nil {
|
||||
return 0, err
|
||||
return id, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
@@ -42,7 +44,9 @@ func (ks *PostgresKeystore) WriteKeyPair(ctx context.Context, keypair keypair.Ke
|
||||
|
||||
// LoadPrivateKey loads a private key as a crypto primitive for direct use. An id is used to search for the private key.
|
||||
func (ks *PostgresKeystore) LoadPrivateKey(ctx context.Context, publicKey string) (*ecdsa.PrivateKey, error) {
|
||||
var privateKeyString string
|
||||
var (
|
||||
privateKeyString string
|
||||
)
|
||||
|
||||
if err := ks.db.QueryRow(ctx, ks.queries.LoadKeyPair, publicKey).Scan(&privateKeyString); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -11,6 +11,9 @@ type Queries struct {
|
||||
WriteKeyPair string `query:"write-key-pair"`
|
||||
LoadKeyPair string `query:"load-key-pair"`
|
||||
// OTX
|
||||
CreateOTX string `query:"create-otx"`
|
||||
// Dispatch
|
||||
CreateDispatchStatus string `query:"create-dispatch-status"`
|
||||
}
|
||||
|
||||
func LoadQueries(q goyesql.Queries) (*Queries, error) {
|
||||
|
||||
25
internal/store/dispatch.go
Normal file
25
internal/store/dispatch.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
func (s *PostgresStore) CreateDispatchStatus(ctx context.Context, dispatch DispatchStatus) (uint, error) {
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := s.db.QueryRow(
|
||||
ctx,
|
||||
s.queries.CreateDispatchStatus,
|
||||
dispatch.OtxId,
|
||||
dispatch.Status,
|
||||
dispatch.TrackingId,
|
||||
).Scan(&id); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
25
internal/store/otx.go
Normal file
25
internal/store/otx.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
|
||||
func (s *PostgresStore) CreateOTX(ctx context.Context, otx OTX) (uint, error) {
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := s.db.QueryRow(
|
||||
ctx,
|
||||
s.queries.CreateOTX,
|
||||
otx.RawTx,
|
||||
otx.TxHash,
|
||||
otx.From,
|
||||
otx.Data,
|
||||
otx.GasPrice,
|
||||
otx.Nonce,
|
||||
otx.TrackingId,
|
||||
).Scan(&id); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
25
internal/store/postgres.go
Normal file
25
internal/store/postgres.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/queries"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type (
|
||||
Opts struct {
|
||||
PostgresPool *pgxpool.Pool
|
||||
Queries *queries.Queries
|
||||
}
|
||||
|
||||
PostgresStore struct {
|
||||
db *pgxpool.Pool
|
||||
queries *queries.Queries
|
||||
}
|
||||
)
|
||||
|
||||
func NewPostgresStore(o Opts) Store {
|
||||
return &PostgresStore{
|
||||
db: o.PostgresPool,
|
||||
queries: o.Queries,
|
||||
}
|
||||
}
|
||||
32
internal/store/store.go
Normal file
32
internal/store/store.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/status"
|
||||
)
|
||||
|
||||
type (
|
||||
OTX struct {
|
||||
RawTx string
|
||||
TxHash string
|
||||
From string
|
||||
Data string
|
||||
GasPrice uint64
|
||||
Nonce uint64
|
||||
TrackingId string
|
||||
}
|
||||
|
||||
DispatchStatus struct {
|
||||
OtxId uint
|
||||
Status status.Status
|
||||
TrackingId string
|
||||
}
|
||||
|
||||
Store interface {
|
||||
// OTX (Custodial originating transactions).
|
||||
CreateOTX(ctx context.Context, otx OTX) (id uint, err error)
|
||||
// Dispatch status.
|
||||
CreateDispatchStatus(ctx context.Context, dispatch DispatchStatus) (id uint, err error)
|
||||
}
|
||||
)
|
||||
@@ -2,6 +2,7 @@ package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -9,27 +10,40 @@ import (
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type SystemPayload struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
}
|
||||
type (
|
||||
AccountPayload struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
|
||||
accountEventPayload struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
)
|
||||
|
||||
func PrepareAccount(
|
||||
nonceProvider nonce.Noncestore,
|
||||
js nats.JetStreamContext,
|
||||
noncestore nonce.Noncestore,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := nonceProvider.SetNewAccountNonce(ctx, p.PublicKey); err != nil {
|
||||
if err := noncestore.SetNewAccountNonce(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -55,21 +69,40 @@ func PrepareAccount(
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.accountNewNonce", eventJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GiftGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
js nats.JetStreamContext,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
@@ -78,11 +111,12 @@ func GiftGasProcessor(
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params
|
||||
builtTx, err := celoProvider.SignGasTransferTx(
|
||||
system.PrivateKey,
|
||||
celo.GasTransferTxOpts{
|
||||
@@ -93,17 +127,49 @@ func GiftGasProcessor(
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
RawTx: hex.EncodeToString(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: system.PublicKey,
|
||||
Data: string(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
OtxId: id,
|
||||
TrackingId: p.TrackingId,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
@@ -114,6 +180,28 @@ func GiftGasProcessor(
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.giftNewAccountGas", eventJson)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -123,18 +211,21 @@ func GiftGasProcessor(
|
||||
|
||||
func GiftTokenProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
js nats.JetStreamContext,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
publicKey := w3.A(p.PublicKey)
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
@@ -142,38 +233,70 @@ func GiftTokenProcessor(
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["mint"].EncodeArgs(publicKey, system.GiftableTokenValue)
|
||||
input, err := system.Abis["mintTo"].EncodeArgs(w3.A(p.PublicKey), system.GiftableTokenValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ABI encode failed %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params.
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
system.PrivateKey,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: system.GiftableToken,
|
||||
InputData: input,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
GasPrice: big.NewInt(20000000000),
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
RawTx: hex.EncodeToString(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: system.PublicKey,
|
||||
Data: string(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
OtxId: id,
|
||||
TrackingId: p.TrackingId,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
@@ -184,6 +307,28 @@ func GiftTokenProcessor(
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.giftNewAccountVoucher", eventJson)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -192,6 +337,7 @@ func GiftTokenProcessor(
|
||||
}
|
||||
|
||||
// TODO: https://github.com/grassrootseconomics/cic-custodial/issues/43
|
||||
// TODO:
|
||||
func RefillGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
@@ -201,7 +347,7 @@ func RefillGasProcessor(
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p SystemPayload
|
||||
p AccountPayload
|
||||
balance big.Int
|
||||
)
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
@@ -3,21 +3,35 @@ package task
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/celo-org/celo-blockchain/core/types"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/status"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type TxPayload struct {
|
||||
Tx *types.Transaction `json:"tx"`
|
||||
}
|
||||
type (
|
||||
TxPayload struct {
|
||||
OtxId uint `json:"otxId"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
Tx *types.Transaction `json:"tx"`
|
||||
}
|
||||
|
||||
dispatchEventPayload struct {
|
||||
TrackingId string
|
||||
TxHash string
|
||||
}
|
||||
)
|
||||
|
||||
func TxDispatch(
|
||||
celoProvider *celo.Provider,
|
||||
js nats.JetStreamContext,
|
||||
pg store.Store,
|
||||
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
@@ -26,14 +40,53 @@ func TxDispatch(
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
dispatchStatus := store.DispatchStatus{
|
||||
OtxId: p.OtxId,
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventPayload := &dispatchEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
// TODO: Handle all fail cases
|
||||
if err := celoProvider.Client.CallCtx(
|
||||
ctx,
|
||||
eth.SendTx(p.Tx).Returns(&txHash),
|
||||
); err != nil {
|
||||
// TODO: Coreect error status
|
||||
dispatchStatus.Status = status.FailGasPrice
|
||||
|
||||
_, err := pg.CreateDispatchStatus(ctx, dispatchStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.dispatchFail", eventJson, nats.MsgId(txHash.Hex()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
dispatchStatus.TrackingId = status.Successful
|
||||
eventPayload.TxHash = txHash.Hex()
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.dispatchSuccessful", eventJson, nats.MsgId(txHash.Hex()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
183
internal/tasker/task/sign.go
Normal file
183
internal/tasker/task/sign.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
type (
|
||||
TransferPayload struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
From string `json:"from" `
|
||||
To string `json:"to"`
|
||||
VoucherAddress string `json:"voucherAddress"`
|
||||
Amount int64 `json:"amount"`
|
||||
}
|
||||
|
||||
transferEventPayload struct {
|
||||
DispatchTaskId string `json:"dispatchTaskId"`
|
||||
OTXId uint `json:"otxId"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
TxHash string `json:"txHash"`
|
||||
}
|
||||
)
|
||||
|
||||
func SignTransfer(
|
||||
celoProvider *celo.Provider,
|
||||
js nats.JetStreamContext,
|
||||
keystore keystore.Keystore,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
logger logf.Logger,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p TransferPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(
|
||||
ctx,
|
||||
system.LockPrefix+p.From,
|
||||
system.LockTimeout,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
key, err := keystore.LoadPrivateKey(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nonce, err := noncestore.Acquire(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["transfer"].EncodeArgs(w3.A(p.To), big.NewInt(p.Amount))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params.
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
key,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: w3.A(p.VoucherAddress),
|
||||
InputData: input,
|
||||
GasPrice: big.NewInt(20000000000),
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
RawTx: hex.EncodeToString(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: p.From,
|
||||
Data: string(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
OtxId: id,
|
||||
TrackingId: p.TrackingId,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
dispatchTask, err := taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &transferEventPayload{
|
||||
DispatchTaskId: dispatchTask.ID,
|
||||
OTXId: id,
|
||||
TrackingId: p.TrackingId,
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.transferSign", eventJson, nats.MsgId(builtTx.Hash().Hex()))
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user