-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[view] Add ability to get BCS output from view functions (#624)
NOTE: this is temporary, needs to be fixed to have a completely separate path in the SDK instead of the hack that is here.
- Loading branch information
1 parent
cb4d657
commit 4503a0a
Showing
16 changed files
with
586 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-disable no-console */ | ||
|
||
/** | ||
* This example shows how to use the Aptos client to create accounts, fund them, and transfer between them. | ||
* Similar to ./simple_transfer.ts, but uses transferCoinTransaction to generate the transaction. | ||
*/ | ||
|
||
import { | ||
Account, | ||
AccountAddress, | ||
Aptos, | ||
AptosConfig, | ||
Deserializer, | ||
Network, | ||
NetworkToNetworkName, | ||
U64, | ||
} from "@aptos-labs/ts-sdk"; | ||
|
||
const ALICE_INITIAL_BALANCE = 100_000_000; | ||
const BOB_INITIAL_BALANCE = 0; | ||
const TRANSFER_AMOUNT = 1_000_000; | ||
|
||
// Set up the client | ||
const APTOS_NETWORK: Network = NetworkToNetworkName[process.env.APTOS_NETWORK ?? Network.DEVNET]; | ||
const config = new AptosConfig({ network: APTOS_NETWORK }); | ||
const aptos = new Aptos(config); | ||
|
||
/** | ||
* Prints the balance of an account | ||
* @param name | ||
* @param accountAddress | ||
* @returns {Promise<number>} | ||
* | ||
*/ | ||
const balance = async (name: string, accountAddress: AccountAddress): Promise<number> => { | ||
const output = await aptos.experimental.viewBinary({ | ||
payload: { | ||
function: "0x1::coin::balance", | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"], | ||
functionArguments: [accountAddress], | ||
}, | ||
}); | ||
const deser = new Deserializer(output); | ||
const [wrappedAmount] = deser.deserializeVector(U64); | ||
const amount = Number(wrappedAmount.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 Alice, and transfer between them using transferCoinTransaction.", | ||
); | ||
|
||
// Create two accounts | ||
const alice = Account.generate(); | ||
const bob = Account.generate(); | ||
|
||
console.log("=== Addresses ===\n"); | ||
console.log(`Alice's address is: ${alice.accountAddress}`); | ||
console.log(`Bob's address is: ${bob.accountAddress}`); | ||
|
||
// Fund the accounts | ||
console.log("\n=== Funding accounts ===\n"); | ||
|
||
// Fund alice account | ||
await aptos.fundAccount({ | ||
accountAddress: alice.accountAddress, | ||
amount: ALICE_INITIAL_BALANCE, | ||
}); | ||
|
||
// Show the balances | ||
console.log("\n=== Initial Balances ===\n"); | ||
const aliceBalance = await balance("Alice", alice.accountAddress); | ||
const bobBalance = await balance("Bob", bob.accountAddress); | ||
|
||
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 | ||
console.log(`\n=== Transfer ${TRANSFER_AMOUNT} from Alice to Bob ===\n`); | ||
const transaction = await aptos.transferCoinTransaction({ | ||
sender: alice.accountAddress, | ||
recipient: bob.accountAddress, | ||
amount: TRANSFER_AMOUNT, | ||
}); | ||
const pendingTxn = await aptos.signAndSubmitTransaction({ signer: alice, transaction }); | ||
const response = await aptos.waitForTransaction({ transactionHash: pendingTxn.hash }); | ||
console.log(`Committed transaction: ${response.hash}`); | ||
|
||
console.log("\n=== Balances after transfer ===\n"); | ||
const newAliceBalance = await balance("Alice", alice.accountAddress); | ||
const newBobBalance = await balance("Bob", bob.accountAddress); | ||
|
||
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { LedgerVersionArg } from "../types"; | ||
import { AptosConfig } from "./aptosConfig"; | ||
import { InputViewFunctionData } from "../transactions"; | ||
import { viewBinary } from "../internal/experimental"; | ||
|
||
/** | ||
* A class to have experimental functionality to the SDK. Anything used here is subject to change. | ||
* @group Experimental | ||
* @experimental | ||
*/ | ||
export class Experimental { | ||
/** | ||
* Initializes a new instance of the Aptos client with the provided configuration. | ||
* | ||
* @param config - The configuration settings for the Aptos client. | ||
* @param config.network - The network to connect to (e.g., Testnet, Mainnet). | ||
* @param config.nodeUrl - The URL of the Aptos node to connect to. | ||
* @param config.faucetUrl - The URL of the faucet to use for funding accounts. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"; | ||
* | ||
* async function runExample() { | ||
* // Create a new Aptos client with Testnet configuration | ||
* const config = new AptosConfig({ network: Network.TESTNET }); // Specify your own network if needed | ||
* const aptos = new Aptos(config); | ||
* | ||
* console.log("Aptos client initialized:", aptos); | ||
* } | ||
* runExample().catch(console.error); | ||
* ``` | ||
* @group Experimental | ||
* @experimental | ||
*/ | ||
constructor(readonly config: AptosConfig) {} | ||
|
||
async viewBinary(args: { | ||
payload: InputViewFunctionData; | ||
options?: LedgerVersionArg & { convert?: undefined }; | ||
}): Promise<Uint8Array>; | ||
async viewBinary<T extends {}>(args: { | ||
payload: InputViewFunctionData; | ||
options: LedgerVersionArg & { convert: (input: Uint8Array) => T }; | ||
}): Promise<T>; | ||
|
||
/** | ||
* Returns BCS encoded results of the view function. It can also convert the results to a specific type, if a | ||
* converter is provided. | ||
* | ||
* @experimental | ||
* @group Experimental | ||
* @param args | ||
*/ | ||
async viewBinary<T extends {} = Uint8Array>(args: { | ||
payload: InputViewFunctionData; | ||
options?: LedgerVersionArg & { convert?: (input: Uint8Array) => T }; | ||
}): Promise<Uint8Array | T> { | ||
return viewBinary<T>({ ...args, aptosConfig: this.config }); | ||
} | ||
} |
Oops, something went wrong.