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
  • On Chain trade calculation
  • Cross Chain trade calculation
  • What is next?
  • Advanced documentation

Was this helpful?

  1. Integrate SDK

Trade calculation

Before trade calculation you should Set up SDK first.

PreviousSet up SDKNextSwap Execution

Last updated 11 months ago

Was this helpful?

Swaps in our SDK separated to two different type of swaps:

  • On-chain swaps- Since Rubic aggregates several DEXs, users can execute On-Chain Swaps and Bridges on Ethereum, BNB Smart Chain, Polygon, Harmony, Avalanche, Fantom, Moonriver, Solana, Arbitrum, Aurora, NEAR, and Telos other networks. By also utilizing external DEXs, users are ensured the best possible rates.

  • Cross-chain swaps: Rubic's protocol offers easy trading for more than 15,000+ assets across Polygon, BNB Smart Chain, Ethereum, Avalanche, MoonRiver, Fantom, Solana, Harmony, Arbitrum, Aurora, NEAR, Telos and other networks, using our custom and unique Cross-Chain Protocol. We utilize liquidity pools and our own smart contracts, Rubic provides an extremely easy-to-use Cross-Chain solution to all traders. Cross-Chain swaps are available between 80+ major networks right now

On Chain trade calculation

To use on-chain swaps and calculate them, you need an instantTrades object located in the . Inside the object is the calculateTrade method, which takes the input token, the input swap amount, the output token address, and the transaction parameters.

calculateTrade(
  fromToken:
    | Token
    | {
          address: string;
          blockchain: BlockchainName;
      }
    | PriceToken,
  fromAmount: string | number,
  toToken: Token | string | PriceToken,
  options?: OnChainManagerCalculationOptions
): Promise<Array<OnChainTrade | OnChainTradeError>>

The method returns an array of calculated DEX which can be represented as OnChainTrade object or OnChainTradeError object. Both contains DEX type, but first contains the trade and the second contains error reason. Example:

const sdk = await SDK.createSDK(config);

const fromToken = {
  blockchain: BLOCKCHAIN_NAME.ETHEREUM,
  address: '0x0000000000000000000000000000000000000000'
};
const fromAmount = 1;
const toTokenAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';

// calculate trades
const trades = await sdk.onChainManager.calculateTrade(fromToken, fromAmount, toTokenAddress);
const bestTrade = trades[0];

Done!

Cross Chain trade calculation

async calculateTrade(
  fromToken:
    | Token
    | {
          address: string;
          blockchain: BlockchainName;
      }
    | PriceToken,
  fromAmount: string | number | BigNumber,
  toToken:
    | Token
    | {
          address: string;
          blockchain: BlockchainName;
      }
    | PriceToken,
  options?: CrossChainManagerCalculationOptions
): Promise<WrappedCrossChainTrade[]>

It returns WrappedCrossChainTrade object.

interface WrappedCrossChainTrade {
    /**
     * Calculated cross chain trade.
     * Sometimes trade can be calculated even if error was thrown.
     * Equals `null` in case error is critical and trade cannot be calculated.
     */
    trade: CrossChainTrade | null;

    /**
     * Type of calculated trade.
     */
    tradeType: CrossChainTradeType;

    /**
     * Error, thrown during calculation.
     */
    error?: RubicSdkError;
}

Using example:

// create SDK instance
const sdk = await RubicSDK.SDK.createSDK(configuration);

// Define trade parametres
const fromToken = {
    blockchain: BLOCKCHAIN_NAME.ETHEREUM,
    address: '0x0000000000000000000000000000000000000000'
};
const fromAmount = 1;
const toToken = {
    blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN,
    address: '0xe9e7cea3dedca5984780bafc599bd69add087d56'
};

// calculated trades
const trades = await sdk.crossChainManager.calculateTrade(fromToken, fromAmount, toToken);
const bestTrade = trades[0];

What is next?

In this step, you get the real trade object. You have one last step left - execute a swap.

To use cross-chain swaps and calculate them, you need a crossChain object, located in the . Inside the object is the calculateTrade method, which takes the input token, the input swap amount, the output token, and the transaction parameters.

⚒️
3️⃣
SDK instance
SDK instance
Advanced documentation