Skip to content

Commit

Permalink
[examples] Cleanup TODOs in examples
Browse files Browse the repository at this point in the history
Cleans up some warnings and todos around missing pieces in the examples.

Additionally, ensures all examples run during testing
  • Loading branch information
gregnazario committed Jan 21, 2024
1 parent ed792bd commit 2bd7f6b
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 55 deletions.
4 changes: 2 additions & 2 deletions examples/javascript/simple_transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const {
Network,
AccountAddress,
U64,
APTOS_COIN,
} = require("@aptos-labs/ts-sdk");

const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const COIN_STORE = `0x1::coin::CoinStore<${APTOS_COIN}>`;
const ALICE_INITIAL_BALANCE = 100_000_000;
const BOB_INITIAL_BALANCE = 100;
Expand All @@ -22,7 +22,7 @@ const APTOS_NETWORK = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network

/**
* Prints the balance of an account
* @param aptos
* @param sdk
* @param name
* @param address
* @returns {Promise<*>}
Expand Down
6 changes: 3 additions & 3 deletions examples/typescript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"_build:esm": "tsup simple_transfer.ts simple_sponsored_transaction.ts simple_digital_asset.ts multisig_v2.ts --platform node --format esm --dts --out-dir dist",
"simple_transfer": "ts-node --esm dist/simple_transfer.js",
"simple_sponsored_transaction": "ts-node --esm dist/simple_sponsored_transaction.js",
"simple_digital_asset": "pnpm run build && ts-node --esm dist/simple_digital_asset.js",
"multisig_v2": "pnpm run build && ts-node --esm dist/multisig_v2.js",
"test": "run-s build simple_transfer simple_sponsored_transaction"
"simple_digital_asset": "ts-node --esm dist/simple_digital_asset.js",
"multisig_v2": "ts-node --esm dist/multisig_v2.js",
"test": "run-s build simple_transfer simple_sponsored_transaction simple_digital_asset multisig_v2"
},
"keywords": [],
"author": "",
Expand Down
8 changes: 3 additions & 5 deletions examples/typescript-esm/simple_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
import {
Account,
AccountAddress,
Aptos,
Aptos, APTOS_COIN,
AptosConfig,
Network,
NetworkToNetworkName,
parseTypeTag,
} from "@aptos-labs/ts-sdk";

// TODO: There currently isn't a way to use the APTOS_COIN in the COIN_STORE due to a regex
const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const COIN_STORE = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
const COIN_STORE = `0x1::coin::CoinStore<${APTOS_COIN}>`;
const ALICE_INITIAL_BALANCE = 100_000_000;
const BOB_INITIAL_BALANCE = 100;
const TRANSFER_AMOUNT = 100;
Expand All @@ -29,7 +27,7 @@ const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] |
* @param aptos
* @param name
* @param address
* @returns {Promise<*>}
* @returns {Promise<number>}
*
*/
const balance = async (aptos: Aptos, name: string, address: AccountAddress) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript-esm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"declarationMap": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"module": "ESNext",
"module": "ES2020",
"moduleResolution": "bundler",
"noImplicitAny": true,
"outDir": "./dist",
"sourceMap": true,
"target": "es2020",
"target": "ES2020",
"pretty": true
},
"include": [
Expand Down
13 changes: 7 additions & 6 deletions examples/typescript/batch_funds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
NetworkToNetworkName,
UserTransactionResponse,
} from "@aptos-labs/ts-sdk";
import { nowSeconds } from "./utils";

const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.DEVNET;

Expand All @@ -39,7 +40,7 @@ async function main() {
const transactionsCount = 10;
const totalTransactions = accountsCount * transactionsCount;

const start = Date.now() / 1000; // current time in seconds
const start = nowSeconds();

console.log("starting...");
// create senders and recipients accounts
Expand All @@ -52,7 +53,7 @@ async function main() {
for (const sender of senders) {
console.log(sender.accountAddress.toString());
}
let last = Date.now() / 1000;
let last = nowSeconds();
console.log(
`${senders.length} sender accounts and ${recipients.length} recipient accounts created in ${last - start} seconds`,
);
Expand All @@ -68,8 +69,8 @@ async function main() {

await Promise.all(funds);

console.log(`${funds.length} sender accounts funded in ${Date.now() / 1000 - last} seconds`);
last = Date.now() / 1000;
console.log(`${funds.length} sender accounts funded in ${nowSeconds() - last} seconds`);
last = nowSeconds();

// read sender accounts
const balances: Array<Promise<AccountData>> = [];
Expand All @@ -78,8 +79,8 @@ async function main() {
}
await Promise.all(balances);

console.log(`${balances.length} sender account balances checked in ${Date.now() / 1000 - last} seconds`);
last = Date.now() / 1000;
console.log(`${balances.length} sender account balances checked in ${nowSeconds() - last} seconds`);
last = nowSeconds();

// create transactions
const payloads: InputGenerateTransactionPayloadData[] = [];
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/batch_mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function main() {
creator: sender,
description: "Batch Collection",
name: COLLECTION_NAME,
uri: "http://aptos.dev",
uri: "https://aptos.dev",
});
const pendingTxn = await aptos.signAndSubmitTransaction({ signer: sender, transaction });
await aptos.waitForTransaction({ transactionHash: pendingTxn.hash });
Expand All @@ -67,7 +67,7 @@ async function main() {
COLLECTION_NAME,
`my ${i} token description`,
`my ${i} token`,
"http://aptos.dev/nft",
"https://aptos.dev/nft",
[],
[],
[],
Expand Down
5 changes: 2 additions & 3 deletions examples/typescript/custom_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/
import "dotenv";
import { Aptos, AptosConfig, ClientResponse, ClientRequest, Network, NetworkToNetworkName } from "@aptos-labs/ts-sdk";
// eslint-disable-next-line import/no-commonjs
const superagent = require("superagent");
import superagent from "superagent";

// Default to devnet, but allow for overriding
const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK] || Network.DEVNET;
Expand Down Expand Up @@ -60,7 +59,7 @@ export async function superagentCustomClient<Req, Res>(
method,
};

const response = await superagent.get(`${url}?${params}`, request);
const response = (await superagent.get(`${url}?${params}`, request as any)) as any;
return {
status: response.status,
statusText: response.statusText,
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/external_signing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AccountAuthenticator,
AccountAuthenticatorEd25519,
Aptos,
APTOS_COIN,
AptosConfig,
Deserializer,
Ed25519PrivateKey,
Expand All @@ -22,8 +23,7 @@ import {
} from "@aptos-labs/ts-sdk";
import nacl from "tweetnacl";

const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const COIN_STORE = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
const COIN_STORE = `0x1::coin::CoinStore<${APTOS_COIN}>`;
const COLD_INITIAL_BALANCE = 100_000_000;
const HOT_INITIAL_BALANCE = 100;
const TRANSFER_AMOUNT = 100;
Expand Down
5 changes: 2 additions & 3 deletions examples/typescript/multi_agent_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import {
parseTypeTag,
Network,
NetworkToNetworkName,
APTOS_COIN,
} from "@aptos-labs/ts-sdk";

// TODO: There currently isn't a way to use the APTOS_COIN in the COIN_STORE due to a regex
const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const COIN_STORE = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
const COIN_STORE = `0x1::coin::CoinStore<${APTOS_COIN}>`;
const ALICE_INITIAL_BALANCE = 100_000_000;
const BOB_INITIAL_BALANCE = 100_000_000;
const TRANSFER_AMOUNT = 10;
Expand Down
3 changes: 2 additions & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.3.1",
"dotenv": "^16.3.2",
"npm-run-all": "latest",
"superagent": "^8.1.2",
"tweetnacl": "^1.0.3"
Expand All @@ -30,6 +30,7 @@
},
"devDependencies": {
"@types/node": "latest",
"@types/superagent": "^8.1.1",
"ts-node": "latest",
"typescript": "latest"
}
Expand Down
49 changes: 34 additions & 15 deletions examples/typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions examples/typescript/simple_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
* This example shows how to use the Aptos client to create accounts, fund them, and transfer between them.
*/

import { Account, AccountAddress, Aptos, AptosConfig, Network, NetworkToNetworkName } from "@aptos-labs/ts-sdk";

// TODO: There currently isn't a way to use the APTOS_COIN in the COIN_STORE due to a regex
const APTOS_COIN = "0x1::aptos_coin::AptosCoin";
const COIN_STORE = "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>";
import {
Account,
AccountAddress,
Aptos,
APTOS_COIN,
AptosConfig,
Network,
NetworkToNetworkName,
} from "@aptos-labs/ts-sdk";

const COIN_STORE = `0x1::coin::CoinStore<${APTOS_COIN}>`;
const ALICE_INITIAL_BALANCE = 100_000_000;
const BOB_INITIAL_BALANCE = 100;
const TRANSFER_AMOUNT = 100;
Expand Down
1 change: 0 additions & 1 deletion examples/typescript/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ const initLiquidityPool = async (aptos: Aptos, swap: AccountAddress, deployer: A
return response.hash;
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const createFungibleAsset = async (aptos: Aptos, admin: Account): Promise<void> => {
await new Promise<void>((resolve) => {
readline.question(
Expand Down
4 changes: 2 additions & 2 deletions examples/typescript/transfer_coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const aptos = new Aptos(config);

/**
* Prints the balance of an account
* @param aptos
* @param name
* @param address
* @param accountAddress
* @param versionToWaitFor
* @returns {Promise<number>}
*
*/
Expand Down
12 changes: 9 additions & 3 deletions examples/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from "fs";
import { AccountAddress } from "@aptos-labs/ts-sdk";

/* eslint-disable no-console */

/* eslint-disable max-len */

/**
Expand Down Expand Up @@ -35,9 +36,7 @@ export function compilePackage(

/**
* A convenience function to get the compiled package metadataBytes and byteCode
* @param packageDir
* @param outputFile
* @param namedAddresses
* @param filePath
*/
export function getPackageBytesToPublish(filePath: string) {
// current working directory - the root folder of this repo
Expand All @@ -52,3 +51,10 @@ export function getPackageBytesToPublish(filePath: string) {

return { metadataBytes, byteCode };
}

/**
* current time in seconds
*/
export function nowSeconds(): number {
return Date.now() / 1000;
}

0 comments on commit 2bd7f6b

Please sign in to comment.