Skip to content

SDK quickstart

Create a viem wallet client for the active account, then bind SilentSwap to the gateway chain.

import {
  createSilentSwapClient,
} from '@silentswap/sdk';
import {
  createWalletClient,
  custom,
  parseUnits,
  type Address,
} from 'viem';
import { mainnet } from 'viem/chains';
 
declare const provider: Parameters<typeof custom>[0];
declare const account: Address;
 
const walletClient = createWalletClient({
  account,
  chain: mainnet,
  transport: custom(provider),
});
 
const client = createSilentSwapClient({
  walletClient,
});
 
const quote = await client.quote({
  privacy: true,
  inputAddress: account,
  sourceAsset: 'USDC',
  sourceChainId: 1,
  outputs: [{
    address: '0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC',
    chainId: 8453,
    amount: parseUnits('25', 6),
  }],
});
 
// Render every output and fee before this line.
const order = await client.placeOrder(quote);
 
const stop = client.trackOrderViaWebSocket(
  order.reference,
  (state) => console.log(state.status),
  console.error,
);
 
stop;

Keep the unsubscribe function for component cleanup. For a narrated approval step, call hasAllowance, then ensureAllowance, before placeOrder; the latter safely rechecks allowance.