Skip to content

Commit

Permalink
Fix ADA temporarily out-dated balance bug
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes committed Oct 30, 2024
1 parent 824ce4a commit aee4bb4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions src/cardano/CardanoEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -534,6 +534,9 @@ export class CardanoEngine extends CurrencyEngine<
})
}
})

// Update balance incase the UTXO set changed:
this.updateBalanceFromUtxos(this.currencyInfo.currencyCode)
}

async signTx(
Expand Down Expand Up @@ -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<string> => {
const { bech32Address } = asSafeCardanoWalletInfo(this.walletInfo).keys

Expand Down

0 comments on commit aee4bb4

Please sign in to comment.