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 = {
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);
}
}