From 50dbdce57704d1dc6c5ee5c2d32413333ea6c507 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 11:13:46 +0300 Subject: [PATCH 01/14] add exchanges --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 94c0ad8..c3f1b30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.80", + "version": "0.19.81-rc0", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index e96b992..9f5b83d 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -20,11 +20,18 @@ const exchangeToType: Partial> = { 'CHERRYSWAP': 'UniswapV2', 'OKXSWAP': 'UniswapV2', 'INTERNAL_POOL_V2': 'UniswapV2', - 'INTERNAL_POOL_V3': "OrionV3", - 'INTERNAL_POOL_V3_0_01': "OrionV3", - 'INTERNAL_POOL_V3_0_05': "OrionV3", - 'INTERNAL_POOL_V3_0_3': "OrionV3", - 'INTERNAL_POOL_V3_1_0': "OrionV3", + 'UniswapV3_0_05': 'UniswapV3', + 'UniswapV3_0_3': 'UniswapV3', + 'UniswapV3_1_0': 'UniswapV3', + 'INTERNAL_POOL_V3': 'OrionV3', + 'INTERNAL_POOL_V3_0_01': 'OrionV3', + 'INTERNAL_POOL_V3_0_05': 'OrionV3', + 'INTERNAL_POOL_V3_0_3': 'OrionV3', + 'INTERNAL_POOL_V3_1_0': 'OrionV3', + 'PancakeSwapV3_0_01': 'PancakeSwapV3', + 'PancakeSwapV3_0_05': 'PancakeSwapV3', + 'PancakeSwapV3_0_25': 'PancakeSwapV3', + 'PancakeSwapV3_1_0': 'PancakeSwapV3', 'CURVE': "Curve", 'CURVE_FACTORY': "Curve", } From c202f3523afee8a1912fc81a1b909e74cb1da1c8 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 11:16:21 +0300 Subject: [PATCH 02/14] add Factory --- src/Unit/Exchange/generateSwapCalldata.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 9f5b83d..9e652b3 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -9,7 +9,7 @@ import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/comm const EXECUTOR_SWAP_FUNCTION = "func_70LYiww" -export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" +export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" | "PancakeSwapV3" const exchangeToType: Partial> = { 'SPOOKYSWAP': 'UniswapV2', From 4378286243ff90b613813310c1a37f3667a98320 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 11:35:06 +0300 Subject: [PATCH 03/14] update exchanges --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index c3f1b30..4cd621f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc0", + "version": "0.19.81-rc1", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 9e652b3..b8c9a7b 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -28,10 +28,10 @@ const exchangeToType: Partial> = { 'INTERNAL_POOL_V3_0_05': 'OrionV3', 'INTERNAL_POOL_V3_0_3': 'OrionV3', 'INTERNAL_POOL_V3_1_0': 'OrionV3', - 'PancakeSwapV3_0_01': 'PancakeSwapV3', - 'PancakeSwapV3_0_05': 'PancakeSwapV3', - 'PancakeSwapV3_0_25': 'PancakeSwapV3', - 'PancakeSwapV3_1_0': 'PancakeSwapV3', + 'PANCAKESWAP_V3_0_01': 'PancakeSwapV3', + 'PANCAKESWAP_V3_0_05': 'PancakeSwapV3', + 'PANCAKESWAP_V3_0_25': 'PancakeSwapV3', + 'PANCAKESWAP_V3_1_0': 'PancakeSwapV3', 'CURVE': "Curve", 'CURVE_FACTORY': "Curve", } From 58add34a6c53097b82007accb687d3400e6642c2 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 12:11:32 +0300 Subject: [PATCH 04/14] add redirect pancake to uni --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 143 +++++++++++----------- 2 files changed, 72 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index 4cd621f..7c179e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc1", + "version": "0.19.81-rc2", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index b8c9a7b..1065f59 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -7,54 +7,55 @@ import type Unit from '../index.js'; import { simpleFetch } from 'simple-typed-fetch'; import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/common.js'; -const EXECUTOR_SWAP_FUNCTION = "func_70LYiww" +const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww' -export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" | "PancakeSwapV3" +export type Factory = 'UniswapV2' | 'UniswapV3' | 'Curve' | 'OrionV2' | 'OrionV3' // | 'PancakeSwapV3' const exchangeToType: Partial> = { - 'SPOOKYSWAP': 'UniswapV2', - 'PANCAKESWAP': 'UniswapV2', - 'UNISWAP': 'UniswapV2', - 'QUICKSWAP': 'UniswapV2', - 'ORION_POOL': 'UniswapV2', - 'CHERRYSWAP': 'UniswapV2', - 'OKXSWAP': 'UniswapV2', - 'INTERNAL_POOL_V2': 'UniswapV2', - 'UniswapV3_0_05': 'UniswapV3', - 'UniswapV3_0_3': 'UniswapV3', - 'UniswapV3_1_0': 'UniswapV3', - 'INTERNAL_POOL_V3': 'OrionV3', - 'INTERNAL_POOL_V3_0_01': 'OrionV3', - 'INTERNAL_POOL_V3_0_05': 'OrionV3', - 'INTERNAL_POOL_V3_0_3': 'OrionV3', - 'INTERNAL_POOL_V3_1_0': 'OrionV3', - 'PANCAKESWAP_V3_0_01': 'PancakeSwapV3', - 'PANCAKESWAP_V3_0_05': 'PancakeSwapV3', - 'PANCAKESWAP_V3_0_25': 'PancakeSwapV3', - 'PANCAKESWAP_V3_1_0': 'PancakeSwapV3', - 'CURVE': "Curve", - 'CURVE_FACTORY': "Curve", + SPOOKYSWAP: 'UniswapV2', + PANCAKESWAP: 'UniswapV2', + UNISWAP: 'UniswapV2', + QUICKSWAP: 'UniswapV2', + ORION_POOL: 'UniswapV2', + CHERRYSWAP: 'UniswapV2', + OKXSWAP: 'UniswapV2', + INTERNAL_POOL_V2: 'UniswapV2', + UniswapV3_0_05: 'UniswapV3', + UniswapV3_0_3: 'UniswapV3', + UniswapV3_1_0: 'UniswapV3', + INTERNAL_POOL_V3: 'OrionV3', + INTERNAL_POOL_V3_0_01: 'OrionV3', + INTERNAL_POOL_V3_0_05: 'OrionV3', + INTERNAL_POOL_V3_0_3: 'OrionV3', + INTERNAL_POOL_V3_1_0: 'OrionV3', + // TODO: add PancakeSwapV3 factory + PANCAKESWAP_V3_0_01: 'UniswapV3', + PANCAKESWAP_V3_0_05: 'UniswapV3', + PANCAKESWAP_V3_0_25: 'UniswapV3', + PANCAKESWAP_V3_1_0: 'UniswapV3', + CURVE: 'Curve', + CURVE_FACTORY: 'Curve', } export type SwapInfo = { - pool: string, - assetIn: string, - assetOut: string, + pool: string + assetIn: string + assetOut: string factory: string } export type CallParams = { - isMandatory?: boolean, - target?: string, - gaslimit?: BigNumber, + isMandatory?: boolean + target?: string + gaslimit?: BigNumber value?: BigNumber } export type GenerateSwapCalldataParams = { - amount: BigNumberish, - minReturnAmount: BigNumberish, - receiverAddress: string, - path: ArrayLike, + amount: BigNumberish + minReturnAmount: BigNumberish + receiverAddress: string + path: ArrayLike unit: Unit } @@ -67,28 +68,28 @@ export default async function generateSwapCalldata({ }: GenerateSwapCalldataParams ): Promise<{ calldata: string, swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct }> { if (path_ == undefined || path_.length == 0) { - throw new Error(`Empty path`); + throw new Error('Empty path'); } - const wethAddress = safeGet(unit.contracts, "WETH") - const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry") + const wethAddress = safeGet(unit.contracts, 'WETH') + const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry') const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); let path = SafeArray.from(path_).map((swapInfo) => { swapInfo.assetIn = safeGet(assetToAddress, swapInfo.assetIn); swapInfo.assetOut = safeGet(assetToAddress, swapInfo.assetOut); - return swapInfo; + return swapInfo; }) const factory = path.first().factory if (!path.every(swapInfo => swapInfo.factory === factory)) { - throw new Error(`Supporting only swaps with single factory`); + throw new Error('Supporting only swaps with single factory'); } - + const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { srcToken: path.first().assetIn, dstToken: path.last().assetOut, srcReceiver: swapExecutorContractAddress ?? '', dstReceiver: receiverAddress, - amount: amount, - minReturnAmount: minReturnAmount, + amount, + minReturnAmount, flags: 0 } @@ -108,29 +109,28 @@ export default async function generateSwapCalldata({ if (swapInfo.assetOut == ethers.constants.AddressZero) swapInfo.assetOut = wethAddress return swapInfo; }); - let calldata: string switch (exchangeToType[factory]) { - case "OrionV2": { + case 'OrionV2': { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case "UniswapV2": { + case 'UniswapV2': { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case "UniswapV3": { + case 'UniswapV3': { calldata = await generateUni3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case "OrionV3": { + case 'OrionV3': { calldata = await generateOrion3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case "Curve": { + case 'Curve': { calldata = await generateCurveStableSwapCalls( amountNativeDecimals, exchangeContractAddress, @@ -159,26 +159,26 @@ export async function generateUni2Calls( const currentSwap = path.get(i) const nextSwap = path.get(i + 1) - const calldata = executorInterface.encodeFunctionData("swapUniV2", [ + const calldata = executorInterface.encodeFunctionData('swapUniV2', [ currentSwap.pool, currentSwap.assetIn, currentSwap.assetOut, - defaultAbiCoder.encode(["uint256"], [concat(["0x03", nextSwap.pool])]), + defaultAbiCoder.encode(['uint256'], [concat(['0x03', nextSwap.pool])]), ] ) calls.push(addCallParams(calldata)) } } const lastSwap = path.last(); - const calldata = executorInterface.encodeFunctionData("swapUniV2", [ + const calldata = executorInterface.encodeFunctionData('swapUniV2', [ lastSwap.pool, lastSwap.assetIn, lastSwap.assetOut, - defaultAbiCoder.encode(["uint256"], [concat(["0x03", exchangeAddress])]), + defaultAbiCoder.encode(['uint256'], [concat(['0x03', exchangeAddress])]), ]) calls.push(addCallParams(calldata)) - return generateCalls(calls) + return await generateCalls(calls) } async function generateUni3Calls( @@ -194,20 +194,20 @@ async function generateUni3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) + let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData("uniswapV3SwapTo", [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData('uniswapV3SwapTo', [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return generateCalls([calldata]) + return await generateCalls([calldata]) } async function generateOrion3Calls( @@ -223,20 +223,20 @@ async function generateOrion3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) + let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData("orionV3SwapTo", [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData('orionV3SwapTo', [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return generateCalls([calldata]) + return await generateCalls([calldata]) } async function generateCurveStableSwapCalls( @@ -248,7 +248,7 @@ async function generateCurveStableSwapCalls( curveRegistry: string ) { if (path.length > 1) { - throw new Error("Supporting only single stable swap on curve") + throw new Error('Supporting only single stable swap on curve') } const executorInterface = SwapExecutor__factory.createInterface() const registry = CurveRegistry__factory.connect(curveRegistry, provider) @@ -262,7 +262,7 @@ async function generateCurveStableSwapCalls( const calls: BytesLike[] = [] if (executorAllowance.lt(amount)) { const calldata = addCallParams( - executorInterface.encodeFunctionData("safeApprove", [ + executorInterface.encodeFunctionData('safeApprove', [ swap.assetIn, swap.pool, ethers.constants.MaxUint256 @@ -270,7 +270,7 @@ async function generateCurveStableSwapCalls( ) calls.push(calldata) } - let calldata = executorInterface.encodeFunctionData("curveSwapStableAmountIn", [ + let calldata = executorInterface.encodeFunctionData('curveSwapStableAmountIn', [ pool, assetOut, i, @@ -282,7 +282,7 @@ async function generateCurveStableSwapCalls( calldata = addCallParams(calldata) calls.push(calldata) - return generateCalls(calls) + return await generateCalls(calls) } // Adds additional byte to single swap with settings @@ -292,31 +292,30 @@ function addCallParams( ) { let firstByte = 0 if (callParams) { - if (callParams.value !== undefined) { + if (callParams.value !== undefined) { firstByte += 16 // 00010000 - const encodedValue = ethers.utils.solidityPack(["uint128"], [callParams.value]) + const encodedValue = ethers.utils.solidityPack(['uint128'], [callParams.value]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedValue, calldata])) } if (callParams.target !== undefined) { firstByte += 32 // 00100000 - const encodedAddress = ethers.utils.solidityPack(["address"], [callParams.target]) + const encodedAddress = ethers.utils.solidityPack(['address'], [callParams.target]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedAddress, calldata])) } if (callParams.gaslimit !== undefined) { firstByte += 64 // 01000000 - const encodedGaslimit = ethers.utils.solidityPack(["uint32"], [callParams.gaslimit]) + const encodedGaslimit = ethers.utils.solidityPack(['uint32'], [callParams.gaslimit]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedGaslimit, calldata])) } if (callParams.isMandatory !== undefined) firstByte += 128 // 10000000 } - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, calldata])) return calldata } - async function generateCalls(calls: BytesLike[]) { const executorInterface = SwapExecutor__factory.createInterface() - return "0x" + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) + return '0x' + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) } From 1538c74c488d066bd9c63e8ae2bbebabbe115d04 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 12:28:24 +0300 Subject: [PATCH 05/14] mock PancakeSwapV3 generateCall function --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 50 +++++++++++++++++++---- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 7c179e9..8abde0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc2", + "version": "0.19.81-rc3", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 1065f59..d306be0 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -9,7 +9,7 @@ import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/comm const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww' -export type Factory = 'UniswapV2' | 'UniswapV3' | 'Curve' | 'OrionV2' | 'OrionV3' // | 'PancakeSwapV3' +export type Factory = 'UniswapV2' | 'UniswapV3' | 'Curve' | 'OrionV2' | 'OrionV3' | 'PancakeSwapV3' const exchangeToType: Partial> = { SPOOKYSWAP: 'UniswapV2', @@ -28,11 +28,10 @@ const exchangeToType: Partial> = { INTERNAL_POOL_V3_0_05: 'OrionV3', INTERNAL_POOL_V3_0_3: 'OrionV3', INTERNAL_POOL_V3_1_0: 'OrionV3', - // TODO: add PancakeSwapV3 factory - PANCAKESWAP_V3_0_01: 'UniswapV3', - PANCAKESWAP_V3_0_05: 'UniswapV3', - PANCAKESWAP_V3_0_25: 'UniswapV3', - PANCAKESWAP_V3_1_0: 'UniswapV3', + PANCAKESWAP_V3_0_01: 'PancakeSwapV3', + PANCAKESWAP_V3_0_05: 'PancakeSwapV3', + PANCAKESWAP_V3_0_25: 'PancakeSwapV3', + PANCAKESWAP_V3_1_0: 'PancakeSwapV3', CURVE: 'Curve', CURVE_FACTORY: 'Curve', } @@ -86,7 +85,7 @@ export default async function generateSwapCalldata({ const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { srcToken: path.first().assetIn, dstToken: path.last().assetOut, - srcReceiver: swapExecutorContractAddress ?? '', + srcReceiver: swapExecutorContractAddress, dstReceiver: receiverAddress, amount, minReturnAmount, @@ -134,13 +133,17 @@ export default async function generateSwapCalldata({ calldata = await generateCurveStableSwapCalls( amountNativeDecimals, exchangeContractAddress, - swapExecutorContractAddress ?? '', + swapExecutorContractAddress, path, unit.provider, curveRegistryAddress ); break; } + case 'PancakeSwapV3': { + calldata = await generatePancake3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) + break; + } default: { throw new Error(`Factory ${factory} is not supported`) } @@ -210,6 +213,37 @@ async function generateUni3Calls( return await generateCalls([calldata]) } +async function generatePancake3Calls( + amount: BigNumberish, + exchangeContractAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider +) { + const encodedPools: BytesLike[] = [] + for (const swap of path) { + // TODO: change to PancakeV3Pool__factory + const pool = UniswapV3Pool__factory.connect(swap.pool, provider) + const token0 = await pool.token0() + const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() + const unwrapWETH = swap.assetOut === ethers.constants.AddressZero + + let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) + encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) + let firstByte = 0 + if (unwrapWETH) firstByte += 32 + if (!zeroForOne) firstByte += 128 + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) + encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) + encodedPools.push(encodedPool) + } + const executorInterface = SwapExecutor__factory.createInterface() + // TODO: change to pancakeV3SwapTo + let calldata = executorInterface.encodeFunctionData('uniswapV3SwapTo', [encodedPools, exchangeContractAddress, amount]) + calldata = addCallParams(calldata) + + return await generateCalls([calldata]) +} + async function generateOrion3Calls( amount: BigNumberish, exchangeContractAddress: string, From 3a44ea7a62110cdddad5afa211836c40ce8a3b70 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 12:56:12 +0300 Subject: [PATCH 06/14] add uniswapv3 exchange --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 8abde0a..cc90874 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc3", + "version": "0.19.81-rc4", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index d306be0..3226f1d 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -20,6 +20,7 @@ const exchangeToType: Partial> = { CHERRYSWAP: 'UniswapV2', OKXSWAP: 'UniswapV2', INTERNAL_POOL_V2: 'UniswapV2', + UniswapV3: 'UniswapV3', UniswapV3_0_05: 'UniswapV3', UniswapV3_0_3: 'UniswapV3', UniswapV3_1_0: 'UniswapV3', @@ -28,10 +29,6 @@ const exchangeToType: Partial> = { INTERNAL_POOL_V3_0_05: 'OrionV3', INTERNAL_POOL_V3_0_3: 'OrionV3', INTERNAL_POOL_V3_1_0: 'OrionV3', - PANCAKESWAP_V3_0_01: 'PancakeSwapV3', - PANCAKESWAP_V3_0_05: 'PancakeSwapV3', - PANCAKESWAP_V3_0_25: 'PancakeSwapV3', - PANCAKESWAP_V3_1_0: 'PancakeSwapV3', CURVE: 'Curve', CURVE_FACTORY: 'Curve', } @@ -140,10 +137,6 @@ export default async function generateSwapCalldata({ ); break; } - case 'PancakeSwapV3': { - calldata = await generatePancake3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) - break; - } default: { throw new Error(`Factory ${factory} is not supported`) } From a30688d4625173a20ff96a07d983da10f1c2bd06 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 13:24:20 +0300 Subject: [PATCH 07/14] return swapExecutorContractAddress --- src/Unit/Exchange/generateSwapCalldata.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 3226f1d..bea0418 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -82,7 +82,7 @@ export default async function generateSwapCalldata({ const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { srcToken: path.first().assetIn, dstToken: path.last().assetOut, - srcReceiver: swapExecutorContractAddress, + srcReceiver: swapExecutorContractAddress ?? '', dstReceiver: receiverAddress, amount, minReturnAmount, @@ -130,7 +130,7 @@ export default async function generateSwapCalldata({ calldata = await generateCurveStableSwapCalls( amountNativeDecimals, exchangeContractAddress, - swapExecutorContractAddress, + swapExecutorContractAddress ?? '', path, unit.provider, curveRegistryAddress From ebf768b67ebbe1105cf0ba325a082dcaf6357831 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 13:28:17 +0300 Subject: [PATCH 08/14] remove generatePancake3Calls --- src/Unit/Exchange/generateSwapCalldata.ts | 31 ----------------------- 1 file changed, 31 deletions(-) diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index bea0418..d17a87e 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -206,37 +206,6 @@ async function generateUni3Calls( return await generateCalls([calldata]) } -async function generatePancake3Calls( - amount: BigNumberish, - exchangeContractAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider -) { - const encodedPools: BytesLike[] = [] - for (const swap of path) { - // TODO: change to PancakeV3Pool__factory - const pool = UniswapV3Pool__factory.connect(swap.pool, provider) - const token0 = await pool.token0() - const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() - const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - - let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) - encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) - let firstByte = 0 - if (unwrapWETH) firstByte += 32 - if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) - encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) - encodedPools.push(encodedPool) - } - const executorInterface = SwapExecutor__factory.createInterface() - // TODO: change to pancakeV3SwapTo - let calldata = executorInterface.encodeFunctionData('uniswapV3SwapTo', [encodedPools, exchangeContractAddress, amount]) - calldata = addCallParams(calldata) - - return await generateCalls([calldata]) -} - async function generateOrion3Calls( amount: BigNumberish, exchangeContractAddress: string, From 1f9838e48d799b0a959258e1854b278dbd5f2c30 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 13:41:31 +0300 Subject: [PATCH 09/14] remove exchangeToType map --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 208 +++++++++++----------- 2 files changed, 104 insertions(+), 106 deletions(-) diff --git a/package.json b/package.json index cc90874..bf9a863 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc4", + "version": "0.19.81-rc5", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index d17a87e..be0534e 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -7,67 +7,63 @@ import type Unit from '../index.js'; import { simpleFetch } from 'simple-typed-fetch'; import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/common.js'; -const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww' +const EXECUTOR_SWAP_FUNCTION = "func_70LYiww" -export type Factory = 'UniswapV2' | 'UniswapV3' | 'Curve' | 'OrionV2' | 'OrionV3' | 'PancakeSwapV3' +// export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" -const exchangeToType: Partial> = { - SPOOKYSWAP: 'UniswapV2', - PANCAKESWAP: 'UniswapV2', - UNISWAP: 'UniswapV2', - QUICKSWAP: 'UniswapV2', - ORION_POOL: 'UniswapV2', - CHERRYSWAP: 'UniswapV2', - OKXSWAP: 'UniswapV2', - INTERNAL_POOL_V2: 'UniswapV2', - UniswapV3: 'UniswapV3', - UniswapV3_0_05: 'UniswapV3', - UniswapV3_0_3: 'UniswapV3', - UniswapV3_1_0: 'UniswapV3', - INTERNAL_POOL_V3: 'OrionV3', - INTERNAL_POOL_V3_0_01: 'OrionV3', - INTERNAL_POOL_V3_0_05: 'OrionV3', - INTERNAL_POOL_V3_0_3: 'OrionV3', - INTERNAL_POOL_V3_1_0: 'OrionV3', - CURVE: 'Curve', - CURVE_FACTORY: 'Curve', -} +/* const exchangeToType: Partial> = { + 'SPOOKYSWAP': 'UniswapV2', + 'PANCAKESWAP': 'UniswapV2', + 'UNISWAP': 'UniswapV2', + 'QUICKSWAP': 'UniswapV2', + 'ORION_POOL': 'UniswapV2', + 'CHERRYSWAP': 'UniswapV2', + 'OKXSWAP': 'UniswapV2', + 'INTERNAL_POOL_V2': 'UniswapV2', + 'INTERNAL_POOL_V3': "OrionV3", + 'INTERNAL_POOL_V3_0_01': "OrionV3", + 'INTERNAL_POOL_V3_0_05': "OrionV3", + 'INTERNAL_POOL_V3_0_3': "OrionV3", + 'INTERNAL_POOL_V3_1_0': "OrionV3", + 'CURVE': "Curve", + 'CURVE_FACTORY': "Curve", +} */ export type SwapInfo = { - pool: string - assetIn: string - assetOut: string + pool: string, + assetIn: string, + assetOut: string, factory: string } export type CallParams = { - isMandatory?: boolean - target?: string - gaslimit?: BigNumber + isMandatory?: boolean, + target?: string, + gaslimit?: BigNumber, value?: BigNumber } export type GenerateSwapCalldataParams = { - amount: BigNumberish - minReturnAmount: BigNumberish - receiverAddress: string - path: ArrayLike + amount: BigNumberish, + minReturnAmount: BigNumberish, + receiverAddress: string, + path: ArrayLike, unit: Unit } export default async function generateSwapCalldata({ - amount, - minReturnAmount, - receiverAddress, - path: path_, - unit -}: GenerateSwapCalldataParams + amount, + minReturnAmount, + receiverAddress, + path: path_, + unit + }: GenerateSwapCalldataParams ): Promise<{ calldata: string, swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct }> { if (path_ == undefined || path_.length == 0) { - throw new Error('Empty path'); + throw new Error(`Empty path`); } - const wethAddress = safeGet(unit.contracts, 'WETH') - const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry') + const wethAddress = safeGet(unit.contracts, "WETH") + const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry") const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); let path = SafeArray.from(path_).map((swapInfo) => { swapInfo.assetIn = safeGet(assetToAddress, swapInfo.assetIn); @@ -76,7 +72,7 @@ export default async function generateSwapCalldata({ }) const factory = path.first().factory if (!path.every(swapInfo => swapInfo.factory === factory)) { - throw new Error('Supporting only swaps with single factory'); + throw new Error(`Supporting only swaps with single factory`); } const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { @@ -84,8 +80,8 @@ export default async function generateSwapCalldata({ dstToken: path.last().assetOut, srcReceiver: swapExecutorContractAddress ?? '', dstReceiver: receiverAddress, - amount, - minReturnAmount, + amount: amount, + minReturnAmount: minReturnAmount, flags: 0 } @@ -106,34 +102,35 @@ export default async function generateSwapCalldata({ return swapInfo; }); + let calldata: string - switch (exchangeToType[factory]) { - case 'OrionV2': { + switch (factory) { + case "OrionV2": { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case 'UniswapV2': { + case "UniswapV2": { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case 'UniswapV3': { + case "UniswapV3": { calldata = await generateUni3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case 'OrionV3': { + case "OrionV3": { calldata = await generateOrion3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case 'Curve': { + case "Curve": { calldata = await generateCurveStableSwapCalls( - amountNativeDecimals, - exchangeContractAddress, - swapExecutorContractAddress ?? '', - path, - unit.provider, - curveRegistryAddress + amountNativeDecimals, + exchangeContractAddress, + swapExecutorContractAddress ?? '', + path, + unit.provider, + curveRegistryAddress ); break; } @@ -145,8 +142,8 @@ export default async function generateSwapCalldata({ } export async function generateUni2Calls( - exchangeAddress: string, - path: SafeArray + exchangeAddress: string, + path: SafeArray ) { const executorInterface = SwapExecutor__factory.createInterface() const calls: BytesLike[] = [] @@ -155,33 +152,33 @@ export async function generateUni2Calls( const currentSwap = path.get(i) const nextSwap = path.get(i + 1) - const calldata = executorInterface.encodeFunctionData('swapUniV2', [ - currentSwap.pool, - currentSwap.assetIn, - currentSwap.assetOut, - defaultAbiCoder.encode(['uint256'], [concat(['0x03', nextSwap.pool])]), - ] + const calldata = executorInterface.encodeFunctionData("swapUniV2", [ + currentSwap.pool, + currentSwap.assetIn, + currentSwap.assetOut, + defaultAbiCoder.encode(["uint256"], [concat(["0x03", nextSwap.pool])]), + ] ) calls.push(addCallParams(calldata)) } } const lastSwap = path.last(); - const calldata = executorInterface.encodeFunctionData('swapUniV2', [ + const calldata = executorInterface.encodeFunctionData("swapUniV2", [ lastSwap.pool, lastSwap.assetIn, lastSwap.assetOut, - defaultAbiCoder.encode(['uint256'], [concat(['0x03', exchangeAddress])]), + defaultAbiCoder.encode(["uint256"], [concat(["0x03", exchangeAddress])]), ]) calls.push(addCallParams(calldata)) - return await generateCalls(calls) + return generateCalls(calls) } async function generateUni3Calls( - amount: BigNumberish, - exchangeContractAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider + amount: BigNumberish, + exchangeContractAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider ) { const encodedPools: BytesLike[] = [] for (const swap of path) { @@ -190,27 +187,27 @@ async function generateUni3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) + let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData('uniswapV3SwapTo', [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData("uniswapV3SwapTo", [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return await generateCalls([calldata]) + return generateCalls([calldata]) } async function generateOrion3Calls( - amount: BigNumberish, - exchangeContractAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider + amount: BigNumberish, + exchangeContractAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider ) { const encodedPools: BytesLike[] = [] for (const swap of path) { @@ -219,32 +216,32 @@ async function generateOrion3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) + let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData('orionV3SwapTo', [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData("orionV3SwapTo", [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return await generateCalls([calldata]) + return generateCalls([calldata]) } async function generateCurveStableSwapCalls( - amount: BigNumberish, - exchangeContractAddress: string, - executorAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider, - curveRegistry: string + amount: BigNumberish, + exchangeContractAddress: string, + executorAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider, + curveRegistry: string ) { if (path.length > 1) { - throw new Error('Supporting only single stable swap on curve') + throw new Error("Supporting only single stable swap on curve") } const executorInterface = SwapExecutor__factory.createInterface() const registry = CurveRegistry__factory.connect(curveRegistry, provider) @@ -258,15 +255,15 @@ async function generateCurveStableSwapCalls( const calls: BytesLike[] = [] if (executorAllowance.lt(amount)) { const calldata = addCallParams( - executorInterface.encodeFunctionData('safeApprove', [ - swap.assetIn, - swap.pool, - ethers.constants.MaxUint256 - ]) + executorInterface.encodeFunctionData("safeApprove", [ + swap.assetIn, + swap.pool, + ethers.constants.MaxUint256 + ]) ) calls.push(calldata) } - let calldata = executorInterface.encodeFunctionData('curveSwapStableAmountIn', [ + let calldata = executorInterface.encodeFunctionData("curveSwapStableAmountIn", [ pool, assetOut, i, @@ -278,40 +275,41 @@ async function generateCurveStableSwapCalls( calldata = addCallParams(calldata) calls.push(calldata) - return await generateCalls(calls) + return generateCalls(calls) } // Adds additional byte to single swap with settings function addCallParams( - calldata: BytesLike, - callParams?: CallParams + calldata: BytesLike, + callParams?: CallParams ) { let firstByte = 0 if (callParams) { if (callParams.value !== undefined) { firstByte += 16 // 00010000 - const encodedValue = ethers.utils.solidityPack(['uint128'], [callParams.value]) + const encodedValue = ethers.utils.solidityPack(["uint128"], [callParams.value]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedValue, calldata])) } if (callParams.target !== undefined) { firstByte += 32 // 00100000 - const encodedAddress = ethers.utils.solidityPack(['address'], [callParams.target]) + const encodedAddress = ethers.utils.solidityPack(["address"], [callParams.target]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedAddress, calldata])) } if (callParams.gaslimit !== undefined) { firstByte += 64 // 01000000 - const encodedGaslimit = ethers.utils.solidityPack(['uint32'], [callParams.gaslimit]) + const encodedGaslimit = ethers.utils.solidityPack(["uint32"], [callParams.gaslimit]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedGaslimit, calldata])) } if (callParams.isMandatory !== undefined) firstByte += 128 // 10000000 } - const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, calldata])) return calldata } + async function generateCalls(calls: BytesLike[]) { const executorInterface = SwapExecutor__factory.createInterface() - return '0x' + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) + return "0x" + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) } From e1c30e6a29d2973fb6a02ca026daefe67745b6b8 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 14:12:25 +0300 Subject: [PATCH 10/14] remove toUpperCase from factory --- src/services/Aggregator/ws/schemas/swapInfoSchema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts index c03ff97..288838b 100644 --- a/src/services/Aggregator/ws/schemas/swapInfoSchema.ts +++ b/src/services/Aggregator/ws/schemas/swapInfoSchema.ts @@ -37,7 +37,7 @@ const swapInfoSchemaBase = baseMessageSchema.extend({ p: z.string(), // pool address ai: z.string().toUpperCase(), // asset in ao: z.string().toUpperCase(), // asset out - f: z.string().toUpperCase(), // factory + f: z.string(), // factory })) }); From 5c8cc5d81557671c37946690e5259f5563b3ee27 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 14:12:48 +0300 Subject: [PATCH 11/14] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bf9a863..2a7d529 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc5", + "version": "0.19.81-rc6", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", From 663b09988221d99005c0010b6cb8902d311d2d07 Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Fri, 8 Sep 2023 14:35:04 +0300 Subject: [PATCH 12/14] apply linter --- src/Unit/Exchange/generateSwapCalldata.ts | 166 +++++++++++----------- 1 file changed, 82 insertions(+), 84 deletions(-) diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index be0534e..516a40d 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -7,7 +7,7 @@ import type Unit from '../index.js'; import { simpleFetch } from 'simple-typed-fetch'; import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/common.js'; -const EXECUTOR_SWAP_FUNCTION = "func_70LYiww" +const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww' // export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" @@ -30,40 +30,40 @@ const EXECUTOR_SWAP_FUNCTION = "func_70LYiww" } */ export type SwapInfo = { - pool: string, - assetIn: string, - assetOut: string, + pool: string + assetIn: string + assetOut: string factory: string } export type CallParams = { - isMandatory?: boolean, - target?: string, - gaslimit?: BigNumber, + isMandatory?: boolean + target?: string + gaslimit?: BigNumber value?: BigNumber } export type GenerateSwapCalldataParams = { - amount: BigNumberish, - minReturnAmount: BigNumberish, - receiverAddress: string, - path: ArrayLike, + amount: BigNumberish + minReturnAmount: BigNumberish + receiverAddress: string + path: ArrayLike unit: Unit } export default async function generateSwapCalldata({ - amount, - minReturnAmount, - receiverAddress, - path: path_, - unit - }: GenerateSwapCalldataParams + amount, + minReturnAmount, + receiverAddress, + path: path_, + unit +}: GenerateSwapCalldataParams ): Promise<{ calldata: string, swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct }> { if (path_ == undefined || path_.length == 0) { - throw new Error(`Empty path`); + throw new Error('Empty path'); } - const wethAddress = safeGet(unit.contracts, "WETH") - const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry") + const wethAddress = safeGet(unit.contracts, 'WETH') + const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry') const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); let path = SafeArray.from(path_).map((swapInfo) => { swapInfo.assetIn = safeGet(assetToAddress, swapInfo.assetIn); @@ -72,7 +72,7 @@ export default async function generateSwapCalldata({ }) const factory = path.first().factory if (!path.every(swapInfo => swapInfo.factory === factory)) { - throw new Error(`Supporting only swaps with single factory`); + throw new Error('Supporting only swaps with single factory'); } const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { @@ -80,8 +80,8 @@ export default async function generateSwapCalldata({ dstToken: path.last().assetOut, srcReceiver: swapExecutorContractAddress ?? '', dstReceiver: receiverAddress, - amount: amount, - minReturnAmount: minReturnAmount, + amount, + minReturnAmount, flags: 0 } @@ -102,35 +102,34 @@ export default async function generateSwapCalldata({ return swapInfo; }); - let calldata: string switch (factory) { - case "OrionV2": { + case 'OrionV2': { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case "UniswapV2": { + case 'UniswapV2': { swapDescription.srcReceiver = path.first().pool calldata = await generateUni2Calls(exchangeContractAddress, path); break; } - case "UniswapV3": { + case 'UniswapV3': { calldata = await generateUni3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case "OrionV3": { + case 'OrionV3': { calldata = await generateOrion3Calls(amountNativeDecimals, exchangeContractAddress, path, unit.provider) break; } - case "Curve": { + case 'Curve': { calldata = await generateCurveStableSwapCalls( - amountNativeDecimals, - exchangeContractAddress, - swapExecutorContractAddress ?? '', - path, - unit.provider, - curveRegistryAddress + amountNativeDecimals, + exchangeContractAddress, + swapExecutorContractAddress ?? '', + path, + unit.provider, + curveRegistryAddress ); break; } @@ -142,8 +141,8 @@ export default async function generateSwapCalldata({ } export async function generateUni2Calls( - exchangeAddress: string, - path: SafeArray + exchangeAddress: string, + path: SafeArray ) { const executorInterface = SwapExecutor__factory.createInterface() const calls: BytesLike[] = [] @@ -152,33 +151,33 @@ export async function generateUni2Calls( const currentSwap = path.get(i) const nextSwap = path.get(i + 1) - const calldata = executorInterface.encodeFunctionData("swapUniV2", [ - currentSwap.pool, - currentSwap.assetIn, - currentSwap.assetOut, - defaultAbiCoder.encode(["uint256"], [concat(["0x03", nextSwap.pool])]), - ] + const calldata = executorInterface.encodeFunctionData('swapUniV2', [ + currentSwap.pool, + currentSwap.assetIn, + currentSwap.assetOut, + defaultAbiCoder.encode(['uint256'], [concat(['0x03', nextSwap.pool])]), + ] ) calls.push(addCallParams(calldata)) } } const lastSwap = path.last(); - const calldata = executorInterface.encodeFunctionData("swapUniV2", [ + const calldata = executorInterface.encodeFunctionData('swapUniV2', [ lastSwap.pool, lastSwap.assetIn, lastSwap.assetOut, - defaultAbiCoder.encode(["uint256"], [concat(["0x03", exchangeAddress])]), + defaultAbiCoder.encode(['uint256'], [concat(['0x03', exchangeAddress])]), ]) calls.push(addCallParams(calldata)) - return generateCalls(calls) + return await generateCalls(calls) } async function generateUni3Calls( - amount: BigNumberish, - exchangeContractAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider + amount: BigNumberish, + exchangeContractAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider ) { const encodedPools: BytesLike[] = [] for (const swap of path) { @@ -187,27 +186,27 @@ async function generateUni3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) + let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData("uniswapV3SwapTo", [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData('uniswapV3SwapTo', [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return generateCalls([calldata]) + return await generateCalls([calldata]) } async function generateOrion3Calls( - amount: BigNumberish, - exchangeContractAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider + amount: BigNumberish, + exchangeContractAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider ) { const encodedPools: BytesLike[] = [] for (const swap of path) { @@ -216,32 +215,32 @@ async function generateOrion3Calls( const zeroForOne = token0.toLowerCase() === swap.assetIn.toLowerCase() const unwrapWETH = swap.assetOut === ethers.constants.AddressZero - let encodedPool = ethers.utils.solidityPack(["uint256"], [pool.address]) + let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address]) encodedPool = ethers.utils.hexDataSlice(encodedPool, 1) let firstByte = 0 if (unwrapWETH) firstByte += 32 if (!zeroForOne) firstByte += 128 - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) encodedPool = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, encodedPool])) encodedPools.push(encodedPool) } const executorInterface = SwapExecutor__factory.createInterface() - let calldata = executorInterface.encodeFunctionData("orionV3SwapTo", [encodedPools, exchangeContractAddress, amount]) + let calldata = executorInterface.encodeFunctionData('orionV3SwapTo', [encodedPools, exchangeContractAddress, amount]) calldata = addCallParams(calldata) - return generateCalls([calldata]) + return await generateCalls([calldata]) } async function generateCurveStableSwapCalls( - amount: BigNumberish, - exchangeContractAddress: string, - executorAddress: string, - path: SafeArray, - provider: ethers.providers.JsonRpcProvider, - curveRegistry: string + amount: BigNumberish, + exchangeContractAddress: string, + executorAddress: string, + path: SafeArray, + provider: ethers.providers.JsonRpcProvider, + curveRegistry: string ) { if (path.length > 1) { - throw new Error("Supporting only single stable swap on curve") + throw new Error('Supporting only single stable swap on curve') } const executorInterface = SwapExecutor__factory.createInterface() const registry = CurveRegistry__factory.connect(curveRegistry, provider) @@ -255,15 +254,15 @@ async function generateCurveStableSwapCalls( const calls: BytesLike[] = [] if (executorAllowance.lt(amount)) { const calldata = addCallParams( - executorInterface.encodeFunctionData("safeApprove", [ - swap.assetIn, - swap.pool, - ethers.constants.MaxUint256 - ]) + executorInterface.encodeFunctionData('safeApprove', [ + swap.assetIn, + swap.pool, + ethers.constants.MaxUint256 + ]) ) calls.push(calldata) } - let calldata = executorInterface.encodeFunctionData("curveSwapStableAmountIn", [ + let calldata = executorInterface.encodeFunctionData('curveSwapStableAmountIn', [ pool, assetOut, i, @@ -275,41 +274,40 @@ async function generateCurveStableSwapCalls( calldata = addCallParams(calldata) calls.push(calldata) - return generateCalls(calls) + return await generateCalls(calls) } // Adds additional byte to single swap with settings function addCallParams( - calldata: BytesLike, - callParams?: CallParams + calldata: BytesLike, + callParams?: CallParams ) { let firstByte = 0 if (callParams) { if (callParams.value !== undefined) { firstByte += 16 // 00010000 - const encodedValue = ethers.utils.solidityPack(["uint128"], [callParams.value]) + const encodedValue = ethers.utils.solidityPack(['uint128'], [callParams.value]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedValue, calldata])) } if (callParams.target !== undefined) { firstByte += 32 // 00100000 - const encodedAddress = ethers.utils.solidityPack(["address"], [callParams.target]) + const encodedAddress = ethers.utils.solidityPack(['address'], [callParams.target]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedAddress, calldata])) } if (callParams.gaslimit !== undefined) { firstByte += 64 // 01000000 - const encodedGaslimit = ethers.utils.solidityPack(["uint32"], [callParams.gaslimit]) + const encodedGaslimit = ethers.utils.solidityPack(['uint32'], [callParams.gaslimit]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedGaslimit, calldata])) } if (callParams.isMandatory !== undefined) firstByte += 128 // 10000000 } - const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte]) + const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte]) calldata = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, calldata])) return calldata } - async function generateCalls(calls: BytesLike[]) { const executorInterface = SwapExecutor__factory.createInterface() - return "0x" + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) + return '0x' + executorInterface.encodeFunctionData(EXECUTOR_SWAP_FUNCTION, [ethers.constants.AddressZero, calls]).slice(74) } From 0b67ea0b8694b264d29e7be4340a22d842dcb00e Mon Sep 17 00:00:00 2001 From: lomonoshka Date: Wed, 20 Sep 2023 15:43:46 +0400 Subject: [PATCH 13/14] Added debug logs --- package.json | 2 +- src/Unit/Exchange/generateSwapCalldata.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8af2ed2..fe24c38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.81-rc9", + "version": "0.19.81-rc10", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 516a40d..c15ac49 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -62,6 +62,8 @@ export default async function generateSwapCalldata({ if (path_ == undefined || path_.length == 0) { throw new Error('Empty path'); } + console.log("Initial path") + console.log(path_) const wethAddress = safeGet(unit.contracts, 'WETH') const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry') const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); @@ -74,7 +76,9 @@ export default async function generateSwapCalldata({ if (!path.every(swapInfo => swapInfo.factory === factory)) { throw new Error('Supporting only swaps with single factory'); } - + console.log("Updated path") + console.log(path) + console.log(factory) const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { srcToken: path.first().assetIn, dstToken: path.last().assetOut, From c40cc93b9422597a967e07480f39814c584346e7 Mon Sep 17 00:00:00 2001 From: TheJuze Date: Wed, 27 Sep 2023 14:05:20 +0300 Subject: [PATCH 14/14] remove comments and logs --- package-lock.json | 4 ++-- src/Unit/Exchange/generateSwapCalldata.ts | 25 ----------------------- 2 files changed, 2 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index c90285d..39122eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.87-rc1", + "version": "0.19.81-rc11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.19.87-rc1", + "version": "0.19.81-rc11", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index c15ac49..c781c28 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -9,26 +9,6 @@ import type { PromiseOrValue } from '@orionprotocol/contracts/lib/ethers-v5/comm const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww' -// export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3" - -/* const exchangeToType: Partial> = { - 'SPOOKYSWAP': 'UniswapV2', - 'PANCAKESWAP': 'UniswapV2', - 'UNISWAP': 'UniswapV2', - 'QUICKSWAP': 'UniswapV2', - 'ORION_POOL': 'UniswapV2', - 'CHERRYSWAP': 'UniswapV2', - 'OKXSWAP': 'UniswapV2', - 'INTERNAL_POOL_V2': 'UniswapV2', - 'INTERNAL_POOL_V3': "OrionV3", - 'INTERNAL_POOL_V3_0_01': "OrionV3", - 'INTERNAL_POOL_V3_0_05': "OrionV3", - 'INTERNAL_POOL_V3_0_3': "OrionV3", - 'INTERNAL_POOL_V3_1_0': "OrionV3", - 'CURVE': "Curve", - 'CURVE_FACTORY': "Curve", -} */ - export type SwapInfo = { pool: string assetIn: string @@ -62,8 +42,6 @@ export default async function generateSwapCalldata({ if (path_ == undefined || path_.length == 0) { throw new Error('Empty path'); } - console.log("Initial path") - console.log(path_) const wethAddress = safeGet(unit.contracts, 'WETH') const curveRegistryAddress = safeGet(unit.contracts, 'curveRegistry') const { assetToAddress, swapExecutorContractAddress, exchangeContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); @@ -76,9 +54,6 @@ export default async function generateSwapCalldata({ if (!path.every(swapInfo => swapInfo.factory === factory)) { throw new Error('Supporting only swaps with single factory'); } - console.log("Updated path") - console.log(path) - console.log(factory) const swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct = { srcToken: path.first().assetIn, dstToken: path.last().assetOut,