Skip to content

Commit a506bc4

Browse files
committed
itest: add assertSpendableBalance helper func
This will report balance that we can 100% spend.
1 parent 8e7b6d6 commit a506bc4

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

itest/assets_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/btcsuite/btcd/chaincfg/chainhash"
1818
"github.com/btcsuite/btcd/wire"
1919
"github.com/davecgh/go-spew/spew"
20+
tapfn "github.com/lightninglabs/taproot-assets/fn"
2021
"github.com/lightninglabs/taproot-assets/itest"
2122
"github.com/lightninglabs/taproot-assets/proof"
2223
"github.com/lightninglabs/taproot-assets/rfq"
@@ -42,6 +43,7 @@ import (
4243
"github.com/lightningnetwork/lnd/macaroons"
4344
"github.com/lightningnetwork/lnd/record"
4445
"github.com/stretchr/testify/require"
46+
"golang.org/x/exp/maps"
4547
"google.golang.org/grpc"
4648
"google.golang.org/grpc/credentials"
4749
"google.golang.org/protobuf/proto"
@@ -1576,6 +1578,71 @@ func assertAssetBalance(t *testing.T, client *tapClient, assetID []byte,
15761578

15771579
t.Logf("Failed to assert expected balance of %d, current "+
15781580
"assets: %v", expectedBalance, toProtoJSON(t, r))
1581+
1582+
utxos, err3 := client.ListUtxos(ctxb, &taprpc.ListUtxosRequest{})
1583+
require.NoError(t, err3)
1584+
1585+
t.Logf("Current UTXOs: %v", toProtoJSON(t, utxos))
1586+
1587+
t.Fatalf("Failed to assert balance: %v", err)
1588+
}
1589+
}
1590+
1591+
// assertSpendableBalance differs from assertAssetBalance in that it asserts
1592+
// that the entire balance is spendable. We consider something spendable if we
1593+
// have a local script key for it.
1594+
func assertSpendableBalance(t *testing.T, client *tapClient, assetID []byte,
1595+
expectedBalance uint64) {
1596+
1597+
t.Helper()
1598+
1599+
ctxb := context.Background()
1600+
ctxt, cancel := context.WithTimeout(ctxb, shortTimeout)
1601+
defer cancel()
1602+
1603+
err := wait.NoError(func() error {
1604+
utxos, err := client.ListUtxos(ctxt, &taprpc.ListUtxosRequest{})
1605+
if err != nil {
1606+
return err
1607+
}
1608+
1609+
assets := tapfn.FlatMap(
1610+
maps.Values(utxos.ManagedUtxos),
1611+
func(utxo *taprpc.ManagedUtxo) []*taprpc.Asset {
1612+
return utxo.Assets
1613+
},
1614+
)
1615+
1616+
relevantAssets := fn.Filter(func(utxo *taprpc.Asset) bool {
1617+
return bytes.Equal(utxo.AssetGenesis.AssetId, assetID)
1618+
}, assets)
1619+
1620+
var assetSum uint64
1621+
for _, asset := range relevantAssets {
1622+
if asset.ScriptKeyIsLocal {
1623+
assetSum += asset.Amount
1624+
}
1625+
}
1626+
1627+
if assetSum != expectedBalance {
1628+
return fmt.Errorf("expected balance %d, got %d", expectedBalance,
1629+
assetSum)
1630+
}
1631+
1632+
return nil
1633+
}, shortTimeout)
1634+
if err != nil {
1635+
r, err2 := client.ListAssets(ctxb, &taprpc.ListAssetRequest{})
1636+
require.NoError(t, err2)
1637+
1638+
t.Logf("Failed to assert expected balance of %d, current "+
1639+
"assets: %v", expectedBalance, toProtoJSON(t, r))
1640+
1641+
utxos, err3 := client.ListUtxos(ctxb, &taprpc.ListUtxosRequest{})
1642+
require.NoError(t, err3)
1643+
1644+
t.Logf("Current UTXOs: %v", toProtoJSON(t, utxos))
1645+
15791646
t.Fatalf("Failed to assert balance: %v", err)
15801647
}
15811648
}

0 commit comments

Comments
 (0)