Summary
Sui JSON-RPC is the standard interface for interacting with the Sui blockchain, enabling developers to query chain data, submit transactions, and subscribe to events. This article explains the core methods, how to connect to Sui endpoints, and common pitfalls to avoid when building on Sui.
Quick Decision: How to Connect to Sui JSON-RPC
Before diving into methods, decide which endpoint type fits your use case. For simple queries and light traffic, a public endpoint works, but for production apps, you need reliability and scalability. OnFinality provides managed Sui RPC endpoints that handle the infrastructure burden, allowing you to focus on your application.
If you are building a production app, consider using a dedicated node to avoid rate limits and ensure consistent performance. For testing or small projects, a shared public endpoint is sufficient. Check the RPC pricing to understand the options.
What is Sui JSON-RPC?
Sui JSON-RPC is a remote procedure call protocol that allows clients to interact with the Sui blockchain. It follows the JSON-RPC 2.0 specification, providing a standardized way to query chain state, submit transactions, and listen for events. Unlike Ethereum's JSON-RPC, Sui uses its own set of methods tailored to its object-centric data model.
Sui's JSON-RPC is essential for developers building wallets, explorers, dApps, and indexing services. It abstracts the complexity of the underlying node, offering a clean interface to read and write data.
Sui JSON-RPC Methods Overview
Sui's JSON-RPC methods are grouped into several categories:
- Chain Information:
sui_getChainIdentifier,sui_getProtocolConfig,sui_getLatestCheckpointSequenceNumber - Objects:
sui_getObject,sui_multiGetObjects,sui_getDynamicFieldObject - Transactions:
sui_getTransactionBlock,sui_executeTransactionBlock,sui_dryRunTransactionBlock - Events:
sui_getEvents,sui_subscribeEvent - Coin and Balance:
sui_getBalance,sui_getAllBalances,sui_getCoins - Move Modules:
sui_getMoveFunctionArgTypes,sui_getNormalizedMoveModule
Each method serves a specific purpose, and understanding them is key to efficient development.
Sui JSON-RPC Endpoints
To interact with Sui, you need an endpoint URL. OnFinality provides public endpoints for both Sui Mainnet and Testnet. For production use, you should sign up for a dedicated endpoint to get higher throughput and reliability.
Here are the official OnFinality public endpoints:
- Sui Mainnet:
https://sui-rpc.publicnode.com - Sui Testnet:
https://sui-testnet-rpc.publicnode.com
These endpoints are for testing and light use. For production, consider using a dedicated node to avoid rate limits and ensure uptime.
How to Make Sui JSON-RPC Requests
You can interact with Sui JSON-RPC using any HTTP client. Here's an example using curl to get the chain identifier:
curl -X POST https://sui-rpc.publicnode.com \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "sui_getChainIdentifier",
"params": []
}'
This returns the chain identifier, which is useful for verifying you're connected to the correct network.
Sui JSON-RPC vs. Ethereum JSON-RPC: Key Differences
Sui's JSON-RPC differs significantly from Ethereum's. While Ethereum uses account-based models, Sui uses objects. This affects how you query data and submit transactions.
| Aspect | Sui JSON-RPC | Ethereum JSON-RPC |
|---|---|---|
| Data Model | Object-centric | Account-centric |
| Transaction Format | Sui-specific | Standard Ethereum |
| Common Methods | sui_getObject, sui_executeTransactionBlock | eth_getBalance, eth_sendTransaction |
| Event Subscription | sui_subscribeEvent | eth_subscribe |
Understanding these differences helps you avoid confusion when migrating from Ethereum.
Common Sui JSON-RPC Errors and Troubleshooting
When working with Sui JSON-RPC, you may encounter errors. Here are common ones and how to fix them:
-32000Server error: Often due to invalid parameters. Check your request payload.-32602Invalid params: Ensure you're passing the correct types and required fields.-32601Method not found: Verify the method name is spelled correctly and supported.-32700Parse error: Your JSON is malformed. Validate your request body.
If you're using a public endpoint and hitting rate limits, consider upgrading to a dedicated node for higher limits.
Sui JSON-RPC Best Practices
To build reliable applications on Sui, follow these practices:
- Use WebSocket for real-time updates: For event subscriptions, use WebSocket to avoid polling.
- Batch requests: When querying multiple objects, use
sui_multiGetObjectsto reduce round trips. - Handle rate limits gracefully: Implement exponential backoff for retries.
- Monitor your usage: Track your request volume to avoid unexpected limits.
Key Takeaways
- Sui JSON-RPC is essential for interacting with the Sui blockchain.
- Choose the right endpoint type based on your needs: public for testing, dedicated for production.
- Understand the differences between Sui and Ethereum JSON-RPC to avoid confusion.
- Troubleshoot common errors by checking your request format and method names.
- For production, consider using a managed service like OnFinality to ensure reliability.
Frequently Asked Questions
What is the Sui JSON-RPC endpoint?
OnFinality provides public endpoints for Sui Mainnet and Testnet. For production, you can get a dedicated endpoint from OnFinality's Sui network page.
How do I get the Sui chain ID?
Use the sui_getChainIdentifier method to get the chain identifier.
Can I use WebSocket with Sui JSON-RPC?
Yes, Sui supports WebSocket for event subscriptions. OnFinality provides WebSocket endpoints for Sui.
What is the difference between Sui and Ethereum JSON-RPC?
Sui uses an object-centric model, while Ethereum uses accounts. This affects the methods and data structures used.
How do I troubleshoot Sui JSON-RPC errors?
Check your request format, method names, and parameters. If you're hitting rate limits, consider upgrading to a dedicated node.