import Big from 'big.js'; import { IChangeAmountParams, changeInAmount, checkApprovalBeforeSwap, prepareToSwap, } from '../../swap'; import { BSC_TESTNET_NETWORK, BSC_ACCOUNT } from '../mock'; export const swapOnBscTestnet = () => { const IN_ASSET = { networkSymbol: 'XX1', decimals: 18, contractAddress: '0xa2C3290cC286688e83Cd37e04553b51Bc8b35d89', }; const OUT_ASSET = { networkSymbol: 'XX2', decimals: 18, contractAddress: '0xdf1546436D9EBfF2F5B09bfC4Fa7Ad5822e52177', }; const BEW_SWAP_CONTRACT = '0xf2C4545a43770530cc4df4cB0a54C21D088BE2d7'; const TESTNET_ROUTERS = [ { type: 'uniswap', // testnet pancake: https://testnet.bscscan.com/address/0xc80251cd06db2383746ebd85fff5b3c94e5fefd7 address: '0xc80251cD06db2383746Ebd85fff5B3c94E5FEfd7', }, ]; describe('swap on bsc testnet', () => { it('swap:EXACT_INPUT on bsc-testnet via bew-swap', async () => { // input amount change const txData: IChangeAmountParams = { network: BSC_TESTNET_NETWORK, amount: '0.001', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: TESTNET_ROUTERS, }; const { quotes, path } = await changeInAmount(txData); const quote1 = quotes[0]; console.log('quotes: ', quotes); console.log( `in ${quote1.type} price: `, `1 ${txData.inAsset.networkSymbol} ~= ${new Big(quote1.outAmount) .div(quote1.inAmount) .toFixed()} ${txData.outAsset.networkSymbol}`, ); // approval const { needApproval, buildApproval } = await checkApprovalBeforeSwap({ network: BSC_TESTNET_NETWORK, asset: IN_ASSET, amount: quote1.inAmount, from: BSC_ACCOUNT.address, spenderAddress: BEW_SWAP_CONTRACT, }); if (needApproval) { const { sign, send: sendApproval } = await buildApproval(); // await sendApproval(sign(BSC_ACCOUNT.privateKey)); } // swap const { fee, sign, send: swap } = await prepareToSwap({ network: BSC_TESTNET_NETWORK, from: BSC_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, outAmount: quote1.outAmount, type: 'EXACT_INPUT', timeout: 1200, config: { swapContract: BEW_SWAP_CONTRACT, routerContract: TESTNET_ROUTERS[0].address, path, }, }); console.log({ fee }); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(result).toHaveProperty('transactionHash'); }); it('swap:EXACT_INPUT on bsc-testnet via pancakeswap', async () => { // input amount change const txData: IChangeAmountParams = { network: BSC_TESTNET_NETWORK, amount: '0.1', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: TESTNET_ROUTERS, }; const { quotes, path } = await changeInAmount(txData); const quote1 = quotes[0]; console.log('quotes: ', quotes); console.log( `in ${quote1.type} price: `, `1 ${txData.inAsset.networkSymbol} ~= ${new Big(quote1.outAmount) .div(quote1.inAmount) .toFixed()} ${txData.outAsset.networkSymbol}`, ); // approval const { needApproval, buildApproval } = await checkApprovalBeforeSwap({ network: BSC_TESTNET_NETWORK, asset: IN_ASSET, amount: quote1.inAmount, from: BSC_ACCOUNT.address, spenderAddress: TESTNET_ROUTERS[0].address, }); if (needApproval) { const { sign, send: sendApproval } = await buildApproval(); // await sendApproval(sign(BSC_ACCOUNT.privateKey)); } // swap const { fee, sign, send: swap } = await prepareToSwap({ network: BSC_TESTNET_NETWORK, from: BSC_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, outAmount: quote1.outAmount, type: 'EXACT_INPUT', timeout: 1200, config: { swapContract: '0xc80251cD06db2383746Ebd85fff5B3c94E5FEfd7', path, }, }); console.log({ fee }); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(result).toHaveProperty('transactionHash'); }); }); };