Skip to content

Commit d70c48e

Browse files
committed
all info stuff
1 parent 8160efa commit d70c48e

File tree

10 files changed

+285
-1
lines changed

10 files changed

+285
-1
lines changed

loopd/daemon.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
604604
depositManager = deposit.NewManager(depoCfg)
605605

606606
// Static address deposit withdrawal manager setup.
607+
withdrawalStore := withdraw.NewSqlStore(baseDb)
607608
withdrawalCfg := &withdraw.ManagerConfig{
608609
StaticAddressServerClient: staticAddressClient,
609610
AddressManager: staticAddressManager,
@@ -612,6 +613,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
612613
ChainParams: d.lnd.ChainParams,
613614
ChainNotifier: d.lnd.ChainNotifier,
614615
Signer: d.lnd.Signer,
616+
Store: withdrawalStore,
615617
}
616618
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)
617619

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS withdrawals;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CREATE TABLE IF NOT EXISTS withdrawals (
2+
-- id is the auto-incrementing primary key for a withdrawal.
3+
id INTEGER PRIMARY KEY,
4+
5+
withdrawal_tx_id TEXT NOT NULL UNIQUE,
6+
7+
deposit_outpoints TEXT NOT NULL,
8+
9+
total_deposit_amount BIGINT NOT NULL,
10+
11+
withdrawn_amount BIGINT NOT NULL,
12+
13+
change_amount BIGINT NOT NULL,
14+
15+
confirmation_height BIGINT NOT NULL
16+
);

loopdb/sqlc/models.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- name: CreateWithdrawal :exec
2+
INSERT INTO withdrawals (
3+
withdrawal_tx_id,
4+
deposit_outpoints,
5+
total_deposit_amount,
6+
withdrawn_amount,
7+
change_amount,
8+
confirmation_height
9+
) VALUES (
10+
$1,
11+
$2,
12+
$3,
13+
$4,
14+
$5,
15+
$6
16+
);
17+
18+
-- name: AllWithdrawals :many
19+
SELECT
20+
*
21+
FROM
22+
withdrawals
23+
ORDER BY
24+
id ASC;

loopdb/sqlc/static_address_withdrawals.sql.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staticaddr/withdraw/manager.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type ManagerConfig struct {
8282

8383
// Signer is the signer client that is used to sign transactions.
8484
Signer lndclient.SignerClient
85+
86+
Store *SqlStore
8587
}
8688

8789
// newWithdrawalRequest is used to send withdrawal request to the manager main
@@ -621,7 +623,7 @@ func (m *Manager) handleWithdrawal(ctx context.Context,
621623
int32(m.initiationHeight.Load()),
622624
)
623625
select {
624-
case <-confChan:
626+
case tx := <-confChan:
625627
err = m.cfg.DepositManager.TransitionDeposits(
626628
ctx, deposits, deposit.OnWithdrawn,
627629
deposit.Withdrawn,
@@ -637,6 +639,11 @@ func (m *Manager) handleWithdrawal(ctx context.Context,
637639
delete(m.finalizedWithdrawalTxns, txHash)
638640
m.mu.Unlock()
639641

642+
m.cfg.Store.CreateWithdrawal(
643+
ctx, tx.Tx, tx.BlockHeight, deposits,
644+
addrParams.PkScript,
645+
)
646+
640647
case err := <-errChan:
641648
log.Errorf("Error waiting for confirmation: %v",
642649
err)

staticaddr/withdraw/sql_store.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
withdrawnAmount, changeAmount := int64(0), int64(0)
47+
if len(tx.TxOut) == 1 {
48+
withdrawnAmount = tx.TxOut[0].Value
49+
} else if len(tx.TxOut) == 2 {
50+
withdrawnAmount, changeAmount = tx.TxOut[0].Value, tx.TxOut[1].Value
51+
if bytes.Equal(changePkScript, tx.TxOut[0].PkScript) {
52+
changeAmount = tx.TxOut[0].Value
53+
withdrawnAmount = tx.TxOut[1].Value
54+
}
55+
}
56+
57+
createArgs := sqlc.CreateWithdrawalParams{
58+
WithdrawalTxID: tx.TxHash().String(),
59+
DepositOutpoints: strings.Join(strOutpoints, ","),
60+
TotalDepositAmount: totalAmount,
61+
WithdrawnAmount: withdrawnAmount,
62+
ChangeAmount: changeAmount,
63+
ConfirmationHeight: int64(confirmationHeight),
64+
}
65+
66+
return s.baseDB.ExecTx(ctx, &loopdb.SqliteTxOptions{},
67+
func(q *sqlc.Queries) error {
68+
return q.CreateWithdrawal(ctx, createArgs)
69+
})
70+
}
71+
72+
// AllWithdrawals retrieves all known withdrawals.
73+
func (s *SqlStore) AllWithdrawals(ctx context.Context) ([]*Withdrawal, error) {
74+
var allWithdrawals []*Withdrawal
75+
76+
err := s.baseDB.ExecTx(ctx, loopdb.NewSqlReadOpts(),
77+
func(q *sqlc.Queries) error {
78+
var err error
79+
80+
withdrawals, err := q.AllWithdrawals(ctx)
81+
if err != nil {
82+
return err
83+
}
84+
85+
for _, withdrawal := range withdrawals {
86+
w, err := s.toWithdrawal(withdrawal)
87+
if err != nil {
88+
return err
89+
}
90+
91+
allWithdrawals = append(allWithdrawals, w)
92+
}
93+
94+
return nil
95+
})
96+
if err != nil {
97+
return nil, err
98+
}
99+
100+
return allWithdrawals, nil
101+
}
102+
103+
// toDeposit converts an sql deposit to a deposit.
104+
func (s *SqlStore) toWithdrawal(row sqlc.Withdrawal) (*Withdrawal, error) {
105+
txHash, err := chainhash.NewHashFromStr(row.WithdrawalTxID)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
return &Withdrawal{
111+
TxID: *txHash,
112+
DepositOutpoints: strings.Split(row.DepositOutpoints, ","),
113+
TotalDepositAmount: btcutil.Amount(row.TotalDepositAmount),
114+
WithdrawnAmount: btcutil.Amount(row.WithdrawnAmount),
115+
ChangeAmount: btcutil.Amount(row.ChangeAmount),
116+
ConfirmationHeight: uint32(row.ConfirmationHeight),
117+
}, nil
118+
}

staticaddr/withdraw/withdrawal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package withdraw
2+
3+
import (
4+
"github.com/btcsuite/btcd/btcutil"
5+
"github.com/btcsuite/btcd/chaincfg/chainhash"
6+
)
7+
8+
type Withdrawal struct {
9+
TxID chainhash.Hash
10+
DepositOutpoints []string
11+
TotalDepositAmount btcutil.Amount
12+
WithdrawnAmount btcutil.Amount
13+
ChangeAmount btcutil.Amount
14+
ConfirmationHeight uint32
15+
}

0 commit comments

Comments
 (0)