diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index ed32ea9..f3fd6f4 100644 --- a/src/Unit/Exchange/generateSwapCalldata.ts +++ b/src/Unit/Exchange/generateSwapCalldata.ts @@ -2,7 +2,7 @@ import type { ExchangeWithGenericSwap } from '@orionprotocol/contracts/lib/ether import { UniswapV3Pool__factory, ERC20__factory, SwapExecutor__factory, CurveRegistry__factory } from '@orionprotocol/contracts/lib/ethers-v5/index.js'; import { BigNumber, ethers } from 'ethers'; import { concat, defaultAbiCoder, type BytesLike } from 'ethers/lib/utils.js'; -import { safeGet, SafeArray } from '../../utils/safeGetters.js'; +import must, { safeGet, SafeArray } from '../../utils/safeGetters.js'; import type Unit from '../index.js'; import { simpleFetch } from 'simple-typed-fetch'; @@ -29,6 +29,8 @@ export type GenerateSwapCalldataParams = { minReturnAmount: string, receiverAddress: string, path: ArrayLike, + exchangeContractAddress?: string, + swapExecutorContractAddress?: string, unit: Unit } @@ -37,12 +39,21 @@ export default async function generateSwapCalldata({ minReturnAmount, receiverAddress, path: path_, + exchangeContractAddress, + swapExecutorContractAddress, unit }: GenerateSwapCalldataParams ): Promise<{ calldata: string, swapDescription: ExchangeWithGenericSwap.SwapDescriptionStruct }> { const wethAddress = safeGet(unit.contracts, "WETH") const curveRegistryAddress = safeGet(unit.contracts, "curveRegistry") - const { exchangeContractAddress, swapExecutorContractAddress } = await simpleFetch(unit.blockchainService.getInfo)(); + if (swapExecutorContractAddress === undefined || swapExecutorContractAddress === undefined) { + const info = await simpleFetch(unit.blockchainService.getInfo)(); + if (swapExecutorContractAddress === undefined) swapExecutorContractAddress = info.swapExecutorContractAddress + if (exchangeContractAddress === undefined) exchangeContractAddress = info.exchangeContractAddress + } + must(swapExecutorContractAddress !== undefined) + must(exchangeContractAddress !== undefined) + const path = SafeArray.from(path_) if (path == undefined || path.length == 0) { diff --git a/src/utils/safeGetters.ts b/src/utils/safeGetters.ts index 3bf4336..17f273f 100644 --- a/src/utils/safeGetters.ts +++ b/src/utils/safeGetters.ts @@ -48,4 +48,13 @@ export function safeGet(obj: Partial>, key: string, errorMe const value = obj[key]; if (value === undefined) throw new Error(`Key '${key.toString()}' not found in object. Available keys: ${Object.keys(obj).join(', ')}.${errorMessage ? ` ${errorMessage}` : ''}`); return value; +} + +const prefix = 'Requirement not met'; + +export default function must(condition: unknown, message?: string | (() => string)): asserts condition { + if (condition) return; + const provided = typeof message === 'function' ? message() : message; + const value = provided ? `${prefix}: ${provided}` : prefix; + throw new Error(value); } \ No newline at end of file