2022-11-30 10:51:24 +01:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/celo-org/celo-blockchain/common"
|
|
|
|
"github.com/celo-org/celo-blockchain/core/types"
|
|
|
|
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
2023-02-09 08:42:15 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/status"
|
2022-11-30 10:51:24 +01:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
|
|
|
"github.com/hibiken/asynq"
|
2023-02-09 08:42:15 +01:00
|
|
|
"github.com/nats-io/nats.go"
|
2022-11-30 10:51:24 +01:00
|
|
|
)
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
type (
|
|
|
|
TxPayload struct {
|
|
|
|
OtxId uint `json:"otxId"`
|
|
|
|
TrackingId string `json:"trackingId"`
|
|
|
|
Tx *types.Transaction `json:"tx"`
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchEventPayload struct {
|
|
|
|
TrackingId string
|
|
|
|
TxHash string
|
|
|
|
}
|
|
|
|
)
|
2022-11-30 10:51:24 +01:00
|
|
|
|
|
|
|
func TxDispatch(
|
|
|
|
celoProvider *celo.Provider,
|
2023-02-09 08:42:15 +01:00
|
|
|
js nats.JetStreamContext,
|
|
|
|
pg store.Store,
|
|
|
|
|
2022-11-30 10:51:24 +01:00
|
|
|
) func(context.Context, *asynq.Task) error {
|
|
|
|
return func(ctx context.Context, t *asynq.Task) error {
|
|
|
|
var (
|
|
|
|
p TxPayload
|
|
|
|
txHash common.Hash
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchStatus := store.DispatchStatus{
|
|
|
|
OtxId: p.OtxId,
|
|
|
|
TrackingId: p.TrackingId,
|
|
|
|
}
|
|
|
|
|
|
|
|
eventPayload := &dispatchEventPayload{
|
|
|
|
TrackingId: p.TrackingId,
|
2022-11-30 10:51:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := celoProvider.Client.CallCtx(
|
|
|
|
ctx,
|
|
|
|
eth.SendTx(p.Tx).Returns(&txHash),
|
|
|
|
); err != nil {
|
2023-02-09 08:42:15 +01:00
|
|
|
// TODO: Coreect error status
|
|
|
|
dispatchStatus.Status = status.FailGasPrice
|
|
|
|
|
|
|
|
_, err := pg.CreateDispatchStatus(ctx, dispatchStatus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
eventJson, err := json.Marshal(eventPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = js.Publish("CUSTODIAL.dispatchFail", eventJson, nats.MsgId(txHash.Hex()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchStatus.TrackingId = status.Successful
|
|
|
|
eventPayload.TxHash = txHash.Hex()
|
|
|
|
|
|
|
|
eventJson, err := json.Marshal(eventPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = js.Publish("CUSTODIAL.dispatchSuccessful", eventJson, nats.MsgId(txHash.Hex()))
|
|
|
|
if err != nil {
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|