export generateCalldata function without unit param

This commit is contained in:
lomonoshka
2023-10-27 15:41:22 +04:00
parent 1fc201a626
commit 92d7e2028e
5 changed files with 64 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.20.10",
"version": "0.20.10-rc0",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -12,10 +12,12 @@ import { exchangeToNativeDecimals, generateCalls, pathCallWithBalance } from './
import { generateApproveCall, generateTransferCall } from './callGenerators/erc20.js';
import { generateCurveStableSwapCall } from './callGenerators/curve.js';
import type { SingleSwap } from '../../types.js';
import type { AddressLike } from 'ethers';
import { addressLikeToString } from '../../utils/addressLikeToString.js';
export type Factory = "UniswapV2" | "UniswapV3" | "Curve" | "OrionV2" | "OrionV3"
export type GenerateSwapCalldataParams = {
export type GenerateSwapCalldataWithUnitParams = {
amount: BigNumberish
minReturnAmount: BigNumberish
receiverAddress: string
@@ -23,13 +25,25 @@ export type GenerateSwapCalldataParams = {
unit: Unit
}
export default async function generateSwapCalldata({
export type GenerateSwapCalldataParams = {
amount: BigNumberish
minReturnAmount: BigNumberish
receiverAddress: string
path: ArrayLike<SingleSwap>
wethAddress: AddressLike,
curveRegistryAddress: AddressLike,
swapExecutorContractAddress: AddressLike,
exchangeContractAddress: AddressLike,
provider: JsonRpcProvider
}
export async function generateSwapCalldataWithUnit({
amount,
minReturnAmount,
receiverAddress,
path: arrayLikePath,
unit
}: GenerateSwapCalldataParams
}: GenerateSwapCalldataWithUnitParams
): Promise<{ calldata: string, swapDescription: LibValidator.SwapDescriptionStruct }> {
if (arrayLikePath == undefined || arrayLikePath.length == 0) {
throw new Error('Empty path');
@@ -43,6 +57,36 @@ export default async function generateSwapCalldata({
return swapInfo;
})
return generateSwapCalldata({
amount,
minReturnAmount,
receiverAddress,
path,
wethAddress,
curveRegistryAddress,
swapExecutorContractAddress,
exchangeContractAddress,
provider: unit.provider
})
}
export async function generateSwapCalldata({
amount,
minReturnAmount,
receiverAddress,
path: arrayLikePath,
wethAddress: wethAddressLike,
curveRegistryAddress: curveRegistryAddressLike,
swapExecutorContractAddress: swapExecutorContractAddressLike,
exchangeContractAddress: exchangeContractAddressLike,
provider,
}: GenerateSwapCalldataParams) {
const wethAddress = await addressLikeToString(wethAddressLike)
const curveRegistryAddress = await addressLikeToString(curveRegistryAddressLike)
const swapExecutorContractAddress = await addressLikeToString(swapExecutorContractAddressLike)
const exchangeContractAddress = await addressLikeToString(exchangeContractAddressLike)
let path = SafeArray.from(arrayLikePath)
const { factory, assetIn: srcToken } = path.first()
const dstToken = path.last().assetOut
@@ -55,13 +99,14 @@ export default async function generateSwapCalldata({
minReturnAmount,
flags: 0
}
const amountNativeDecimals = await exchangeToNativeDecimals(srcToken, amount, unit.provider);
const amountNativeDecimals = await exchangeToNativeDecimals(srcToken, amount, provider);
path = SafeArray.from(arrayLikePath).map((singleSwap) => {
if (singleSwap.assetIn == ethers.ZeroAddress) singleSwap.assetIn = wethAddress
if (singleSwap.assetOut == ethers.ZeroAddress) singleSwap.assetOut = wethAddress
return singleSwap;
});
const isSingleFactorySwap = path.every(singleSwap => singleSwap.factory === factory)
let calldata: BytesLike
if (isSingleFactorySwap) {
@@ -73,7 +118,7 @@ export default async function generateSwapCalldata({
amountNativeDecimals,
swapExecutorContractAddress,
curveRegistryAddress,
unit.provider
provider
))
} else {
({ swapDescription, calldata } = await processMultiFactorySwaps(
@@ -83,7 +128,7 @@ export default async function generateSwapCalldata({
amountNativeDecimals,
swapExecutorContractAddress,
curveRegistryAddress,
unit.provider
provider
))
}

View File

@@ -1,7 +1,7 @@
import type Unit from '../index.js';
import deposit, { type DepositParams } from './deposit.js';
import getSwapInfo, { type GetSwapInfoParams } from './getSwapInfo.js';
import generateSwapCalldata, { type GenerateSwapCalldataParams } from './generateSwapCalldata.js';
import {generateSwapCalldataWithUnit, type GenerateSwapCalldataParams } from './generateSwapCalldata.js';
import withdraw, { type WithdrawParams } from './withdraw.js';
type PureDepositParams = Omit<DepositParams, 'unit'>
@@ -39,7 +39,7 @@ export default class Exchange {
}
public generateSwapCalldata(params: PureGenerateSwapCalldataParams) {
return generateSwapCalldata({
return generateSwapCalldataWithUnit({
...params,
unit: this.unit
})

View File

@@ -4,6 +4,7 @@ BigNumber.config({ EXPONENTIAL_AT: 1e+9 });
export * as config from './config/index.js';
export { default as Unit } from './Unit/index.js';
export { default as Orion } from './Orion/index.js';
export {generateSwapCalldata} from './Unit/Exchange/generateSwapCalldata.js';
export { default as factories} from './constants/factories.js';
export * as utils from './utils/index.js';
export * as services from './services/index.js';

View File

@@ -0,0 +1,9 @@
import type { AddressLike } from "ethers";
export async function addressLikeToString(address: AddressLike): Promise<string> {
address = await address
if (typeof address !== 'string') {
address = await address.getAddress()
}
return address
}