mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-29 00:46:46 +01:00
Mohammed Sohail
a47e44f262
* refactor main entry point for starting services * minor fixes around ctx propagation * improve otx marker js subscriber
84 lines
1.5 KiB
Go
84 lines
1.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/enum"
|
|
)
|
|
|
|
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) {
|
|
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,
|
|
otx.GasLimit,
|
|
otx.TransferValue,
|
|
otx.Nonce,
|
|
).Scan(&id); err != nil {
|
|
return id, err
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
return nil
|
|
}
|