|
1 | 1 | package deposit |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "database/sql" |
| 7 | + "encoding/hex" |
6 | 8 |
|
7 | 9 | "github.com/btcsuite/btcd/btcutil" |
8 | 10 | "github.com/btcsuite/btcd/chaincfg/chainhash" |
@@ -39,10 +41,6 @@ func (s *SqlStore) CreateDeposit(ctx context.Context, deposit *Deposit) error { |
39 | 41 | Amount: int64(deposit.Value), |
40 | 42 | ConfirmationHeight: deposit.ConfirmationHeight, |
41 | 43 | TimeoutSweepPkScript: deposit.TimeOutSweepPkScript, |
42 | | - WithdrawalSweepAddress: sql.NullString{ |
43 | | - String: deposit.WithdrawalSweepAddress, |
44 | | - Valid: deposit.WithdrawalSweepAddress != "", |
45 | | - }, |
46 | 44 | } |
47 | 45 |
|
48 | 46 | updateArgs := sqlc.InsertDepositUpdateParams{ |
@@ -81,18 +79,34 @@ func (s *SqlStore) UpdateDeposit(ctx context.Context, deposit *Deposit) error { |
81 | 79 | } |
82 | 80 | ) |
83 | 81 |
|
| 82 | + var finalizedWithdrawalTx string |
| 83 | + if deposit.FinalizedWithdrawalTx != nil { |
| 84 | + var buffer bytes.Buffer |
| 85 | + err := deposit.FinalizedWithdrawalTx.Serialize( |
| 86 | + &buffer, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + finalizedWithdrawalTx = hex.EncodeToString(buffer.Bytes()) |
| 93 | + } |
| 94 | + |
84 | 95 | updateArgs := sqlc.UpdateDepositParams{ |
85 | 96 | DepositID: deposit.ID[:], |
86 | 97 | TxHash: txHash, |
87 | 98 | OutIndex: outIndex.Int32, |
88 | 99 | ConfirmationHeight: confirmationHeight.Int64, |
89 | | - ExpirySweepTxid: deposit.ExpirySweepTxid[:], |
90 | | - WithdrawalSweepAddress: sql.NullString{ |
91 | | - String: deposit.WithdrawalSweepAddress, |
92 | | - Valid: deposit.WithdrawalSweepAddress != "", |
| 100 | + FinalizedWithdrawalTx: sql.NullString{ |
| 101 | + String: finalizedWithdrawalTx, |
| 102 | + Valid: finalizedWithdrawalTx != "", |
93 | 103 | }, |
94 | 104 | } |
95 | 105 |
|
| 106 | + if deposit.ExpirySweepTxid != (chainhash.Hash{}) { |
| 107 | + updateArgs.ExpirySweepTxid = deposit.ExpirySweepTxid[:] |
| 108 | + } |
| 109 | + |
96 | 110 | return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{}, |
97 | 111 | func(q *sqlc.Queries) error { |
98 | 112 | err := q.UpdateDeposit(ctx, updateArgs) |
@@ -200,17 +214,31 @@ func (s *SqlStore) toDeposit(row sqlc.Deposit, |
200 | 214 | expirySweepTxid = *hash |
201 | 215 | } |
202 | 216 |
|
| 217 | + var finalizedWithdrawalTx *wire.MsgTx |
| 218 | + if row.FinalizedWithdrawalTx.Valid { |
| 219 | + finalizedWithdrawalTx = &wire.MsgTx{} |
| 220 | + tx, err := hex.DecodeString(row.FinalizedWithdrawalTx.String) |
| 221 | + if err != nil { |
| 222 | + return nil, err |
| 223 | + } |
| 224 | + |
| 225 | + err = finalizedWithdrawalTx.Deserialize(bytes.NewReader(tx)) |
| 226 | + if err != nil { |
| 227 | + return nil, err |
| 228 | + } |
| 229 | + } |
| 230 | + |
203 | 231 | return &Deposit{ |
204 | 232 | ID: id, |
205 | 233 | state: fsm.StateType(lastUpdate.UpdateState), |
206 | 234 | OutPoint: wire.OutPoint{ |
207 | 235 | Hash: *txHash, |
208 | 236 | Index: uint32(row.OutIndex), |
209 | 237 | }, |
210 | | - Value: btcutil.Amount(row.Amount), |
211 | | - ConfirmationHeight: row.ConfirmationHeight, |
212 | | - TimeOutSweepPkScript: row.TimeoutSweepPkScript, |
213 | | - ExpirySweepTxid: expirySweepTxid, |
214 | | - WithdrawalSweepAddress: row.WithdrawalSweepAddress.String, |
| 238 | + Value: btcutil.Amount(row.Amount), |
| 239 | + ConfirmationHeight: row.ConfirmationHeight, |
| 240 | + TimeOutSweepPkScript: row.TimeoutSweepPkScript, |
| 241 | + ExpirySweepTxid: expirySweepTxid, |
| 242 | + FinalizedWithdrawalTx: finalizedWithdrawalTx, |
215 | 243 | }, nil |
216 | 244 | } |
0 commit comments