Code Monkey home page Code Monkey logo

aptos-ts-sdk's Introduction

Typescript SDK for Aptos

Discord NPM Package Version NPM Package Downloads

This library is experimental. Therefore, the API is unstable and may change without warning.

The Aptos TypeScript SDK provides a convenient way to interact with the Aptos blockchain using TypeScript. It offers a set of utility functions, classes, and types to simplify the integration process and enhance developer productivity.

This repository supports version >= 0.0.0 of the Aptos SDK npm package.

Installation

For use in Node.js or a web application

Install with your favorite package manager such as npm, yarn, or pnpm:

pnpm install @aptos-labs/ts-sdk@experimental
For use in a browser

You can add the SDK to your web application using a script tag:

<script src="https://unpkg.com/@aptos-labs/ts-sdk@experimental/dist/browser/index.global.js" />

Then, the SDK can be accessed through window.aptosSDK.

Usage

Initialize Aptos to access the SDK API.

// initiate the main entry point into Aptos SDK
const aptos = new Aptos();

If you want to pass in a custom config

// an optional config information for the SDK client instance.
const config = new AptosConfig({ network: Network.LOCAL });
const aptos = new Aptos(config);

Read data from chain


const modules = await aptos.getAccountModules({ accountAddress: "0x123" });

Keys management (default to Ed25519)

Note: We introduce a Single Sender authentication (as introduced in AIP-55). Generating an account defaults to Single Sender unified authentication with the option to use the Legacy Ed25519


Generate new keys

const account = Account.generate(); // defaults to Single Sender Ed25519
const account = Account.generate({ scheme: SingingSchemeInput.Secp256k1 }); // Single Sender Secp256k1
const account = Account.generate({ legacy: true }); // use Legacy Ed25519

Derive from private key

const aptos = new Aptos();
// This functions 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
// Read more https://github.com/aptos-labs/aptos-ts-sdk/blob/main/src/api/account.ts#L364
const account = await aptos.deriveAccountFromPrivateKey({ privateKey: privateKey });

Derive from path

const path = "m/44'/637'/0'/0'/1";
const mnemonic = "various float stumble...";
const account = Account.fromDerivationPath({ path, mnemonic });

Submit transaction


Single Signer transaction

Using transaction submission api

const alice: Account = Account.generate();
const bobAddress = "0xb0b";
const transaction = await aptos.generateTransaction({
  sender: alice.accountAddress.toString(),
  data: {
    function: "0x1::coin::transfer",
    type_arguments: [new TypeTagStruct(StructTag.fromString("0x1::aptos_coin::AptosCoin"))],
    arguments: [AccountAddress.fromHexInput(bobAddress), new U64(100)],
  },
});

let committedTransaction = await aptos.signAndSubmitTransaction({ signer: alice, transaction });

Using built in transferCoinTransaction

const alice: Account;
const bobAddress = "0xb0b";
const transaction = await aptos.transferCoinTransaction({
  sender: alice.accountAddress.toString(),
  recipient: bob,
  amount: 100,
});

const pendingTransaction = await aptos.signAndSubmitTransaction({ signer: alice, transaction });

Documentation and examples

  • For in-depth examples, check out the examples folder with ready-made package.json files to get you going quickly!

Testing

To run the SDK tests, simply run from the root of this repository:

Note: make sure aptos local node is up and running. Take a look at the local development network guide for more details.

pnpm test

Troubleshooting

If you see import error when you do this

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

It could be your tsconfig.json is not incompatible, make sure your moduleResolution 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, checkout out the contributing guide.

aptos-ts-sdk's People

Contributors

gregnazario avatar 0xmaayan avatar xbtmatt avatar 0xmigo avatar heliuchuan avatar banool avatar 0xaptosj avatar hardsetting avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.