From aee4bb44e42b46903945039e1060a1b562ffa027 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Wed, 30 Oct 2024 13:59:52 -0700 Subject: [PATCH] Fix ADA temporarily out-dated balance bug --- CHANGELOG.md | 1 + src/cardano/CardanoEngine.ts | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c16829510..2fb245210 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- fixed: (ADA) Improved balance calculation by deriving from UTXO state. - fixed: (ADA) Race condition between network queries and local transaction processing causing incorrect UTXO state. ## 4.26.2 (2024-10-29) diff --git a/src/cardano/CardanoEngine.ts b/src/cardano/CardanoEngine.ts index 85664320c..e44fde5d1 100644 --- a/src/cardano/CardanoEngine.ts +++ b/src/cardano/CardanoEngine.ts @@ -238,14 +238,14 @@ export class CardanoEngine extends CurrencyEngine< }) const clean = asKoiosBalance(raw) - const mainnetBal = clean[0]?.balance ?? '0' - this.updateBalance(this.currencyInfo.currencyCode, mainnetBal) - // Merge unseen utxos into wallet state: const networkUtxos = clean[0]?.utxo_set ?? [] for (const utxo of networkUtxos) { this.addUnseenUtxo(utxo) } + + // Network balance may be out of date, so we'll calculate it from utxos: + this.updateBalanceFromUtxos(this.currencyInfo.currencyCode) } catch (e) { this.log.warn('queryBalance error: ', e) } @@ -534,6 +534,9 @@ export class CardanoEngine extends CurrencyEngine< }) } }) + + // Update balance incase the UTXO set changed: + this.updateBalanceFromUtxos(this.currencyInfo.currencyCode) } async signTx( @@ -635,6 +638,11 @@ export class CardanoEngine extends CurrencyEngine< ) } + private updateBalanceFromUtxos(currencyCode: string): void { + const balance = this.utxos.reduce((acc, utxo) => add(acc, utxo.value), '0') + this.updateBalance(currencyCode, balance) + } + getStakeAddress = async (): Promise => { const { bech32Address } = asSafeCardanoWalletInfo(this.walletInfo).keys