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"
|
2023-03-06 09:18:41 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/pub"
|
2023-02-20 10:56:30 +01:00
|
|
|
"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
|
2023-03-06 09:18:41 +01:00
|
|
|
eventPayload pub.EventPayload
|
2023-02-20 10:56:30 +01:00
|
|
|
dispathchTx common.Hash
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
|
|
|
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash := payload.Tx.Hash().Hex()
|
|
|
|
|
|
|
|
dispatchStatus.OtxId, eventPayload.OtxId = payload.OtxId, payload.OtxId
|
|
|
|
eventPayload.TxHash = txHash
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
if err := cu.Pub.Publish(pub.DispatchFail, txHash, eventPayload); err != nil {
|
2023-02-20 10:56:30 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
if err := cu.Pub.Publish(pub.DispatchSuccess, txHash, eventPayload); err != nil {
|
2023-02-20 10:56:30 +01:00
|
|
|
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|