Sort some code

This commit is contained in:
Simon Warta 2020-02-10 12:02:27 +01:00
parent 46ddff93ec
commit 6e53302d49
4 changed files with 23 additions and 32 deletions

View File

@ -1,6 +1,5 @@
import { ChainId } from "@iov/bcp";
import { Bip39, Random } from "@iov/crypto";
import { UserProfile } from "@iov/keycontrol";
import * as constants from "../constants";
import { setSecretAndCreateIdentities } from "../profile";
@ -17,6 +16,6 @@ export async function generate(args: ReadonlyArray<string>): Promise<void> {
const mnemonic = Bip39.encode(Random.getBytes(16)).toString();
console.info(`FAUCET_MNEMONIC="${mnemonic}"`);
const profile = new UserProfile();
await setSecretAndCreateIdentities(profile, mnemonic, chainId);
// Log the addresses
await setSecretAndCreateIdentities(mnemonic, chainId);
}

View File

@ -1,4 +1,3 @@
import { UserProfile } from "@iov/keycontrol";
import cors = require("@koa/cors");
import { createCosmWasmConnector } from "@cosmwasm/bcp";
import Koa from "koa";
@ -8,7 +7,7 @@ import { isValidAddress } from "../../addresses";
import * as constants from "../../constants";
import { logAccountsState, logSendJob } from "../../debugging";
import { Faucet } from "../../faucet";
import { availableTokensFromHolder, identitiesOfFirstWallet } from "../../multichainhelpers";
import { availableTokensFromHolder } from "../../multichainhelpers";
import { setSecretAndCreateIdentities } from "../../profile";
import { SendJob } from "../../types";
import { HttpError } from "./httperror";
@ -28,15 +27,8 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
);
}
// Connection
const blockchainBaseUrl = args[0];
const port = constants.port;
const profile = new UserProfile();
if (!constants.mnemonic) {
throw new Error("The FAUCET_MNEMONIC environment variable is not set");
}
const connector = createCosmWasmConnector(
blockchainBaseUrl,
constants.addressPrefix,
@ -44,20 +36,18 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
);
console.info(`Connecting to blockchain ${blockchainBaseUrl} ...`);
const connection = await connector.establishConnection();
console.info(`Connected to network: ${connection.chainId()}`);
const connectedChainId = connection.chainId();
console.info(`Connected to network: ${connectedChainId}`);
await setSecretAndCreateIdentities(profile, constants.mnemonic, connectedChainId);
// Profile
if (!constants.mnemonic) throw new Error("The FAUCET_MNEMONIC environment variable is not set");
const profile = await setSecretAndCreateIdentities(constants.mnemonic, connection.chainId());
// Faucet
const faucet = new Faucet(constants.tokenConfig, connection, connector.codec, profile);
const chainTokens = await faucet.loadTokenTickers();
console.info("Chain tokens:", chainTokens);
const accounts = await faucet.loadAccounts();
logAccountsState(accounts);
let availableTokens = availableTokensFromHolder(accounts[0]);
console.info("Available tokens:", availableTokens);
setInterval(async () => {
@ -66,8 +56,6 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
console.info("Available tokens:", availableTokens);
}, 60_000);
const distibutorIdentities = identitiesOfFirstWallet(profile).slice(1);
await faucet.refill();
setInterval(async () => faucet.refill(), 60_000); // ever 60 seconds
@ -92,7 +80,7 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
context.response.body = {
status: "ok",
nodeUrl: blockchainBaseUrl,
chainId: connectedChainId,
chainId: connection.chainId(),
chainTokens: chainTokens,
availableTokens: availableTokens,
holder: updatedAccounts[0],
@ -122,7 +110,7 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
throw new HttpError(422, `Token is not available. Available tokens are: ${tokens}`);
}
const sender = distibutorIdentities[getCount() % distibutorIdentities.length];
const sender = faucet.distributors[getCount() % faucet.distributors.length];
try {
const job: SendJob = {
@ -144,6 +132,7 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
// koa sends 404 by default
}
});
const port = constants.port;
console.info(`Starting webserver on port ${port} ...`);
api.listen(port);
}

View File

@ -2,6 +2,7 @@ import { TokenConfiguration } from "@cosmwasm/bcp";
import {
Account,
BlockchainConnection,
Identity,
isBlockInfoFailed,
isBlockInfoPending,
SendTransaction,
@ -20,6 +21,12 @@ import { SendJob } from "./types";
export class Faucet {
/** will be private soon */
public readonly tokenManager: TokenManager;
public get holder(): Identity {
return identitiesOfFirstWallet(this.profile)[0];
}
public get distributors(): readonly Identity[] {
return identitiesOfFirstWallet(this.profile).slice(1);
}
private readonly connection: BlockchainConnection;
private readonly codec: TxCodec;
@ -91,8 +98,6 @@ export class Faucet {
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 this.loadAccounts();
logAccountsState(accounts);
const holderAccount = accounts[0];
@ -113,7 +118,7 @@ export class Faucet {
);
for (const refillDistibutor of refillDistibutors) {
jobs.push({
sender: holderIdentity,
sender: this.holder,
recipient: refillDistibutor.address,
amount: this.tokenManager.refillAmount(token),
});

View File

@ -5,11 +5,8 @@ import { identityToAddress } from "./addresses";
import * as constants from "./constants";
import { debugPath } from "./hdpaths";
export async function setSecretAndCreateIdentities(
profile: UserProfile,
mnemonic: string,
chainId: ChainId,
): Promise<void> {
export async function setSecretAndCreateIdentities(mnemonic: string, chainId: ChainId): Promise<UserProfile> {
const profile = new UserProfile();
if (profile.wallets.value.length !== 0) {
throw new Error("Profile already contains wallets");
}
@ -27,4 +24,5 @@ export async function setSecretAndCreateIdentities(
const address = identityToAddress(identity);
console.info(`Created ${role} (${debugPath(path)}): ${address}`);
}
return profile;
}