import { MAX_INTEGER } from 'ethereumjs-util'; import Big from 'big.js'; import { IChangeAmountParams, changeInAmount, changeOutAmount, checkApprovalBeforeSwap, prepareToSwap, } from '../../swap'; import { BSC_MAINNET_ACCOUNT, BSC_MAINNET_NETWORK } from '../mock'; export const swapOnBscMainnet = () => { // For pancake mainnet const IN_ASSET = { decimals: 18, networkSymbol: 'BNB', contractAddress: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', }; // const OUT_ASSET = { // decimals: 18, // networkSymbol: 'WBNB', // contractAddress: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', // }; const OUT_ASSET = { decimals: 18, networkSymbol: 'CAKE', contractAddress: '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82', }; const BEW_SWAP_CONTRACT = '0x48d30Aa96019D881eaA0b96556c99463D38b8EC3'; const ROUTERS = [ { type: 'pancake', address: '0x10ED43C718714eb63d5aA57B78B54704E256024E', }, ]; const SLIPPAGE = '0.01'; const BEW_SWAP_FEE = '0.01'; // 1% // For pancake describe('swap on bsc-mainnet via pancakeswap', () => { it('swap:EXACT_INPUT on bsc-mainnet via pancake-swap', async () => { const txData: IChangeAmountParams = { network: BSC_MAINNET_NETWORK, amount: '0.01', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: 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}`, ); console.log('paths: ', path); // approval const { needApproval, buildApproval } = await checkApprovalBeforeSwap({ network: BSC_MAINNET_NETWORK, asset: IN_ASSET, amount: quote1.inAmount, from: BSC_MAINNET_ACCOUNT.address, spenderAddress: ROUTERS[0].address, }); if (needApproval) { const { sign, send: sendApproval } = await buildApproval({ allowance: MAX_INTEGER.toString(), }); // await sendApproval(sign(BSC_ACCOUNT.privateKey)); } // swap const { fee, sign, send: swap } = await prepareToSwap({ network: BSC_MAINNET_NETWORK, from: BSC_MAINNET_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, // Need to reduce slippage. outAmount: (Number(quote1.outAmount) * (1 - +SLIPPAGE)).toString(), type: 'EXACT_INPUT', timeout: 1200, config: { swapContract: ROUTERS[0].address, path, }, }); console.log({ fee }); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(quote1.type).toMatch('pancake'); }); it('swap:EXACT_OUTPUT on bsc-mainnet via pancake-swap', async () => { const txData: IChangeAmountParams = { network: BSC_MAINNET_NETWORK, amount: '0.1', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: ROUTERS, }; const { quotes, path } = await changeOutAmount(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}`, ); console.log('paths: ', path); // swap const { fee, sign, send: swap } = await prepareToSwap({ network: BSC_MAINNET_NETWORK, from: BSC_MAINNET_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, outAmount: quote1.outAmount, type: 'EXACT_OUTPUT', timeout: 1200, config: { swapContract: ROUTERS[0].address, path, }, }); console.log({ fee }); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(quote1.type).toMatch('pancake'); }); }); // For bew swap describe('swap on bsc-mainnet via bew swap', () => { it('swap:EXACT_INPUT on bsc-mainnet via bew-swap', async () => { const txData: IChangeAmountParams = { network: BSC_MAINNET_NETWORK, amount: '0.01', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: 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}`, ); console.log('paths: ', path); // approval const { needApproval, buildApproval } = await checkApprovalBeforeSwap({ network: BSC_MAINNET_NETWORK, asset: IN_ASSET, amount: quote1.inAmount, from: BSC_MAINNET_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_MAINNET_NETWORK, from: BSC_MAINNET_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, // Need to reduce slippage + service fee for bew-swap. outAmount: (Number(quote1.outAmount) * (1 - +SLIPPAGE - +BEW_SWAP_FEE)).toString(), type: 'EXACT_INPUT', timeout: 1200, config: { swapContract: BEW_SWAP_CONTRACT, routerContract: ROUTERS[0].address, path, }, }); console.log('fee: ', fee); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(quote1.type).toMatch('pancake'); }); it('swap:EXACT_OUTPUT on bsc-mainnet via bew-swap', async () => { const txData: IChangeAmountParams = { network: BSC_MAINNET_NETWORK, amount: '0.1', inAsset: IN_ASSET, outAsset: OUT_ASSET, routers: ROUTERS, }; const { quotes, path } = await changeOutAmount(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}`, ); console.log('paths: ', path); // swap const { fee, sign, send: swap } = await prepareToSwap({ network: BSC_MAINNET_NETWORK, from: BSC_MAINNET_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, outAmount: quote1.outAmount, type: 'EXACT_OUTPUT', timeout: 1200, config: { swapContract: BEW_SWAP_CONTRACT, routerContract: ROUTERS[0].address, path, }, }); console.log({ fee }); // const result = await swap(sign(BSC_ACCOUNT.privateKey)); // console.log(result); // expect(quote1.type).toMatch('pancake'); }); }); };