# ETON SUI DOCUMENT ## Sui TypeScript SDK The Sui TypeScript SDK is a modular library of tools for interacting with the Sui blockchain. Use it to send queries to RPC nodes, build and sign transactions, and interact with a Sui or local network. ### Requestment - [Node.js](https://nodejs.org/) (version 18.20+, 20.10 or higher) ### Installation `npm install @mysten/sui.js` ### 1. CREATE WALLET To interact with Sui, you first need a Sui wallet. Use `CreateWallet.js` to create a Sui wallet or use [Sui Wallet](https://chromewebstore.google.com/detail/sui-wallet/opcgpfmipidbgpenhmajoajpbobppdil) ```js // Import the Ed25519Keypair class from the @mysten/sui.js/keypairs/ed25519 library import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'; // Import the bip39 module and the English wordlist from the @scure/bip39 library import * as bip39 from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english"; // Function to generate a valid wallet with a mnemonic seed phrase and corresponding wallet address function generateValidWallet() { let seedPhrase; // Variable to hold the generated mnemonic seed phrase let seedPhraseValid; // Variable to check if the generated seed phrase is valid let keypair; // Variable to hold the generated keypair let walletAddress; // Variable to hold the generated wallet address do { // Generate a mnemonic seed phrase using the English wordlist seedPhrase = bip39.generateMnemonic(wordlist); // Validate the generated seed phrase seedPhraseValid = bip39.validateMnemonic(seedPhrase, wordlist); if (seedPhraseValid) { // If the seed phrase is valid, derive a keypair from it keypair = Ed25519Keypair.deriveKeypair(seedPhrase); // Get the wallet address from the keypair's public key walletAddress = keypair.getPublicKey().toSuiAddress(); } } while (!seedPhraseValid); // Repeat the process until a valid seed phrase is generated // Return the valid seed phrase and wallet address return { seedPhrase, walletAddress }; } // Generate a valid wallet and destructure the result into seedPhrase and walletAddress const { seedPhrase, walletAddress } = generateValidWallet(); console.log("Seed phrase: ", seedPhrase); console.log("Wallet address: ", walletAddress); ``` After running the script, you will receive two types of information: the `Seed Phrase` and the `Address`. - Seed Pharse: is a sequence of 12, 18, or 24 random words used to recover a cryptocurrency wallet. It acts as a master security key to access your crypto assets. If you lose access to your wallet, you can use the seed phrase to restore the wallet on a new device. The seed phrase is very important and should be stored in a secure place because whoever has it will have access to your wallet. - Address: is a string of characters that represents a wallet on the blockchain. This address is used to send and receive digital assets like cryptocurrencies.Each wallet has a unique address, and it functions similarly to a bank account number, but for transactions on the blockchain. ### 2. Faucet To operate and deploy transactions, we need gas, which is the SUI coin. In testnet or devnet environments, there are faucets available to receive SUI completely for free. ```js // Import the getFaucetHost function and requestSuiFromFaucetV0 function from the @mysten/sui.js/faucet library import { getFaucetHost, requestSuiFromFaucetV0 } from '@mysten/sui.js/faucet'; // Use the requestSuiFromFaucetV0 function to request SUI tokens from the faucet // This function takes an object with the faucet host URL and the recipient address as parameters const result = await requestSuiFromFaucetV0({ // Get the faucet host URL for the 'devnet' network using getFaucetHost host: getFaucetHost('devnet'), // Specify the recipient address where the SUI tokens should be sent recipient: '0xa36685efa3218d6ed3f9ced255f3888911eb07bfad006223b670e248326b96c4', }); console.log(JSON.stringify(result, null, 2)); ``` ### 3. Connecting to Sui Network The SuiClient 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: - Local: http://127.0.0.1:9000 - Devnet: https://fullnode.devnet.sui.io:443 - Testnet: https://fullnode.testnet.sui.io:443 - Mainnet: https://fullnode.mainnet.sui.io:443 ```js // Import the required functions and classes from the @mysten/sui.js/client library import { getFullnodeUrl, SuiClient } from '@mysten/sui.js/client'; // Create a new SuiClient instance, connecting to the fullnode URL on the testnet const client = new SuiClient({ url: getFullnodeUrl('testnet') }); // Define the owner variable with the wallet address, represented as a hexadecimal string const owner = '0x5aaff9678d0feabda7f8756c50dc55d972df130055ececc0ee1f9ea9a6246df0'; // Use the getCoins method to retrieve information about the coins owned by the specified address const result = await client.getCoins({ owner: owner, }); console.log(JSON.stringify(result, null, 2)); ``` Response ```json { "data": [ { "coinType": "0x2::sui::SUI", // The type of the coin (Contract Address), in this case "SUI", identified by the code "0x2::sui::SUI". "coinObjectId": "0xc2fd40f3da1a7445a660d690e1ae36f1c2b9d7b0eeb45a8ef24164f0da83bc5f", // Object ID of the coin object within the blockchain system. "version": "341467969", // The version of the transaction or state of the coin object. "digest": "9Ntr27eLaKpFnrmx5gyB81DtzgeEQ6g4V3vgYDu7nfSK", // The hash value used to verify the integrity of the transaction or coin object. "balance": "189313688", // The current balance of the coin, represented as a string of integers. (Display is in MIST, where 1 SUI equals 1,000,000,000 MIST. This means the balance of 189,313,688 MIST is equivalent to 0.189313688 SUI.) "previousTransaction": "BYn3njUaLoD876htSiGrpLZF9gUSc6xYFPng3FaVCrkn" // The identifier of the previous transaction the coin was involved in. } ], "nextCursor": "0xc2fd40f3da1a7445a660d690e1ae36f1c2b9d7b0eeb45a8ef24164f0da83bc5f", // A cursor used to reference the next position when fetching more data (if available). "hasNextPage": false // Flag indicating whether there is more data available (for pagination). } ``` ### 4. Transfer Sui To create a transaction for transferring SUI via blockchain, you will need the **Seed Phrase** of the sender's wallet and the **recipient's wallet address**. ```js // Import necessary modules from the @mysten/sui.js library import { getFullnodeUrl, SuiClient } from '@mysten/sui.js/client'; import { TransactionBlock } from '@mysten/sui.js/transactions'; import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519'; import { MIST_PER_SUI } from '@mysten/sui.js/utils'; // Create a new SuiClient instance, connecting to the testnet fullnode URL const client = new SuiClient({ url: getFullnodeUrl('testnet') }); // Define the mnemonic seed phrase used to generate the keypair const seedPhrase = "wall exhaust sand suggest profit voice mango brisk artist educate gesture media"; // Define the recipient address const addressReceiver = '0xa36685efa3218d6ed3f9ced255f3888911eb07bfad006223b670e248326b96c4'; // Derive the keypair from the provided seed phrase const keypair = Ed25519Keypair.deriveKeypair(seedPhrase); // Get the public address from the keypair const address = keypair.getPublicKey().toSuiAddress(); // Define the amount to send, converting from SUI to MIST const amoutSend = 0.1 * Number(MIST_PER_SUI); // Set the gas budget for the transaction const gasBudgetAmount = 5000000; // Create a new transaction block const txb = new TransactionBlock(); // Split the coin object for the specified amount to be sent const [coinOut] = txb.splitCoins(txb.gas, [txb.pure(amoutSend)]); // Add the transferObjects transaction to the transaction block txb.transferObjects([coinOut], txb.pure(addressReceiver)); // Set the gas budget for the transaction block txb.setGasBudget(gasBudgetAmount); // Sign and execute the transaction block using the derived keypair const result = await client.signAndExecuteTransactionBlock({ signer: keypair, transactionBlock: txb, requestType: 'WaitForLocalExecution', // Wait for local execution of the transaction options: { showEffects: true, // Show the effects of the transaction showBalanceChanges: true // Show balance changes after the transaction } }); console.log(JSON.stringify(result, null, 2)); ``` ### References - [Sui Documnet](https://docs.sui.io/) - [Sui Wallet](https://chromewebstore.google.com/detail/sui-wallet/opcgpfmipidbgpenhmajoajpbobppdil)