feat: update transferAuthPayload

* switch to MAX_INT for value and use revoke flag
* tranfer handler: fix nonce rollback on EOA account
This commit is contained in:
Mohamed Sohail 2023-07-05 12:16:38 +08:00
parent d848ab4690
commit 5d177920b9
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
2 changed files with 56 additions and 9 deletions

View File

@ -60,7 +60,7 @@ func SignTransfer(cu *custodial.Custodial) func(context.Context, *asynq.Task) er
} }
defer func() { defer func() {
if err != nil { if err != nil {
if nErr := cu.Noncestore.Return(ctx, cu.SystemPublicKey); nErr != nil { if nErr := cu.Noncestore.Return(ctx, payload.From); nErr != nil {
err = nErr err = nErr
} }
} }

View File

@ -6,18 +6,21 @@ import (
"math/big" "math/big"
"github.com/bsm/redislock" "github.com/bsm/redislock"
"github.com/celo-org/celo-blockchain/accounts/abi"
"github.com/celo-org/celo-blockchain/common/hexutil" "github.com/celo-org/celo-blockchain/common/hexutil"
"github.com/grassrootseconomics/celoutils" "github.com/grassrootseconomics/celoutils"
"github.com/grassrootseconomics/cic-custodial/internal/custodial" "github.com/grassrootseconomics/cic-custodial/internal/custodial"
"github.com/grassrootseconomics/cic-custodial/internal/store" "github.com/grassrootseconomics/cic-custodial/internal/store"
"github.com/grassrootseconomics/cic-custodial/internal/tasker" "github.com/grassrootseconomics/cic-custodial/internal/tasker"
"github.com/grassrootseconomics/cic-custodial/pkg/enum" "github.com/grassrootseconomics/cic-custodial/pkg/enum"
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
) )
type TransferAuthPayload struct { type TransferAuthPayload struct {
AuthorizeFor string `json:"authorizeFor"`
AuthorizedAddress string `json:"authorizedAddress"` AuthorizedAddress string `json:"authorizedAddress"`
AuthorizedAmount uint64 `json:"authorizedAmount"` Revoke bool `json:"revoke"`
TrackingId string `json:"trackingId"` TrackingId string `json:"trackingId"`
VoucherAddress string `json:"voucherAddress"` VoucherAddress string `json:"voucherAddress"`
} }
@ -25,8 +28,9 @@ type TransferAuthPayload struct {
func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Context, *asynq.Task) error { func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Context, *asynq.Task) error {
return func(ctx context.Context, t *asynq.Task) error { return func(ctx context.Context, t *asynq.Task) error {
var ( var (
err error err error
payload TransferAuthPayload networkBalance big.Int
payload TransferAuthPayload
) )
if err := json.Unmarshal(t.Payload(), &payload); err != nil { if err := json.Unmarshal(t.Payload(), &payload); err != nil {
@ -35,7 +39,7 @@ func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Co
lock, err := cu.LockProvider.Obtain( lock, err := cu.LockProvider.Obtain(
ctx, ctx,
lockPrefix+cu.SystemPublicKey, lockPrefix+payload.AuthorizeFor,
lockTimeout, lockTimeout,
&redislock.Options{ &redislock.Options{
RetryStrategy: lockRetry(), RetryStrategy: lockRetry(),
@ -46,28 +50,38 @@ func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Co
} }
defer lock.Release(ctx) defer lock.Release(ctx)
nonce, err := cu.Noncestore.Acquire(ctx, cu.SystemPublicKey) key, err := cu.Store.LoadPrivateKey(ctx, payload.AuthorizeFor)
if err != nil {
return err
}
nonce, err := cu.Noncestore.Acquire(ctx, payload.AuthorizeFor)
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() {
if err != nil { if err != nil {
if nErr := cu.Noncestore.Return(ctx, cu.SystemPublicKey); nErr != nil { if nErr := cu.Noncestore.Return(ctx, payload.AuthorizeFor); nErr != nil {
err = nErr err = nErr
} }
} }
}() }()
authorizeAmount := big.NewInt(0).Sub(abi.MaxUint256, big.NewInt(1))
if payload.Revoke {
authorizeAmount = big.NewInt(0)
}
input, err := cu.Abis[custodial.Approve].EncodeArgs( input, err := cu.Abis[custodial.Approve].EncodeArgs(
celoutils.HexToAddress(payload.AuthorizedAddress), celoutils.HexToAddress(payload.AuthorizedAddress),
new(big.Int).SetUint64(payload.AuthorizedAmount), authorizeAmount,
) )
if err != nil { if err != nil {
return err return err
} }
builtTx, err := cu.CeloProvider.SignContractExecutionTx( builtTx, err := cu.CeloProvider.SignContractExecutionTx(
cu.SystemPrivateKey, key,
celoutils.ContractExecutionTxOpts{ celoutils.ContractExecutionTxOpts{
ContractAddress: celoutils.HexToAddress(payload.VoucherAddress), ContractAddress: celoutils.HexToAddress(payload.VoucherAddress),
InputData: input, InputData: input,
@ -102,6 +116,13 @@ func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Co
return err return err
} }
if err := cu.CeloProvider.Client.CallCtx(
ctx,
eth.Balance(celoutils.HexToAddress(payload.AuthorizeFor), nil).Returns(&networkBalance),
); err != nil {
return err
}
disptachJobPayload, err := json.Marshal(TxPayload{ disptachJobPayload, err := json.Marshal(TxPayload{
OtxId: id, OtxId: id,
Tx: builtTx, Tx: builtTx,
@ -122,6 +143,32 @@ func SignTransferAuthorizationProcessor(cu *custodial.Custodial) func(context.Co
return err return err
} }
gasRefillPayload, err := json.Marshal(AccountPayload{
PublicKey: payload.AuthorizeFor,
TrackingId: payload.TrackingId,
})
if err != nil {
return err
}
if !balanceCheck(networkBalance) {
if err := cu.Store.GasLock(ctx, payload.AuthorizeFor); err != nil {
return err
}
_, err = cu.TaskerClient.CreateTask(
ctx,
tasker.AccountRefillGasTask,
tasker.DefaultPriority,
&tasker.Task{
Payload: gasRefillPayload,
},
)
if err != nil {
return err
}
}
return nil return nil
} }
} }