Sui
Getting Started
You can use two different options to implement Sui operations with the Everstake wallet SDK.
Option 1: REST API
You can use REST API to call methods which are described in Swagger with detailed examples
https://wallet-sdk-api.everstake.oneTo use transactions from REST API you can use the following approach:
import { Transaction } from '@mysten/sui/transactions';
import { SuiClient } from '@mysten/sui/client';
const client = new SuiClient({url: suiRpcUrl});
// REST API json response object
const apiResponse = {...}
const apiResponseString = JSON.stringify(apiResponse)
const tx = Transaction.from(apiResponseString)
// Sign and execute the transaction
const txDetails = await client.signAndExecuteTransaction({
transaction: tx,
signer: yourKeypair
});
// transaction hash
console.log(tx.digest); Option 2: TypeScript library
You can install and import Wallet SDK for Javascript/TypeScript.
Step. 1: Installing the Library
Install the npm library or yarn by copying the code below.
$ npm install @everstake/wallet-sdk-sui$ yarn add @everstake/wallet-sdk-suiStep. 2: Import Wallet SDK
After installing the package, you can import the Sui module and use the SDK:
Import ES6
// import module
import { Sui } from '@everstake/wallet-sdk-sui';
// or you can also use
import * as Sui from '@everstake/wallet-sdk-sui';Import ES5
// import module
const { Sui } = require("@everstake/wallet-sdk-sui");Getting Info
The Sui SDK provides several read-only methods to retrieve information about balances and stakes:
getBalanceByAddress(address): Retrieves the Sui balance for a given address. Returns a BigNumber representing the balance in MIST.getStakes(address): Gets all staking positions for a given address.
Balance Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
// Initialize Sui client with the desired network
const client = new Sui('mainnet');
// User address
const address = '0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234';
// Get the balance
const balance = await client.getBalanceByAddress(address);
// Convert from MIST to SUI (1 SUI = 10^9 MIST)
const balanceInSui = Number(balance) / 1e9;
console.log(`Balance: ${balanceInSui} SUI`);Staking Positions Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
// Initialize Sui client with the desired network
const client = new Sui('testnet');
// User address
const address = '0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234';
// Get all staking positions
const delegatedStakes = await client.getStakes(address);
// Example of processing stake information
if (delegatedStakes.length > 0) {
delegatedStakes.forEach((delegatedStake, index) => {
console.log(`DelegatedStake #${index + 1}:`);
console.log(` Validator: ${delegatedStake.validatorAddress}`);
console.log(` Staking Pool: ${delegatedStake.stakingPool}`);
delegatedStake.stakes.forEach((stake, index) => {
console.log(` Stake #${index + 1}:`);
console.log(` ID: ${stake.stakedSuiId}`);
console.log(` Amount: ${stake.principal} MIST`);
console.log(` Status: ${stake.status}`);
console.log(` Request Epoch: ${stake.stakeRequestEpoch}`);
console.log(` Active Epoch: ${stake.stakeActiveEpoch}`);
if (stake.status === 'Active') {
console.log(` Estimated Reward: ${stake.estimatedReward} MIST`);
}
})
});
} else {
console.log('No active stakes found');
}Stake
stake(amount): Creates a transaction to stake SUI tokens with a validator. This method requires the amount to be greater than or equal to the minimum required for staking (1^9 MIST).
Stake Code Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { fromHex } from '@mysten/sui/utils';
// Initialize Sui client with the desired network
const client = new Sui('testnet');
// Amount to stake in MIST
const amount = '1000000000';
// Create a staking transaction
const stakeTx = await client.stake(amount);
// Get the private key
const privateKey = process.env.SUI_PK || '';
const pkBytes = fromHex(privateKey);
const keypair = Ed25519Keypair.fromSecretKey(pkBytes);
// Sign and execute the transaction
const txDetails = await client.client.signAndExecuteTransaction({
transaction: stakeTx,
signer: keypair,
});
console.log(tx.digest); // transaction hashUnstake
unstake(stakedSuiId): Creates a transaction to unstake previously staked SUI tokens. This method requires the ID of the staked SUI object to be withdrawn.
Unstake Code Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { fromHex } from '@mysten/sui/utils';
// Initialize Sui client with the desired network
const client = new Sui('testnet');
// The staked SUI ID to withdraw
const stakedSuiId = '0x0ae7349842915f9f3c4e7e3dfe607bff607701f2ca432ef04bd57f7eb8367002';
// Create an unstaking transaction
const unstakeTx = await client.unstake(stakedSuiId);
// Get the private key
const privateKey = process.env.SUI_PK || '';
const pkBytes = fromHex(privateKey);
const keypair = Ed25519Keypair.fromSecretKey(pkBytes);
// Sign and execute the transaction
const txDetails = await client.client.signAndExecuteTransaction({
transaction: unstakeTx,
signer: keypair,
});
console.log(txDetails.digest); // transaction hashBalance
getBalanceByAddress(address): Retrieves the Sui balance for a given address. This method returns a BigInt representing the balance in the smallest denomination (MIST). To convert to SUI, divide by 10^9.
Balance Code Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
// Initialize Sui client with the desired network
const client = new Sui('mainnet');
// User address
const address = '0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234';
// Get the balance
const balance = await client.getBalanceByAddress(address);
// Convert from MIST to SUI (1 SUI = 10^9 MIST)
const balanceInSui = Number(balance) / 1e9;
console.log(`Balance: ${balanceInSui} SUI`);Get Existing Stakes
The getStakeBalanceByAddress method retrieves all delegated stakes for a specific address.
getStakeBalanceByAddress(address): returns all staking information for the given address
Get Stake Balance Code Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
// Initialize Sui client
const client = new Sui('mainnet');
// User Public Address
const address = '0x1bae2a9343bd546e14c5e696b45be50b7d215bb627949e1e5f82470bee9bdb62';
// Get delegated stakes for the address
const balances = await client.getStakeBalanceByAddress(address);
console.log(balances); // array of DelegatedStake objectsSend transfer
sendTransfer(recipientAddress, amount): Creates a transaction to transfer SUI tokens to a recipient. This method requires the recipient's address and the amount to send.
Send Transfer Code Example
// Import SDK
import { Sui } from '@everstake/wallet-sdk-sui';
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { fromHex } from '@mysten/sui/utils';
// Initialize Sui client with the desired network
const client = new Sui('testnet');
// Recipient address
const recipientAddress = '0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234';
// Amount to send in MIST
const amount = '1000000000';
// Create a transfer transaction
const transferTx = await client.sendTransfer(recipientAddress, amount);
// Get the private key
const privateKey = process.env.SUI_PK || '';
const pkBytes = fromHex(privateKey);
const keypair = Ed25519Keypair.fromSecretKey(pkBytes);
// Sign and execute the transaction
const txDetails = await client.client.signAndExecuteTransaction({
transaction: transferTx,
signer: keypair,
});
console.log(txDetails.digest); // transaction hash
