2023-02-15 08:05:43 +01:00
|
|
|
package store
|
|
|
|
|
2023-02-21 18:34:22 +01:00
|
|
|
import (
|
|
|
|
"context"
|
2023-04-11 12:14:49 +02:00
|
|
|
"math/big"
|
2023-02-21 18:34:22 +01:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
type (
|
|
|
|
Otx struct {
|
|
|
|
TrackingId string
|
|
|
|
Type enum.OtxType
|
|
|
|
RawTx string
|
|
|
|
TxHash string
|
|
|
|
From string
|
|
|
|
Data string
|
|
|
|
GasLimit uint64
|
|
|
|
TransferValue uint64
|
|
|
|
GasPrice *big.Int
|
|
|
|
Nonce uint64
|
|
|
|
}
|
|
|
|
txStatus struct {
|
|
|
|
CreatedAt time.Time `db:"created_at" json:"createdAt"`
|
|
|
|
Status string `db:"status" json:"status"`
|
|
|
|
TransferValue uint64 `db:"transfer_value" json:"transferValue"`
|
|
|
|
TxHash string `db:"tx_hash" json:"txHash"`
|
|
|
|
Type string `db:"type" json:"txType"`
|
|
|
|
}
|
|
|
|
)
|
2023-02-21 18:34:22 +01:00
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
func (s *PgStore) 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,
|
2023-04-11 12:14:49 +02:00
|
|
|
).Scan(
|
|
|
|
&id,
|
|
|
|
); err != nil {
|
2023-02-15 08:05:43 +01:00
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
2023-02-21 18:34:22 +01:00
|
|
|
|
2023-04-13 12:38:23 +02:00
|
|
|
func (s *PgStore) GetNextNonce(
|
|
|
|
ctx context.Context,
|
|
|
|
publicAddress string,
|
|
|
|
) (uint64, error) {
|
|
|
|
var (
|
|
|
|
lastNonce uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.db.QueryRow(
|
|
|
|
ctx,
|
|
|
|
s.queries.GetNextNonce,
|
|
|
|
publicAddress,
|
|
|
|
).Scan(
|
|
|
|
&lastNonce,
|
|
|
|
); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastNonce, nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
func (s *PgStore) GetTxStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
trackingId string,
|
|
|
|
) (txStatus, error) {
|
2023-02-21 18:34:22 +01:00
|
|
|
var (
|
2023-04-11 12:14:49 +02:00
|
|
|
tx txStatus
|
2023-02-21 18:34:22 +01:00
|
|
|
)
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
rows, err := s.db.Query(
|
2023-02-21 18:34:22 +01:00
|
|
|
ctx,
|
|
|
|
s.queries.GetTxStatusByTrackingId,
|
|
|
|
trackingId,
|
2023-04-11 12:14:49 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return tx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pgxscan.ScanOne(
|
|
|
|
&tx,
|
|
|
|
rows,
|
2023-02-21 18:34:22 +01:00
|
|
|
); err != nil {
|
2023-04-11 12:14:49 +02:00
|
|
|
return tx, err
|
2023-02-21 18:34:22 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
return tx, nil
|
2023-02-21 18:34:22 +01:00
|
|
|
}
|
2023-03-01 18:13:23 +01:00
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
func (s *PgStore) CreateDispatchStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
otxId uint,
|
|
|
|
otxStatus enum.OtxStatus,
|
|
|
|
) error {
|
2023-03-08 07:49:09 +01:00
|
|
|
if _, err := s.db.Exec(
|
|
|
|
ctx,
|
|
|
|
s.queries.CreateDispatchStatus,
|
2023-04-11 12:14:49 +02:00
|
|
|
otxId,
|
|
|
|
otxStatus,
|
2023-03-08 07:49:09 +01:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
func (s *PgStore) UpdateDispatchStatus(
|
|
|
|
ctx context.Context,
|
|
|
|
txSuccess bool,
|
|
|
|
txHash string,
|
|
|
|
txBlock uint64,
|
|
|
|
) error {
|
2023-03-02 10:12:39 +01:00
|
|
|
var (
|
|
|
|
status = enum.SUCCESS
|
|
|
|
)
|
|
|
|
|
2023-04-11 12:14:49 +02:00
|
|
|
if !txSuccess {
|
2023-03-02 10:12:39 +01:00
|
|
|
status = enum.REVERTED
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.db.Exec(
|
|
|
|
ctx,
|
2023-04-11 12:14:49 +02:00
|
|
|
s.queries.UpdateDispatchStatus,
|
|
|
|
txHash,
|
2023-03-02 10:12:39 +01:00
|
|
|
status,
|
2023-04-11 12:14:49 +02:00
|
|
|
txBlock,
|
2023-03-02 10:12:39 +01:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-01 18:13:23 +01:00
|
|
|
return nil
|
|
|
|
}
|