constchain=137; // Polygon (Mainnet)consttokenIn='0x0000000000000000000000000000000000000000'; // use zero address for native token (ETH, POL (MATIC), etc.)consttokenOut='0x57999936fC9A9EC0751a8D146CcE11901Be8beD0'; // VRSW on Polygonconstamount='5000000000000000000'; // 5 POL (MATIC)constroute=awaitrouter.getRoute(tokenIn, tokenOut, amount, chain, { isExactInput:true });
The received Route object can be used to visualize the trading route steps, generate transaction data or execute the route.
The latest versions of SDK support routers of versions 3 (vRouter 1.3, preferable) and 2 (vRouter 1.2).
A TransactionRequest object can be created using the generateTransactionDataV3 method:
// userAddress is the address that will receive tokens from the vRouterconsttxRequest=router.generateTransactionDataV3(route, userAddress);
This object can be transferred to client and used to send a transaction using ethers.js v5:
// signer is the wallet that will send the transaction to the vRouterconsttx=awaitsigner.sendTransaction(txRequest);awaittx.wait();
You can also prepare the multiswap data and execute the multiswap:
// userAddress is the address that will receive tokens from the vRouterconstmultiSwapData=router.generateMultiSwapDataV3(route, userAddress);// amount of native tokens to sendconstvalue=route.tokenIn.isNative ?route.isExactInput ?route.tokenIn.balanceBN :route.slippageThresholdAmount.balanceBN :undefined;// signer is the wallet that will send the transaction to the vRouterconsttx=awaitrouter.executeMultiSwapV3(route.chain, multiSwapData, signer, value);awaittx.wait();
A TransactionRequest object can be created using the generateTransactionDataV2 method:
// userAddress is the address that will receive tokens from the vRouterconsttxRequest=router.generateTransactionDataV2(route, userAddress);
This object can be transferred to client and used to send a transaction using ethers.js v5:
// signer is the wallet that will send the transaction to the vRouterconsttx=awaitsigner.sendTransaction(txRequest);awaittx.wait();
You can also prepare the multicall data and execute the multicall:
// userAddress is the address that will receive tokens from the vRouterconstmulticallData=router.generateMulticallDataV2(route, userAddress);// amount of native tokens to sendconstvalue=route.tokenIn.isNative ?route.isExactInput ?route.tokenIn.balanceBN :route.slippageThresholdAmount.balanceBN :undefined;// signer is the wallet that will send the transaction to the vRouterconsttx=awaitrouter.executeMulticallV2(route.chain, multicallData, signer, value);awaittx.wait();
Complete Examples
You can check out our complete examples in the following repositories: