Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

chore: make various code fixes #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/call.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -83,7 +83,7 @@ async function tryAll<T>(
multicall2: Multicall | null,
calls: Call[],
overrides?: CallOverrides,
): Promise<(T | null)[]> {
): Promise<Array<T | null>> {
const contract = multicall2
? new Contract(multicall2.address, multicall2Abi, provider)
: null;
Expand Down Expand Up @@ -124,7 +124,7 @@ async function tryEach<T>(
multicall3: Multicall | null,
calls: FailableCall[],
overrides?: CallOverrides,
): Promise<(T | null)[]> {
): Promise<Array<T | null>> {
const contract = multicall3
? new Contract(multicall3.address, multicall3Abi, provider)
: null;
Expand Down
2 changes: 1 addition & 1 deletion src/calls.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
20 changes: 10 additions & 10 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,19 +22,19 @@ 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) {
const name = callFunction.name;
if (!name) {
continue;
}
const getCall = this.#makeCallFunction(name);
const getCall = this.makeCallFunction(name);
if (!this[name]) {
Object.defineProperty(this, name, {
enumerable: true,
Expand All @@ -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 {
Expand Down
80 changes: 39 additions & 41 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ interface ProviderConfig {
* Represents a Multicall provider. Used to execute multiple Calls.
*/
class Provider {
#provider?: EthersProvider;
#config: Partial<ProviderConfig>;
#multicall: Multicall | null;
#multicall2: Multicall | null;
#multicall3: Multicall | null;
provider?: EthersProvider;
config: Partial<ProviderConfig>;
multicall: Multicall | null;
multicall2: Multicall | null;
multicall3: Multicall | null;

/**
* Create a provider.
Expand All @@ -39,11 +39,11 @@ class Provider {
provider: EthersProvider,
config?: Partial<ProviderConfig>,
) {
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);
}

/**
Expand All @@ -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);
}

Expand All @@ -68,11 +68,11 @@ class Provider {
* @returns List of fetched data
*/
async all<T>(calls: Call[], overrides?: CallOverrides): Promise<T[]> {
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<T>(provider, multicall, calls, overrides);
}

Expand All @@ -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<T>(provider, multicall, calls, overrides);
}

Expand All @@ -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,
Expand All @@ -123,15 +123,15 @@ class Provider {
return await callTryEach<T>(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':
Expand All @@ -143,7 +143,7 @@ class Provider {
}
}

#isAvailable(multicall: Multicall | null, block?: BlockTag): boolean {
isAvailable(multicall: Multicall | null, block?: BlockTag): boolean {
if (!multicall) {
return false;
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion test/contract.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"module": "ES2022",
"moduleResolution": "Node",

"allowSyntheticDefaultImports": true,
"declaration": true,
Expand Down