Zarnith Docs
Try Now
  • Welcome
  • Getting Started
    • Quickstart
    • The Tech Behind Zarnith
  • Fee Router SDK
    • Installation
    • Core Concepts
  • Quick Start
  • Examples
    • Revenue Sharing dApp
    • Rollup Sequencer Incentive Manager
Powered by GitBook
On this page

Quick Start

Here's a basic example to get you started with the ZarnithFi Router SDK:

import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import { RouterSDK } from '@zarnithfi/sol-fee-router';

// Setup connection and wallet
const connection = new Connection('https://api.mainnet-beta.solana.com');
const wallet = new Keypair(); // In a real app, use a proper wallet adapter

// Initialize the RouterSDK
const routerSDK = new RouterSDK(connection, wallet);

// Define destinations with percentage splits
const destinations = [
  {
    address: new PublicKey('Treasuryaddress'),
    percentage: 50 // 50%
  },
  {
    address: new PublicKey('Developeraddress'),
    percentage: 30 // 30%
  },
  {
    address: new PublicKey('Marketingaddress'),
    percentage: 20 // 20%
  }
];

// Create a router
const createRouter = async () => {
  try {
    const signature = await routerSDK.createRouter(destinations);
    console.log(`Router created with transaction signature: ${signature}`);
  } catch (error) {
    console.error('Error creating router:', error);
  }
};

// Route SOL to destinations
const routeSol = async () => {
  try {
    const routerAddress = await routerSDK.getRouterAddress();
    const signature = await routerSDK.routeSolFees(routerAddress, 1); // Route 1 SOL
    console.log(`SOL routed with transaction signature: ${signature}`);
  } catch (error) {
    console.error('Error routing SOL:', error);
  }
};

Initialize the SDK

import { Connection, PublicKey } from '@solana/web3.js';
import { RouterSDK } from 'zarnith-router';
import { useWallet } from '@solana/wallet-adapter-react'; // or your preferred wallet provider

// Initialize the SDK
const connection = new Connection("https://api.mainnet-beta.solana.com");
const wallet = useWallet(); // Use your wallet adapter
const routerSDK = new RouterSDK(connection, wallet);

Create a New Router

// Define destinations and percentages (must sum to 100%)
const destinations = [
  {
    address: new PublicKey('...'), // Treasury
    percentage: 50 // 50%
  },
  {
    address: new PublicKey('...'), // Development team
    percentage: 30 // 30%
  },
  {
    address: new PublicKey('...'), // Marketing
    percentage: 20 // 20%
  }
];

// Create the router
const signature = await routerSDK.createRouter(destinations);
console.log(`Router created: ${signature}`);

Route SOL to Destinations

// Get the router address for the current wallet
const routerAddress = await routerSDK.getRouterAddress();

// Route 1 SOL to the destinations
const signature = await routerSDK.routeSolFees(routerAddress, 1); // 1 SOL
console.log(`SOL routed: ${signature}`);

Update Router Destinations

// Define new destinations
const newDestinations = [
  {
    address: new PublicKey('...'),
    percentage: 40 // 40%
  },
  {
    address: new PublicKey('...'),
    percentage: 40 // 40%
  },
  {
    address: new PublicKey('...'),
    percentage: 20 // 20%
  }
];

// Update the router destinations
const signature = await routerSDK.updateDestinations(routerAddress, newDestinations);
console.log(`Router updated: ${signature}`);

Get Router Data

// Fetch router data
const routerData = await routerSDK.getRouterData(routerAddress);

console.log(`Router owner: ${routerData.owner.toString()}`);
console.log('Destinations:');
routerData.destinations.forEach(dest => {
  console.log(`  ${dest.address.toString()}: ${dest.percentage}%`);
});

Close Router and Reclaim Rent

// Close the router account
const signature = await routerSDK.closeRouter(routerAddress);
console.log(`Router closed: ${signature}`);
PreviousCore ConceptsNextRevenue Sharing dApp

Last updated 1 month ago