From a1455bdda9f3b39525b0462ae27d1d235a239074 Mon Sep 17 00:00:00 2001 From: Mikhail Gladchenko Date: Mon, 27 May 2024 11:51:18 +0100 Subject: [PATCH] feat: moved to swapUniV2Scaled function --- package.json | 2 +- src/Unit/Exchange/callGenerators/uniswapV2.ts | 23 +++++++++++++++---- src/Unit/Exchange/generateSwapCalldata.ts | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 9aa6c78..638a0f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.76-rc114", + "version": "0.20.76-rc115", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/callGenerators/uniswapV2.ts b/src/Unit/Exchange/callGenerators/uniswapV2.ts index 9742895..86778bc 100644 --- a/src/Unit/Exchange/callGenerators/uniswapV2.ts +++ b/src/Unit/Exchange/callGenerators/uniswapV2.ts @@ -3,6 +3,15 @@ import { SafeArray } from "../../../utils/safeGetters.js" import { type BytesLike, type BigNumberish, concat, ethers, toBeHex } from "ethers" import { addCallParams } from "./utils.js" import type { SingleSwap } from "../../../types.js" +import { BigNumber } from 'bignumber.js'; + +const BILLION = 1000000000; +const TEN_THOUSANDS = 10000; + +function countScaledFee(fee: string) { + // The count is needed for the swapUniV2Scaled function, where the denominator is one billion + return new BigNumber(fee).multipliedBy(BILLION).div(TEN_THOUSANDS).toNumber(); +} export async function generateUni2Calls( path: SafeArray, @@ -20,17 +29,20 @@ export async function generateUni2Calls( currentSwap.assetIn, currentSwap.assetOut, nextSwap.pool, - nextSwap.fee + currentSwap.fee ) calls.push(call) } } + const lastSwap = path.last(); - const calldata = executorInterface.encodeFunctionData('swapUniV2', [ + const fee = lastSwap.fee ?? 3; + const scaledFee = countScaledFee(fee.toString()); + const calldata = executorInterface.encodeFunctionData('swapUniV2Scaled', [ lastSwap.pool, lastSwap.assetIn, lastSwap.assetOut, - ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat(['0x03', recipient])]), + ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat([toBeHex(scaledFee), recipient])]), ]) calls.push(addCallParams(calldata)) @@ -45,11 +57,12 @@ export function generateUni2Call( fee: BigNumberish = 3, ) { const executorInterface = SwapExecutor__factory.createInterface() - const calldata = executorInterface.encodeFunctionData('swapUniV2', [ + const scaledFee = countScaledFee(fee.toString()); + const calldata = executorInterface.encodeFunctionData('swapUniV2Scaled', [ pool, assetIn, assetOut, - ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat([toBeHex(fee), recipient])]), + ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat([toBeHex(scaledFee), recipient])]), ]) return addCallParams(calldata) } diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index a600947..d58a68a 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -135,7 +135,7 @@ export async function generateSwapCalldata({ swapInfo.assetOut = swapInfo.assetOut.toLowerCase() return swapInfo; }); - console.log('path', path); + logger?.(`path: ${path}`); const { assetIn: srcToken } = path.first(); const { assetOut: dstToken } = path.last();