From 85cd71ddfebe36e0e42b555cc21f7ccf25d9b09f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 10 Feb 2020 11:35:13 +0100 Subject: [PATCH] Move profile into faucet --- packages/faucet/src/actions/start/start.ts | 8 ++++---- packages/faucet/src/faucet.spec.ts | 7 ++++--- packages/faucet/src/faucet.ts | 23 ++++++++++++++-------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/faucet/src/actions/start/start.ts b/packages/faucet/src/actions/start/start.ts index 68d73210..29b6027a 100644 --- a/packages/faucet/src/actions/start/start.ts +++ b/packages/faucet/src/actions/start/start.ts @@ -71,10 +71,10 @@ export async function start(args: ReadonlyArray): Promise { const distibutorIdentities = identitiesOfFirstWallet(profile).slice(1); - const faucet = new Faucet(constants.tokenConfig, connection, connector.codec); + const faucet = new Faucet(constants.tokenConfig, connection, connector.codec, profile); - await faucet.refill(profile); - setInterval(async () => faucet.refill(profile), 60_000); // ever 60 seconds + await faucet.refill(); + setInterval(async () => faucet.refill(), 60_000); // ever 60 seconds console.info("Creating webserver ..."); const api = new Koa(); @@ -136,7 +136,7 @@ export async function start(args: ReadonlyArray): Promise { amount: faucet.tokenManager.creditAmount(ticker), }; logSendJob(job); - await faucet.send(profile, job); + await faucet.send(job); } catch (e) { console.error(e); throw new HttpError(500, "Sending tokens failed"); diff --git a/packages/faucet/src/faucet.spec.ts b/packages/faucet/src/faucet.spec.ts index 2b9029d4..fe5610b7 100644 --- a/packages/faucet/src/faucet.spec.ts +++ b/packages/faucet/src/faucet.spec.ts @@ -72,7 +72,8 @@ describe("Faucet", () => { it("can be constructed", async () => { pendingWithoutCosmos(); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); - const faucet = new Faucet(defaultConfig, connection, codec); + const { profile } = await makeProfile(); + const faucet = new Faucet(defaultConfig, connection, codec, profile); expect(faucet).toBeTruthy(); connection.disconnect(); }); @@ -83,9 +84,9 @@ describe("Faucet", () => { pendingWithoutCosmos(); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); const { profile, holder } = await makeProfile(); - const faucet = new Faucet(defaultConfig, connection, codec); + const faucet = new Faucet(defaultConfig, connection, codec, profile); const recipient = makeRandomAddress(); - await faucet.send(profile, { + await faucet.send({ amount: { quantity: "23456", fractionalDigits: 6, diff --git a/packages/faucet/src/faucet.ts b/packages/faucet/src/faucet.ts index 862320a6..ac8f1b8d 100644 --- a/packages/faucet/src/faucet.ts +++ b/packages/faucet/src/faucet.ts @@ -25,17 +25,24 @@ export class Faucet { private readonly connection: BlockchainConnection; private readonly codec: TxCodec; + private readonly profile: UserProfile; - public constructor(config: TokenConfiguration, connection: BlockchainConnection, codec: TxCodec) { + public constructor( + config: TokenConfiguration, + connection: BlockchainConnection, + codec: TxCodec, + profile: UserProfile, + ) { this.tokenManager = new TokenManager(config); this.connection = connection; this.codec = codec; + this.profile = profile; } /** * Creates and posts a send transaction. Then waits until the transaction is in a block. */ - public async send(profile: UserProfile, job: SendJob): Promise { + public async send(job: SendJob): Promise { const sendWithFee = await this.connection.withDefaultFee({ kind: "bcp/send", chainId: this.connection.chainId(), @@ -47,7 +54,7 @@ export class Faucet { }); const nonce = await this.connection.getNonce({ pubkey: job.sender.pubkey }); - const signed = await profile.signTransaction(job.sender, sendWithFee, this.codec, nonce); + const signed = await this.profile.signTransaction(job.sender, sendWithFee, this.codec, nonce); const post = await this.connection.postTx(this.codec.bytesToPost(signed)); const blockInfo = await post.blockInfo.waitFor(info => !isBlockInfoPending(info)); @@ -56,13 +63,13 @@ export class Faucet { } } - public async refill(profile: UserProfile): Promise { + public async refill(): Promise { console.info(`Connected to network: ${this.connection.chainId()}`); console.info(`Tokens on network: ${(await loadTokenTickers(this.connection)).join(", ")}`); - const holderIdentity = identitiesOfFirstWallet(profile)[0]; + const holderIdentity = identitiesOfFirstWallet(this.profile)[0]; - const accounts = await loadAccounts(profile, this.connection); + const accounts = await loadAccounts(this.profile, this.connection); logAccountsState(accounts); const holderAccount = accounts[0]; const distributorAccounts = accounts.slice(1); @@ -91,12 +98,12 @@ export class Faucet { if (jobs.length > 0) { for (const job of jobs) { logSendJob(job); - await this.send(profile, job); + await this.send(job); await sleep(50); } console.info("Done refilling accounts."); - logAccountsState(await loadAccounts(profile, this.connection)); + logAccountsState(await loadAccounts(this.profile, this.connection)); } else { console.info("Nothing to be done. Anyways, thanks for checking."); }