Bài viết
Chia sẻ kiến thức của bạn.
Setting Up a Project to Test the SUI Name Service (SuiNS)
The SUI Name Service (SuiNS) is a decentralized naming system on the SUI blockchain that allows users to map human-readable names (e.g., "0xduck.sui") to blockchain addresses or other data, enhancing usability and accessibility.
For developers, testing the SuiNS SDK is an essential step to ensure applications can resolve these names correctly. In this article, we’ll walk you through the process of setting up a project to test the SuiNS SDK, from preparing your development environment to querying a name record like "0xduck.sui" and understanding the results.
Introduction to SUI Name Service
The SUI Name Service (SuiNS) simplifies blockchain interactions by allowing users to register memorable names instead of using complex cryptographic addresses. For example, instead of sending tokens to "0x1234...abcd", you could send them to "0xduck.sui". Testing the SuiNS SDK ensures that your application can correctly interact with this system, retrieving and interpreting name records as needed.
In this project, we’ll set up a Node.js environment, connect to the SUI mainnet, and write a script to query the name record for "0xduck.sui". By the end, you’ll have a working setup to explore SuiNS further.
Prerequisites
Before starting, ensure you have the following:
-
Node.js (version 14 or higher) and npm (version 6 or higher) installed. Download them from nodejs.org if needed.
-
Basic understanding of JavaScript, particularly asynchronous programming (e.g., async/await).
-
A code editor like Visual Studio Code (VS Code) for writing and running scripts.
-
An internet connection to install packages and connect to the SUI.
Setting Up the Development Environment
Let’s create a new project directory and initialize it as a Node.js project. This will provide a clean workspace for our test. Step-by-Step Instructions:
- Open your terminal (e.g., Command Prompt, PowerShell, or a Unix shell).
Create a new directory:
mkdir suins-test-project
cd suins-test-project
Initialize a Node.js project: Run this command to create a package.json file with default settings:
npm init -y
Your project is now set up with a basic structure.
Installing Dependencies
Run the following in your terminal:
npm install @mysten/suins @mysten/sui
Verify the Installation: Check that the packages are added to package.json under dependencies. You can also confirm the installed versions by running:
npm list @mysten/suins @mysten/sui
This should output something like:
suins-test-project@1.0.0
├── @mysten/sui@x.x.x
└── @mysten/suins@y.y.y
Configuring the SUI Client
The SUI client connects your project to the SUI blockchain. For testing, we’ll use the mainnet.
Create a Script File:
In your project directory, create a file named test-suins.js
:
touch test-suins.js # On Unix/macOS
echo. > test-suins.js # On Windows
Open test-suins.js
in your editor and add the following:
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
// Create a SUI client connected to the testnet
const suiClient = new SuiClient({ url: getFullnodeUrl('testnet') });
Note: If you encounter an error about ES modules (e.g., "Cannot use import statement outside a module"), add "type": "module" to your package.json:
{
"name": "suins-test-project",
"version": "1.0.0",
"type": "module",
...
}
Testing the Name Service
Now, let’s write a script to query the name record for "0xduck.sui" and log the result.
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
import { SuinsClient } from '@mysten/suins';
// Create a SUI client connected to the testnet
// const suiClient = new SuiClient({ url: getFullnodeUrl('mainnet') });
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
// Create a SuiNS client using the SUI client
const suinsClient = new SuinsClient({
client,
network: 'mainnet',
});
// Function to test the name service
async function testNameService() {
try {
// Query the name record for 'demo.sui'
const nameRecord = await suinsClient.getNameRecord('0xduck.sui');
// Log the result
console.log('Name Record for "0xduck.sui":', nameRecord);
} catch (error) {
console.error('Error fetching name record:', error.message);
}
}
testNameService();
In your terminal, execute:
node test-suins.js
- SDKs and Developer Tools