|
| 1 | +package withdraw |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/btcutil" |
| 9 | + "github.com/btcsuite/btcd/chaincfg/chainhash" |
| 10 | + "github.com/btcsuite/btcd/wire" |
| 11 | + "github.com/lightninglabs/loop/loopdb" |
| 12 | + "github.com/lightninglabs/loop/loopdb/sqlc" |
| 13 | + "github.com/lightninglabs/loop/staticaddr/deposit" |
| 14 | + "github.com/lightningnetwork/lnd/clock" |
| 15 | +) |
| 16 | + |
| 17 | +// SqlStore is the backing store for static address withdrawals. |
| 18 | +type SqlStore struct { |
| 19 | + baseDB *loopdb.BaseDB |
| 20 | + |
| 21 | + clock clock.Clock |
| 22 | +} |
| 23 | + |
| 24 | +// NewSqlStore constructs a new SQLStore from a BaseDB. The BaseDB is agnostic |
| 25 | +// to the underlying driver which can be postgres or sqlite. |
| 26 | +func NewSqlStore(db *loopdb.BaseDB) *SqlStore { |
| 27 | + return &SqlStore{ |
| 28 | + baseDB: db, |
| 29 | + |
| 30 | + clock: clock.NewDefaultClock(), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// CreateWithdrawal creates a static address withdrawal record in the database. |
| 35 | +func (s *SqlStore) CreateWithdrawal(ctx context.Context, tx *wire.MsgTx, |
| 36 | + confirmationHeight uint32, deposits []*deposit.Deposit, |
| 37 | + changePkScript []byte) error { |
| 38 | + |
| 39 | + strOutpoints := make([]string, len(deposits)) |
| 40 | + totalAmount := int64(0) |
| 41 | + for i, deposit := range deposits { |
| 42 | + strOutpoints[i] = deposit.OutPoint.String() |
| 43 | + totalAmount += int64(deposit.Value) |
| 44 | + } |
| 45 | + |
| 46 | + // Populate the optional change amount. |
| 47 | + withdrawnAmount, changeAmount := int64(0), int64(0) |
| 48 | + if len(tx.TxOut) == 1 { |
| 49 | + withdrawnAmount = tx.TxOut[0].Value |
| 50 | + } else if len(tx.TxOut) == 2 { |
| 51 | + withdrawnAmount, changeAmount = tx.TxOut[0].Value, tx.TxOut[1].Value |
| 52 | + if bytes.Equal(changePkScript, tx.TxOut[0].PkScript) { |
| 53 | + changeAmount = tx.TxOut[0].Value |
| 54 | + withdrawnAmount = tx.TxOut[1].Value |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + createArgs := sqlc.CreateWithdrawalParams{ |
| 59 | + WithdrawalTxID: tx.TxHash().String(), |
| 60 | + DepositOutpoints: strings.Join(strOutpoints, ","), |
| 61 | + TotalDepositAmount: totalAmount, |
| 62 | + WithdrawnAmount: withdrawnAmount, |
| 63 | + ChangeAmount: changeAmount, |
| 64 | + ConfirmationHeight: int64(confirmationHeight), |
| 65 | + } |
| 66 | + |
| 67 | + return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{}, |
| 68 | + func(q *sqlc.Queries) error { |
| 69 | + return q.CreateWithdrawal(ctx, createArgs) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +// AllWithdrawals retrieves all known withdrawals. |
| 74 | +func (s *SqlStore) AllWithdrawals(ctx context.Context) ([]*Withdrawal, error) { |
| 75 | + var allWithdrawals []*Withdrawal |
| 76 | + |
| 77 | + err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(), |
| 78 | + func(q *sqlc.Queries) error { |
| 79 | + var err error |
| 80 | + |
| 81 | + withdrawals, err := q.AllWithdrawals(ctx) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + for _, withdrawal := range withdrawals { |
| 87 | + w, err := s.toWithdrawal(withdrawal) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + allWithdrawals = append(allWithdrawals, w) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + return allWithdrawals, nil |
| 102 | +} |
| 103 | + |
| 104 | +// toDeposit converts an sql deposit to a deposit. |
| 105 | +func (s *SqlStore) toWithdrawal(row sqlc.Withdrawal) (*Withdrawal, error) { |
| 106 | + txHash, err := chainhash.NewHashFromStr(row.WithdrawalTxID) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + return &Withdrawal{ |
| 112 | + TxID: *txHash, |
| 113 | + DepositOutpoints: strings.Split(row.DepositOutpoints, ","), |
| 114 | + TotalDepositAmount: btcutil.Amount(row.TotalDepositAmount), |
| 115 | + WithdrawnAmount: btcutil.Amount(row.WithdrawnAmount), |
| 116 | + ChangeAmount: btcutil.Amount(row.ChangeAmount), |
| 117 | + ConfirmationHeight: uint32(row.ConfirmationHeight), |
| 118 | + }, nil |
| 119 | +} |
0 commit comments