Skip to content

Commit f55b950

Browse files
committed
loopdb: add asset params to store
1 parent 8d36dda commit f55b950

File tree

11 files changed

+252
-9
lines changed

11 files changed

+252
-9
lines changed

loopdb/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ type SwapStore interface {
3030
UpdateLoopOut(ctx context.Context, hash lntypes.Hash, time time.Time,
3131
state SwapStateData) error
3232

33+
// UpdateLoopOutAssetInfo updates the asset information for a loop out
34+
// swap.
35+
UpdateLoopOutAssetInfo(ctx context.Context, hash lntypes.Hash,
36+
asset *LoopOutAssetSwap) error
37+
3338
// FetchLoopInSwaps returns all swaps currently in the store.
3439
FetchLoopInSwaps(ctx context.Context) ([]*LoopIn, error)
3540

loopdb/sql_store.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ func (db *BaseDB) FetchLoopOutSwaps(ctx context.Context) ([]*LoopOut,
3939
return err
4040
}
4141

42+
var assetInfo *sqlc.LoopoutSwapsAsset
43+
if swap.IsAssetSwap {
44+
assetI, err := tx.GetLoopOutSwapAssetInfo(
45+
ctx, swap.SwapHash,
46+
)
47+
if err != nil {
48+
return err
49+
}
50+
assetInfo = &assetI
51+
}
52+
4253
loopOut, err := ConvertLoopOutRow(
4354
db.network, sqlc.GetLoopOutSwapRow(swap),
44-
updates,
55+
updates, assetInfo,
4556
)
4657
if err != nil {
4758
return err
@@ -71,13 +82,22 @@ func (db *BaseDB) FetchLoopOutSwap(ctx context.Context,
7182
return err
7283
}
7384

85+
var assetInfo *sqlc.LoopoutSwapsAsset
86+
if swap.IsAssetSwap {
87+
assetI, err := tx.GetLoopOutSwapAssetInfo(ctx, hash[:])
88+
if err != nil {
89+
return err
90+
}
91+
assetInfo = &assetI
92+
}
93+
7494
updates, err := tx.GetSwapUpdates(ctx, swap.SwapHash)
7595
if err != nil {
7696
return err
7797
}
7898

7999
loopOut, err = ConvertLoopOutRow(
80-
db.network, swap, updates,
100+
db.network, swap, updates, assetInfo,
81101
)
82102
if err != nil {
83103
return err
@@ -126,10 +146,43 @@ func (db *BaseDB) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
126146
return err
127147
}
128148

149+
// If the loop is an asset loop out, we'll also insert the
150+
// asset details.
151+
if swap.AssetSwapInfo != nil {
152+
assetInfo := swap.AssetSwapInfo
153+
err = tx.InsertLoopOutAsset(
154+
ctx, sqlc.InsertLoopOutAssetParams{
155+
SwapHash: hash[:],
156+
AssetID: assetInfo.AssetId,
157+
SwapRfqID: assetInfo.SwapRfqId,
158+
PrepayRfqID: assetInfo.PrepayRfqId,
159+
},
160+
)
161+
if err != nil {
162+
return err
163+
}
164+
}
165+
129166
return nil
130167
})
131168
}
132169

170+
func (db *BaseDB) UpdateLoopOutAssetInfo(ctx context.Context, hash lntypes.Hash,
171+
asset *LoopOutAssetSwap) error {
172+
173+
log.Debugf("Updating asset info for swap %v %v", hash, asset)
174+
writeOpts := NewSqlWriteOpts()
175+
return db.ExecTx(ctx, writeOpts, func(tx *sqlc.Queries) error {
176+
return tx.UpdateLoopOutAssetOffchainPayments(
177+
ctx, sqlc.UpdateLoopOutAssetOffchainPaymentsParams{
178+
SwapHash: hash[:],
179+
AssetAmtPaidSwap: int64(asset.SwapPaidAmt),
180+
AssetAmtPaidPrepay: int64(asset.PrepayPaidAmt),
181+
},
182+
)
183+
})
184+
}
185+
133186
// BatchCreateLoopOut adds multiple initiated swaps to the store.
134187
func (db *BaseDB) BatchCreateLoopOut(ctx context.Context,
135188
swaps map[lntypes.Hash]*LoopOutContract) error {
@@ -499,6 +552,7 @@ func loopOutToInsertArgs(hash lntypes.Hash,
499552
MaxPrepayRoutingFee: int64(loopOut.MaxPrepayRoutingFee),
500553
PublicationDeadline: loopOut.SwapPublicationDeadline.UTC(),
501554
PaymentTimeout: int32(loopOut.PaymentTimeout.Seconds()),
555+
IsAssetSwap: loopOut.AssetSwapInfo != nil,
502556
}
503557
}
504558

@@ -543,7 +597,8 @@ func swapToHtlcKeysInsertArgs(hash lntypes.Hash,
543597
// ConvertLoopOutRow converts a database row containing a loop out swap to a
544598
// LoopOut struct.
545599
func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
546-
updates []sqlc.SwapUpdate) (*LoopOut, error) {
600+
updates []sqlc.SwapUpdate, assetInfo *sqlc.LoopoutSwapsAsset) (*LoopOut,
601+
error) {
547602

548603
htlcKeys, err := fetchHtlcKeys(
549604
row.SenderScriptPubkey, row.ReceiverScriptPubkey,
@@ -601,6 +656,16 @@ func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
601656
},
602657
}
603658

659+
if assetInfo != nil {
660+
loopOut.Contract.AssetSwapInfo = &LoopOutAssetSwap{
661+
AssetId: assetInfo.AssetID,
662+
SwapRfqId: assetInfo.SwapRfqID,
663+
PrepayRfqId: assetInfo.PrepayRfqID,
664+
SwapPaidAmt: btcutil.Amount(assetInfo.AssetAmtPaidSwap), // nolint: lll
665+
PrepayPaidAmt: btcutil.Amount(assetInfo.AssetAmtPaidPrepay), // nolint: lll
666+
}
667+
}
668+
604669
if row.OutgoingChanSet != "" {
605670
chanSet, err := ConvertOutgoingChanSet(row.OutgoingChanSet)
606671
if err != nil {

loopdb/sql_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ const (
2222
testLabel = "test label"
2323
)
2424

25+
var (
26+
testAssetId = []byte{
27+
1, 1, 1, 1, 2, 2, 2, 2,
28+
3, 3, 3, 3, 4, 4, 4, 4,
29+
1, 1, 1, 1, 2, 2, 2, 2,
30+
3, 3, 3, 3, 4, 4, 4, 4,
31+
}
32+
)
33+
2534
// TestSqliteLoopOutStore tests all the basic functionality of the current
2635
// sqlite swap store.
2736
func TestSqliteLoopOutStore(t *testing.T) {
@@ -80,6 +89,17 @@ func TestSqliteLoopOutStore(t *testing.T) {
8089
t.Run("labelled swap", func(t *testing.T) {
8190
testSqliteLoopOutStore(t, &labelledSwap)
8291
})
92+
93+
assetSwap := unrestrictedSwap
94+
assetSwap.AssetSwapInfo = &LoopOutAssetSwap{
95+
AssetId: testAssetId,
96+
PrepayRfqId: testAssetId,
97+
SwapRfqId: testAssetId,
98+
}
99+
100+
t.Run("asset swap", func(t *testing.T) {
101+
testSqliteLoopOutStore(t, &assetSwap)
102+
})
83103
}
84104

85105
// testSqliteLoopOutStore tests the basic functionality of the current sqlite
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS loopout_swaps_assets;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
ALTER TABLE loopout_swaps ADD COLUMN is_asset_swap BOOLEAN NOT NULL DEFAULT FALSE;
2+
3+
CREATE TABLE IF NOT EXISTS loopout_swaps_assets (
4+
-- swap_hash points to the parent loop out swap hash.
5+
swap_hash BLOB PRIMARY KEY REFERENCES loopout_swaps(swap_hash),
6+
7+
-- asset_id is the asset that is used to pay the swap invoice.
8+
asset_id BYTEA NOT NULL,
9+
10+
-- swap_rfq_id is the RFQ id that will be used to pay the swap invoice.
11+
swap_rfq_id BYTEA NOT NULL,
12+
13+
-- prepay_rfq_id is the RFQ id that will be used to pay the prepay
14+
-- invoice.
15+
prepay_rfq_id BYTEA NOT NULL,
16+
17+
-- asset_amt_paid_swap is the actual asset amt that has been paid for
18+
-- the swap invoice.
19+
asset_amt_paid_swap BIGINT NOT NULL DEFAULT 0,
20+
21+
-- asset_amt_paid_prepay is the actual asset amt that has been paid for
22+
-- the prepay invoice.
23+
asset_amt_paid_prepay BIGINT NOT NULL DEFAULT 0
24+
)

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: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

loopdb/sqlc/queries/swaps.sql

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ JOIN
2626
WHERE
2727
swaps.swap_hash = $1;
2828

29+
-- name: GetLoopOutSwapAssetInfo :one
30+
SELECT
31+
*
32+
FROM
33+
loopout_swaps_assets
34+
WHERE
35+
swap_hash = $1;
36+
2937
-- name: GetLoopInSwaps :many
3038
SELECT
3139
swaps.*,
@@ -106,11 +114,29 @@ INSERT INTO loopout_swaps (
106114
max_prepay_routing_fee,
107115
publication_deadline,
108116
single_sweep,
109-
payment_timeout
117+
payment_timeout,
118+
is_asset_swap
110119
) VALUES (
111-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
120+
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
112121
);
113122

123+
-- name: InsertLoopOutAsset :exec
124+
INSERT INTO loopout_swaps_assets (
125+
swap_hash,
126+
asset_id,
127+
swap_rfq_id,
128+
prepay_rfq_id
129+
) VALUES (
130+
$1, $2, $3, $4
131+
);
132+
133+
-- name: UpdateLoopOutAssetOffchainPayments :exec
134+
UPDATE loopout_swaps_assets
135+
SET
136+
asset_amt_paid_swap = $2,
137+
asset_amt_paid_prepay = $3
138+
WHERE swap_hash = $1;
139+
114140
-- name: InsertLoopIn :exec
115141
INSERT INTO loopin_swaps (
116142
swap_hash,

0 commit comments

Comments
 (0)