sui_dart 0.8.5
sui_dart: ^0.8.5 copied to clipboard
A cross-platform SDK for Sui blockchain, supporting Mobile, Web and Desktop.
Sui Dart SDK #
Installation #
dependencies:
sui_dart: ^0.5.0
Demo #
Usage #
Connecting to Sui Network #
/// connect to devnet
final devnetClient = SuiClient(SuiUrls.devnet);
/// connect to testnet
final testnetClient = SuiClient(SuiUrls.testnet);
/// connect to mainnet
final mainnetClient = SuiClient(SuiUrls.mainnet);
Getting coins from the faucet #
Faucet V0
final faucet = FaucetClient(SuiUrls.faucetDev);
await faucet.requestSuiFromFaucetV0('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
Faucet V1
final faucet = FaucetClient(SuiUrls.faucetDev);
await faucet.requestSuiFromFaucetV1('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
Sui Account #
Create account with private key
/// Ed25519 account
final ed25519 = SuiAccount.ed25519Account();
final ed25519Import = SuiAccount.fromPrivateKey(ed25519.privateKey());
/// Secp256k1 account
final secp256k1 = SuiAccount.secp256k1Account();
final sepc256k1Import = SuiAccount.fromPrivateKey(secp256k1.privateKey());
/// Secp256r1 account
final secp256r1 = SuiAccount.secp256r1Account();
final sepc256r1Import = SuiAccount.fromPrivateKey(secp256r1.privateKey());
Create account with mnemonic
/// create mnemonics
final mnemonics = SuiAccount.generateMnemonic();
/// Ed25519 account
final ed25519 = SuiAccount.fromMnemonics(mnemonics, SignatureScheme.Ed25519);
/// Secp256k1 account
final secp256k1 = SuiAccount.fromMnemonics(mnemonics, SignatureScheme.Secp256k1);
/// Secp256r1 account
final secp256r1 = SuiAccount.fromMnemonics(mnemonics, SignatureScheme.Secp256r1);
Building Transactions #
Transactions are programmable transaction blocks (PTBs): a sequence of commands
that run atomically. Build one with the Transaction class.
final tx = Transaction();
Inputs
Add objects and pure (BCS-encoded) values as inputs:
// Object inputs, by id
tx.object('0x2619f581cb1864d07c89453a69611202669fdc4784fb59b9cb4278ec60756011');
// Pure values (u64/u128/u256 take a BigInt)
tx.pure.u64(BigInt.from(1000));
tx.pure.address('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
tx.pure.string('hello');
tx.pure.boolean(true);
tx.pure.vector('u64', [1, 2, 3]);
tx.pure.option('u64', BigInt.from(5));
Commands
Split coins off the gas coin and transfer them:
final tx = Transaction();
final coin = tx.splitCoins(tx.gas, [1000]);
tx.transferObjects([coin], recipient);
Transfer objects:
tx.transferObjects(
[tx.object('0x2619f581cb1864d07c89453a69611202669fdc4784fb59b9cb4278ec60756011')],
recipient,
);
Merge coins:
tx.mergeCoins('0x922ec73939b3288f6da39ebefb0cb88c6c54817441254d448bd2491ac4dd0cbd', [
'0x8dafc96dec7f8d635e052a6da9a4153e37bc4d59ed44c45006e4e9d17d07f80d',
]);
Call a Move function. The target is packageId::module::function; pass generic
type parameters with typeArguments:
tx.moveCall(
'0x...::nft::mint',
arguments: [tx.pure.string('Example NFT')],
);
final newCoin = tx.moveCall(
'0x2::coin::split',
typeArguments: ['0x2::sui::SUI'],
arguments: [tx.object('0xCoinId'), tx.pure.u64(BigInt.from(1000))],
);
Build a vector of objects, publish, or upgrade a package:
tx.makeMoveVec(objects: [tx.object('0x1'), tx.object('0x2')]);
final upgradeCap = tx.publish(modules, dependencies);
tx.transferObjects([upgradeCap], recipient);
tx.upgrade(
modules: modules,
dependencies: dependencies,
packageId: '0x...',
ticket: upgradeTicket,
);
Using results
Every command returns a TransactionResult you can chain into later commands.
Index into it for a specific output:
final coins = tx.splitCoins(tx.gas, [1000, 2000]);
tx.transferObjects([coins[0]], alice);
tx.transferObjects([coins[1]], bob);
A single return value can be passed straight through:
final nft = tx.moveCall('0x...::nft::mint', arguments: [tx.pure.string('My NFT')]);
tx.transferObjects([nft], recipient);
coinWithBalance
coinWithBalance selects and merges the sender's coins at build time to produce
a coin of an exact amount (using the gas coin for SUI). Add it with tx.add:
final tx = Transaction();
// 1 SUI
final coin = tx.add(coinWithBalance(balance: 1000000000));
tx.transferObjects([coin], recipient);
// A custom coin type
final usdc = tx.add(coinWithBalance(
type: '0x...::usdc::USDC',
balance: 1000000,
));
Use createBalance(...) for a Balance<T> instead of a Coin<T>. Both require a
sender and a client at build time.
Gas and sender
The client sets the sender, gas price, budget, and payment automatically. Override any of them when needed:
tx.setSender(account.getAddress());
tx.setGasBudget(BigInt.from(50000000));
tx.setGasPrice(BigInt.from(1000));
Signing and Executing #
JSON-RPC is deprecated; execute transactions over gRPC with SuiGrpcClient. The
gRPC client and its transaction helpers are separate imports:
import 'package:sui_dart/sui.dart';
import 'package:sui_dart/grpc/client.dart';
import 'package:sui_dart/grpc/grpc_resolution_client.dart';
final client = SuiGrpcClient(SuiGrpcClientOptions(
baseUrl: 'fullnode.mainnet.sui.io',
port: 443,
));
Build, sign, and execute in one call. Inputs (coins, objects, gas) resolve over gRPC, and the sender defaults to the account's address:
final account = SuiAccount.fromMnemonics(mnemonics, SignatureScheme.Ed25519);
final tx = Transaction();
final coin = tx.splitCoins(tx.gas, [1000]);
tx.transferObjects([coin], account.getAddress());
final result = await client.signAndExecuteTransaction(
account,
tx,
include: const TransactionIncludeOptions(effects: true),
);
print(result.digest);
Simulate (dry-run) a transaction without executing it:
tx.setSender(account.getAddress());
final sim = await client.simulateTransaction(
tx,
include: const TransactionIncludeOptions(effects: true, events: true),
);
Sign and execute manually (for multisig, sponsored, or delayed execution):
tx.setSender(account.getAddress());
final bytes = await client.buildTransaction(tx);
final signed = account.keyPair.signTransactionBlock(bytes);
final result = await client.executeTransaction(
bytes,
[signed.signature],
include: const TransactionIncludeOptions(effects: true),
);
Serializing
Pass an in-progress transaction between processes as JSON, then rebuild it:
// Async: resolves intents like coinWithBalance first
final json = await tx.toJsonAsync(
SerializeTransactionOptions(resolutionClient: GrpcResolutionClient(client)),
);
// Sync: for a transaction with no unresolved intents
final json = tx.toJson();
final restored = Transaction.from(json);
Reading APIs #
Get Owned Objects
final client = SuiClient(SuiUrls.devnet);
final objects = await client.getOwnedObjects('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
Get Objects
final client = SuiClient(SuiUrls.devnet);
final obj = await client.getObject('0x0d49dbda185cd0941b71315edb594276731f21b2232d8713f319b02c462a2da7',
options: SuiObjectDataOptions(showContent: true)
);
final objs = await client.multiGetObjects([
'0x0d49dbda185cd0941b71315edb594276731f21b2232d8713f319b02c462a2da7',
'0x922ec73939b3288f6da39ebefb0cb88c6c54817441254d448bd2491ac4dd0cbd'
], options: SuiObjectDataOptions(showType: true));
Get Transaction
final client = SuiClient(SuiUrls.devnet);
final txn = await client.getTransactionBlock('6oH779AUs2WpwW77xCVGbYqK1FYVamRqHjV6A5wCV8Qj',
options: SuiTransactionBlockResponseOptions(showEffects: true)
);
final txns = await client.multiGetTransactionBlocks([
'9znMGToLRRa8yZvjCUfj1FJmq4gpb8QwpibFAUffuto1',
'4CEFMajEtM62MthwY1xR3Bcddoh2h5wc7jeKEy7WWsbv'
], options: SuiTransactionBlockResponseOptions(showInput: true, showEffects: true));
Get Checkpoints
final client = SuiClient(SuiUrls.devnet);
final checkpoint = await client.getCheckpoint('338000');
final checkpoints = await client.getCheckpoints(descendingOrder: true);
Get Coins
final client = SuiClient(SuiUrls.devnet);
final coins = await client.getCoins(
'0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2',
coinType: '0x2::sui::SUI');
final allCoins = await client.getAllCoins('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
final suiBalance = await client.getBalance('0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2');
Events APIs #
Querying events
final client = SuiClient(SuiUrls.devnet);
final events = await client.queryEvents(
{"Sender": "0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2"},
limit: 2
);
/// Or with EventFilter
final events = await client.queryEventsByFilter(
EventFilter(sender: "0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2"),
limit: 2
);
WebsocketClient subscribeEvent
final client = WebsocketClient(SuiUrls.webSocketDevnet);
final subscription = client.subscribeEvent({"Sender": "0xa2d8bb82df40770ac5bc8628d8070b041a13386fef17db27b32f3b0f316ae5a2"})
.listen((event) {
print(event);
}, onError: (e) {
print(e.toString());
});