Skip to content

Commit

Permalink
Also find addresses during collection that are fully staked
Browse files Browse the repository at this point in the history
Their account balance is 0, but their stake is > 0.
  • Loading branch information
sisou committed Nov 22, 2023
1 parent 557c889 commit ee39208
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/lib/NetworkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlainVestingContract & { address: string }>; // TODO
}
Expand Down
26 changes: 18 additions & 8 deletions src/lib/WalletInfoCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -475,20 +476,29 @@ export default class WalletInfoCollector {
}));
}

private static async _getBalances(accounts: BasicAccountInfo[]): Promise<Map<string, number>> {
private static async _getBalances(
accounts: BasicAccountInfo[],
): Promise<Map<string, {balance: number, stake: number}>> {
const userFriendlyAddresses = accounts.map((account) => account.address);
await WalletInfoCollector._networkInitializationPromise;
const balances = await NetworkClient.Instance.getBalance(userFriendlyAddresses);
const result = new Map<string, {balance: number, stake: number}>();
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<string, number>,
balances?: Map<string, {balance: number, stake: number}>,
): void {
for (const newAccount of newAccounts) {
const existingAccountInfo = walletInfo.accounts.get(newAccount.address);
Expand All @@ -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);
}
}
Expand Down

0 comments on commit ee39208

Please sign in to comment.