Merged with main

This commit is contained in:
lomonoshka
2023-10-10 19:03:38 +04:00
70 changed files with 9828 additions and 753 deletions

View File

@@ -1,5 +1,5 @@
import { SwapExecutor__factory, CurveRegistry__factory } from "@orionprotocol/contracts/lib/ethers-v5/index.js"
import type { BigNumberish, providers } from "ethers"
import { SwapExecutor__factory, CurveRegistry__factory } from "@orionprotocol/contracts/lib/ethers-v6/index.js"
import type { BigNumberish, JsonRpcProvider } from "ethers"
import { addCallParams } from "./utils.js"
import type { SingleSwap } from "../../../types.js"
@@ -7,7 +7,7 @@ export async function generateCurveStableSwapCall(
amount: BigNumberish,
to: string,
swap: SingleSwap,
provider: providers.JsonRpcProvider,
provider: JsonRpcProvider,
curveRegistry: string
) {
const executorInterface = SwapExecutor__factory.createInterface()

View File

@@ -1,13 +1,15 @@
import { SwapExecutor__factory } from "@orionprotocol/contracts/lib/ethers-v5/index.js"
import { SwapExecutor__factory } from "@orionprotocol/contracts/lib/ethers-v6/index.js"
import type { BigNumberish } from "ethers"
import { type CallParams, addCallParams } from "./utils.js"
import type { AddressLike } from "ethers"
export async function generateTransferCall(
token: string,
target: string,
token: AddressLike,
target: AddressLike,
amount: BigNumberish,
callParams?: CallParams
) {
const executorInterface = SwapExecutor__factory.createInterface()
const calldata = executorInterface.encodeFunctionData('safeTransfer', [
token,
@@ -19,8 +21,8 @@ export async function generateTransferCall(
}
export async function generateApproveCall(
token: string,
target: string,
token: AddressLike,
target: AddressLike,
amount: BigNumberish,
callParams?: CallParams
) {

View File

@@ -1,8 +1,6 @@
import { SwapExecutor__factory } from "@orionprotocol/contracts/lib/ethers-v5/index.js"
import { SafeArray } from "../../../utils/safeGetters.js"
import { BigNumber } from "ethers"
import type { BytesLike, BigNumberish } from "ethers"
import { defaultAbiCoder, concat } from "ethers/lib/utils.js"
import { type BytesLike, type BigNumberish, concat, ethers, toBeHex } from "ethers"
import { addCallParams, generateCalls } from "./utils.js"
import type { SingleSwap } from "../../../types.js"
@@ -31,7 +29,7 @@ export async function generateUni2Calls(
lastSwap.pool,
lastSwap.assetIn,
lastSwap.assetOut,
defaultAbiCoder.encode(['uint256'], [concat(['0x03', recipient])]),
ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat(['0x03', recipient])]),
])
calls.push(addCallParams(calldata))
@@ -43,14 +41,14 @@ export async function generateUni2Call(
assetIn: string,
assetOut: string,
recipient: string,
fee: BigNumberish = BigNumber.from(3),
fee: BigNumberish = 3,
) {
const executorInterface = SwapExecutor__factory.createInterface()
const calldata = executorInterface.encodeFunctionData('swapUniV2', [
pool,
assetIn,
assetOut,
defaultAbiCoder.encode(['uint256'], [concat([BigNumber.from(fee).toHexString(), recipient])]),
ethers.AbiCoder.defaultAbiCoder().encode(['uint256'], [concat([toBeHex(fee), recipient])]),
])
return addCallParams(calldata)
}

View File

@@ -1,5 +1,5 @@
import { SwapExecutor__factory, UniswapV3Pool__factory } from "@orionprotocol/contracts/lib/ethers-v5/index.js"
import { type BigNumberish, providers, type BytesLike, ethers } from "ethers"
import { SwapExecutor__factory, UniswapV3Pool__factory } from "@orionprotocol/contracts/lib/ethers-v6/index.js"
import { type BigNumberish , type BytesLike, ethers, JsonRpcProvider } from "ethers"
import { SafeArray } from "../../../utils/safeGetters.js"
import { addCallParams, generateCalls } from "./utils.js"
import type { SingleSwap } from "../../../types.js"
@@ -8,7 +8,7 @@ export async function generateUni3Call(
swap: SingleSwap,
amount: BigNumberish | undefined,
recipient: string,
provider: providers.JsonRpcProvider
provider: JsonRpcProvider
) {
if (typeof amount === 'undefined') amount = 0
@@ -23,7 +23,7 @@ export async function generateOrion3Call(
swap: SingleSwap,
amount: BigNumberish | undefined,
recipient: string,
provider: providers.JsonRpcProvider
provider: JsonRpcProvider
) {
if (typeof amount === 'undefined') amount = 0
@@ -38,9 +38,9 @@ export async function generateUni3Calls(
path: SafeArray<SingleSwap>,
amount: BigNumberish,
recipient: string,
provider: providers.JsonRpcProvider
provider: JsonRpcProvider
) {
const encodedPools: BytesLike[] = []
const encodedPools: BigNumberish[] = []
for (const swap of path) {
const encodedPool = await encodePoolV3(swap.pool, swap.assetIn, swap.assetOut, provider)
encodedPools.push(encodedPool)
@@ -56,9 +56,9 @@ export async function generateOrion3Calls(
path: SafeArray<SingleSwap>,
amount: BigNumberish,
recipient: string,
provider: providers.JsonRpcProvider
provider: JsonRpcProvider
) {
const encodedPools: BytesLike[] = []
const encodedPools: BigNumberish[] = []
for (const swap of path) {
const encodedPool = await encodePoolV3(swap.pool, swap.assetIn, swap.assetOut, provider)
encodedPools.push(encodedPool)
@@ -74,19 +74,19 @@ export async function encodePoolV3(
poolAddress: string,
assetInAddress: string,
assetOutAddress: string,
provider: providers.JsonRpcProvider
provider: JsonRpcProvider
) {
const pool = UniswapV3Pool__factory.connect(poolAddress, provider)
const token0 = await pool.token0()
const zeroForOne = token0.toLowerCase() === assetInAddress.toLowerCase()
const unwrapWETH = assetOutAddress === ethers.constants.AddressZero
const unwrapWETH = assetOutAddress === ethers.ZeroAddress
let encodedPool = ethers.utils.solidityPack(['uint256'], [pool.address])
encodedPool = ethers.utils.hexDataSlice(encodedPool, 1)
let encodedPool = ethers.solidityPacked(['uint256'], [await pool.getAddress()])
encodedPool = ethers.dataSlice(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]))
const encodedFirstByte = ethers.solidityPacked(['uint8'], [firstByte])
encodedPool = ethers.hexlify(ethers.concat([encodedFirstByte, encodedPool]))
return encodedPool
}

View File

@@ -1,14 +1,14 @@
import type { PromiseOrValue } from "@orionprotocol/contracts/lib/ethers-v5/common.js"
import { ERC20__factory, SwapExecutor__factory } from "@orionprotocol/contracts/lib/ethers-v5/index.js"
import { type BytesLike, ethers, BigNumber, type BigNumberish, providers } from "ethers"
import { ERC20__factory, SwapExecutor__factory } from "@orionprotocol/contracts/lib/ethers-v6/index.js"
import type { AddressLike } from "ethers"
import { type BytesLike, ethers, type BigNumberish } from "ethers"
const EXECUTOR_SWAP_FUNCTION = 'func_70LYiww'
export type CallParams = {
isMandatory?: boolean,
target?: string,
gaslimit?: BigNumber,
value?: BigNumber
gaslimit?: BigNumberish,
value?: BigNumberish
}
export type PatchParams = {
@@ -19,7 +19,7 @@ export type PatchParams = {
export function pathCallWithBalance(
calldata: BytesLike,
tokenAddress: string,
tokenAddress: AddressLike,
patchParams: PatchParams = { skipCallDataPatching: false, skipValuePatching: true }
) {
const executorInterface = SwapExecutor__factory.createInterface()
@@ -28,7 +28,7 @@ export function pathCallWithBalance(
calldata,
skipMaskAndOffset,
tokenAddress,
ethers.constants.MaxUint256])
ethers.MaxUint256])
return addCallParams(calldata)
}
@@ -40,31 +40,31 @@ export function addCallParams(
if (callParams) {
if (callParams.value !== undefined) {
firstByte += 16 // 00010000
const encodedValue = ethers.utils.solidityPack(['uint128'], [callParams.value])
calldata = ethers.utils.hexlify(ethers.utils.concat([encodedValue, calldata]))
const encodedValue = ethers.solidityPacked(['uint128'], [callParams.value])
calldata = ethers.hexlify(ethers.concat([encodedValue, calldata]))
}
if (callParams.target !== undefined) {
firstByte += 32 // 00100000
const encodedAddress = ethers.utils.solidityPack(['address'], [callParams.target])
calldata = ethers.utils.hexlify(ethers.utils.concat([encodedAddress, calldata]))
const encodedAddress = ethers.solidityPacked(['address'], [callParams.target])
calldata = ethers.hexlify(ethers.concat([encodedAddress, calldata]))
}
if (callParams.gaslimit !== undefined) {
firstByte += 64 // 01000000
const encodedGaslimit = ethers.utils.solidityPack(['uint32'], [callParams.gaslimit])
calldata = ethers.utils.hexlify(ethers.utils.concat([encodedGaslimit, calldata]))
const encodedGaslimit = ethers.solidityPacked(['uint32'], [callParams.gaslimit])
calldata = ethers.hexlify(ethers.concat([encodedGaslimit, calldata]))
}
if (callParams.isMandatory !== undefined) firstByte += 128 // 10000000
}
const encodedFirstByte = ethers.utils.solidityPack(['uint8'], [firstByte])
calldata = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, calldata]))
const encodedFirstByte = ethers.solidityPacked(['uint8'], [firstByte])
calldata = ethers.hexlify(ethers.concat([encodedFirstByte, calldata]))
return calldata
}
export function createPatchMask(calldata: BytesLike, patchParams?: PatchParams) {
let firstByte = 0
let mask = ethers.utils.solidityPack(["uint256"], [(calldata.length - 4) / 2 - 32])
mask = ethers.utils.hexDataSlice(mask, 1)
let mask = ethers.solidityPacked(["uint256"], [(calldata.length - 4) / 2 - 32])
mask = ethers.dataSlice(mask, 1)
if (patchParams) {
if (patchParams.skipOnZeroAmount !== undefined && patchParams.skipOnZeroAmount === false) {
firstByte += 32
@@ -79,23 +79,25 @@ export function createPatchMask(calldata: BytesLike, patchParams?: PatchParams)
console.log(firstByte)
}
}
const encodedFirstByte = ethers.utils.solidityPack(["uint8"], [firstByte])
mask = ethers.utils.hexlify(ethers.utils.concat([encodedFirstByte, mask]))
const encodedFirstByte = ethers.solidityPacked(["uint8"], [firstByte])
mask = ethers.hexlify(ethers.concat([encodedFirstByte, mask]))
console.log(mask)
return mask
}
export 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.ZeroAddress, calls]).slice(74)
}
export async function exchangeToNativeDecimals(token: PromiseOrValue<string>, amount: BigNumberish, provider: providers.JsonRpcProvider) {
export async function exchangeToNativeDecimals(token: AddressLike, amount: BigNumberish, provider: ethers.JsonRpcProvider) {
token = await token
let decimals = 18
if (token !== ethers.constants.AddressZero) {
if (typeof token !== "string") token = await token.getAddress()
let decimals = 18n
if (token !== ethers.ZeroAddress) {
const contract = ERC20__factory.connect(token, provider)
decimals = await contract.decimals()
decimals = BigInt(await contract.decimals())
}
return BigNumber.from(amount).mul(BigNumber.from(10).pow(decimals)).div(BigNumber.from(10).pow(8))
return BigInt(amount) * (BigInt(10) ** decimals) / (BigInt(10) ** 8n)
}