Rubic Documents
  • RUBIC
    • Overview
    • Rubic's Ecosystem
    • Security
    • Tokenomics
    • Roadmap
    • Rubic Discord Roles
    • “Swap to Earn” Program on Rubic
    • MEV-bot Protection
    • B2B Cross-Chain Toolkit
      • Use Cases
      • Why Rubic?
    • Refund Guidelines for Stuck Transactions
  • ⚒️Integrate SDK
    • SDK Overview
    • SDK Architecture
    • SDK Advantages
    • SDK Integration
    • 1️⃣Install SDK
    • 2️⃣Set up SDK
    • 3️⃣Trade calculation
    • 4️⃣Swap Execution
    • 📜Advanced documentation
    • Migration from V2 to V3
    • Contact Us
  • Integrate Widget
    • Widget Overview
    • Instruction
    • White Label Widget Integration Instruction
    • Partners
    • Listing of tokens
  • Rubic API
    • 🔐Tokens API
    • Referrer And Rate Limits
    • Rubic API
      • Supported chains
      • Supported providers
      • Request Quote
      • Request Data
      • Get Cross-Chain Status
      • Integration example
        • Swaps from EVM
        • Swaps from Solana
        • Swaps from TON
        • Swaps from Tron
        • Swaps from Bitcoin
        • Swaps VIA deposit
      • Swagger
      • Models
        • OnChainTradeType
        • CrossChainTradeType
        • FeesDto
        • RoutingDto
        • TransactionDto
        • ErrorDto
      • Errors
    • Monetization And Fees
  • FAQ
    • What is Rubic?
    • How to start using Rubic?
    • Who can use our service?
    • Is KYC required?
    • What are the minimum and maximum trade sizes?
    • Should I list my token before using your service?
    • How to create a Swap?
    • How long does it take to complete a transaction?
    • How is the privacy and security of the transaction ensured?
    • How does Rubic unite different DEX platforms?
    • Why does Rubic offer a choice between different providers?
    • How do I add the BNB Smart Chain and Polygon networks to MetaMask?
    • What should I do when I see the message: “Trading on Uniswap is not available?”
    • What should I do if Uniswap doesn’t display any rates?
    • How can we integrate the Rubic Relay widget?
    • How do we get listed on Rubic?
    • How to Complete swaps to/from TON network using Rubic
  • Contacts
    • Community
    • PR Marketing
    • Business Development
    • Support
    • Influencers Collaboration Requests
  • Legal Documentation
    • Privacy Policy
    • Terms of Use
    • Third-Party Software
  • Audits
    • MixBytes Audit
  • Pitch Deck
Powered by GitBook
On this page
  • SDK Initiation
  • Configuration providing
  • Your wallet address as integrator
  • Update SDK configuration
  • SDK object
  • Advanced documentation

Was this helpful?

  1. Integrate SDK

Set up SDK

After you have installed the SDK, you first need to set it up.

SDK Initiation

After installation, you will need to initialize the SDK.

Set up after CDN installation

const config = {
  rpcProviders: {
    BSC: {
      rpcList: ['https://bsc-dataseed.binance.org/', ...]
    },
    POLYGON: {
      rpcList: ['https://polygon-rpc.com', ...]
    },
    TRON: {
      rpcList: [
        {
          fullHost: '<tron-api>',
          headers: { "TRON-PRO-API-KEY": 'your api key' }
        }
      ]
    }
  }
};

const rubicSdk = await RubicSDK.SDK.createSDK(config);

Set up after npm installation

In case of npm installation, you have to import SDK from rubic-sdk package. Then you can initiate it:

import SDK, { BLOCKCHAIN_NAME, Configuration } from 'rubic-sdk';

const config: Configuration = {
  rpcProviders: {
    BSC: {
      rpcList: ['https://bsc-dataseed.binance.org/', ...]
    },
    POLYGON: {
      rpcList: ['https://polygon-rpc.com', ...]
    },
    TRON: {
      rpcList: [
        {
          fullHost: '<tron-api>',
          headers: { "TRON-PRO-API-KEY": 'your api key' }
        }
      ]
    }
  }
};

const rubicSDK = await SDK.createSDK(config)

Configuration providing

To create SDK instance you should provide config parameter, this is an object containing the rpc, wallets, http client settings, and your personal integrator wallet address.

interface Configuration {
  readonly rpcProviders: RpcProviders;
  readonly walletProvider?: WalletProvider;
  readonly httpClient?: HttpClient;
  readonly providerAddress?: Partial<ProviderAddress>;
}

Providing RPC options

RpcProviders is Rpc data to connect to blockchains you will use. You have to pass rpcProvider for each blockchain you will use with sdk, as presented in example below:

type RpcProviders =  Partial<
  Record<EvmBlockchainName, RpcProvider<string>> &
  Record<TronBlockchainName, RpcProvider<TronWebProvider>> &
  Record<SolanaBlockchainName, RpcProvider<string>>
>;

interface RpcProvider<T> {
  /**
   * Rpc links. Copy it from your rpc provider website
   * (like Infura, Quicknode, Getblock, Moralis, etc).
   */
  readonly rpcList: T[];
}

Providing wallet options

If at the time when you initiating SDK, users have already logged into your platform, you can provide walletProvider object, but it is no necessary, until you want to swap with SDK:

type WalletProvider = Partial<{
  readonly [CHAIN_TYPE.EVM]?: WalletProviderCore<provider | Web3>;
  readonly [CHAIN_TYPE.TRON]?: WalletProviderCore<typeof TronWeb>;
  readonly [CHAIN_TYPE.SOLANA]?: WalletProviderCore<SolanaWeb3>;
}>;

interface WalletProviderCore<T> {
  /**
   * Core provider.
   */
  readonly core: T;

  /**
   * User wallet address.
   */
  readonly address: string;
}

If user with address 0xB72DE277A5s577C945B0C52acD8fD2a7cFDaFD18 logged in with Ethereum network, right walletProvider object should be as follows:.

const walletProvider = {
  [CHAIN_TYPE.EVM]: {
    core: window.ethereum,
    address: '0x0123....89'
  }
};

Your own client for HTTP requests

You can also pass your own http client as httpClient parameter (e.g. HttpClient in Angular) if you have it, to not duplicate http clients and decrease bundle size.

Your wallet address as integrator

This is one of the most important steps, please send to the Rubic team the providerAddress - the wallet address to which the fees will be credited. To make it work properly, you need to contact us so that we can whitelist you.

Update SDK configuration

When user finally connects the walet, you have to notify SDK about it.

const defaultConfig = {
  rpcProviders: {
    BSC: {
      rpcList: ['https://bsc-dataseed.binance.org/', ...]
    },
    POLYGON: {
      rpcList: ['https://polygon-rpc.com', ...]
    },
    TRON: {
      rpcList: [
        {
          fullHost: '<tron-api>',
          headers: { "TRON-PRO-API-KEY": 'your api key' }
        }
      ]
    }
  }
};
const rubicSDK = await SDK.createSDK(defaultConfig);

const newConfig = {
  ...defaultConfig,
  walletProvider: {
    [CHAIN_TYPE.EVM]: {
      core: window.ethereum,
      address: '0x0123....89'
    }
  }
};
await rubicSDK.updateConfiguration(newConfig);

You should do it on every address and network change events

SDK object

When SDK was initiated, you are able to refer it's fields and methods.

interface SDK {
    /**
     * On-chain manager object. Use it to calculate and create on-chain trades.
     */
    public readonly onChainManager: OnChainManager;

    /**
     * Cross-chain trades manager object. Use it to calculate and create cross-chain trades.
     */
    public readonly crossChainManager: CrossChainManager;

    /**
     * On-chain status manager object. Use it for special providers, which requires more than one trade.
     */
    public readonly onChainStatusManager: OnChainStatusManager;

    /**
     * Cross-chain status manager object. Use it to get trade statuses on source and target network.
     */
    public readonly crossChainStatusManager: CrossChainStatusManager;

    /**
     * Cross-chain symbiosis manager object. Use it to get pending trades in symbiosis and revert them.
     */
    public readonly crossChainSymbiosisManager: CrossChainSymbiosisManager;

    /**
     * Deflation token manager object. Use it to check specific token for fees or deflation.
     */
    public readonly deflationTokenManager: DeflationTokenManager;

    /**
     * Use it to create limit order.
     */
    public readonly limitOrderManager: LimitOrderManager;

    /**
     * Can be used to get `Web3Public` instance by blockchain name to get public information from blockchain.
     */
    public get web3PublicService(): Web3PublicService {
        return Injector.web3PublicService;
    }

    /**
     * Can be used to send transactions and execute smart contracts methods.
     */
    public get web3PrivateService(): Web3PrivateService {
        return Injector.web3PrivateService;
    }

    /**
     * Use it to get gas price information.
     */
    public get gasPriceApi(): GasPriceApi {
        return Injector.gasPriceApi;
    }

    /**
     * Use it to get coingecko price information.
     */
    public get coingeckoApi(): CoingeckoApi {
        return Injector.coingeckoApi;
    }

    /**
     * Updates sdk configuration and sdk entities dependencies.
     */
    public async updateConfiguration(configuration: Configuration): Promise<void> {
        const [web3PublicService, web3PrivateService, httpClient] = await Promise.all([
            SDK.createWeb3PublicService(configuration),
            SDK.createWeb3PrivateService(configuration),
            SDK.createHttpClient(configuration)
        ]);

        Injector.createInjector(web3PublicService, web3PrivateService, httpClient);
    }

    public updateWalletProvider(walletProvider: WalletProvider): void {
        Injector.web3PrivateService.updateWeb3PrivateStorage(walletProvider);
    }

    public updateWalletProviderCore(
        chainType: keyof WalletProvider,
        walletProviderCore: WalletProviderCore
    ): void {
        Injector.web3PrivateService.updateWeb3Private(chainType, walletProviderCore);
    }

    public updateWalletAddress(chainType: keyof WalletProvider, address: string): void {
        Injector.web3PrivateService.updateWeb3PrivateAddress(chainType, address);
    }
}
PreviousInstall SDKNextTrade calculation

Last updated 7 months ago

Was this helpful?

⚒️
2️⃣
Advanced documentation