2023-02-20 10:56:30 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
2023-03-16 13:03:51 +01:00
|
|
|
"github.com/bsm/redislock"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
|
|
|
"github.com/grassrootseconomics/celoutils"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
2023-03-06 09:18:41 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/pub"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
2023-02-21 18:34:22 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/enum"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch"
|
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
TransferPayload struct {
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
From string `json:"from" `
|
|
|
|
To string `json:"to"`
|
|
|
|
VoucherAddress string `json:"voucherAddress"`
|
2023-02-21 18:34:22 +01:00
|
|
|
Amount uint64 `json:"amount"`
|
2023-02-20 10:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
transferEventPayload struct {
|
|
|
|
DispatchTaskId string `json:"dispatchTaskId"`
|
|
|
|
OTXId uint `json:"otxId"`
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
TxHash string `json:"txHash"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func SignTransfer(cu *custodial.Custodial) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
2023-03-01 18:13:23 +01:00
|
|
|
err error
|
2023-02-20 10:56:30 +01:00
|
|
|
payload TransferPayload
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
|
|
|
return fmt.Errorf("account: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := cu.LockProvider.Obtain(
|
|
|
|
ctx,
|
2023-03-08 07:49:09 +01:00
|
|
|
lockPrefix+payload.From,
|
2023-02-20 10:56:30 +01:00
|
|
|
cu.SystemContainer.LockTimeout,
|
2023-03-16 13:03:51 +01:00
|
|
|
&redislock.Options{
|
|
|
|
RetryStrategy: lockRetry(),
|
|
|
|
},
|
2023-02-20 10:56:30 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
|
|
|
key, err := cu.Keystore.LoadPrivateKey(ctx, payload.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, err := cu.Noncestore.Acquire(ctx, payload.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if nErr := cu.Noncestore.Return(ctx, cu.SystemContainer.PublicKey); nErr != nil {
|
|
|
|
err = nErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
input, err := cu.SystemContainer.Abis["transfer"].EncodeArgs(w3.A(payload.To), new(big.Int).SetUint64(payload.Amount))
|
2023-02-20 10:56:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Review gas params.
|
|
|
|
builtTx, err := cu.CeloProvider.SignContractExecutionTx(
|
|
|
|
key,
|
|
|
|
celoutils.ContractExecutionTxOpts{
|
|
|
|
ContractAddress: w3.A(payload.VoucherAddress),
|
|
|
|
InputData: input,
|
|
|
|
GasFeeCap: celoutils.SafeGasFeeCap,
|
|
|
|
GasTipCap: celoutils.SafeGasTipCap,
|
|
|
|
GasLimit: cu.SystemContainer.TokenTransferGasLimit,
|
|
|
|
Nonce: nonce,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawTx, err := builtTx.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
id, err := cu.PgStore.CreateOtx(ctx, store.OTX{
|
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
Type: enum.TRANSFER_VOUCHER,
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: payload.From,
|
|
|
|
Data: hexutil.Encode(builtTx.Data()),
|
|
|
|
GasPrice: builtTx.GasPrice().Uint64(),
|
|
|
|
GasLimit: builtTx.Gas(),
|
|
|
|
TransferValue: payload.Amount,
|
|
|
|
Nonce: builtTx.Nonce(),
|
2023-02-20 10:56:30 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
disptachJobPayload, err := json.Marshal(TxPayload{
|
|
|
|
OtxId: id,
|
|
|
|
Tx: builtTx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
2023-02-24 17:46:46 +01:00
|
|
|
ctx,
|
2023-02-20 10:56:30 +01:00
|
|
|
tasker.DispatchTxTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gasRefillPayload, err := json.Marshal(AccountPayload{
|
2023-03-08 07:49:09 +01:00
|
|
|
PublicKey: payload.From,
|
|
|
|
TrackingId: payload.TrackingId,
|
2023-02-20 10:56:30 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
2023-02-24 17:46:46 +01:00
|
|
|
ctx,
|
2023-02-20 10:56:30 +01:00
|
|
|
tasker.AccountRefillGasTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: gasRefillPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
eventPayload := &transferEventPayload{
|
|
|
|
OTXId: id,
|
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
if err := cu.Pub.Publish(
|
|
|
|
pub.SignTransfer,
|
2023-02-20 10:56:30 +01:00
|
|
|
builtTx.Hash().Hex(),
|
|
|
|
eventPayload,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|