Skip to content

Commit

Permalink
Update the Faucet class and fix param naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin authored and gregnazario committed Oct 11, 2023
1 parent 335a60f commit 659c2f4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/api/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { Account } from "./account";
import { AptosConfig } from "./aptos_config";
import { Faucet } from "./faucet";
import { General } from "./general";
import { Transaction } from "./transaction";
import { TransactionSubmission } from "./transaction_submission";
Expand All @@ -12,6 +13,8 @@ export class Aptos {

readonly account: Account;

readonly faucet: Faucet;

readonly general: General;

readonly transaction: Transaction;
Expand All @@ -38,13 +41,14 @@ export class Aptos {
constructor(settings?: AptosConfig) {
this.config = new AptosConfig(settings);
this.account = new Account(this.config);
this.faucet = new Faucet(this.config);
this.general = new General(this.config);
this.transaction = new Transaction(this.config);
this.transactionSubmission = new TransactionSubmission(this.config);
}
}

export interface Aptos extends Account, General, Transaction, TransactionSubmission {}
export interface Aptos extends Account, Faucet, General, Transaction, TransactionSubmission {}

/**
In TypeScript, we can’t inherit or extend from more than one class,
Expand All @@ -68,6 +72,7 @@ function applyMixin(targetClass: any, baseClass: any, baseClassProp: string) {
}

applyMixin(Aptos, Account, "account");
applyMixin(Aptos, Faucet, "faucet");
applyMixin(Aptos, General, "general");
applyMixin(Aptos, Transaction, "transaction");
applyMixin(Aptos, TransactionSubmission, "transactionSubmission");
2 changes: 1 addition & 1 deletion src/api/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Faucet {
* @param timeoutSecs Timeout in seconds. Defaults to 20 seconds.
* @returns Hashes of submitted transactions
*/
async fundAccount(args: { accountAddress: HexInput; amount: number; timeoutSecs?: number }): Promise<Array<string>> {
async fundAccount(args: { accountAddress: HexInput; amount: number; timeoutSecs?: number }): Promise<string> {
const txnStrings = await fundAccount({ aptosConfig: this.config, ...args });
return txnStrings;
}
Expand Down
26 changes: 11 additions & 15 deletions src/internal/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { AptosConfig } from "../api/aptos_config";
import { postAptosFaucet } from "../client";
import { AccountAddress } from "../core";
import { HexInput, TransactionResponse } from "../types";
import { HexInput } from "../types";
import { DEFAULT_TXN_TIMEOUT_SEC } from "../utils/const";
import { waitForTransaction } from "./transaction";

Expand All @@ -20,26 +20,22 @@ export async function fundAccount(args: {
accountAddress: HexInput;
amount: number;
timeoutSecs?: number;
}): Promise<Array<string>> {
}): Promise<string> {
const { aptosConfig, accountAddress, amount } = args;
const timeoutSecs = args.timeoutSecs ?? DEFAULT_TXN_TIMEOUT_SEC;
const { data } = await postAptosFaucet<any, Array<string>>({
const { data } = await postAptosFaucet<any, { txn_hashes: Array<string> }>({
aptosConfig,
path: "mint",
params: {
accountAddress: AccountAddress.fromHexInput({
input: accountAddress,
}).toString(),
path: "fund",
body: {
address: AccountAddress.fromHexInput({ input: accountAddress }).toString(),
amount,
},
originMethod: "fundAccount",
});

const promises: Promise<TransactionResponse>[] = [];
for (let i = 0; i < data.length; i += 1) {
const txnHash = data[i];
promises.push(waitForTransaction({ aptosConfig, txnHash, extraArgs: { timeoutSecs } }));
}
await Promise.all(promises);
return data;
const txnHash = data.txn_hashes[0];

await waitForTransaction({ aptosConfig, txnHash, extraArgs: { timeoutSecs } });

return txnHash;
}
27 changes: 27 additions & 0 deletions tests/e2e/api/faucet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

import { Aptos } from "../../../src/api/aptos";
import { AptosConfig } from "../../../src/api/aptos_config";
import { Account } from "../../../src/core/account";
import { SigningScheme } from "../../../src/types";
import { Network } from "../../../src/utils/apiEndpoints";

describe("Faucet", () => {
test("it should fund an account", async () => {
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);
const testAccount = Account.generate({ scheme: SigningScheme.Ed25519 });

// Fund the account
await aptos.fundAccount({ accountAddress: testAccount.accountAddress.toString(), amount: 10_000_000 });

// Check the balance
let resource = await aptos.getAccountResource({
accountAddress: testAccount.accountAddress.toString(),
resourceType: "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
});
let amount = Number((resource.data as { coin: { value: string } }).coin.value);
expect(amount).toBe(10_000_000);
});
});

0 comments on commit 659c2f4

Please sign in to comment.