Skip to content

Getting Started

This guide will walk you through the fundamentals of using RISE Chain's Shred API to build real-time blockchain applications.

Core Concepts

Before diving into code, let's understand the key concepts:

Shreds

Lightweight packets containing transaction state updates that propagate instantly across the network.

Synchronous Transactions

Transactions that return receipts immediately, eliminating the wait for block inclusion.

Real-Time Subscriptions

WebSocket connections that stream shreds and events as they happen.

Your First Shred Transaction

Let's send your first transaction using the Shred API's synchronous confirmation feature.

Import Required Modules

index.ts
import { createWalletClient, createPublicClient, http, parseEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { riseTestnet } from 'viem/chains'
import { sendTransactionSync } from 'shreds/viem'

Set Up Client

index.ts
// Create account from private key
const account = privateKeyToAccount('0x...')
 
// Create wallet client
const walletClient = createWalletClient({
  account,
  chain: riseTestnet,
  transport: http()
})

Send a Synchronous Transaction

index.ts
async function sendInstantTransaction() {
  // Send ETH with instant confirmation
  const receipt = await sendTransactionSync(walletClient, {
    to: '0x742d35Cc6634C0532925a3b844Bc9e7595f6E98d',
    value: parseEther('0.1'),
  })
  
  console.log('Transaction confirmed instantly!')
  console.log('Transaction receipt:', receipt)
}
 
sendInstantTransaction()

Reading State with Shreds

RISE Chain's state reading methods automatically use the latest shred state, giving you real-time accuracy.

read-state.ts
import { createPublicClient, http, formatEther } from 'viem'
import { riseTestnet } from 'viem/chains'
 
const client = createPublicClient({
  chain: riseTestnet,
  transport: http()
})
 
async function checkBalance() {
  // This returns the balance including pending shred updates
  const balance = await client.getBalance({
    address: '0x742d35Cc6634C0532925a3b844Bc9e7595f6E98d'
  })
  
  console.log('Real-time balance:', formatEther(balance), 'ETH')
}
 
// Check balance multiple times to see real-time updates
setInterval(checkBalance, 1000)

Subscribing to Shreds

For real-time applications, subscribe to shred updates using WebSocket connections.

subscribe-shreds.ts
import { createPublicClient, webSocket } from 'viem'
import { riseTestnet } from 'viem/chains'
import { watchShreds } from 'shreds/viem'
 
const wsClient = createPublicClient({
  chain: riseTestnet,
  transport: webSocket()
})
 
// Subscribe to all shreds
const unwatch = watchShreds(wsClient, {
  onShred: (shred) => {
    console.log('New shred received:', {
      index: shred.shredIndex,
      timestamp: shred.blockTimestamp,
      transactions: shred.transactions.length
    })
  }
})
 
// Unsubscribe when done
// unwatch()

Watching for Events

Monitor smart contract events in real-time with shred-enhanced event watching.

watch-events.ts
import { createPublicClient, webSocket, parseAbi } from 'viem'
import { riseTestnet } from 'viem/chains'
 
const wsClient = createPublicClient({
  chain: riseTestnet,
  transport: webSocket()
})
 
const abi = parseAbi([
  'event Transfer(address indexed from, address indexed to, uint256 value)'
])
 
// Watch for Transfer events
const unwatch = wsClient.watchContractEvent({
  address: '0x...', // Token contract address
  abi,
  eventName: 'Transfer',
  onLogs: (logs) => {
    logs.forEach(log => {
      console.log('Transfer detected:', {
        from: log.args.from,
        to: log.args.to,
        value: log.args.value?.toString()
      })
    })
  }
})

Next Steps

Now that you understand the basics: