Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDKv2] Update the Faucet class and fix param naming #10

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this I see is why you don't want to pass the string, and instead the account address, we'll have to review this whole input strategy afterwards

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Including the args as input param on some places, we can revisit after Alpha :)


// 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);
});
});
Loading