Stake SUI with SDK
This tutorial provides a complete, end-to-end guide for building a simple web application that allows users to stake their SUI tokens directly from a browser wallet. We will delegate to the blockeden.xyz validator by interacting with the Sui blockchain using the official TypeScript SDK.
Why This Approach?
This method is designed with security and simplicity in mind, offering several key advantages for both developers and users:
- Zero Key Exposure: The user's private keys never leave their wallet. The wallet handles all cryptographic signing locally within its secure environment. Your application only constructs and requests a signature for a transaction, adhering to the best practices for non-custodial interactions.
- Universal Wallet Compatibility: By leveraging the
Sui Wallet Standard, this approach works seamlessly with all major Sui wallets, including Sui Wallet, Suiet, Nightly, SafePal, and others. This frees you from writing wallet-specific code. - Reliable and Simple: The blockeden.xyz validator address is hard-coded into our application. This simplifies the logic and removes a potential point of failure or manipulation that could arise from fetching the address from an external, potentially compromised API.
Prerequisites
Before you start writing code, please ensure your development environment is set up with the following:
-
A funded Sui browser wallet: You'll need a browser extension wallet that supports the Sui Wallet Standard. Make sure it's funded with enough SUI to cover both the desired stake amount and the small network transaction fees (gas).
-
Node.js: You need Node.js version 18 or newer to run the TypeScript code and manage packages.
-
Sui Packages: Install the necessary Sui libraries using
npmor your preferred package manager. Open your terminal and run:npm install @mysten/sui @mysten/dapp-kit
1. Set Up the Sui Client
The first step is to establish a connection to the Sui blockchain. We'll use the SuiClient class from the @mysten/sui library, which is our primary interface for reading data from the network.
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
// Initialize a client for the Sui Mainnet
export const client = new SuiClient({
url: getFullnodeUrl('mainnet'), // ← Switch to 'testnet' or 'devnet' if needed
});
This client instance will be used for tasks like fetching coin balances and querying stake information. Note that you can easily switch networks by changing the parameter to getFullnodeUrl('testnet') or getFullnodeUrl('devnet') for testing.