Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve react example #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/react/src/components/BlockchainSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ interface BlockchainSelectorProps {
}

const blockchains = [
{
value: BLOCKCHAIN_NAME.AVALANCHE,
label: 'Avalanche'
},
{
value: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN,
label: 'BNB Chain'
},
{
value: BLOCKCHAIN_NAME.POLYGON,
label: 'Polygon'
},
{
value: BLOCKCHAIN_NAME.FANTOM,
label: 'Fantom'
}
];

Expand Down
12 changes: 10 additions & 2 deletions examples/react/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const Form = ({}: FormProps) => {
if (sdk) {
setLoading(true);
try {
console.log('Calculating trade, be patient...')
if (fromBlockchain === toBlockchain) {
const wrappedTrades = await (sdk.instantTrades.calculateTrade(fromToken, String(amount), toToken))
const bestTrade = wrappedTrades.filter(el => !(el instanceof LifiTrade))[0];
Expand All @@ -71,8 +72,15 @@ const Form = ({}: FormProps) => {
}
} else {
const wrappedTrades = await (sdk.crossChain.calculateTrade(fromToken, String(amount), toToken))
const bestTrade = wrappedTrades[0];
setTrade(bestTrade.trade);
console.log('Response:', wrappedTrades)
const validTrades = wrappedTrades.filter(x => x.trade != null)
const bestTrade = validTrades.length > 0 ? validTrades[0] : undefined
if (bestTrade) {
console.log('Best trade found:', bestTrade, bestTrade?.trade?.toTokenAmountMin.toString())
setTrade(bestTrade.trade);
} else {
console.log(`0 valid trades found from ${wrappedTrades.length} in the response`)
}
}
} finally {
setLoading(false);
Expand Down
24 changes: 15 additions & 9 deletions examples/react/src/constants/sdk-config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { BLOCKCHAIN_NAME, Configuration } from 'rubic-sdk';

export const configuration: Configuration = {
rpcProviders: {
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: {
mainRpc: 'https://bsc-dataseed.binance.org/'
},
[BLOCKCHAIN_NAME.POLYGON]: {
mainRpc: 'https://polygon-rpc.com'
}
}
}
rpcProviders: {
[BLOCKCHAIN_NAME.AVALANCHE]: {
mainRpc: 'https://rpc.ankr.com/avalanche_fuji'
},
[BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN]: {
mainRpc: 'https://bsc-dataseed.binance.org/'
},
[BLOCKCHAIN_NAME.POLYGON]: {
mainRpc: 'https://polygon-rpc.com'
},
[BLOCKCHAIN_NAME.FANTOM]: {
mainRpc: 'https://rpc.ftm.tools'
},
}
}
84 changes: 82 additions & 2 deletions examples/react/src/constants/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,97 @@ import {BLOCKCHAIN_NAME} from "rubic-sdk";
export const tokens = [
{
address: '0x0000000000000000000000000000000000000000',
name: 'Polygon',
name: 'AVAX',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7',
name: 'WAVAX',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
name: 'USDC',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664',
name: 'USDC.e',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7',
name: 'USDT',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0xc7198437980c041c805a1edcba50c1ce5db95118',
name: 'USDT.e',
blockchain: BLOCKCHAIN_NAME.AVALANCHE
},
{
address: '0x0000000000000000000000000000000000000000',
name: 'MATIC',
blockchain: BLOCKCHAIN_NAME.POLYGON
},
{
address: '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270',
name: 'WMATIC',
blockchain: BLOCKCHAIN_NAME.POLYGON
},
{
address: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
name: 'USDC',
blockchain: BLOCKCHAIN_NAME.POLYGON
},
{
address: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f',
name: 'USDT',
blockchain: BLOCKCHAIN_NAME.POLYGON
},
{
address: '0x0000000000000000000000000000000000000000',
name: 'BNB',
blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN
},
{
address: '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c',
name: 'WBNB',
blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN
},
{
address: '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d',
name: 'USDC',
blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN
}
},
{
address: '0x55d398326f99059ff775485246999027b3197955',
name: 'USDT',
blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN
},
{
address: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
name: 'BUSD',
blockchain: BLOCKCHAIN_NAME.BINANCE_SMART_CHAIN
},
{
address: '0x0000000000000000000000000000000000000000',
name: 'FTM',
blockchain: BLOCKCHAIN_NAME.FANTOM
},
{
address: '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83',
name: 'WFTM',
blockchain: BLOCKCHAIN_NAME.FANTOM
},
{
address: '0x04068da6c83afcfa0e13ba15a6696662335d5b75',
name: 'USDC',
blockchain: BLOCKCHAIN_NAME.FANTOM
},
{
address: '0x049d68029688eabf473097a2fc38ef61633a3c7a',
name: 'fUSDT',
blockchain: BLOCKCHAIN_NAME.FANTOM
},
]