2023-02-20 10:56:30 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/celo-org/celo-blockchain/common"
|
|
|
|
"github.com/celo-org/celo-blockchain/core/types"
|
|
|
|
"github.com/grassrootseconomics/celoutils"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/custodial"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
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/module/eth"
|
|
|
|
"github.com/hibiken/asynq"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TxPayload struct {
|
|
|
|
OtxId uint `json:"otxId"`
|
|
|
|
Tx *types.Transaction `json:"tx"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func DispatchTx(cu *custodial.Custodial) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
|
|
|
payload TxPayload
|
|
|
|
dispatchStatus store.DispatchStatus
|
|
|
|
dispathchTx common.Hash
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
dispatchStatus.OtxId = payload.OtxId
|
2023-02-20 10:56:30 +01:00
|
|
|
|
|
|
|
if err := cu.CeloProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.SendTx(payload.Tx).Returns(&dispathchTx),
|
|
|
|
); err != nil {
|
|
|
|
switch err.Error() {
|
|
|
|
case celoutils.ErrGasPriceLow:
|
2023-02-21 18:34:22 +01:00
|
|
|
dispatchStatus.Status = enum.FAIL_LOW_GAS_PRICE
|
2023-02-20 10:56:30 +01:00
|
|
|
case celoutils.ErrInsufficientGas:
|
2023-02-21 18:34:22 +01:00
|
|
|
dispatchStatus.Status = enum.FAIL_NO_GAS
|
2023-02-20 10:56:30 +01:00
|
|
|
case celoutils.ErrNonceLow:
|
2023-02-21 18:34:22 +01:00
|
|
|
dispatchStatus.Status = enum.FAIL_LOW_NONCE
|
|
|
|
default:
|
|
|
|
dispatchStatus.Status = enum.FAIL_UNKNOWN_RPC_ERROR
|
2023-02-20 10:56:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cu.PgStore.CreateDispatchStatus(ctx, dispatchStatus); err != nil {
|
|
|
|
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
dispatchStatus.Status = enum.IN_NETWORK
|
2023-02-20 10:56:30 +01:00
|
|
|
|
|
|
|
if err := cu.PgStore.CreateDispatchStatus(ctx, dispatchStatus); err != nil {
|
|
|
|
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|