diff --git a/src/call.ts b/src/call.ts index f2f4c7f..553f7c6 100644 --- a/src/call.ts +++ b/src/call.ts @@ -1,12 +1,12 @@ import type { JsonFragment, JsonFragmentType, Provider } from 'ethers'; import { Contract, Result, concat } from 'ethers'; -import deploylessMulticallAbi from './abi/deploylessMulticall.json' assert { type: 'json' }; -import deploylessMulticall2Abi from './abi/deploylessMulticall2.json' assert { type: 'json' }; -import deploylessMulticall3Abi from './abi/deploylessMulticall3.json' assert { type: 'json' }; -import multicallAbi from './abi/multicall.json' assert { type: 'json' }; -import multicall2Abi from './abi/multicall2.json' assert { type: 'json' }; -import multicall3Abi from './abi/multicall3.json' assert { type: 'json' }; +import deploylessMulticallAbi from './abi/deploylessMulticall.json'; +import deploylessMulticall2Abi from './abi/deploylessMulticall2.json'; +import deploylessMulticall3Abi from './abi/deploylessMulticall3.json'; +import multicallAbi from './abi/multicall.json'; +import multicall2Abi from './abi/multicall2.json'; +import multicall3Abi from './abi/multicall3.json'; import type { Params } from './abi.js'; import Abi from './abi.js'; import type { Multicall } from './multicall.js'; @@ -83,7 +83,7 @@ async function tryAll( multicall2: Multicall | null, calls: Call[], overrides?: CallOverrides, -): Promise<(T | null)[]> { +): Promise> { const contract = multicall2 ? new Contract(multicall2.address, multicall2Abi, provider) : null; @@ -124,7 +124,7 @@ async function tryEach( multicall3: Multicall | null, calls: FailableCall[], overrides?: CallOverrides, -): Promise<(T | null)[]> { +): Promise> { const contract = multicall3 ? new Contract(multicall3.address, multicall3Abi, provider) : null; diff --git a/src/calls.ts b/src/calls.ts index 222d27d..178517d 100644 --- a/src/calls.ts +++ b/src/calls.ts @@ -1,4 +1,4 @@ -import multicallAbi from './abi/multicall.json' assert { type: 'json' }; +import multicallAbi from './abi/multicall.json'; import type { Call } from './call.js'; import Contract from './contract.js'; diff --git a/src/contract.ts b/src/contract.ts index a16f52f..e0169d7 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -11,8 +11,8 @@ import type { Call } from './call.js'; * daiContract.balanceOf(address); // returns a Call object */ class Contract { - #address: string; - #functions: JsonFragment[]; + address: string; + functions: JsonFragment[]; // eslint-disable-next-line @typescript-eslint/no-explicit-any [key: string]: Call | any; @@ -22,11 +22,11 @@ class Contract { * @param abi ABI of the contract */ constructor(address: string, abi: JsonFragment[]) { - this.#address = address; + this.address = address; - this.#functions = abi.filter((x) => x.type === 'function'); - const callFunctions = this.#functions.filter( - (x) => x.stateMutability === 'pure' || x.stateMutability === 'view', + this.functions = abi.filter((x) => x.type === 'function'); + const callFunctions = this.functions.filter( + (f) => f.stateMutability === 'pure' || f.stateMutability === 'view', ); for (const callFunction of callFunctions) { @@ -34,7 +34,7 @@ class Contract { if (!name) { continue; } - const getCall = this.#makeCallFunction(name); + const getCall = this.makeCallFunction(name); if (!this[name]) { Object.defineProperty(this, name, { enumerable: true, @@ -45,10 +45,10 @@ class Contract { } } - #makeCallFunction(name: string) { + makeCallFunction(name: string) { return (...params: Params): Call => { - const address = this.#address; - const func = this.#functions.find((f) => f.name === name); + const address = this.address; + const func = this.functions.find((f) => f.name === name); const inputs = func?.inputs || []; const outputs = func?.outputs || []; return { diff --git a/src/provider.ts b/src/provider.ts index a386127..2e922b0 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -22,11 +22,11 @@ interface ProviderConfig { * Represents a Multicall provider. Used to execute multiple Calls. */ class Provider { - #provider?: EthersProvider; - #config: Partial; - #multicall: Multicall | null; - #multicall2: Multicall | null; - #multicall3: Multicall | null; + provider?: EthersProvider; + config: Partial; + multicall: Multicall | null; + multicall2: Multicall | null; + multicall3: Multicall | null; /** * Create a provider. @@ -39,11 +39,11 @@ class Provider { provider: EthersProvider, config?: Partial, ) { - this.#provider = provider; - this.#config = config || {}; - this.#multicall = this.#getMulticall(chainId, 1); - this.#multicall2 = this.#getMulticall(chainId, 2); - this.#multicall3 = this.#getMulticall(chainId, 3); + this.provider = provider; + this.config = config || {}; + this.multicall = this.getMulticall(chainId, 1); + this.multicall2 = this.getMulticall(chainId, 2); + this.multicall3 = this.getMulticall(chainId, 3); } /** @@ -52,10 +52,10 @@ class Provider { * @returns Ether balance fetching call */ getEthBalance(address: string): Call { - const multicall = this.#multicall3 || this.#multicall2 || this.#multicall; - if (!multicall) { + const multicall = this.multicall3 || this.multicall2 || this.multicall; + if (!multicall) throw Error('Multicall contract is not available on this network.'); - } + return getEthBalance(address, multicall.address); } @@ -68,11 +68,11 @@ class Provider { * @returns List of fetched data */ async all(calls: Call[], overrides?: CallOverrides): Promise { - if (!this.#provider) { + if (!this.provider) throw Error('Provider should be initialized before use.'); - } - const multicall = this.#getContract('BASIC', overrides?.blockTag); - const provider = this.#provider; + + const multicall = this.getContract('BASIC', overrides?.blockTag); + const provider = this.provider; return await callAll(provider, multicall, calls, overrides); } @@ -87,11 +87,11 @@ class Provider { calls: Call[], overrides?: CallOverrides, ): Promise<(T | null)[]> { - if (!this.#provider) { + if (!this.provider) throw Error('Provider should be initialized before use.'); - } - const multicall = this.#getContract('TRY_ALL', overrides?.blockTag); - const provider = this.#provider; + + const multicall = this.getContract('TRY_ALL', overrides?.blockTag); + const provider = this.provider; return await callTryAll(provider, multicall, calls, overrides); } @@ -109,11 +109,11 @@ class Provider { canFail: boolean[], overrides?: CallOverrides, ): Promise<(T | null)[]> { - if (!this.#provider) { + if (!this.provider) throw Error('Provider should be initialized before use.'); - } - const multicall = this.#getContract('TRY_EACH', overrides?.blockTag); - const provider = this.#provider; + + const multicall = this.getContract('TRY_EACH', overrides?.blockTag); + const provider = this.provider; const failableCalls = calls.map((call, index) => { return { ...call, @@ -123,15 +123,15 @@ class Provider { return await callTryEach(provider, multicall, failableCalls, overrides); } - #getContract(call: CallType, block?: BlockTag): Multicall | null { - const multicall = this.#isAvailable(this.#multicall, block) - ? this.#multicall + getContract(call: CallType, block?: BlockTag): Multicall | null { + const multicall = this.isAvailable(this.multicall, block) + ? this.multicall : null; - const multicall2 = this.#isAvailable(this.#multicall2, block) - ? this.#multicall2 + const multicall2 = this.isAvailable(this.multicall2, block) + ? this.multicall2 : null; - const multicall3 = this.#isAvailable(this.#multicall3, block) - ? this.#multicall3 + const multicall3 = this.isAvailable(this.multicall3, block) + ? this.multicall3 : null; switch (call) { case 'BASIC': @@ -143,7 +143,7 @@ class Provider { } } - #isAvailable(multicall: Multicall | null, block?: BlockTag): boolean { + isAvailable(multicall: Multicall | null, block?: BlockTag): boolean { if (!multicall) { return false; } @@ -156,7 +156,7 @@ class Provider { return multicall.block < block; } - #getMulticall(chainId: number, version: 1 | 2 | 3): Multicall | null { + getMulticall(chainId: number, version: 1 | 2 | 3): Multicall | null { function getRegistryMulticall( chainId: number, version: 1 | 2 | 3, @@ -171,14 +171,12 @@ class Provider { } } - const customMulticall = this.#config?.multicall; - if (!customMulticall) { - return getRegistryMulticall(chainId, version); - } + const customMulticall = this.config?.multicall; + if (!customMulticall) return getRegistryMulticall(chainId, version); + const address = customMulticall.address; - if (!address) { - return getRegistryMulticall(chainId, version); - } + if (!address) return getRegistryMulticall(chainId, version); + return { address, block: customMulticall.block || 0, diff --git a/test/contract.test.ts b/test/contract.test.ts index 34090e4..49c78a6 100644 --- a/test/contract.test.ts +++ b/test/contract.test.ts @@ -1,6 +1,7 @@ import { describe, test, expect } from 'vitest'; -import { Call, Contract } from '../src'; +import { Contract } from '../src'; +import type { Call } from '../src'; import erc20Abi from './abi/erc20.json'; diff --git a/tsconfig.json b/tsconfig.json index 5aa0a73..c79f757 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,8 @@ { "compilerOptions": { "target": "ES2022", - "module": "NodeNext", - "moduleResolution": "NodeNext", + "module": "ES2022", + "moduleResolution": "Node", "allowSyntheticDefaultImports": true, "declaration": true,