3️⃣Trade calculation

Before trade calculation you should Set up SDK first.

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 SDK instance. 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

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

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.

Last updated