Rubic Documents
Search…
⌃K
2⃣

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: Configuration = {
rpcProviders: {
BSC: {
mainRpc: 'https://bsc-dataseed.binance.org/'
},
POLYGON: {
mainRpc: 'https://polygon-rpc.com'
}
}
};
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: {
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: {
mainRpc: 'https://bsc-dataseed.binance.org/' // BSC RPC
},
[BLOCKCHAIN_NAME.POLYGON]: {
mainRpc: 'https://polygon-rpc.com' // Polygon RPC
},
// You should setup RPC for every supported blockchain.
}
};
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: Partial<Record<BlockchainName, RpcProvider>>;
readonly walletProvider?: WalletProvider;
readonly httpClient?: HttpClient;
readonly providerAddress?: string;
}

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<BlockchainName, RpcProvider>>;
interface RpcProvider {
/**
* Rpc link. Copy it from your rpc provider website
* (like Infura, Quicknode, Getblock, Moralis, etc).
*/
mainRpc: string,
/**
* Same as mainRpc. Will be used instead of mainRpc
* if mainRpc is out of timeout = mainPrcTimeout.
*/
spareRpc?: string,
/**
* Specifies timeout in ms after which mainRpc will be
* replaced with the spareRpc (if spareRpc is defined)
*/
mainRpcTimeout?: number,
/**
* Before the `mainRpc` link is applied to the sdk, all the `mainRpc` links
* will be health-checked by receiving and verifying the predefined data.
* If an error occurs during the request the `mainRpc` will be replaced with a spare one.
* This `healthCheckTimeout` parameter allows you to set the maximum allowable timeout when
* checking the `mainRpc`.
*/
healthCheckTimeout?: number
}
Only the mainRpc field is required, the other fields are optional and are passed on at will. Example of rpcProviders object:
const rpcProvides = {
[BLOCKCHAIN_NAME.POLYGON]: {
mainRpc: 'https://polygon-rpc.com',
spareRpc: 'https://rpc-mainnet.maticvigil.com ',
mainRpcTimeout: 5000,
healthCheckTimeout: 5000
},
[BLOCKCHAIN_NAME.ETHEREUM]: {
mainRpc: 'https://cloudflare-eth.com/'
},
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: {
mainRpc: 'https://bsc-dataseed1.binance.org/'
}
};

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:
interface WalletProvider = {
/**
* Core provider.
*/
readonly core: provider | Web3;
/**
* User wallet address.
*/
readonly address: string;
/**
* Selected chain in user wallet.
*/
readonly chainId: number | string;
}
If user with address 0xB72DE277A5s577C945B0C52acD8fD2a7cFDaFD18 logged in with Ethereum network, right walletProvider object should be as follows:.
const walletProvider = {
core: window.ethereum,
address: '0xB72DE277A5s577C945B0C52acD8fD2a7cFDaFD18',
chainid: 1
};

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: {
mainRpc: 'https://bsc-dataseed.binance.org/'
},
POLYGON: {
mainRpc: 'https://polygon-rpc.com'
}
}
};
const rubicSDK = await SDK.createSDK(defaultConfig);
const newConfig = {
...defaultConfig,
walletProvider: {
core: window.ethereum,
address: '0x0123....89',
chainId: 56
}
};
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 trades manager object. Use it
* to calculate and create instant trades.
*/
public readonly instantTrades: InstantTradesManager;
/**
* Cross-chain trades manager object. Use it
* to calculate and create cross-chain trades.
*/
public readonly crossChain: CrossChainManager;
/**
* Cross-chain symbiosis manager object. Use it
* to get pending trades in symbiosis and revert them.
*/
public readonly crossChainSymbiosisManager: CrossChainSymbiosisManager;
/**
* Tokens manager object. Use it to fetch and store tokens data.
*/
public readonly tokens = new TokensManager();
/**
* Can be used to get `Web3Public` instance by blockchain
* name to get public information from blockchain.
*/
public readonly web3PublicService = Injector.web3PublicService;
/**
* Can be used to send transactions and execute smart contracts methods.
*/
public readonly web3Private = Injector.web3Private;
/**
* Use it to get gas price information.
*/
public readonly gasPriceApi = Injector.gasPriceApi;
/**
* Use it to get crypto price information.
*/
public readonly cryptoPriceApi = Injector.coingeckoApi;
/**
* Updates sdk configuration and sdk entities dependencies.
*/
public async updateConfiguration(configuration: Configuration): Promise<void>;
}