Move and test loadAccounts
This commit is contained in:
parent
accef527fe
commit
46ddff93ec
@ -8,7 +8,7 @@ import { isValidAddress } from "../../addresses";
|
||||
import * as constants from "../../constants";
|
||||
import { logAccountsState, logSendJob } from "../../debugging";
|
||||
import { Faucet } from "../../faucet";
|
||||
import { availableTokensFromHolder, identitiesOfFirstWallet, loadAccounts } from "../../multichainhelpers";
|
||||
import { availableTokensFromHolder, identitiesOfFirstWallet } from "../../multichainhelpers";
|
||||
import { setSecretAndCreateIdentities } from "../../profile";
|
||||
import { SendJob } from "../../types";
|
||||
import { HttpError } from "./httperror";
|
||||
@ -55,13 +55,13 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
|
||||
const chainTokens = await faucet.loadTokenTickers();
|
||||
console.info("Chain tokens:", chainTokens);
|
||||
|
||||
const accounts = await loadAccounts(profile, connection);
|
||||
const accounts = await faucet.loadAccounts();
|
||||
logAccountsState(accounts);
|
||||
|
||||
let availableTokens = availableTokensFromHolder(accounts[0]);
|
||||
console.info("Available tokens:", availableTokens);
|
||||
setInterval(async () => {
|
||||
const updatedAccounts = await loadAccounts(profile, connection);
|
||||
const updatedAccounts = await faucet.loadAccounts();
|
||||
availableTokens = availableTokensFromHolder(updatedAccounts[0]);
|
||||
console.info("Available tokens:", availableTokens);
|
||||
}, 60_000);
|
||||
@ -88,7 +88,7 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
|
||||
"See https://github.com/iov-one/iov-faucet for all further information.\n";
|
||||
break;
|
||||
case "/status": {
|
||||
const updatedAccounts = await loadAccounts(profile, connection);
|
||||
const updatedAccounts = await faucet.loadAccounts();
|
||||
context.response.body = {
|
||||
status: "ok",
|
||||
nodeUrl: blockchainBaseUrl,
|
||||
|
||||
@ -119,4 +119,20 @@ describe("Faucet", () => {
|
||||
connection.disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadAccounts", () => {
|
||||
it("works", async () => {
|
||||
pendingWithoutCosmos();
|
||||
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig);
|
||||
const { profile, holder } = await makeProfile();
|
||||
const faucet = new Faucet(defaultConfig, connection, codec, profile);
|
||||
const accounts = await faucet.loadAccounts();
|
||||
const expectedHolderAccount = await connection.getAccount({ pubkey: holder.pubkey });
|
||||
assert(expectedHolderAccount);
|
||||
expect(accounts).toEqual([
|
||||
{ address: expectedHolderAccount.address, balance: expectedHolderAccount.balance },
|
||||
]);
|
||||
connection.disconnect();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { TokenConfiguration } from "@cosmwasm/bcp";
|
||||
import {
|
||||
Account,
|
||||
BlockchainConnection,
|
||||
isBlockInfoFailed,
|
||||
isBlockInfoPending,
|
||||
@ -10,8 +11,9 @@ import {
|
||||
import { UserProfile } from "@iov/keycontrol";
|
||||
import { sleep } from "@iov/utils";
|
||||
|
||||
import { identityToAddress } from "./addresses";
|
||||
import { debugAccount, logAccountsState, logSendJob } from "./debugging";
|
||||
import { availableTokensFromHolder, identitiesOfFirstWallet, loadAccounts } from "./multichainhelpers";
|
||||
import { availableTokensFromHolder, identitiesOfFirstWallet } from "./multichainhelpers";
|
||||
import { TokenManager } from "./tokenmanager";
|
||||
import { SendJob } from "./types";
|
||||
|
||||
@ -63,13 +65,35 @@ export class Faucet {
|
||||
return (await this.connection.getAllTokens()).map(token => token.tokenTicker);
|
||||
}
|
||||
|
||||
public async loadAccounts(): Promise<ReadonlyArray<Pick<Account, "address" | "balance">>> {
|
||||
const addresses = identitiesOfFirstWallet(this.profile).map(identity => identityToAddress(identity));
|
||||
|
||||
const out: Account[] = [];
|
||||
for (const address of addresses) {
|
||||
const response = await this.connection.getAccount({ address: address });
|
||||
if (response) {
|
||||
out.push({
|
||||
address: response.address,
|
||||
balance: response.balance,
|
||||
});
|
||||
} else {
|
||||
out.push({
|
||||
address: address,
|
||||
balance: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
public async refill(): Promise<void> {
|
||||
console.info(`Connected to network: ${this.connection.chainId()}`);
|
||||
console.info(`Tokens on network: ${(await this.loadTokenTickers()).join(", ")}`);
|
||||
|
||||
const holderIdentity = identitiesOfFirstWallet(this.profile)[0];
|
||||
|
||||
const accounts = await loadAccounts(this.profile, this.connection);
|
||||
const accounts = await this.loadAccounts();
|
||||
logAccountsState(accounts);
|
||||
const holderAccount = accounts[0];
|
||||
const distributorAccounts = accounts.slice(1);
|
||||
@ -103,7 +127,7 @@ export class Faucet {
|
||||
}
|
||||
|
||||
console.info("Done refilling accounts.");
|
||||
logAccountsState(await loadAccounts(this.profile, this.connection));
|
||||
logAccountsState(await this.loadAccounts());
|
||||
} else {
|
||||
console.info("Nothing to be done. Anyways, thanks for checking.");
|
||||
}
|
||||
|
||||
@ -1,38 +1,11 @@
|
||||
import { Account, BlockchainConnection, Identity, TokenTicker } from "@iov/bcp";
|
||||
import { Account, Identity, TokenTicker } from "@iov/bcp";
|
||||
import { UserProfile } from "@iov/keycontrol";
|
||||
|
||||
import { identityToAddress } from "./addresses";
|
||||
|
||||
export function identitiesOfFirstWallet(profile: UserProfile): ReadonlyArray<Identity> {
|
||||
const wallet = profile.wallets.value[0];
|
||||
return profile.getIdentities(wallet.id);
|
||||
}
|
||||
|
||||
export async function loadAccounts(
|
||||
profile: UserProfile,
|
||||
connection: BlockchainConnection,
|
||||
): Promise<ReadonlyArray<Account>> {
|
||||
const addresses = identitiesOfFirstWallet(profile).map(identity => identityToAddress(identity));
|
||||
|
||||
const out: Account[] = [];
|
||||
for (const address of addresses) {
|
||||
const response = await connection.getAccount({ address: address });
|
||||
if (response) {
|
||||
out.push({
|
||||
address: response.address,
|
||||
balance: response.balance,
|
||||
});
|
||||
} else {
|
||||
out.push({
|
||||
address: address,
|
||||
balance: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
export function availableTokensFromHolder(holderAccount: Account): ReadonlyArray<TokenTicker> {
|
||||
return holderAccount.balance.map(coin => coin.tokenTicker);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user