2023-06-27 07:57:15 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/bsm/redislock"
|
|
|
|
"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"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/enum"
|
2023-07-05 06:16:38 +02:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
2023-06-27 07:57:15 +02:00
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TransferAuthPayload struct {
|
2023-07-05 09:14:45 +02:00
|
|
|
Amount uint64 `json:"amount"`
|
|
|
|
Authorizer string `json:"authorizer"`
|
2023-06-27 07:57:15 +02:00
|
|
|
AuthorizedAddress string `json:"authorizedAddress"`
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
VoucherAddress string `json:"voucherAddress"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
2023-07-05 06:16:38 +02:00
|
|
|
err error
|
|
|
|
networkBalance big.Int
|
|
|
|
payload TransferAuthPayload
|
2023-06-27 07:57:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lock, err := cu.LockProvider.Obtain(
|
|
|
|
ctx,
|
2023-07-05 09:14:45 +02:00
|
|
|
lockPrefix+payload.Authorizer,
|
2023-06-27 07:57:15 +02:00
|
|
|
lockTimeout,
|
|
|
|
&redislock.Options{
|
|
|
|
RetryStrategy: lockRetry(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lock.Release(ctx)
|
|
|
|
|
2023-07-05 09:14:45 +02:00
|
|
|
key, err := cu.Store.LoadPrivateKey(ctx, payload.Authorizer)
|
2023-07-05 06:16:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:14:45 +02:00
|
|
|
nonce, err := cu.Noncestore.Acquire(ctx, payload.Authorizer)
|
2023-06-27 07:57:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-07-05 09:14:45 +02:00
|
|
|
if nErr := cu.Noncestore.Return(ctx, payload.Authorizer); nErr != nil {
|
2023-06-27 07:57:15 +02:00
|
|
|
err = nErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
input, err := cu.Abis[custodial.Approve].EncodeArgs(
|
|
|
|
celoutils.HexToAddress(payload.AuthorizedAddress),
|
2023-07-05 09:14:45 +02:00
|
|
|
new(big.Int).SetUint64(payload.Amount),
|
2023-06-27 07:57:15 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
builtTx, err := cu.CeloProvider.SignContractExecutionTx(
|
2023-07-05 06:16:38 +02:00
|
|
|
key,
|
2023-06-27 07:57:15 +02:00
|
|
|
celoutils.ContractExecutionTxOpts{
|
|
|
|
ContractAddress: celoutils.HexToAddress(payload.VoucherAddress),
|
|
|
|
InputData: input,
|
|
|
|
GasFeeCap: celoutils.SafeGasFeeCap,
|
|
|
|
GasTipCap: celoutils.SafeGasTipCap,
|
|
|
|
GasLimit: uint64(celoutils.SafeGasLimit),
|
|
|
|
Nonce: nonce,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawTx, err := builtTx.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := cu.Store.CreateOtx(ctx, store.Otx{
|
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
Type: enum.TRANSFER_AUTH,
|
|
|
|
RawTx: hexutil.Encode(rawTx),
|
|
|
|
TxHash: builtTx.Hash().Hex(),
|
|
|
|
From: cu.SystemPublicKey,
|
|
|
|
Data: hexutil.Encode(builtTx.Data()),
|
|
|
|
GasPrice: builtTx.GasPrice(),
|
|
|
|
GasLimit: builtTx.Gas(),
|
|
|
|
TransferValue: 0,
|
|
|
|
Nonce: builtTx.Nonce(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 06:16:38 +02:00
|
|
|
if err := cu.CeloProvider.Client.CallCtx(
|
|
|
|
ctx,
|
2023-07-05 09:14:45 +02:00
|
|
|
eth.Balance(celoutils.HexToAddress(payload.Authorizer), nil).Returns(&networkBalance),
|
2023-07-05 06:16:38 +02:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:57:15 +02:00
|
|
|
disptachJobPayload, err := json.Marshal(TxPayload{
|
|
|
|
OtxId: id,
|
|
|
|
Tx: builtTx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
ctx,
|
|
|
|
tasker.DispatchTxTask,
|
|
|
|
tasker.HighPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: disptachJobPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:14:45 +02:00
|
|
|
// Auto-revoke every session (15 min)
|
|
|
|
// Check if already a revoke request
|
|
|
|
if payload.Amount > 0 {
|
|
|
|
taskPayload, err := json.Marshal(TransferAuthPayload{
|
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
Amount: 0,
|
|
|
|
Authorizer: payload.Authorizer,
|
|
|
|
AuthorizedAddress: payload.AuthorizedAddress,
|
|
|
|
VoucherAddress: payload.VoucherAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
ctx,
|
|
|
|
tasker.SignTransferTaskAuth,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: taskPayload,
|
|
|
|
},
|
2023-07-10 08:30:44 +02:00
|
|
|
asynq.ProcessIn(cu.ApprovalTimeout),
|
2023-07-05 09:14:45 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 06:16:38 +02:00
|
|
|
gasRefillPayload, err := json.Marshal(AccountPayload{
|
2023-07-05 09:14:45 +02:00
|
|
|
PublicKey: payload.Authorizer,
|
2023-07-05 06:16:38 +02:00
|
|
|
TrackingId: payload.TrackingId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !balanceCheck(networkBalance) {
|
2023-07-05 09:14:45 +02:00
|
|
|
if err := cu.Store.GasLock(ctx, payload.Authorizer); err != nil {
|
2023-07-05 06:16:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = cu.TaskerClient.CreateTask(
|
|
|
|
ctx,
|
|
|
|
tasker.AccountRefillGasTask,
|
|
|
|
tasker.DefaultPriority,
|
|
|
|
&tasker.Task{
|
|
|
|
Payload: gasRefillPayload,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:57:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|