mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
export generateCalldata function without unit param
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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';
|
||||
|
||||
9
src/utils/addressLikeToString.ts
Normal file
9
src/utils/addressLikeToString.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user