Sui TypeScript SDK
This is the Sui TypeScript SDK built on the Sui JSON RPC API. It provides utility classes and functions for applications to sign transactions and interact with the Sui network.
WARNING: Note that we are still iterating on the RPC and SDK API before TestNet, therefore please expect frequent breaking changes in the short-term. We expect the API to stabilize after the upcoming TestNet launch.
Working with DevNet
The SDK will be published to npm registry with the same bi-weekly release cycle as the DevNet validators and RPC Server. To use the SDK in your project, you can do:
$ npm install @mysten/sui.js
You can also use your preferred npm client, such as yarn or pnpm.
Working with local network
Note that the latest
tag for the published SDK might go out of sync with the RPC server on the main
branch until the next release. If you're developing against a local network, we recommend using the experimental
-tagged packages, which contain the latest changes from main
.
npm install @mysten/sui.js@experimental
Refer to the JSON RPC topic for instructions about how to start a local network and local RPC server.
Building Locally
To get started you need to install pnpm, then run the following command:
$ pnpm install
$ pnpm sdk build
Type Doc
You can view the generated Type Doc for the current release of the SDK at http://typescript-sdk-docs.s3-website-us-east-1.amazonaws.com/.
For the latest docs for the main
branch, run pnpm doc
and open the doc/index.html in your browser.
Testing
To run unit tests
cd sdk/typescript
pnpm run test:unit
To run E2E tests against local network
cd sdk/typescript
pnpm run prepare:e2e
pnpm run test:e2e
To run E2E tests against DevNet
cd sdk/typescript
VITE_FAUCET_URL='https://faucet.devnet.sui.io:443/gas' VITE_FULLNODE_URL='https://fullnode.devnet.sui.io' pnpm test:e2e
Usage
The JsonRpcProvider
class provides a connection to the JSON-RPC Server and should be used for all read-only operations. The default URLs to connect with the RPC server are:
Examples:
Fetch objects owned by the address 0xbff6ccc8707aa517b4f1b95750a2a8c666012df3
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const objects = await provider.getOwnedObjectRefs(
'0xbff6ccc8707aa517b4f1b95750a2a8c666012df3'
);
Fetch transaction details from a transaction digest:
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const txn = await provider.getTransaction(
'6mn5W1CczLwitHCO9OIUbqirNrQ0cuKdyxaNe16SAME='
);
Fetch transaction events from a transaction digest:
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const txEvents = await provider.getEventsByTransaction(
'6mn5W1CczLwitHCO9OIUbqirNrQ0cuKdyxaNe16SAME='
);
Fetch events by sender address:
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const senderEvents = await provider.getEventsBySender(
'0xbff6ccc8707aa517b4f1b95750a2a8c666012df3'
);
For any operations that involves signing or submitting transactions, you should use the Signer
API. For example:
To transfer a 0x2::coin::Coin<SUI>
:
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const keypair = new Ed25519Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);
const transferTxn = await signer.transferObjectWithRequestType({
objectId: '0x5015b016ab570df14c87649eda918e09e5cc61e0',
gasBudget: 1000,
recipient: '0xd84058cb73bdeabe123b56632713dcd65e1a6c92',
});
console.log('transferTxn', transferTxn);
To split a 0x2::coin::Coin<SUI>
into multiple coins
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const keypair = new Ed25519Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);
const splitTxn = await signer.splitCoinWithRequestType({
coinObjectId: '0x5015b016ab570df14c87649eda918e09e5cc61e0',
splitAmounts: [10, 20, 30],
gasBudget: 1000,
});
console.log('SplitCoin txn', splitTxn);
To merge two coins:
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const keypair = new Ed25519Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);
const mergeTxn = await signer.mergeCoinWithRequestType({
primaryCoin: '0x5015b016ab570df14c87649eda918e09e5cc61e0',
coinToMerge: '0xcc460051569bfb888dedaf5182e76f473ee351af',
gasBudget: 1000,
});
console.log('MergeCoin txn', mergeTxn);
To make a move call:
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const keypair = new Ed25519Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);
const moveCallTxn = await signer.executeMoveCallWithRequestType({
packageObjectId: '0x2',
module: 'devnet_nft',
function: 'mint',
typeArguments: [],
arguments: [
'Example NFT',
'An NFT created by the wallet Command Line Tool',
'ipfs://bafkreibngqhl3gaa7daob4i2vccziay2jjlp435cf66vhono7nrvww53ty',
],
gasBudget: 10000,
});
console.log('moveCallTxn', moveCallTxn);
Subscribe to all events created by transactions sent by account 0xbff6ccc8707aa517b4f1b95750a2a8c666012df3
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const subscriptionId = await provider.subscribeEvent(
{ SenderAddress: '0xbff6ccc8707aa517b4f1b95750a2a8c666012df3' },
(event: SuiEventEnvelope) => {
}
);
const subFoundAndRemoved = await provider.unsubscribeEvent(subscriptionId);
Subscribe to all events created by the devnet_nft
module
import { JsonRpcProvider } from '@mysten/sui.js';
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const devnetNftFilter = {
All: [
{ EventType: 'MoveEvent' },
{ Package: '0x2' },
{ Module: 'devnet_nft' },
],
};
const devNftSub = await provider.subscribeEvent(
devnetNftFilter,
(event: SuiEventEnvelope) => {
}
);
To publish a package:
import { Ed25519Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const { execSync } = require('child_process');
const keypair = new Ed25519Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);
const compiledModules = JSON.parse(
execSync(
`${cliPath} move build --dump-bytecode-as-base64 --path ${packagePath}`,
{ encoding: 'utf-8' }
)
);
const modulesInBytes = compiledModules.map((m) =>
Array.from(new Base64DataBuffer(m).getData())
);
const publishTxn = await signer.publishWithRequestType({
compiledModules: modulesInBytes,
gasBudget: 10000,
});
console.log('publishTxn', publishTxn);
Alternatively, a Secp256k1 can be initiated:
import { Secp256k1Keypair, JsonRpcProvider, RawSigner } from '@mysten/sui.js';
const keypair = new Secp256k1Keypair();
const provider = new JsonRpcProvider('https://fullnode.devnet.sui.io:443');
const signer = new RawSigner(keypair, provider);