Add conditional paremeters

This commit is contained in:
lomonoshka
2023-08-07 12:10:55 +03:00
parent e205ebeb97
commit 994d766391
2 changed files with 22 additions and 2 deletions

View File

@@ -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<SwapInfo>,
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) {

View File

@@ -48,4 +48,13 @@ export function safeGet<V>(obj: Partial<Record<string, V>>, 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);
}