Skip to content

staticaddr: persist withdrawal info #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cmd/loop/staticaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var staticAddressCommands = cli.Command{
newStaticAddressCommand,
listUnspentCommand,
listDepositsCommand,
listWithdrawalsCommand,
listStaticAddressSwapsCommand,
withdrawalCommand,
summaryCommand,
Expand Down Expand Up @@ -246,6 +247,14 @@ var listDepositsCommand = cli.Command{
Action: listDeposits,
}

var listWithdrawalsCommand = cli.Command{
Name: "listwithdrawals",
Usage: "Display a summary of past withdrawals.",
Description: `
`,
Action: listWithdrawals,
}

var listStaticAddressSwapsCommand = cli.Command{
Name: "listswaps",
Usage: "Display a summary of static address related information.",
Expand Down Expand Up @@ -344,6 +353,30 @@ func listDeposits(ctx *cli.Context) error {
return nil
}

func listWithdrawals(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
return cli.ShowCommandHelp(ctx, "withdrawals")
}

client, cleanup, err := getClient(ctx)
if err != nil {
return err
}
defer cleanup()

resp, err := client.ListStaticAddressWithdrawals(
ctxb, &looprpc.ListStaticAddressWithdrawalRequest{},
)
if err != nil {
return err
}

printRespJSON(resp)

return nil
}

func listStaticAddressSwaps(ctx *cli.Context) error {
ctxb := context.Background()
if ctx.NArg() > 0 {
Expand Down
2 changes: 2 additions & 0 deletions loopd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
depositManager = deposit.NewManager(depoCfg)

// Static address deposit withdrawal manager setup.
withdrawalStore := withdraw.NewSqlStore(baseDb)
withdrawalCfg := &withdraw.ManagerConfig{
StaticAddressServerClient: staticAddressClient,
AddressManager: staticAddressManager,
Expand All @@ -612,6 +613,7 @@ func (d *Daemon) initialize(withMacaroonService bool) error {
ChainParams: d.lnd.ChainParams,
ChainNotifier: d.lnd.ChainNotifier,
Signer: d.lnd.Signer,
Store: withdrawalStore,
}
withdrawalManager = withdraw.NewManager(withdrawalCfg, blockHeight)

Expand Down
35 changes: 35 additions & 0 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,41 @@ func (s *swapClientServer) ListStaticAddressDeposits(ctx context.Context,
}, nil
}

// ListStaticAddressWithdrawals returns a list of all finalized withdrawal
// transactions.
func (s *swapClientServer) ListStaticAddressWithdrawals(ctx context.Context,
_ *looprpc.ListStaticAddressWithdrawalRequest) (
*looprpc.ListStaticAddressWithdrawalResponse, error) {

withdrawals, err := s.withdrawalManager.GetAllWithdrawals(ctx)
if err != nil {
return nil, err
}

if len(withdrawals) == 0 {
return &looprpc.ListStaticAddressWithdrawalResponse{}, nil
}

clientWithdrawals := make(
[]*looprpc.StaticAddressWithdrawal, 0, len(withdrawals),
)
for _, w := range withdrawals {
withdrawal := &looprpc.StaticAddressWithdrawal{
TxId: w.TxID.String(),
Outpoints: w.DepositOutpoints,
TotalDepositAmountSatoshis: int64(w.TotalDepositAmount),
WithdrawnAmountSatoshis: int64(w.WithdrawnAmount),
ChangeAmountSatoshis: int64(w.ChangeAmount),
ConfirmationHeight: w.ConfirmationHeight,
}
clientWithdrawals = append(clientWithdrawals, withdrawal)
}

return &looprpc.ListStaticAddressWithdrawalResponse{
Withdrawals: clientWithdrawals,
}, nil
}

// ListStaticAddressSwaps returns a list of all swaps that are currently pending
// or previously succeeded.
func (s *swapClientServer) ListStaticAddressSwaps(ctx context.Context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS withdrawals;
26 changes: 26 additions & 0 deletions loopdb/sqlc/migrations/000015_static_address_withdrawals.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- withdrawals stores finalized static address withdrawals.
CREATE TABLE IF NOT EXISTS withdrawals (
-- id is the auto-incrementing primary key for a withdrawal.
id INTEGER PRIMARY KEY,

-- withdrawal_tx_id is the transaction tx id of the withdrawal.
withdrawal_tx_id TEXT NOT NULL UNIQUE,

-- deposit_outpoints is a concatenated list of outpoints that are used for
-- this withdrawal. The list has the format txid1:idx;txid2:idx;...
deposit_outpoints TEXT NOT NULL,

-- total_deposit_amount is the total amount of the deposits in satoshis.
total_deposit_amount BIGINT NOT NULL,

-- withdrawn_amount is the total amount of the withdrawal. It amounts
-- to the total amount of the deposits minus the fees and optional change.
withdrawn_amount BIGINT NOT NULL,

-- change_amount is the optional change that the user selected.
change_amount BIGINT NOT NULL,

-- confirmation_height is the block height at which the withdrawal was
-- first confirmed.
confirmation_height BIGINT NOT NULL
);
10 changes: 10 additions & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions loopdb/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions loopdb/sqlc/queries/static_address_withdrawals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- name: CreateWithdrawal :exec
INSERT INTO withdrawals (
withdrawal_tx_id,
deposit_outpoints,
total_deposit_amount,
withdrawn_amount,
change_amount,
confirmation_height
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6
);

-- name: AllWithdrawals :many
SELECT
*
FROM
withdrawals
ORDER BY
id ASC;
89 changes: 89 additions & 0 deletions loopdb/sqlc/static_address_withdrawals.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading