diff --git a/src/Unit/Exchange/generateSwapCalldata.ts b/src/Unit/Exchange/generateSwapCalldata.ts index 9c86866..2bfeefa 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, type SafeArray } from '../../utils/safeGetters.js'; +import { safeGet, SafeArray } from '../../utils/safeGetters.js'; import type Unit from '../index.js'; import { simpleFetch } from 'simple-typed-fetch'; @@ -46,7 +46,6 @@ export default async function generateSwapCalldata({ throw new Error(`Empty path`); } const factory = path.first().factory - if (!path.every(e => e.factory === factory)) { throw new Error(`Supporting only swaps with single factory`); } @@ -60,7 +59,7 @@ export default async function generateSwapCalldata({ minReturnAmount: minReturnAmount, flags: 0 } - + let calldata: string switch (factory) { case "OrionV2": { diff --git a/src/utils/safeGetters.ts b/src/utils/safeGetters.ts index 944f498..3bf4336 100644 --- a/src/utils/safeGetters.ts +++ b/src/utils/safeGetters.ts @@ -1,14 +1,18 @@ export class SafeArray extends Array { - public static override from(array: T[]): SafeArray { + public static override from(array: ArrayLike): SafeArray { return new SafeArray(array); } - constructor(array: T[]) { + constructor(array: ArrayLike) { super(array.length); - array.forEach((element, index) => { - this[index] = element; - }) + for (const index in array) { + const value = array[index] + if (value === undefined) { + throw new Error("Array passed to constructor has undefined values") + } + this[index] = value + } } public toArray(): T[] { @@ -26,7 +30,7 @@ export class SafeArray extends Array { public get(this: SafeArray, index: number): T { const value = this.at(index); if (value === undefined) { - throw new Error(`Element at index ${index} is undefined. Array: ${this}`) + throw new Error(`Element at index ${index} is undefined.`) } return value }