Skip to content

Commit

Permalink
fix keyless end-to-end mainnet test to actually wait for funded account
Browse files Browse the repository at this point in the history
  • Loading branch information
alinush committed Jan 17, 2025
1 parent 2c4f327 commit 058b736
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- Add `AccountUtils` class to help with account serialization and deserialization
- Add `SingleKeySigner` interface which adds the ability to get the `AnyPublicKey` from a `SingleKeyAccount`
- We now throw an error earlier when you try to use the faucet with testnet or mainnet, rather than letting the call happen and then fail later.
- Fix the keyless end-to-end test to properly wait for the account to be funded

# 1.33.1 (2024-11-28)

Expand Down
41 changes: 27 additions & 14 deletions examples/typescript/keyless_mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import { AccountAddress, Aptos, AptosConfig, EphemeralKeyPair, Network } from "@aptos-labs/ts-sdk";
import * as readlineSync from "readline-sync";

const TRANSFER_AMOUNT = 10;
const TRANSFER_AMOUNT = 10; // octas
const MAX_GAS_UNITS = 200; // gas units
const GAS_UNIT_PRICE = 100; // octas / gas unit

/**
* Prints the balance of an account
Expand Down Expand Up @@ -58,30 +60,41 @@ const example = async () => {
console.log(`Alice's nonce is: ${aliceEphem.nonce}`);
console.log(`Alice's ephemeral public key is: ${aliceEphem.getPublicKey().toString()}`);

let aliceBalance;
try {
aliceBalance = await balance(aptos, alice.accountAddress);
console.log("\n=== Balances ===\n");
console.log(`Alice's balance is: ${aliceBalance}`);
} catch (error) {
// Example:
// Funded 0x3e42a237d4e6a504d1ce00fb12446be69cff8910e9e226e892558c094353c7dd with 0.03 APT and balance was 3,000,000
// After the transfer(-to-self) of 10 octas with max gas 200 (gas units), the balance was 2,996,000 because TXN took 40 gas units of 100 octas each => 4,000 octas
// https://explorer.aptoslabs.com/txn/0x52f6f117baa09b4afd50e3a1a77e89191a07bbf96ba7402211330eb510c62e72/userTxnOverview?network=mainnet
let aliceBalance = await balance(aptos, alice.accountAddress);
const minBalance = MAX_GAS_UNITS * GAS_UNIT_PRICE + TRANSFER_AMOUNT;
while (aliceBalance < minBalance) {
console.log("\n=== Fund the account ===\n");
console.log(`Address: ${alice.accountAddress}\n`);
console.log(
"The account does not yet exist. Send at least APT to the address below to create the account in order to proceed.\n",
`The account either does not exist or needs more than ${minBalance} octas balance. Fund the account to proceed.\n`,
);
console.log(`Address: ${alice.accountAddress}\n`);
console.log("Press enter once funded");
console.log("Press ENTER once funded...");
readlineSync.question("");
aliceBalance = await balance(aptos, alice.accountAddress);
console.log("\n=== Balances ===\n");
console.log(`Alice's balance is: ${aliceBalance}`);

try {
console.log("Refetching balance...\n");
// eslint-disable-next-line no-await-in-loop
aliceBalance = await balance(aptos, alice.accountAddress);
} catch (error) {
console.log(`Error fetching balance: ${error}\n`);
}
}
console.log("\n=== Balances ===\n");
console.log(`Alice's balance is: ${aliceBalance}`);

// Transfer to yourself to not waste APT
const transaction = await aptos.transferCoinTransaction({
sender: alice.accountAddress,
recipient: alice.accountAddress,
amount: TRANSFER_AMOUNT,
options: { maxGasAmount: 200 },
options: {
maxGasAmount: MAX_GAS_UNITS,
gasUnitPrice: GAS_UNIT_PRICE,
},
});

const committedTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction });
Expand Down

0 comments on commit 058b736

Please sign in to comment.