|
| 1 | +package withdraw |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "database/sql" |
| 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 | +type Querier interface { |
| 18 | + // CreateWithdrawal inserts a new withdrawal. |
| 19 | + CreateWithdrawal(ctx context.Context, |
| 20 | + arg sqlc.CreateWithdrawalParams) error |
| 21 | + |
| 22 | + // CreateWithdrawalDeposit links withdrawal to deposits. |
| 23 | + CreateWithdrawalDeposit(ctx context.Context, |
| 24 | + arg sqlc.CreateWithdrawalDepositParams) error |
| 25 | + |
| 26 | + // GetWithdrawalDeposits retrieves the deposit IDs associated with a |
| 27 | + // withdrawal. |
| 28 | + GetWithdrawalDeposits(ctx context.Context, withdrawalID []byte) ( |
| 29 | + [][]byte, error) |
| 30 | + |
| 31 | + // GetAllWithdrawals retrieves all withdrawals from the database. |
| 32 | + GetAllWithdrawals(ctx context.Context) ([]sqlc.Withdrawal, error) |
| 33 | +} |
| 34 | + |
| 35 | +// BaseDB is the interface that contains all the queries generated by sqlc for |
| 36 | +// the static_address_swaps table and transaction functionality. |
| 37 | +type BaseDB interface { |
| 38 | + Querier |
| 39 | + |
| 40 | + // ExecTx allows for executing a function in the context of a database |
| 41 | + // transaction. |
| 42 | + ExecTx(ctx context.Context, txOptions loopdb.TxOptions, |
| 43 | + txBody func(Querier) error) error |
| 44 | +} |
| 45 | + |
| 46 | +// SqlStore is the backing store for static address withdrawals. |
| 47 | +type SqlStore struct { |
| 48 | + baseDB BaseDB |
| 49 | + depositStore deposit.Store |
| 50 | + clock clock.Clock |
| 51 | +} |
| 52 | + |
| 53 | +// NewSqlStore constructs a new SQLStore from a BaseDB. The BaseDB is agnostic |
| 54 | +// to the underlying driver which can be postgres or sqlite. |
| 55 | +func NewSqlStore(db BaseDB, depositStore deposit.Store) *SqlStore { |
| 56 | + return &SqlStore{ |
| 57 | + baseDB: db, |
| 58 | + depositStore: depositStore, |
| 59 | + clock: clock.NewDefaultClock(), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// CreateWithdrawal creates a static address withdrawal record in the database. |
| 64 | +func (s *SqlStore) CreateWithdrawal(ctx context.Context, tx *wire.MsgTx, |
| 65 | + confirmationHeight uint32, deposits []*deposit.Deposit, |
| 66 | + changePkScript []byte) error { |
| 67 | + |
| 68 | + strOutpoints := make([]string, len(deposits)) |
| 69 | + totalAmount := int64(0) |
| 70 | + for i, deposit := range deposits { |
| 71 | + strOutpoints[i] = deposit.OutPoint.String() |
| 72 | + totalAmount += int64(deposit.Value) |
| 73 | + } |
| 74 | + |
| 75 | + // Populate the optional change amount. |
| 76 | + withdrawnAmount, changeAmount := int64(0), int64(0) |
| 77 | + if len(tx.TxOut) == 1 { |
| 78 | + withdrawnAmount = tx.TxOut[0].Value |
| 79 | + } else if len(tx.TxOut) == 2 { |
| 80 | + withdrawnAmount, changeAmount = tx.TxOut[0].Value, tx.TxOut[1].Value |
| 81 | + if bytes.Equal(changePkScript, tx.TxOut[0].PkScript) { |
| 82 | + changeAmount = tx.TxOut[0].Value |
| 83 | + withdrawnAmount = tx.TxOut[1].Value |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + id, err := GetRandomWithdrawalID() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + createArgs := sqlc.CreateWithdrawalParams{ |
| 93 | + WithdrawalID: id[:], |
| 94 | + WithdrawalTxID: tx.TxHash().String(), |
| 95 | + TotalDepositAmount: totalAmount, |
| 96 | + WithdrawnAmount: withdrawnAmount, |
| 97 | + ChangeAmount: changeAmount, |
| 98 | + ConfirmationHeight: sql.NullInt64{ |
| 99 | + Int64: int64(confirmationHeight), |
| 100 | + Valid: confirmationHeight > 0, |
| 101 | + }, |
| 102 | + InitiationTime: s.clock.Now(), |
| 103 | + } |
| 104 | + |
| 105 | + return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{}, |
| 106 | + func(q Querier) error { |
| 107 | + err := q.CreateWithdrawal(ctx, createArgs) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + for _, deposit := range deposits { |
| 113 | + err = q.CreateWithdrawalDeposit(ctx, |
| 114 | + sqlc.CreateWithdrawalDepositParams{ |
| 115 | + WithdrawalID: createArgs.WithdrawalID, |
| 116 | + DepositID: deposit.ID[:], |
| 117 | + }) |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return nil |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +// GetAllWithdrawals retrieves all static address withdrawals from the |
| 129 | +// database. It returns a slice of Withdrawal structs, each containing a list |
| 130 | +// of associated deposits. |
| 131 | +func (s *SqlStore) GetAllWithdrawals(ctx context.Context) ([]Withdrawal, |
| 132 | + error) { |
| 133 | + |
| 134 | + withdrawals, err := s.baseDB.GetAllWithdrawals(ctx) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + result := make([]Withdrawal, 0, len(withdrawals)) |
| 140 | + for _, w := range withdrawals { |
| 141 | + depositIDs, err := s.baseDB.GetWithdrawalDeposits(ctx, |
| 142 | + w.WithdrawalID) |
| 143 | + |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + deposits := make([]*deposit.Deposit, 0, len(depositIDs)) |
| 149 | + for _, dID := range depositIDs { |
| 150 | + deposit, err := s.depositStore.GetDeposit( |
| 151 | + ctx, deposit.ID(dID), |
| 152 | + ) |
| 153 | + if err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + deposits = append(deposits, deposit) |
| 157 | + } |
| 158 | + |
| 159 | + txID, err := chainhash.NewHashFromStr(w.WithdrawalTxID) |
| 160 | + if err != nil { |
| 161 | + return nil, err |
| 162 | + } |
| 163 | + |
| 164 | + result = append(result, Withdrawal{ |
| 165 | + ID: ID(w.WithdrawalID), |
| 166 | + TxID: *txID, |
| 167 | + Deposits: deposits, |
| 168 | + TotalDepositAmount: btcutil.Amount(w.TotalDepositAmount), |
| 169 | + WithdrawnAmount: btcutil.Amount(w.WithdrawnAmount), |
| 170 | + ChangeAmount: btcutil.Amount(w.ChangeAmount), |
| 171 | + InitiationTime: w.InitiationTime, |
| 172 | + ConfirmationHeight: w.ConfirmationHeight.Int64, |
| 173 | + }) |
| 174 | + } |
| 175 | + |
| 176 | + return result, nil |
| 177 | +} |
0 commit comments