diff --git a/src/api/aptos.ts b/src/api/aptos.ts index adcb435e1..810d47af5 100644 --- a/src/api/aptos.ts +++ b/src/api/aptos.ts @@ -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"; @@ -12,6 +13,8 @@ export class Aptos { readonly account: Account; + readonly faucet: Faucet; + readonly general: General; readonly transaction: Transaction; @@ -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, @@ -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"); diff --git a/src/api/faucet.ts b/src/api/faucet.ts index 0d1119fc3..17de12cf0 100644 --- a/src/api/faucet.ts +++ b/src/api/faucet.ts @@ -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> { + async fundAccount(args: { accountAddress: HexInput; amount: number; timeoutSecs?: number }): Promise { const txnStrings = await fundAccount({ aptosConfig: this.config, ...args }); return txnStrings; } diff --git a/src/internal/faucet.ts b/src/internal/faucet.ts index 7991d3a96..cf8f06bc1 100644 --- a/src/internal/faucet.ts +++ b/src/internal/faucet.ts @@ -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"; @@ -20,26 +20,22 @@ export async function fundAccount(args: { accountAddress: HexInput; amount: number; timeoutSecs?: number; -}): Promise> { +}): Promise { const { aptosConfig, accountAddress, amount } = args; const timeoutSecs = args.timeoutSecs ?? DEFAULT_TXN_TIMEOUT_SEC; - const { data } = await postAptosFaucet>({ + const { data } = await postAptosFaucet }>({ 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[] = []; - 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; } diff --git a/tests/e2e/api/faucet.test.ts b/tests/e2e/api/faucet.test.ts new file mode 100644 index 000000000..b00909e19 --- /dev/null +++ b/tests/e2e/api/faucet.test.ts @@ -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); + }); +});