Skip to content

Commit 0567590

Browse files
authored
Always yield native balance in balances (#5308)
1 parent a3477de commit 0567590

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

ironfish/src/wallet/account/account.ts

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -942,40 +942,64 @@ export class Account {
942942
sequence: number | null
943943
}> {
944944
const head = await this.getHead()
945-
if (!head) {
946-
return
947-
}
948945

949-
const pendingByAsset = await this.getPendingDeltas(head.sequence, tx)
950-
const unconfirmedByAsset = await this.getUnconfirmedDeltas(head.sequence, confirmations, tx)
946+
let hasNative = false
951947

952-
for await (const { assetId, balance } of this.walletDb.getUnconfirmedBalances(this, tx)) {
953-
const { delta: unconfirmedDelta, count: unconfirmedCount } = unconfirmedByAsset.get(
954-
assetId,
955-
) ?? {
956-
delta: 0n,
957-
count: 0,
958-
}
948+
if (head) {
949+
const pendingByAsset = await this.getPendingDeltas(head.sequence, tx)
950+
const unconfirmedByAsset = await this.getUnconfirmedDeltas(
951+
head.sequence,
952+
confirmations,
953+
tx,
954+
)
959955

960-
const { delta: pendingDelta, count: pendingCount } = pendingByAsset.get(assetId) ?? {
961-
delta: 0n,
962-
count: 0,
963-
}
956+
for await (const { assetId, balance } of this.walletDb.getUnconfirmedBalances(this, tx)) {
957+
const { delta: unconfirmedDelta, count: unconfirmedCount } = unconfirmedByAsset.get(
958+
assetId,
959+
) ?? {
960+
delta: 0n,
961+
count: 0,
962+
}
963+
964+
const { delta: pendingDelta, count: pendingCount } = pendingByAsset.get(assetId) ?? {
965+
delta: 0n,
966+
count: 0,
967+
}
968+
969+
const { balance: available, noteCount: availableNoteCount } =
970+
await this.calculateAvailableBalance(head.sequence, assetId, confirmations, tx)
964971

965-
const { balance: available, noteCount: availableNoteCount } =
966-
await this.calculateAvailableBalance(head.sequence, assetId, confirmations, tx)
972+
if (!hasNative && Asset.nativeId().equals(assetId)) {
973+
hasNative = true
974+
}
967975

976+
yield {
977+
assetId,
978+
unconfirmed: balance.unconfirmed,
979+
unconfirmedCount,
980+
confirmed: balance.unconfirmed - unconfirmedDelta,
981+
pending: balance.unconfirmed + pendingDelta,
982+
pendingCount,
983+
available,
984+
availableNoteCount,
985+
blockHash: balance.blockHash,
986+
sequence: balance.sequence,
987+
}
988+
}
989+
}
990+
991+
if (!hasNative) {
968992
yield {
969-
assetId,
970-
unconfirmed: balance.unconfirmed,
971-
unconfirmedCount,
972-
confirmed: balance.unconfirmed - unconfirmedDelta,
973-
pending: balance.unconfirmed + pendingDelta,
974-
pendingCount,
975-
available,
976-
availableNoteCount,
977-
blockHash: balance.blockHash,
978-
sequence: balance.sequence,
993+
assetId: Asset.nativeId(),
994+
unconfirmed: 0n,
995+
unconfirmedCount: 0,
996+
confirmed: 0n,
997+
pending: 0n,
998+
pendingCount: 0,
999+
available: 0n,
1000+
availableNoteCount: 0,
1001+
blockHash: head?.hash ?? null,
1002+
sequence: head?.sequence ?? null,
9791003
}
9801004
}
9811005
}

0 commit comments

Comments
 (0)