feat: moved to swapUniV2Scaled function

This commit is contained in:
Mikhail Gladchenko
2024-05-27 11:51:18 +01:00
parent cec0688c8e
commit a1455bdda9
3 changed files with 20 additions and 7 deletions

View File

@@ -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",

View File

@@ -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<SingleSwap>,
@@ -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)
}

View File

@@ -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();