Move connection and codec into Faucet

This commit is contained in:
Simon Warta 2020-02-10 11:12:51 +01:00
parent 5f32796bfc
commit d378df6b8c
3 changed files with 32 additions and 14 deletions

View File

@ -72,10 +72,10 @@ export async function start(args: ReadonlyArray<string>): Promise<void> {
const distibutorIdentities = identitiesOfFirstWallet(profile).slice(1);
const faucet = new Faucet(constants.tokenConfig);
const faucet = new Faucet(constants.tokenConfig, connection, connector.codec);
await faucet.refill(profile, connection, connector.codec);
setInterval(async () => faucet.refill(profile, connection, connector.codec), 60_000); // ever 60 seconds
await faucet.refill(profile);
setInterval(async () => faucet.refill(profile), 60_000); // ever 60 seconds
console.info("Creating webserver ...");
const api = new Koa();

View File

@ -1,8 +1,16 @@
import { TokenConfiguration } from "@cosmwasm/bcp";
import { CosmWasmCodec, CosmWasmConnection, TokenConfiguration } from "@cosmwasm/bcp";
import { CosmosAddressBech32Prefix } from "@cosmwasm/sdk";
import { Faucet } from "./faucet";
const dummyConfig: TokenConfiguration = {
function pendingWithoutCosmos(): void {
if (!process.env.COSMOS_ENABLED) {
return pending("Set COSMOS_ENABLED to enable Cosmos node-based tests");
}
}
const httpUrl = "http://localhost:1317";
const defaultConfig: TokenConfiguration = {
bankTokens: [
{
ticker: "TOKENZ",
@ -18,12 +26,17 @@ const dummyConfig: TokenConfiguration = {
},
],
};
const defaultPrefix = "cosmos" as CosmosAddressBech32Prefix;
const codec = new CosmWasmCodec(defaultPrefix, defaultConfig.bankTokens);
describe("Faucet", () => {
describe("constructor", () => {
it("can be constructed", () => {
const faucet = new Faucet(dummyConfig);
it("can be constructed", async () => {
pendingWithoutCosmos();
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig);
const faucet = new Faucet(defaultConfig, connection, codec);
expect(faucet).toBeTruthy();
connection.disconnect();
});
});
});

View File

@ -18,17 +18,22 @@ export class Faucet {
/** will be private soon */
public readonly tokenManager: TokenManager;
public constructor(config: TokenConfiguration) {
private readonly connection: BlockchainConnection;
private readonly codec: TxCodec;
public constructor(config: TokenConfiguration, connection: BlockchainConnection, codec: TxCodec) {
this.tokenManager = new TokenManager(config);
this.connection = connection;
this.codec = codec;
}
public async refill(profile: UserProfile, connection: BlockchainConnection, codec: TxCodec): Promise<void> {
console.info(`Connected to network: ${connection.chainId()}`);
console.info(`Tokens on network: ${(await loadTokenTickers(connection)).join(", ")}`);
public async refill(profile: UserProfile): Promise<void> {
console.info(`Connected to network: ${this.connection.chainId()}`);
console.info(`Tokens on network: ${(await loadTokenTickers(this.connection)).join(", ")}`);
const holderIdentity = identitiesOfFirstWallet(profile)[0];
const accounts = await loadAccounts(profile, connection);
const accounts = await loadAccounts(profile, this.connection);
logAccountsState(accounts);
const holderAccount = accounts[0];
const distributorAccounts = accounts.slice(1);
@ -58,12 +63,12 @@ export class Faucet {
if (jobs.length > 0) {
for (const job of jobs) {
logSendJob(job);
await send(profile, connection, codec, job);
await send(profile, this.connection, this.codec, job);
await sleep(50);
}
console.info("Done refilling accounts.");
logAccountsState(await loadAccounts(profile, connection));
logAccountsState(await loadAccounts(profile, this.connection));
} else {
console.info("Nothing to be done. Anyways, thanks for checking.");
}