Skip to content

Commit

Permalink
Merge pull request #250 from orionprotocol/generateSwapCallData-fix
Browse files Browse the repository at this point in the history
OP-5228: generateSwapCallData fix
  • Loading branch information
cheshirrrcat authored May 27, 2024
2 parents 7a832a8 + a1455bd commit 5315a7e
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 80 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.21.1",
"version": "0.20.76-rc115",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
Expand Down
26 changes: 20 additions & 6 deletions src/Unit/Exchange/callGenerators/uniswapV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -19,17 +28,21 @@ export async function generateUni2Calls(
currentSwap.pool,
currentSwap.assetIn,
currentSwap.assetOut,
nextSwap.pool
nextSwap.pool,
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))

Expand All @@ -44,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)
}
}
Loading

0 comments on commit 5315a7e

Please sign in to comment.