|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/** |
| 4 | + * This example shows how to use the Aptos client to create accounts, fund them, and transfer between them. |
| 5 | + * Similar to ./simple_transfer.ts, but uses transferCoinTransaction to generate the transaction. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + Account, |
| 10 | + AccountAddress, |
| 11 | + Aptos, |
| 12 | + AptosConfig, |
| 13 | + Deserializer, |
| 14 | + Network, |
| 15 | + NetworkToNetworkName, |
| 16 | + U64, |
| 17 | +} from "@aptos-labs/ts-sdk"; |
| 18 | + |
| 19 | +const ALICE_INITIAL_BALANCE = 100_000_000; |
| 20 | +const BOB_INITIAL_BALANCE = 0; |
| 21 | +const TRANSFER_AMOUNT = 1_000_000; |
| 22 | + |
| 23 | +// Set up the client |
| 24 | +const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK ?? Network.DEVNET]; |
| 25 | +const config = new AptosConfig({ network: APTOS_NETWORK }); |
| 26 | +const aptos = new Aptos(config); |
| 27 | + |
| 28 | +/** |
| 29 | + * Prints the balance of an account |
| 30 | + * @param name |
| 31 | + * @param accountAddress |
| 32 | + * @returns {Promise<number>} |
| 33 | + * |
| 34 | + */ |
| 35 | +const balance = async (name: string, accountAddress: AccountAddress): Promise<number> => { |
| 36 | + const output = await aptos.experimental.viewBinary({ |
| 37 | + payload: { |
| 38 | + function: "0x1::coin::balance", |
| 39 | + typeArguments: ["0x1::aptos_coin::AptosCoin"], |
| 40 | + functionArguments: [accountAddress], |
| 41 | + }, |
| 42 | + }); |
| 43 | + const deser = new Deserializer(output); |
| 44 | + const [wrappedAmount] = deser.deserializeVector(U64); |
| 45 | + const amount = Number(wrappedAmount.value); |
| 46 | + |
| 47 | + console.log(`${name}'s balance is: ${amount}`); |
| 48 | + return amount; |
| 49 | +}; |
| 50 | + |
| 51 | +const example = async () => { |
| 52 | + console.log( |
| 53 | + "This example will create two accounts (Alice and Bob), fund Alice, and transfer between them using transferCoinTransaction.", |
| 54 | + ); |
| 55 | + |
| 56 | + // Create two accounts |
| 57 | + const alice = Account.generate(); |
| 58 | + const bob = Account.generate(); |
| 59 | + |
| 60 | + console.log("=== Addresses ===\n"); |
| 61 | + console.log(`Alice's address is: ${alice.accountAddress}`); |
| 62 | + console.log(`Bob's address is: ${bob.accountAddress}`); |
| 63 | + |
| 64 | + // Fund the accounts |
| 65 | + console.log("\n=== Funding accounts ===\n"); |
| 66 | + |
| 67 | + // Fund alice account |
| 68 | + await aptos.fundAccount({ |
| 69 | + accountAddress: alice.accountAddress, |
| 70 | + amount: ALICE_INITIAL_BALANCE, |
| 71 | + }); |
| 72 | + |
| 73 | + // Show the balances |
| 74 | + console.log("\n=== Initial Balances ===\n"); |
| 75 | + const aliceBalance = await balance("Alice", alice.accountAddress); |
| 76 | + const bobBalance = await balance("Bob", bob.accountAddress); |
| 77 | + |
| 78 | + if (aliceBalance !== ALICE_INITIAL_BALANCE) throw new Error("Alice's balance is incorrect"); |
| 79 | + if (bobBalance !== BOB_INITIAL_BALANCE) throw new Error("Bob's balance is incorrect"); |
| 80 | + |
| 81 | + // Transfer between users |
| 82 | + console.log(`\n=== Transfer ${TRANSFER_AMOUNT} from Alice to Bob ===\n`); |
| 83 | + const transaction = await aptos.transferCoinTransaction({ |
| 84 | + sender: alice.accountAddress, |
| 85 | + recipient: bob.accountAddress, |
| 86 | + amount: TRANSFER_AMOUNT, |
| 87 | + }); |
| 88 | + const pendingTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction }); |
| 89 | + const response = await aptos.waitForTransaction({ transactionHash: pendingTxn.hash }); |
| 90 | + console.log(`Committed transaction: ${response.hash}`); |
| 91 | + |
| 92 | + console.log("\n=== Balances after transfer ===\n"); |
| 93 | + const newAliceBalance = await balance("Alice", alice.accountAddress); |
| 94 | + const newBobBalance = await balance("Bob", bob.accountAddress); |
| 95 | + |
| 96 | + // Bob should have the transfer amount |
| 97 | + if (newBobBalance !== TRANSFER_AMOUNT + BOB_INITIAL_BALANCE) |
| 98 | + throw new Error("Bob's balance after transfer is incorrect"); |
| 99 | + |
| 100 | + // Alice should have the remainder minus gas |
| 101 | + if (newAliceBalance >= ALICE_INITIAL_BALANCE - TRANSFER_AMOUNT) |
| 102 | + throw new Error("Alice's balance after transfer is incorrect"); |
| 103 | +}; |
| 104 | + |
| 105 | +example(); |
0 commit comments