Skip to content

Commit 40614a2

Browse files
committed
loopdb: add asset params to store
1 parent 1440b6b commit 40614a2

File tree

11 files changed

+231
-5
lines changed

11 files changed

+231
-5
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: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,40 @@ func (db *BaseDB) CreateLoopOut(ctx context.Context, hash lntypes.Hash,
126126
return err
127127
}
128128

129+
// If the loop is an asset loop out, we'll also insert the
130+
// asset details.
131+
if swap.AssetSwapInfo != nil {
132+
assetInfo := swap.AssetSwapInfo
133+
err = tx.InsertLoopOutAsset(
134+
ctx, sqlc.InsertLoopOutAssetParams{
135+
SwapHash: hash[:],
136+
AssetID: assetInfo.AssetId,
137+
SwapRfqID: assetInfo.SwapRfqId,
138+
PrepayRfqID: assetInfo.PrepayRfqId,
139+
},
140+
)
141+
if err != nil {
142+
return err
143+
}
144+
}
145+
129146
return nil
130147
})
131148
}
132149

150+
// UpdateLoopOutAssetInfo updates the offchain send amounts of the prepay and
151+
// swap payment for an asset loop out swap.
152+
func (db *BaseDB) UpdateLoopOutAssetInfo(ctx context.Context, hash lntypes.Hash,
153+
asset *LoopOutAssetSwap) error {
154+
155+
return db.UpdateLoopOutAssetOffchainPayments(
156+
ctx, sqlc.UpdateLoopOutAssetOffchainPaymentsParams{
157+
SwapHash: hash[:],
158+
AssetAmtPaidSwap: int64(asset.SwapPaidAmt),
159+
AssetAmtPaidPrepay: int64(asset.PrepayPaidAmt),
160+
})
161+
}
162+
133163
// BatchCreateLoopOut adds multiple initiated swaps to the store.
134164
func (db *BaseDB) BatchCreateLoopOut(ctx context.Context,
135165
swaps map[lntypes.Hash]*LoopOutContract) error {
@@ -543,7 +573,8 @@ func swapToHtlcKeysInsertArgs(hash lntypes.Hash,
543573
// ConvertLoopOutRow converts a database row containing a loop out swap to a
544574
// LoopOut struct.
545575
func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
546-
updates []sqlc.SwapUpdate) (*LoopOut, error) {
576+
updates []sqlc.SwapUpdate) (*LoopOut,
577+
error) {
547578

548579
htlcKeys, err := fetchHtlcKeys(
549580
row.SenderScriptPubkey, row.ReceiverScriptPubkey,
@@ -601,6 +632,20 @@ func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
601632
},
602633
}
603634

635+
if row.AssetID != nil {
636+
loopOut.Contract.AssetSwapInfo = &LoopOutAssetSwap{
637+
AssetId: row.AssetID,
638+
SwapRfqId: row.SwapRfqID,
639+
PrepayRfqId: row.PrepayRfqID,
640+
SwapPaidAmt: uint64(
641+
unmarshalSqlInt64(row.AssetAmtPaidSwap),
642+
),
643+
PrepayPaidAmt: uint64(
644+
unmarshalSqlInt64(row.AssetAmtPaidPrepay),
645+
),
646+
}
647+
}
648+
604649
if row.OutgoingChanSet != "" {
605650
chanSet, err := ConvertOutgoingChanSet(row.OutgoingChanSet)
606651
if err != nil {
@@ -803,3 +848,11 @@ func blobTo33ByteSlice(blob []byte) ([33]byte, error) {
803848

804849
return key, nil
805850
}
851+
852+
func unmarshalSqlInt64(data sql.NullInt64) int64 {
853+
if !data.Valid {
854+
return 0
855+
}
856+
857+
return data.Int64
858+
}

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CREATE TABLE IF NOT EXISTS loopout_swaps_asset_info (
2+
-- swap_hash points to the parent loop out swap hash.
3+
swap_hash BLOB PRIMARY KEY REFERENCES loopout_swaps(swap_hash),
4+
5+
-- asset_id is the asset that is used to pay the swap invoice.
6+
asset_id BYTEA NOT NULL,
7+
8+
-- swap_rfq_id is the RFQ id that will be used to pay the swap invoice.
9+
swap_rfq_id BYTEA NOT NULL,
10+
11+
-- prepay_rfq_id is the RFQ id that will be used to pay the prepay
12+
-- invoice.
13+
prepay_rfq_id BYTEA NOT NULL,
14+
15+
-- asset_amt_paid_swap is the actual asset amt that has been paid for
16+
-- the swap invoice.
17+
asset_amt_paid_swap BIGINT NOT NULL DEFAULT 0,
18+
19+
-- asset_amt_paid_prepay is the actual asset amt that has been paid for
20+
-- the prepay invoice.
21+
asset_amt_paid_prepay BIGINT NOT NULL DEFAULT 0
22+
)

loopdb/sqlc/models.go

Lines changed: 9 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.

loopdb/sqlc/queries/swaps.sql

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22
SELECT
33
swaps.*,
44
loopout_swaps.*,
5-
htlc_keys.*
5+
htlc_keys.*,
6+
loopout_swaps_asset_info.*
67
FROM
78
swaps
89
JOIN
910
loopout_swaps ON swaps.swap_hash = loopout_swaps.swap_hash
1011
JOIN
1112
htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
13+
LEFT JOIN
14+
loopout_swaps_asset_info ON swaps.swap_hash = loopout_swaps_asset_info.swap_hash
1215
ORDER BY
1316
swaps.id;
1417

1518
-- name: GetLoopOutSwap :one
1619
SELECT
1720
swaps.*,
1821
loopout_swaps.*,
19-
htlc_keys.*
22+
htlc_keys.*,
23+
loopout_swaps_asset_info.*
2024
FROM
2125
swaps
2226
JOIN
2327
loopout_swaps ON swaps.swap_hash = loopout_swaps.swap_hash
2428
JOIN
2529
htlc_keys ON swaps.swap_hash = htlc_keys.swap_hash
30+
LEFT JOIN
31+
loopout_swaps_asset_info ON swaps.swap_hash = loopout_swaps_asset_info.swap_hash
2632
WHERE
2733
swaps.swap_hash = $1;
2834

@@ -111,6 +117,23 @@ INSERT INTO loopout_swaps (
111117
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
112118
);
113119

120+
-- name: InsertLoopOutAsset :exec
121+
INSERT INTO loopout_swaps_asset_info (
122+
swap_hash,
123+
asset_id,
124+
swap_rfq_id,
125+
prepay_rfq_id
126+
) VALUES (
127+
$1, $2, $3, $4
128+
);
129+
130+
-- name: UpdateLoopOutAssetOffchainPayments :exec
131+
UPDATE loopout_swaps_asset_info
132+
SET
133+
asset_amt_paid_swap = $2,
134+
asset_amt_paid_prepay = $3
135+
WHERE swap_hash = $1;
136+
114137
-- name: InsertLoopIn :exec
115138
INSERT INTO loopin_swaps (
116139
swap_hash,

0 commit comments

Comments
 (0)