Skip to content

Commit

Permalink
Move encodePath for uniV3 pools from fixtures to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zZoMROT committed Aug 12, 2024
1 parent fd8fddd commit bb8c606
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
12 changes: 6 additions & 6 deletions test/RouterMixedPools.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { ether } = require('@1inch/solidity-utils');
const { ProtocolKey } = require('./helpers/utils');
const { initRouterContracts, adjustV2PoolTimestamps, encodePathExactInput } = require('./helpers/fixtures');
const { ProtocolKey, uniswapV3EncodePath } = require('./helpers/utils');
const { initRouterContracts, adjustV2PoolTimestamps } = require('./helpers/fixtures');
const { createGasUsedTable } = require('./helpers/table');
const { UniswapV2Pools, UniswapV3Pools } = require('./helpers/pools');
const { RoutePlanner, CommandType } = require('@uniswap/universal-router-sdk');
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Mixed pools', async function () {
addr1.address,
'0x8000000000000000000000000000000000000000000000000000000000000000',
'1',
encodePathExactInput([tokens.DAI.target, tokens.USDC.target], [UniswapV3Pools.USDC_DAI.fee]),
uniswapV3EncodePath([tokens.DAI.target, tokens.USDC.target], [UniswapV3Pools.USDC_DAI.fee]),
false,
]);
const { commands, inputs } = planner;
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Mixed pools', async function () {
UniswapV2Pools.USDC_DAI,
amount,
'1',
encodePathExactInput([tokens.WETH.target, tokens.DAI.target], [UniswapV3Pools.WETH_DAI.fee]),
uniswapV3EncodePath([tokens.WETH.target, tokens.DAI.target], [UniswapV3Pools.WETH_DAI.fee]),
false,
]);
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('Mixed pools', async function () {
addr1.address,
'0x8000000000000000000000000000000000000000000000000000000000000000',
'1',
encodePathExactInput([tokens.WETH.target, tokens.USDC.target], [UniswapV3Pools.WETH_USDC.fee]),
uniswapV3EncodePath([tokens.WETH.target, tokens.USDC.target], [UniswapV3Pools.WETH_USDC.fee]),
false,
]);
const { commands, inputs } = planner;
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('Mixed pools', async function () {
UniswapV2Pools.WETH_USDC,
amount,
'1',
encodePathExactInput([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
uniswapV3EncodePath([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
true,
]);
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [
Expand Down
16 changes: 8 additions & 8 deletions test/RouterUniV3.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { ethers } = require('hardhat');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { ether, constants } = require('@1inch/solidity-utils');
const { ProtocolKey } = require('./helpers/utils');
const { initRouterContracts, encodePathExactInput } = require('./helpers/fixtures');
const { ProtocolKey, uniswapV3EncodePath } = require('./helpers/utils');
const { initRouterContracts } = require('./helpers/fixtures');
const { createGasUsedTable } = require('./helpers/table');
const { UniswapV3Pools } = require('./helpers/pools');
const { RoutePlanner, CommandType } = require('@uniswap/universal-router-sdk');
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput([tokens.WETH.target, tokens.DAI.target], [UniswapV3Pools.WETH_DAI.fee]),
uniswapV3EncodePath([tokens.WETH.target, tokens.DAI.target], [UniswapV3Pools.WETH_DAI.fee]),
false,
]);

Expand Down Expand Up @@ -194,7 +194,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput([tokens.WETH.target, tokens.USDC.target, tokens.DAI.target], [UniswapV3Pools.WETH_USDC.fee, UniswapV3Pools.USDC_DAI.fee]),
uniswapV3EncodePath([tokens.WETH.target, tokens.USDC.target, tokens.DAI.target], [UniswapV3Pools.WETH_USDC.fee, UniswapV3Pools.USDC_DAI.fee]),
false,
]);

Expand Down Expand Up @@ -342,7 +342,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
uniswapV3EncodePath([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
true,
]);

Expand Down Expand Up @@ -479,7 +479,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
uniswapV3EncodePath([tokens.DAI.target, tokens.WETH.target], [UniswapV3Pools.WETH_DAI.fee]),
true,
]);

Expand Down Expand Up @@ -608,7 +608,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput([tokens.DAI.target, tokens.WETH.target, tokens.USDC.target], [UniswapV3Pools.WETH_DAI.fee, UniswapV3Pools.WETH_USDC.fee]),
uniswapV3EncodePath([tokens.DAI.target, tokens.WETH.target, tokens.USDC.target], [UniswapV3Pools.WETH_DAI.fee, UniswapV3Pools.WETH_USDC.fee]),
true,
]);

Expand Down Expand Up @@ -759,7 +759,7 @@ describe('Router [UniV3]', async function () {
addr1.address,
amount,
'1',
encodePathExactInput(
uniswapV3EncodePath(
[tokens.DAI.target, tokens.WETH.target, tokens.USDC.target, tokens.USDT.target],
[UniswapV3Pools.WETH_DAI.fee, UniswapV3Pools.WETH_USDC.fee, UniswapV3Pools.USDT_USDC.fee],
),
Expand Down
28 changes: 0 additions & 28 deletions test/helpers/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,7 @@ async function adjustV2PoolTimestamps(ethers, poolsV2) {
await ethers.provider.send('evm_setNextBlockTimestamp', [nextBlockTimestamp]);
}

function encodePathExactInput(tokens, feeAmounts) {
return encodePath(tokens, feeAmounts);
}

// @dev Encodes a path and fee amounts into a single string for uniswap v3 RoutePlanner
// @param path - an array of token addresses
// @param fees - an array of fee amounts associated with each pool
// @returns the encoded path string
// source: https://github.com/Uniswap/universal-router/blob/228f2d151a5fc99836d72ae00f81db92cdb44bd3/test/integration-tests/shared/swapRouter02Helpers.ts#L47
function encodePath(path, fees) {
if (path.length !== fees.length + 1) {
throw new Error('path/fee lengths do not match');
}

let encoded = '0x';
for (let i = 0; i < fees.length; i++) {
// 20 byte encoding of the address
encoded += path[i].slice(2);
// 3 byte encoding of the fee
encoded += fees[i].toString(16).padStart(2 * 3, '0');
}
// encode the final token
encoded += path[path.length - 1].slice(2);

return encoded.toLowerCase();
}

module.exports = {
initRouterContracts,
adjustV2PoolTimestamps,
encodePathExactInput,
};
24 changes: 24 additions & 0 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,32 @@ function paraswapUniV2PoolData(pools) {
return result;
}

// @dev Encodes a path and fee amounts into a single string for uniswap v3 RoutePlanner
// @param path - an array of token addresses
// @param fees - an array of fee amounts associated with each pool
// @returns the encoded path string
// source: https://github.com/Uniswap/universal-router/blob/228f2d151a5fc99836d72ae00f81db92cdb44bd3/test/integration-tests/shared/swapRouter02Helpers.ts#L47
function uniswapV3EncodePath(path, fees) {
if (path.length !== fees.length + 1) {
throw new Error('path/fee lengths do not match');
}

let encoded = '0x';
for (let i = 0; i < fees.length; i++) {
// 20 byte encoding of the address
encoded += path[i].slice(2);
// 3 byte encoding of the fee
encoded += fees[i].toString(16).padStart(2 * 3, '0');
}
// encode the final token
encoded += path[path.length - 1].slice(2);

return encoded.toLowerCase();
}

module.exports = {
ProtocolKey,
percentageOf,
paraswapUniV2PoolData,
uniswapV3EncodePath,
};

0 comments on commit bb8c606

Please sign in to comment.