Skip to content

Commit d9fb682

Browse files
committed
itest: test balances with and without leased flag
1 parent a592d8f commit d9fb682

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

itest/assertions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ func AssertGenesisOutput(t *testing.T, output *taprpc.ManagedUtxo,
18861886
}
18871887

18881888
func AssertAssetBalances(t *testing.T, client taprpc.TaprootAssetsClient,
1889-
simpleAssets, issuableAssets []*taprpc.Asset) {
1889+
simpleAssets, issuableAssets []*taprpc.Asset, includeLeased bool) {
18901890

18911891
t.Helper()
18921892

@@ -1901,7 +1901,8 @@ func AssertAssetBalances(t *testing.T, client taprpc.TaprootAssetsClient,
19011901
}
19021902
assetIDBalances, err := client.ListBalances(
19031903
ctxt, &taprpc.ListBalancesRequest{
1904-
GroupBy: balanceReq,
1904+
GroupBy: balanceReq,
1905+
IncludeLeased: includeLeased,
19051906
},
19061907
)
19071908
require.NoError(t, err)

itest/assets_test.go

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/taproot-assets/internal/test"
2020
"github.com/lightninglabs/taproot-assets/proof"
2121
"github.com/lightninglabs/taproot-assets/taprpc"
22+
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
2223
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
2324
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
2425
"github.com/lightningnetwork/lnd/lnrpc"
@@ -109,7 +110,9 @@ func testMintAssets(t *harnessTest) {
109110
// Now that all our assets have been issued, we'll use the balance
110111
// calls to ensure that we're able to retrieve the proper balance for
111112
// them all.
112-
AssertAssetBalances(t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets)
113+
AssertAssetBalances(
114+
t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets, false,
115+
)
113116

114117
// Check that we can retrieve the group keys for the issuable assets.
115118
assertGroups(t.t, t.tapd, issuableAssets)
@@ -441,7 +444,9 @@ func testMintAssetsWithTapscriptSibling(t *harnessTest) {
441444
rpcIssuableAssets := MintAssetsConfirmBatch(
442445
t.t, t.lndHarness.Miner.Client, t.tapd, issuableAssets,
443446
)
444-
AssertAssetBalances(t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets)
447+
AssertAssetBalances(
448+
t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets, false,
449+
)
445450

446451
// Filter the managed UTXOs to select the genesis UTXO with the
447452
// tapscript sibling.
@@ -617,3 +622,77 @@ func testMintBatchAndTransfer(t *harnessTest) {
617622

618623
require.True(t.t, proto.Equal(originalBatch, afterBatch))
619624
}
625+
626+
// testAssetBalances tests the balance retrieval functionality for issued
627+
// assets. The function mints two batches of assets and asserts if the tapcli
628+
// `assets balance` returns the correct balances. It then funds a vPSBT, putting
629+
// a lease on one of the two batches. It then asserts whether the endpoint still
630+
// returns the correct balances, taking into account the `include_leased` flag.
631+
func testAssetBalances(t *harnessTest) {
632+
ctxb := context.Background()
633+
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
634+
defer cancel()
635+
636+
rpcSimpleAssets := MintAssetsConfirmBatch(
637+
t.t, t.lndHarness.Miner.Client, t.tapd, simpleAssets,
638+
)
639+
rpcIssuableAssets := MintAssetsConfirmBatch(
640+
t.t, t.lndHarness.Miner.Client, t.tapd, issuableAssets,
641+
)
642+
targetAsset := rpcSimpleAssets[0]
643+
644+
// Now that all our assets have been issued, we'll use the balance
645+
// calls to ensure that we're able to retrieve the proper balance for
646+
// them all.
647+
AssertAssetBalances(
648+
t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets, false,
649+
)
650+
651+
var (
652+
targetAssetGenesis = targetAsset.AssetGenesis
653+
aliceTapd = t.tapd
654+
bobLnd = t.lndHarness.Bob
655+
)
656+
657+
// We create a second tapd node that will be used to simulate a second
658+
// party in the test. This tapd node is connected to lnd "Bob".
659+
bobTapd := setupTapdHarness(t.t, t, bobLnd, t.universeServer)
660+
defer func() {
661+
require.NoError(t.t, bobTapd.stop(!*noDelete))
662+
}()
663+
664+
const assetsToSend = 1000
665+
bobAddr, err := bobTapd.NewAddr(ctxt, &taprpc.NewAddrRequest{
666+
AssetId: targetAssetGenesis.AssetId,
667+
Amt: assetsToSend,
668+
})
669+
require.NoError(t.t, err)
670+
671+
// Now we can create our virtual transaction and ask Alice's tapd to
672+
// fund it.
673+
recipients := map[string]uint64{
674+
bobAddr.Encoded: bobAddr.Amount,
675+
}
676+
_, err = aliceTapd.FundVirtualPsbt(
677+
ctxt, &wrpc.FundVirtualPsbtRequest{
678+
Template: &wrpc.FundVirtualPsbtRequest_Raw{
679+
Raw: &wrpc.TxTemplate{
680+
Recipients: recipients,
681+
},
682+
},
683+
},
684+
)
685+
require.NoError(t.t, err)
686+
687+
// With a transaction funding should have led to a lease on the simple
688+
// assets, we'll use the balance calls to ensure that we're able to
689+
// retrieve the proper balances.
690+
rpcEmptyAssets := []*taprpc.Asset{}
691+
AssertAssetBalances(
692+
t.t, t.tapd, rpcEmptyAssets, rpcIssuableAssets, false,
693+
)
694+
695+
AssertAssetBalances(
696+
t.t, t.tapd, rpcSimpleAssets, rpcIssuableAssets, true,
697+
)
698+
}

itest/test_list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var testCases = []*testCase{
1717
name: "mint batch and transfer",
1818
test: testMintBatchAndTransfer,
1919
},
20+
{
21+
name: "asset balances",
22+
test: testAssetBalances,
23+
},
2024
{
2125
name: "asset meta validation",
2226
test: testAssetMeta,

0 commit comments

Comments
 (0)