Skip to content

Commit c60c553

Browse files
Roasbeefguggero
authored andcommitted
itest: add assertSpendableBalance helper func
This will report balance that we can 100% spend.
1 parent 8754bcb commit c60c553

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

itest/assets_test.go

Lines changed: 66 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,70 @@ 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, assetSum)
1629+
}
1630+
1631+
return nil
1632+
}, shortTimeout)
1633+
if err != nil {
1634+
r, err2 := client.ListAssets(ctxb, &taprpc.ListAssetRequest{})
1635+
require.NoError(t, err2)
1636+
1637+
t.Logf("Failed to assert expected balance of %d, current "+
1638+
"assets: %v", expectedBalance, toProtoJSON(t, r))
1639+
1640+
utxos, err3 := client.ListUtxos(ctxb, &taprpc.ListUtxosRequest{})
1641+
require.NoError(t, err3)
1642+
1643+
t.Logf("Current UTXOs: %v", toProtoJSON(t, utxos))
1644+
15791645
t.Fatalf("Failed to assert balance: %v", err)
15801646
}
15811647
}

0 commit comments

Comments
 (0)