2023-02-15 08:05:43 +01:00
|
|
|
package store
|
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2023-02-15 08:05:43 +01:00
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
2023-03-02 10:12:39 +01:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/enum"
|
2023-02-21 18:34:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type TxStatus struct {
|
|
|
|
Type string `db:"type" json:"txType"`
|
|
|
|
TxHash string `db:"tx_hash" json:"txHash"`
|
|
|
|
TransferValue uint64 `db:"transfer_value" json:"transferValue"`
|
|
|
|
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
|
|
|
Status string `db:"status" json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PostgresStore) CreateOtx(ctx context.Context, otx OTX) (uint, error) {
|
2023-02-15 08:05:43 +01:00
|
|
|
var (
|
|
|
|
id uint
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.db.QueryRow(
|
|
|
|
ctx,
|
|
|
|
s.queries.CreateOTX,
|
|
|
|
otx.TrackingId,
|
|
|
|
otx.Type,
|
|
|
|
otx.RawTx,
|
|
|
|
otx.TxHash,
|
|
|
|
otx.From,
|
|
|
|
otx.Data,
|
|
|
|
otx.GasPrice,
|
2023-02-21 18:34:22 +01:00
|
|
|
otx.GasLimit,
|
|
|
|
otx.TransferValue,
|
2023-02-15 08:05:43 +01:00
|
|
|
otx.Nonce,
|
|
|
|
).Scan(&id); err != nil {
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
2023-02-21 18:34:22 +01:00
|
|
|
|
|
|
|
func (s *PostgresStore) GetTxStatusByTrackingId(ctx context.Context, trackingId string) ([]*TxStatus, error) {
|
|
|
|
var (
|
|
|
|
txs []*TxStatus
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := pgxscan.Select(
|
|
|
|
ctx,
|
|
|
|
s.db,
|
|
|
|
&txs,
|
|
|
|
s.queries.GetTxStatusByTrackingId,
|
|
|
|
trackingId,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return txs, nil
|
|
|
|
}
|
2023-03-01 18:13:23 +01:00
|
|
|
|
2023-03-08 07:49:09 +01:00
|
|
|
func (s *PostgresStore) CreateDispatchStatus(ctx context.Context, dispatch DispatchStatus) error {
|
|
|
|
if _, err := s.db.Exec(
|
|
|
|
ctx,
|
|
|
|
s.queries.CreateDispatchStatus,
|
|
|
|
dispatch.OtxId,
|
|
|
|
dispatch.Status,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 10:12:39 +01:00
|
|
|
func (s *PostgresStore) UpdateOtxStatusFromChainEvent(ctx context.Context, chainEvent MinimalTxInfo) error {
|
|
|
|
var (
|
|
|
|
status = enum.SUCCESS
|
|
|
|
)
|
|
|
|
|
|
|
|
if !chainEvent.Success {
|
|
|
|
status = enum.REVERTED
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.db.Exec(
|
|
|
|
ctx,
|
|
|
|
s.queries.UpdateChainStatus,
|
|
|
|
chainEvent.TxHash,
|
|
|
|
status,
|
|
|
|
chainEvent.Block,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:13:23 +01:00
|
|
|
return nil
|
|
|
|
}
|