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 {
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId uint `json:"otxId"`
|
|
|
|
Tx *types.Transaction `json:"tx"`
|
2023-02-09 08:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatchEventPayload struct {
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId uint
|
|
|
|
TxHash string
|
2023-02-09 08:42:15 +01:00
|
|
|
}
|
|
|
|
)
|
2022-11-30 10:51:24 +01:00
|
|
|
|
|
|
|
func TxDispatch(
|
|
|
|
celoProvider *celo.Provider,
|
2023-02-09 08:42:15 +01:00
|
|
|
pg store.Store,
|
2023-02-09 12:23:37 +01:00
|
|
|
js nats.JetStreamContext,
|
2023-02-09 08:42:15 +01:00
|
|
|
|
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{
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId: p.OtxId,
|
2023-02-09 08:42:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
eventPayload := &dispatchEventPayload{
|
2023-02-12 10:50:43 +01:00
|
|
|
OtxId: p.OtxId,
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:50:43 +01:00
|
|
|
dispatchStatus.Status = status.Successful
|
|
|
|
_, err := pg.CreateDispatchStatus(ctx, dispatchStatus)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:42:15 +01:00
|
|
|
eventPayload.TxHash = txHash.Hex()
|
|
|
|
|
|
|
|
eventJson, err := json.Marshal(eventPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:50:43 +01:00
|
|
|
_, err = js.Publish("CUSTODIAL.dispatchSuccess", eventJson, nats.MsgId(txHash.Hex()))
|
2023-02-09 08:42:15 +01:00
|
|
|
if err != nil {
|
2022-11-30 10:51:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|