2023-02-20 10:56:30 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-03-29 18:10:58 +02:00
|
|
|
"math/big"
|
2023-03-08 07:49:09 +01:00
|
|
|
"time"
|
2023-02-20 10:56:30 +01:00
|
|
|
|
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"
|
|
|
|
"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-03-29 18:10:58 +02:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
)
|
|
|
|
|
2023-03-08 07:49:09 +01:00
|
|
|
const (
|
2023-03-29 18:10:58 +02:00
|
|
|
gasGiveToLimit = 250000
|
2023-03-08 07:49:09 +01:00
|
|
|
)
|
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
func AccountRefillGasProcessor(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
|
|
|
|
payload AccountPayload
|
2023-03-29 18:10:58 +02:00
|
|
|
|
|
|
|
nextTime big.Int
|
|
|
|
checkStatus bool
|
2023-02-20 10:56:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
2023-03-29 18:10:58 +02:00
|
|
|
return err
|
2023-02-20 10:56:30 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
if err := cu.CeloProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.CallFunc(
|
|
|
|
cu.Abis[custodial.NextTime],
|
|
|
|
cu.RegistryMap[celoutils.GasFaucet],
|
|
|
|
celoutils.HexToAddress(payload.PublicKey),
|
|
|
|
).Returns(&nextTime),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
// The user recently requested funds, there is a cooldown applied.
|
|
|
|
// We can schedule an attempt after the cooldown period has passed + 10 seconds.
|
2023-03-29 18:10:58 +02:00
|
|
|
if nextTime.Int64() > time.Now().Unix() {
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
ctx,
|
|
|
|
tasker.AccountRefillGasTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: t.Payload(),
|
|
|
|
},
|
2023-05-16 14:20:01 +02:00
|
|
|
asynq.ProcessAt(time.Unix(nextTime.Int64()+10, 0)),
|
2023-03-29 18:10:58 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cu.CeloProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.CallFunc(
|
|
|
|
cu.Abis[custodial.Check],
|
|
|
|
cu.RegistryMap[celoutils.GasFaucet],
|
|
|
|
celoutils.HexToAddress(payload.PublicKey),
|
|
|
|
).Returns(&checkStatus),
|
|
|
|
); err != nil {
|
2023-03-08 07:49:09 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
// The gas faucet backend returns a false status, a poke will fail.
|
|
|
|
if !checkStatus {
|
2023-02-20 10:56:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := cu.LockProvider.Obtain(
|
|
|
|
ctx,
|
2023-03-29 18:10:58 +02:00
|
|
|
lockPrefix+cu.SystemPublicKey,
|
|
|
|
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)
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
nonce, err := cu.Noncestore.Acquire(ctx, cu.SystemPublicKey)
|
2023-02-20 10:56:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-03-29 18:10:58 +02:00
|
|
|
if nErr := cu.Noncestore.Return(ctx, cu.SystemPublicKey); nErr != nil {
|
2023-02-20 10:56:30 +01:00
|
|
|
err = nErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
input, err := cu.Abis[custodial.GiveTo].EncodeArgs(
|
|
|
|
celoutils.HexToAddress(payload.PublicKey),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
builtTx, err := cu.CeloProvider.SignContractExecutionTx(
|
|
|
|
cu.SystemPrivateKey,
|
|
|
|
celoutils.ContractExecutionTxOpts{
|
|
|
|
ContractAddress: cu.RegistryMap[celoutils.GasFaucet],
|
|
|
|
InputData: input,
|
|
|
|
GasFeeCap: celoutils.SafeGasFeeCap,
|
|
|
|
GasTipCap: celoutils.SafeGasTipCap,
|
2023-05-28 14:43:43 +02:00
|
|
|
GasLimit: uint64(celoutils.SafeGasLimit),
|
2023-03-29 18:10:58 +02:00
|
|
|
Nonce: nonce,
|
2023-02-20 10:56:30 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawTx, err := builtTx.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
id, err := cu.Store.CreateOtx(ctx, store.Otx{
|
2023-03-29 18:10:58 +02:00
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
Type: enum.REFILL_GAS,
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: cu.SystemPublicKey,
|
|
|
|
Data: hexutil.Encode(builtTx.Data()),
|
2023-04-06 08:31:07 +02:00
|
|
|
GasPrice: builtTx.GasPrice(),
|
2023-03-29 18:10:58 +02:00
|
|
|
GasLimit: builtTx.Gas(),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|