Butter Network
Github
  • 🌈About Butter Network
  • 🔥ZK Light Client
    • Understanding Light Client
    • Refactored ZK Light Client
  • 🔥Omnichain Development
    • Omnichain Isomorphism
    • More Possibilities
  • 🌈Butter Omnichain Messaging Integration
    • Introduction
    • Integration Guide
      • Message
      • Message with Ton
      • CALLDATA
      • Message And Relay
    • Contract Interface
    • Fee
    • Deployed Contracts
    • 👨‍💻API for Omnichain Message
      • GET Message History by Source Address
      • GET Message Info by Id
      • GET Message Info by Source Hash
  • 🔥Butter Bridge Integration
    • Integration Guide
    • Contract Interface
    • Fee
    • Deployed Contracts
  • 💰Butter Swap Integration
    • Introduction
    • 🟢Integration Guide
      • Contract Interface
      • Fee
    • Deployed Contracts
      • v2.1
    • 👩‍💻API for Routing
      • Integration Guide
      • GET /route
      • GET /swap
      • GET /routeAndSwap
      • GET /supportedChainList
      • GET /findToken
      • 🔴Error Code List
    • 👨‍💻API for Swap Data
      • GET Swap History by Source Hash
      • GET Swap History by Source Address
      • GET Swap Info by Order ID
      • GET Supported Chain Detail List
      • GET Supported Token Detail List
  • 💰Butter Swap User Guide
    • 🫂User Guide
  • Butter Swap SDK
    • Install
    • Config SDK
    • Request Routes
    • Execute Route
    • Others
  • Butter Swap Widget
    • Usage
  • 📢Resources
    • 🏠Website
    • 🫂Telegram
    • 📄X
Powered by GitBook
On this page
  • Installation
  • yarn
  • npm
  • Quick Start
  • Setup SDK
  • Usage
  1. Butter Swap SDK

Install

Integrate Butter SDK to your dApp/Swap UI

Installation

To get started, install the latest version of Butter SDK

yarn

yarn add @butternetwork/sdk

npm

npm i @butternetwork/sdk

Check out our complete examples in the SDK repository, and feel free to file an issue if you encounter any problems.

Quick Start

Setup SDK

Config rpcs

import { butterConfig, ChainType } from "@butternetwork/sdk";

butterConfig.setOptions({
  providers: [],
  rpcs: {
    [ChainType.NEAR]: [],
    [ChainType.SOLANA]: [],
    [ChainType.TRON]: {
      urls: ["https://api.trongrid.io"],
      headers: {
        "TRON-PRO-API-KEY": "your-api-key",
      },
    },
    [ChainType.TON]: [],
  },
});

Config EVM

import { useWalletClient } from "wagmi";

const { data: signer } = useWalletClient();

useEffect(() => {
  butterConfig.setOptions({
    providers: [
      createEvmProvider({
        walletClient: signer as any,
      }),
    ],
  });
}, [signer]);

Config Near

import { setupWalletSelector } from "@near-wallet-selector/core";

const selector = await setupWalletSelector({
  network: nearConfig.networkId as NetworkId,
  debug: true,
  modules: [
    // ...modules
  ],
});

useEffect(() => {
  selector.wallet().then((wallet) => {
    butterConfig.setOptions({
      providers: [
        createNearProvider({
          getWallet: async () => wallet as any,
        }),
      ],
    });
  });
}, [selector]);

Config Solana

import { useWallet } from "@solana/wallet-adapter-react";

const { wallet } = useWallet();

useEffect(() => {
  butterConfig.setOptions({
    providers: [
      createSolanaProvider({
        getWalletAdapter: async () => wallet?.adapter as any,
      }),
    ],
  });
}, [wallet]);

Config Tron

import { useWallet as useTronWallet } from "@tronweb3/tronwallet-adapter-react-hooks";

const { wallet: tronWallet } = useTronWallet();

useEffect(() => {
  butterConfig.setOptions({
    providers: [
      createTronProvider({
        getAdapter: async () => tronWallet?.adapter as any,
      }),
    ],
  });
}, [tronWallet]);

Config Ton

import { useTonConnectUI } from "@tonconnect/ui-react";

const [tonConnectUI, setOptions] = useTonConnectUI();
useEffect(() => {
  if (!tonConnectUI) {
    return;
  }
  butterConfig.setOptions({
    providers: [
      createTonProvider({
        getConnector: async () => tonConnectUI as any,
      }),
    ],
  });
}, [tonConnectUI]);

Usage

Request Routes

import { getRoutes } from "@butternetwork/sdk";

const routes = await getRoutes({
  fromChainId: "22776",
  toChainId: "56",
  amount: "1000",
  tokenInAddress: "0x0000000000000000000000000000000000000000",
  tokenOutAddress: "0x55d398326f99059ff775485246999027b3197955",
  type: "exactIn",
  slippage: "200",
  entrance: "Butter+",
  abortSignal: signal,
});

Request Txn

import { generateSwapData } from "@butternetwork/sdk";

const txn = await generateSwapData({
  hash: "0x123......", // hash from getRoutes
  slippage: "100",
  receiver: "0x123123...",
  from: "0x123123...",
});

Execute Route

import { executeRoute } from "@butternetwork/sdk";
// Near
const hash = await executeRoute(txn);
// Other Chain
// const hash = await executeRoute(txn[0]);
PreviousUser GuideNextConfig SDK

Last updated 4 months ago