> For the complete documentation index, see [llms.txt](https://docs.everstake.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.everstake.com/integrations/everstake-products/wallet-sdk/protocols/sui.md).

# Sui

Learn how to integrate Sui staking using Everstake Wallet SDK.

## Getting Started <a href="#getting-started" id="getting-started"></a>

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](https://wallet-sdk-api.everstake.one/swagger/#/SUI) with detailed examples

```
https://wallet-sdk-api.everstake.one
```

To use transactions from REST API you can use the following approach:

```typescript
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 <a href="#step-1-installing-the-library" id="step-1-installing-the-library"></a>

Install the npm library or yarn by copying the code below.

{% tabs %}
{% tab title="npm" %}

```sh
$ npm install @everstake/wallet-sdk-sui
```

{% endtab %}

{% tab title="yarn" %}

```sh
$ yarn add @everstake/wallet-sdk-sui
```

{% endtab %}
{% endtabs %}

#### Step. 2: Import Wallet SDK <a href="#step-2-import-wallet-sdk" id="step-2-import-wallet-sdk"></a>

After installing the package, you can import the Sui module and use the SDK:

**Import ES6**

```typescript
// import module
import { Sui } from '@everstake/wallet-sdk-sui';
// or you can also use
import * as Sui from '@everstake/wallet-sdk-sui';
```

**Import ES5**

```typescript
// import module
const { Sui } = require("@everstake/wallet-sdk-sui");
```

## Getting Info <a href="#getting-info-sui" id="getting-info-sui"></a>

The Sui SDK provides several read-only methods to retrieve information about balances and stakes:

* <mark style="color:yellow;">`getBalanceByAddress(address)`</mark>: Retrieves the Sui balance for a given address. Returns a BigNumber representing the balance in MIST.
* <mark style="color:yellow;">`getStakes(address)`</mark>: Gets all staking positions for a given address.

**Balance Example**

```typescript
// 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**

```typescript
// 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 <a href="#stake-sui" id="stake-sui"></a>

<mark style="color:yellow;">`stake(amount)`</mark>: 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**

```typescript
// 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 hash
```

## Unstake <a href="#unstake-sui" id="unstake-sui"></a>

<mark style="color:yellow;">`unstake(stakedSuiId)`</mark>: 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**

```typescript
// 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 hash
```

## Balance <a href="#balance-sui" id="balance-sui"></a>

<mark style="color:yellow;">`getBalanceByAddress(address)`</mark>: 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**

```typescript
// 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 <a href="#get-sui-stake-balance" id="get-sui-stake-balance"></a>

The <mark style="color:yellow;">`getStakeBalanceByAddress`</mark> method retrieves all delegated stakes for a specific address.

* <mark style="color:yellow;">`getStakeBalanceByAddress(address)`</mark>: returns all staking information for the given address

**Get Stake Balance Code Example**

```typescript
// 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 objects
```

## Send transfer <a href="#send-transfer-sui" id="send-transfer-sui"></a>

<mark style="color:yellow;">`sendTransfer(recipientAddress, amount`</mark>`)`: 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**

```typescript
// 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
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.everstake.com/integrations/everstake-products/wallet-sdk/protocols/sui.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
