2022-11-30 10:51:24 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/bsm/redislock"
|
2023-02-12 10:50:43 +01:00
|
|
|
"github.com/celo-org/celo-blockchain/common/hexutil"
|
2022-11-30 10:51:24 +01:00
|
|
|
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
2023-02-09 08:42:15 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
2022-11-30 10:51:24 +01:00
|
|
|
"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"
|
2023-02-09 08:42:15 +01:00
|
|
|
"github.com/nats-io/nats.go"
|
2022-11-30 10:51:24 +01:00
|
|
|
)
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
type (
|
|
|
|
AccountPayload struct {
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
accountEventPayload struct {
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
}
|
|
|
|
)
|
2022-11-30 10:51:24 +01:00
|
|
|
|
|
|
|
func PrepareAccount(
|
2023-02-09 08:42:15 +01:00
|
|
|
noncestore nonce.Noncestore,
|
2022-11-30 10:51:24 +01:00
|
|
|
taskerClient *tasker.TaskerClient,
|
2023-02-12 10:50:43 +01:00
|
|
|
js nats.JetStreamContext,
|
2022-11-30 10:51:24 +01:00
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
2023-02-09 08:42:15 +01:00
|
|
|
var (
|
|
|
|
p AccountPayload
|
|
|
|
)
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
if err := noncestore.SetNewAccountNonce(ctx, p.PublicKey); err != nil {
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := taskerClient.CreateTask(
|
2023-02-12 10:50:43 +01:00
|
|
|
tasker.RegisterAccountOnChain,
|
2022-11-30 10:51:24 +01:00
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: t.Payload(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.GiftTokenTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: t.Payload(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:50:43 +01:00
|
|
|
func RegisterAccountOnChainProcessor(
|
2022-11-30 10:51:24 +01:00
|
|
|
celoProvider *celo.Provider,
|
2023-02-12 10:50:43 +01:00
|
|
|
lockProvider *redislock.Client,
|
|
|
|
noncestore nonce.Noncestore,
|
|
|
|
pg store.Store,
|
|
|
|
system *tasker.SystemContainer,
|
|
|
|
taskerClient *tasker.TaskerClient,
|
2023-02-09 08:42:15 +01:00
|
|
|
js nats.JetStreamContext,
|
2023-02-12 10:50:43 +01:00
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
|
|
|
p AccountPayload
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
|
|
|
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input, err := system.Abis["add"].EncodeArgs(w3.A(p.PublicKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Review gas params.
|
|
|
|
builtTx, err := celoProvider.SignContractExecutionTx(
|
|
|
|
system.PrivateKey,
|
|
|
|
celo.ContractExecutionTxOpts{
|
|
|
|
ContractAddress: system.AccountIndexContract,
|
|
|
|
InputData: input,
|
|
|
|
GasPrice: big.NewInt(20000000000),
|
|
|
|
GasLimit: system.TokenTransferGasLimit,
|
|
|
|
Nonce: nonce,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
TrackingId: p.TrackingId,
|
|
|
|
Type: "ACCOUNT_REGISTER",
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: system.PublicKey,
|
|
|
|
Data: hexutil.Encode(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{
|
|
|
|
OtxId: id,
|
|
|
|
Tx: builtTx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.TxDispatchTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.GiftGasTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: t.Payload(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
eventPayload := &accountEventPayload{
|
|
|
|
TrackingId: p.TrackingId,
|
|
|
|
}
|
|
|
|
|
|
|
|
eventJson, err := json.Marshal(eventPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = js.Publish("CUSTODIAL.accountRegister", eventJson)
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GiftGasProcessor(
|
|
|
|
celoProvider *celo.Provider,
|
2022-11-30 10:51:24 +01:00
|
|
|
lockProvider *redislock.Client,
|
2023-02-09 08:42:15 +01:00
|
|
|
noncestore nonce.Noncestore,
|
|
|
|
pg store.Store,
|
2022-11-30 10:51:24 +01:00
|
|
|
system *tasker.SystemContainer,
|
|
|
|
taskerClient *tasker.TaskerClient,
|
2023-02-12 10:50:43 +01:00
|
|
|
js nats.JetStreamContext,
|
2022-11-30 10:51:24 +01:00
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
2023-02-09 08:42:15 +01:00
|
|
|
var (
|
|
|
|
p AccountPayload
|
|
|
|
)
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
2022-11-30 10:51:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
// TODO: Review gas params
|
2022-11-30 10:51:24 +01:00
|
|
|
builtTx, err := celoProvider.SignGasTransferTx(
|
|
|
|
system.PrivateKey,
|
|
|
|
celo.GasTransferTxOpts{
|
|
|
|
To: w3.A(p.PublicKey),
|
|
|
|
Nonce: nonce,
|
|
|
|
Value: system.GiftableGasValue,
|
|
|
|
GasPrice: celo.FixedMinGas,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-02-09 08:42:15 +01:00
|
|
|
|
|
|
|
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{
|
2023-02-12 10:50:43 +01:00
|
|
|
TrackingId: p.TrackingId,
|
|
|
|
Type: "GIFT_GAS",
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: system.PublicKey,
|
|
|
|
Data: hexutil.Encode(builtTx.Data()),
|
|
|
|
GasPrice: builtTx.GasPrice().Uint64(),
|
|
|
|
Nonce: builtTx.Nonce(),
|
2023-02-09 08:42:15 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disptachJobPayload, err := json.Marshal(TxPayload{
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId: id,
|
|
|
|
Tx: builtTx,
|
2022-11-30 10:51:24 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.TxDispatchTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GiftTokenProcessor(
|
|
|
|
celoProvider *celo.Provider,
|
|
|
|
lockProvider *redislock.Client,
|
2023-02-09 08:42:15 +01:00
|
|
|
noncestore nonce.Noncestore,
|
|
|
|
pg store.Store,
|
2022-11-30 10:51:24 +01:00
|
|
|
system *tasker.SystemContainer,
|
|
|
|
taskerClient *tasker.TaskerClient,
|
2023-02-12 10:50:43 +01:00
|
|
|
js nats.JetStreamContext,
|
2022-11-30 10:51:24 +01:00
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
2023-02-09 08:42:15 +01:00
|
|
|
var (
|
|
|
|
p AccountPayload
|
|
|
|
)
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
2022-11-30 10:51:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
input, err := system.Abis["mintTo"].EncodeArgs(w3.A(p.PublicKey), system.GiftableTokenValue)
|
2022-11-30 10:51:24 +01:00
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
// TODO: Review gas params.
|
2022-11-30 10:51:24 +01:00
|
|
|
builtTx, err := celoProvider.SignContractExecutionTx(
|
|
|
|
system.PrivateKey,
|
|
|
|
celo.ContractExecutionTxOpts{
|
|
|
|
ContractAddress: system.GiftableToken,
|
|
|
|
InputData: input,
|
2023-02-09 08:42:15 +01:00
|
|
|
GasPrice: big.NewInt(20000000000),
|
2022-11-30 10:51:24 +01:00
|
|
|
GasLimit: system.TokenTransferGasLimit,
|
|
|
|
Nonce: nonce,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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{
|
2023-02-12 10:50:43 +01:00
|
|
|
TrackingId: p.TrackingId,
|
|
|
|
Type: "GIFT_VOUCHER",
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: system.PublicKey,
|
|
|
|
Data: hexutil.Encode(builtTx.Data()),
|
|
|
|
GasPrice: builtTx.GasPrice().Uint64(),
|
|
|
|
Nonce: builtTx.Nonce(),
|
2023-02-09 08:42:15 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-02-09 08:42:15 +01:00
|
|
|
|
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
disptachJobPayload, err := json.Marshal(TxPayload{
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId: id,
|
|
|
|
Tx: builtTx,
|
2022-11-30 10:51:24 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.TxDispatchTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:29:27 +01:00
|
|
|
// TODO: https://github.com/grassrootseconomics/cic-custodial/issues/43
|
2023-02-09 08:42:15 +01:00
|
|
|
// TODO:
|
2022-11-30 10:51:24 +01:00
|
|
|
func RefillGasProcessor(
|
|
|
|
celoProvider *celo.Provider,
|
|
|
|
nonceProvider nonce.Noncestore,
|
|
|
|
lockProvider *redislock.Client,
|
|
|
|
system *tasker.SystemContainer,
|
|
|
|
taskerClient *tasker.TaskerClient,
|
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
2023-02-09 08:42:15 +01:00
|
|
|
p AccountPayload
|
2022-11-30 10:51:24 +01:00
|
|
|
balance big.Int
|
|
|
|
)
|
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
|
|
|
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := celoProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.Balance(w3.A(p.PublicKey), nil).Returns(&balance),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if belowThreshold := balance.Cmp(system.GasRefillThreshold); belowThreshold > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
|
|
|
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
builtTx, err := celoProvider.SignGasTransferTx(
|
|
|
|
system.PrivateKey,
|
|
|
|
celo.GasTransferTxOpts{
|
|
|
|
To: w3.A(p.PublicKey),
|
|
|
|
Nonce: nonce,
|
|
|
|
Value: system.GasRefillValue,
|
|
|
|
GasPrice: celo.FixedMinGas,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
disptachJobPayload, err := json.Marshal(TxPayload{
|
|
|
|
Tx: builtTx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = taskerClient.CreateTask(
|
|
|
|
tasker.TxDispatchTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|