mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-29 00:46:46 +01:00
Mohammed Sohail
4d13a14dc2
Squashed commit of the following: commit05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 10:03:44 2023 +0300 feat: add status types to dispatcher commit397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 09:39:31 2023 +0300 deps: bump -> cic-celo-sdk commitf2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 16:53:53 2023 +0300 snapshot: 12-ebening commit4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 12:50:43 2023 +0300 xnapshot: 12-02 commit773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 14:23:37 2023 +0300 update: deps initializers commit8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 10:42:15 2023 +0300 wip: refactor taskers commit8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Fri Feb 3 12:29:27 2023 +0300 refactor: decouple sql queries, remove transfer * add inline docs * removed transfer taks in prep for re-write commitb4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 2 12:29:43 2023 +0000 refactor: cmd/service/* and api
183 lines
4.0 KiB
Go
183 lines
4.0 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"math/big"
|
|
|
|
"github.com/bsm/redislock"
|
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
|
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"
|
|
)
|
|
|
|
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,
|
|
keystore keystore.Keystore,
|
|
lockProvider *redislock.Client,
|
|
noncestore nonce.Noncestore,
|
|
pg store.Store,
|
|
system *tasker.SystemContainer,
|
|
taskerClient *tasker.TaskerClient,
|
|
js nats.JetStreamContext,
|
|
) 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{
|
|
TrackingId: p.TrackingId,
|
|
Type: "TRANSFER",
|
|
RawTx: hexutil.Encode(rawTx),
|
|
TxHash: builtTx.Hash().Hex(),
|
|
From: p.From,
|
|
Data: hexutil.Encode(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,
|
|
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
|
|
}
|
|
}
|