import Big from 'big.js'; import { IChangeAmountParams, changeInAmount, checkApprovalBeforeSwap, prepareToSwap, } from '../../swap'; import { ETH_TESTNET_NETWORK, ETH_ACCOUNT } from '../mock'; export const swapOnEthTestnet = () => { // For uniswap testnet const OUT_ASSET = { decimals: 18, networkSymbol: 'UNI', // contractAddress: '0xc778417e063141139fce010982780140aa0cd5ab', contractAddress: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', }; const IN_ASSET = { decimals: 18, networkSymbol: 'DAI', contractAddress: '0xad6d458402f60fd3bd25163575031acdce07538d', }; const BEW_SWAP_CONTRACT = '0x7Bb753F888a2838c1eE0A825058d69d0997c7c63'; const SLIPPAGE = '0.01'; const BEW_SWAP_FEE = '0.01'; // 1% const ROUTERS = [ { type: 'uniswap', address: '0x7a250d5630b4cf539739df2c5dacb4c659f2488d', }, ]; const BSC_ACCOUNT = ETH_ACCOUNT; describe('swap on eth-testnet via bew-swap', () => { const MAX_FEE_PER_GAS = '118000000000'; const MAX_PRIORITY_FEE_PER_GAS = '1183234288'; it('swap:EXACT_INPUT on eth-testnet via bew-swap', async () => { const txData: IChangeAmountParams = { network: ETH_TESTNET_NETWORK, amount: '1', 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: ETH_TESTNET_NETWORK, asset: IN_ASSET, amount: quote1.inAmount, from: BSC_ACCOUNT.address, spenderAddress: BEW_SWAP_CONTRACT, }); if (needApproval) { const { sign, send: sendApproval } = await buildApproval({ gas: { maxFeePerGas: MAX_FEE_PER_GAS, maxPriorityFeePerGas: MAX_PRIORITY_FEE_PER_GAS, }, }); // await sendApproval(sign(BSC_ACCOUNT.privateKey)); } // swap const { fee, sign, send: swap } = await prepareToSwap({ network: ETH_TESTNET_NETWORK, from: BSC_ACCOUNT.address, inAmount: quote1.inAmount, inAsset: IN_ASSET, outAsset: OUT_ASSET, // Need to reduce slippage. outAmount: (Number(quote1.outAmount) * (1 - +SLIPPAGE - +BEW_SWAP_FEE)).toString(), type: 'EXACT_INPUT', timeout: 1200, gas: { maxFeePerGas: MAX_FEE_PER_GAS, maxPriorityFeePerGas: MAX_PRIORITY_FEE_PER_GAS, }, 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('uniswap'); }); }); };