diff --git a/src/lib/NetworkClient.ts b/src/lib/NetworkClient.ts index 2480164b..9c311526 100644 --- a/src/lib/NetworkClient.ts +++ b/src/lib/NetworkClient.ts @@ -50,6 +50,17 @@ export class NetworkClient { return new Map(accounts.map((account, i) => [addresses[i], account.balance])); } + public async getStake(addresses: string[]) { + const client = await this.client; + const accounts = await client.getStakers(addresses); + return new Map(accounts.map((account, i) => [ + addresses[i], + account + ? account.balance + account.inactiveBalance + : 0, + ])); + } + public async getGenesisVestingContracts() { return [] as Array; // TODO } diff --git a/src/lib/WalletInfoCollector.ts b/src/lib/WalletInfoCollector.ts index 90a7f1c6..65cee03a 100644 --- a/src/lib/WalletInfoCollector.ts +++ b/src/lib/WalletInfoCollector.ts @@ -95,7 +95,8 @@ export default class WalletInfoCollector { const balances = await WalletInfoCollector._getBalances([singleAccount]); WalletInfoCollector._addAccounts(walletInfo, singleAccountAsArray, balances); onUpdate(walletInfo, []); - hasActivity = balances.get(singleAccount.address)! > 0 + const balance = balances.get(singleAccount.address)!; + hasActivity = balance.balance > 0 || balance.stake > 0 || (await WalletInfoCollector._networkInitializationPromise! .then(() => NetworkClient.Instance.requestTransactionReceipts(singleAccount.address, 1))) .length > 0; @@ -311,7 +312,7 @@ export default class WalletInfoCollector { const balances = await WalletInfoCollector._getBalances(accountsToCheck); for (const account of accountsToCheck) { const balance = balances.get(account.address); - if (balance !== undefined && balance !== 0) { + if (!!balance && (balance.balance !== 0 || balance.stake !== 0)) { foundAccounts.push(account); hasActivity = true; } @@ -475,20 +476,29 @@ export default class WalletInfoCollector { })); } - private static async _getBalances(accounts: BasicAccountInfo[]): Promise> { + private static async _getBalances( + accounts: BasicAccountInfo[], + ): Promise> { const userFriendlyAddresses = accounts.map((account) => account.address); await WalletInfoCollector._networkInitializationPromise; - const balances = await NetworkClient.Instance.getBalance(userFriendlyAddresses); + const result = new Map(); + const [balances, stakes] = await Promise.all([ + NetworkClient.Instance.getBalance(userFriendlyAddresses), + NetworkClient.Instance.getStake(userFriendlyAddresses), + ]); for (const [address, balance] of balances) { - balances.set(address, Nimiq.Policy.coinsToSatoshis(balance)); + result.set(address, { + balance, + stake: stakes.get(address) || 0, + }); } - return balances; + return result; } private static _addAccounts( walletInfo: WalletInfo, newAccounts: BasicAccountInfo[], - balances?: Map, + balances?: Map, ): void { for (const newAccount of newAccounts) { const existingAccountInfo = walletInfo.accounts.get(newAccount.address); @@ -501,7 +511,7 @@ export default class WalletInfoCollector { `${label}${labelCounter === 1 ? '' : ` ${labelCounter}`}`, Nimiq.Address.fromString(newAccount.address), ); - if (balance !== undefined) accountInfo.balance = balance; + if (!!balance) accountInfo.balance = balance.balance; walletInfo.accounts.set(newAccount.address, accountInfo); } }