Skip to content

Commit 730a7c8

Browse files
committed
itest: add assertSpendableBalance helper func
This will report balance that we can 100% spend.
1 parent d5262c4 commit 730a7c8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

itest/assets_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,60 @@ func assertAssetBalance(t *testing.T, client *tapClient, assetID []byte,
13491349

13501350
t.Logf("Failed to assert expected balance of %d, current "+
13511351
"assets: %v", expectedBalance, toProtoJSON(t, r))
1352+
// assertSpendableBalance differs from assertAssetBalance in that it asserts
1353+
// that the entire balance is spendable. We consider something spendable if we
1354+
// have a local script key for it.
1355+
func assertSpendableBalance(t *testing.T, client *tapClient, assetID []byte,
1356+
expectedBalance uint64) {
1357+
1358+
t.Helper()
1359+
1360+
ctxb := context.Background()
1361+
ctxt, cancel := context.WithTimeout(ctxb, shortTimeout)
1362+
defer cancel()
1363+
1364+
err := wait.NoError(func() error {
1365+
utxos, err := client.ListUtxos(ctxt, &taprpc.ListUtxosRequest{})
1366+
if err != nil {
1367+
return err
1368+
}
1369+
1370+
assets := tapfn.FlatMap(
1371+
maps.Values(utxos.ManagedUtxos),
1372+
func(utxo *taprpc.ManagedUtxo) []*taprpc.Asset {
1373+
return utxo.Assets
1374+
},
1375+
)
1376+
1377+
relevantAssets := fn.Filter(func(utxo *taprpc.Asset) bool {
1378+
return bytes.Equal(utxo.AssetGenesis.AssetId, assetID)
1379+
}, assets)
1380+
1381+
var assetSum uint64
1382+
for _, asset := range relevantAssets {
1383+
if asset.ScriptKeyIsLocal {
1384+
assetSum += asset.Amount
1385+
}
1386+
}
1387+
1388+
if assetSum != expectedBalance {
1389+
return fmt.Errorf("expected balance %d, got %d", expectedBalance, assetSum)
1390+
}
1391+
1392+
return nil
1393+
}, shortTimeout)
1394+
if err != nil {
1395+
r, err2 := client.ListAssets(ctxb, &taprpc.ListAssetRequest{})
1396+
require.NoError(t, err2)
1397+
1398+
t.Logf("Failed to assert expected balance of %d, current "+
1399+
"assets: %v", expectedBalance, toProtoJSON(t, r))
1400+
1401+
utxos, err3 := client.ListUtxos(ctxb, &taprpc.ListUtxosRequest{})
1402+
require.NoError(t, err3)
1403+
1404+
t.Logf("Current UTXOs: %v", toProtoJSON(t, utxos))
1405+
13521406
t.Fatalf("Failed to assert balance: %v", err)
13531407
}
13541408
}

0 commit comments

Comments
 (0)