From 8e1c2e701292c443d19fb0e0c1e869a8643b3559 Mon Sep 17 00:00:00 2001 From: Greg Nazario Date: Wed, 11 Oct 2023 08:47:26 -0700 Subject: [PATCH] [examples] Add a simple coin transfer for javascript example --- .../javascript/simple_transfer/package.json | 15 +++ .../javascript/simple_transfer/pnpm-lock.yaml | 10 ++ .../javascript/simple_transfer/transfer.js | 99 +++++++++++++++++++ package.json | 4 +- 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 examples/javascript/simple_transfer/package.json create mode 100644 examples/javascript/simple_transfer/pnpm-lock.yaml create mode 100644 examples/javascript/simple_transfer/transfer.js diff --git a/examples/javascript/simple_transfer/package.json b/examples/javascript/simple_transfer/package.json new file mode 100644 index 000000000..b48338321 --- /dev/null +++ b/examples/javascript/simple_transfer/package.json @@ -0,0 +1,15 @@ +{ + "name": "js-test", + "version": "1.0.0", + "description": "", + "main": "transfer.js", + "scripts": { + "test": "node transfer.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "aptos": "link:../../.." + } +} diff --git a/examples/javascript/simple_transfer/pnpm-lock.yaml b/examples/javascript/simple_transfer/pnpm-lock.yaml new file mode 100644 index 000000000..4c74acf89 --- /dev/null +++ b/examples/javascript/simple_transfer/pnpm-lock.yaml @@ -0,0 +1,10 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: false + excludeLinksFromLockfile: false + +dependencies: + aptos: + specifier: link:../../.. + version: link:../../.. diff --git a/examples/javascript/simple_transfer/transfer.js b/examples/javascript/simple_transfer/transfer.js new file mode 100644 index 000000000..e8d13439c --- /dev/null +++ b/examples/javascript/simple_transfer/transfer.js @@ -0,0 +1,99 @@ +/** + * This example shows how to use the Aptos client to create accounts, fund them, and transfer between them. + */ + +const { Account, AccountAddress, Aptos, AptosConfig, Network, TypeTagStruct, StructTag, U64 } = require("aptos"); + +const APTOS_COIN = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>"; +const ALICE_INITIAL_BALANCE = 100_000_000; +const BOB_INITIAL_BALANCE = 100; +const TRANSFER_AMOUNT = 0; + +/** + * Prints the balance of an account + * @param aptos + * @param name + * @param address + * @returns {Promise<*>} + * + * TODO: Change to AccountAddress for address + */ +const balance = async (aptos, name, address) => { + let balance = await aptos.getAccountResource({ accountAddress: address, resourceType: APTOS_COIN }); + + let amount = Number(balance.data.coin.value); + + console.log(`${name}'s balance is: ${amount}`); + return amount; +}; + +const example = async () => { + console.log("This example will create two accounts (Alice and Bob), fund them, and transfer between them."); + + // Setup the client + const config = new AptosConfig({ network: Network.DEVNET }); + const aptos = new Aptos(config); + + // Create two accounts + let alice = Account.generate({ scheme: 0 }); + let bob = Account.generate({ scheme: 0 }); + + console.log("=== Addresses ===\n"); + console.log(`Alice's address is: ${alice.accountAddress.toString()}`); + console.log(`Bob's address is: ${bob.accountAddress.toString()}`); + + // Fund the accounts + console.log("\n=== Funding accounts ===\n"); + + const aliceFundTxn = await aptos.faucet.fundAccount({ + accountAddress: alice.accountAddress.toUint8Array(), + amount: ALICE_INITIAL_BALANCE, + }); + console.log("Alice's fund transaction: ", aliceFundTxn); + + const bobFundTxn = await aptos.faucet.fundAccount({ + accountAddress: bob.accountAddress.toUint8Array(), + amount: BOB_INITIAL_BALANCE, + }); + console.log("Bob's fund transaction: ", bobFundTxn); + + // Show the balances + console.log("\n=== Balances ===\n"); + let aliceBalance = await balance(aptos, "Alice", alice.accountAddress.toUint8Array()); + let bobBalance = await balance(aptos, "Bob", bob.accountAddress.toUint8Array()); + + if (aliceBalance !== ALICE_INITIAL_BALANCE) throw new Error("Alice's balance is incorrect"); + if (bobBalance !== BOB_INITIAL_BALANCE) throw new Error("Bob's balance is incorrect"); + + // Transfer between users + const txn = await aptos.transactionSubmission.generateTransaction({ + sender: alice.accountAddress.toString(), + data: { + function: "0x1::coin::transfer", + type_arguments: [new TypeTagStruct(StructTag.fromString("0x1::aptos_coin::AptosCoin"))], + arguments: [AccountAddress.fromHexInput({ input: bob.accountAddress.toString() }), new U64(TRANSFER_AMOUNT)], + }, + }); + + console.log("\n=== Transfer transaction ===\n"); + let signature = aptos.transactionSubmission.signTransaction({ signer: alice, transaction: txn }); + const committedTxn = await aptos.transactionSubmission.submitTransaction({ + transaction: txn, + senderAuthenticator: signature, + }); + console.log(`Committed transaction: ${committedTxn.hash}`); + + console.log("\n=== Balances after transfer ===\n"); + let newAliceBalance = await balance(aptos, "Alice", alice.accountAddress.toUint8Array()); + let newBobBalance = await balance(aptos, "Bob", bob.accountAddress.toUint8Array()); + + // Bob should have the transfer amount + if (newBobBalance !== TRANSFER_AMOUNT + BOB_INITIAL_BALANCE) + throw new Error("Bob's balance after transfer is incorrect"); + + // Alice should have the remainder minus gas + if (newAliceBalance < ALICE_INITIAL_BALANCE - TRANSFER_AMOUNT) + throw new Error("Alice's balance after transfer is incorrect"); +}; + +example(); diff --git a/package.json b/package.json index 76048f6ca..d2dac63ed 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,9 @@ "_build:browser": "tsup src/index.ts --platform browser --format iife --global-name aptosSDK --minify --out-dir dist/browser", "_build:node": "tsup src/index.ts --platform node --format esm,cjs --dts --out-dir dist/node", "_build:types": "tsup src/types/index.ts --dts --out-dir dist/types", - "_fmt": "prettier 'src/**/*.ts' 'tests/**/*.ts' '.eslintrc.js'", + "_fmt": "prettier 'src/**/*.ts' 'tests/**/*.ts' 'examples/**/*.js' '.eslintrc.js'", "fmt": "pnpm _fmt --write", - "lint": "eslint \"**/*.ts\"", + "lint": "eslint '**/*.ts'", "test": "pnpm jest", "unit-test": "pnpm jest tests/unit", "e2e-test": "pnpm jest tests/e2e",