Create SDK

Create Palindrome SDK

Before you can use any of the SDK functions, you need to properly initialize and configure the required clients.

Setup & Initialization

Public Client (on-chain reads)

// config/viem.ts
import { createPublicClient, http } from 'viem';
import { bscTestnet } from 'viem/chains';

export const publicClient = createPublicClient({
  chain: bscTestnet,
  transport: http(),
});

SDK Factory

// lib/createSDK.ts
import { PalindromeEscrowSDK } from '@palindromecryptoescrow/sdk';
import { WalletClient } from 'viem';
import { publicClient } from '@/config/viem';
import { apolloClient } from '@/config/apollo';
import { bscTestnet } from 'viem/chains';

export const createPalindromeSDK = (walletClient?: WalletClient) => {
  return new PalindromeEscrowSDK({
    publicClient,
    walletClient: walletClient ?? undefined,
    chain: bscTestnet,
  });
};

Connect Wallet & Instantiate SDK

import { createPalindromeSDK } from '@/lib/createSDK';
import { createWalletClient, custom } from 'viem';
import { bscTestnet } from 'viem/chains';

export const connectAndInitSDK = async () => {
  if (!window.ethereum) throw new Error('MetaMask not detected');

  const walletClient = createWalletClient({
    chain: bscTestnet,
    transport: custom(window.ethereum),
  });

  const [address] = await walletClient.requestAddresses();
  await walletClient.switchChain({ id: bscTestnet.id });

  const sdk = createPalindromeSDK(walletClient);

  console.log('Palindrome SDK ready for:', address);
  return { sdk, address, walletClient };
};

You can now call any SDK method:

const escrows = await sdk.getEscrows();
const escrow = await sdk.getEscrowByIdParsed(5n);
await sdk.deposit(walletClient, 5n);

Pro Tip

You can set up your own database to manage the data independently. While The Graph specializes in indexing blockchain events and providing efficient querying, managing your own database gives you full control over data structure, storage, and custom queries.

Previous
Getting started