@topo/ts-sdk
v6.1.0
Published
Topo TypeScript SDK
Readme
TypeScript SDK for Topo
The TypeScript SDK allows you to connect, explore, and interact with the Topo blockchain. You can use it to request data, send transactions, set up test environments, and more!
Learn How To Use The TypeScript SDK
Quickstart
Tutorials
Examples
Reference Docs (For looking up specific functions)
Installation
For use in Node.js or a web application
Install with your favorite package manager such as npm, yarn, or pnpm:
pnpm install @topo/ts-sdkFor use with Bun
The SDK is compatible with the Bun runtime. Install the SDK using Bun's package manager:
bun add @topo/ts-sdkFor use in a browser (<= version 1.9.1 only)
You can add the SDK to your web application using a script tag:
<script src="https://unpkg.com/@topo/ts-sdk/dist/browser/index.global.js"></script>Then, the SDK can be accessed through window.topoSDK.
Usage
Create an Topo client in order to access the SDK's functionality.
import { Topo, TopoConfig, Network } from "@topo/ts-sdk"
// You can use TopoConfig to choose which network to connect to
const config = new TopoConfig({ network: Network.TESTNET });
// Topo is the main entrypoint for all functions
const topo = new Topo(config);For Bun users
Bun's HTTP/2 support is not fully mature yet. You need to disable HTTP/2 to ensure the SDK works properly:
import { Topo, TopoConfig, Network } from "@topo/ts-sdk";
const topo = new Topo(new TopoConfig({ network: Network.TESTNET, clientConfig: { http2: false } }));Reading Data From Onchain (Guide)
const accountInfo = await topo.getAccountInfo({ accountAddress: "0x123" });
const modules = await topo.getAccountModules({ accountAddress: "0x123" });
const tokens = await topo.getAccountOwnedTokens({ accountAddress: "0x123" });Account management (default to Ed25519)
Note: We introduce a Single Sender authentication (as introduced in AIP-55). Generating an account defaults to Legacy Ed25519 authentication with the option to use the Single Sender unified authentication.
Generate new keys
const account = Account.generate(); // defaults to Legacy Ed25519
const account = Account.generate({ scheme: SigningSchemeInput.Secp256k1Ecdsa }); // Single Sender Secp256k1
const account = Account.generate({ scheme: SigningSchemeInput.Ed25519, legacy: false }); // Single Sender Ed25519Derive from private key
// Create a private key instance for Ed25519 scheme
const privateKey = new Ed25519PrivateKey("myEd25519privatekeystring");
// Or for Secp256k1 scheme
const privateKey = new Secp256k1PrivateKey("mySecp256k1privatekeystring");
// Derive an account from private key
// This is used as a local calculation and therefore is used to instantiate an `Account`
// that has not had its authentication key rotated
const account = Account.fromPrivateKey({ privateKey });
// also, can use this function that resolves the provided private key type and derives the public key from it
// to support key rotation and differentiation between legacy ed25519 and unified authentications
const topo = new Topo();
const account = await topo.deriveAccountFromPrivateKey({ privateKey });Derive from private key and address
// Create a private key instance for Ed25519 scheme
const privateKey = new Ed25519PrivateKey("myEd25519privatekeystring");
// Or for Secp256k1 scheme
const privateKey = new Secp256k1PrivateKey("mySecp256k1privatekeystring");
// Derive an account from private key and address
// Create an AccountAddress instance from the account address string.
const address = AccountAddress.from("myaccountaddressstring");
// Derive an account from private key and address
const account = Account.fromPrivateKeyAndAddress({ privateKey, address });Derive from path
const path = "m/44'/637'/0'/0'/1";
const mnemonic = "various float stumble...";
const account = Account.fromDerivationPath({ path, mnemonic });Submit transaction (Tutorial)
/**
* This example shows how to use the Topo SDK to send a transaction.
* Don't forget to install @topo/ts-sdk before running this example!
*/
import {
Account,
Topo,
TopoConfig,
Network,
} from "@topo/ts-sdk";
async function example() {
console.log("This example will create two accounts (Alice and Bob) and send a transaction transferring APT to Bob's account.");
// 0. Setup the client and test accounts
const config = new TopoConfig({ network: Network.TESTNET });
const topo = new Topo(config);
let alice = Account.generate();
let bob = Account.generate();
console.log("=== Addresses ===\n");
console.log(`Alice's address is: ${alice.accountAddress}`);
console.log(`Bob's address is: ${bob.accountAddress}`);
console.log("\n=== Funding accounts ===\n");
await topo.fundAccount({
accountAddress: alice.accountAddress,
amount: 100_000_000,
});
await topo.fundAccount({
accountAddress: bob.accountAddress,
amount: 100,
});
console.log("Funded Alice and Bob's accounts!")
// 1. Build
console.log("\n=== 1. Building the transaction ===\n");
const transaction = await topo.transaction.build.simple({
sender: alice.accountAddress,
data: {
// All transactions on Topo are implemented via smart contracts.
function: "0x1::topo_account::transfer",
functionArguments: [bob.accountAddress, 100],
},
});
console.log("Built the transaction!")
// 2. Simulate (Optional)
console.log("\n === 2. Simulating Response (Optional) === \n")
const [userTransactionResponse] = await topo.transaction.simulate.simple({
signerPublicKey: alice.publicKey,
transaction,
});
console.log(userTransactionResponse)
// 3. Sign
console.log("\n=== 3. Signing transaction ===\n");
const senderAuthenticator = topo.transaction.sign({
signer: alice,
transaction,
});
console.log("Signed the transaction!")
// 4. Submit
console.log("\n=== 4. Submitting transaction ===\n");
const submittedTransaction = await topo.transaction.submit.simple({
transaction,
senderAuthenticator,
});
console.log(`Submitted transaction hash: ${submittedTransaction.hash}`);
// 5. Wait for results
console.log("\n=== 5. Waiting for result of transaction ===\n");
const executedTransaction = await topo.waitForTransaction({ transactionHash: submittedTransaction.hash });
console.log(executedTransaction)
};
example();Troubleshooting
If you see an import error when you do this:
import { Topo, TopoConfig, Network } from "@topo/ts-sdk";It could be that your tsconfig.json is not using node. Make sure your moduleResolution in the tsconfig.json is set to node instead of bundler.
Contributing
If you found a bug or would like to request a feature, please file an issue. If, based on the discussion on an issue, you would like to offer a code change, please make a pull request. If neither of these describes what you would like to contribute, check out the contributing guide.
Running unit tests
To run a unit test in this repo, for example, the keyless end-to-end unit test in tests/e2e/api/keyless.test.ts:
pnpm jest keyless.test.ts