Skip to content

Commit

Permalink
Merge pull request #35 from xBA5ED/master
Browse files Browse the repository at this point in the history
Removed 'ethers' imports and replaced them with more specific imports
  • Loading branch information
cavanmflynn authored Apr 8, 2022
2 parents 1fb521a + 46f062b commit 7b9e684
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
17 changes: 10 additions & 7 deletions src/abi.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { ethers } from 'ethers';
import {AbiCoder, ParamType} from '@ethersproject/abi';
import {BytesLike} from '@ethersproject/bytes';
import {keccak256} from '@ethersproject/keccak256';
import {toUtf8Bytes} from '@ethersproject/strings';

export class Abi {
public static encode(name: string, inputs: ethers.utils.ParamType[], params: any[]) {
public static encode(name: string, inputs: ParamType[], params: any[]) {
const functionSignature = getFunctionSignature(name, inputs);
const functionHash = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(functionSignature));
const functionHash = keccak256(toUtf8Bytes(functionSignature));
const functionData = functionHash.substring(2, 10);
const abiCoder = new ethers.utils.AbiCoder();
const abiCoder = new AbiCoder();
const argumentString = abiCoder.encode(inputs, params);
const argumentData = argumentString.substring(2);
const inputData = `0x${functionData}${argumentData}`;
return inputData;
}

public static decode(outputs: ethers.utils.ParamType[], data: ethers.utils.BytesLike) {
const abiCoder = new ethers.utils.AbiCoder();
public static decode(outputs: ParamType[], data: BytesLike) {
const abiCoder = new AbiCoder();
const params = abiCoder.decode(outputs, data);
return params;
}
}

function getFunctionSignature(name: string, inputs: ethers.utils.ParamType[]) {
function getFunctionSignature(name: string, inputs: ParamType[]) {
const types = [];
for (const input of inputs) {
if (input.type === 'tuple') {
Expand Down
8 changes: 5 additions & 3 deletions src/call.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ethers } from 'ethers';
import {Contract} from '@ethersproject/contracts';
import {Provider} from '@ethersproject/providers';

import { Abi } from './abi';
import { multicallAbi } from './abi/multicall';
import { ContractCall } from './types';

export async function all<T extends any[] = any[]>(
calls: ContractCall[],
multicallAddress: string,
provider: ethers.providers.Provider,
provider: Provider,
): Promise<T> {
const multicall = new ethers.Contract(multicallAddress, multicallAbi, provider);
const multicall = new Contract(multicallAddress, multicallAbi, provider);
const callRequests = calls.map(call => {
const callData = Abi.encode(call.name, call.inputs, call.params);
return {
Expand Down
3 changes: 1 addition & 2 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Fragment, FunctionFragment, JsonFragment } from '@ethersproject/abi';
import { utils } from 'ethers';

export class Contract {
private _address: string;
Expand Down Expand Up @@ -39,7 +38,7 @@ export class Contract {
}

function toFragment(abi: JsonFragment[] | string[] | Fragment[]): Fragment[] {
return abi.map((item: JsonFragment | string | Fragment) => utils.Fragment.from(item));
return abi.map((item: JsonFragment | string | Fragment) => Fragment.from(item));
}

function makeCallFunction(contract: Contract, name: string) {
Expand Down
8 changes: 4 additions & 4 deletions src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ethers } from 'ethers';
import {Provider as EthersProvider} from '@ethersproject/abstract-provider';
import { all } from './call';
import { getEthBalance } from './calls';
import { ContractCall } from './types';

export class Provider {
private _provider: ethers.providers.Provider;
private _provider: EthersProvider;
private _multicallAddress: string;

constructor(provider: ethers.providers.Provider, chainId?: number) {
constructor(provider: EthersProvider, chainId?: number) {
this._provider = provider;
this._multicallAddress = getAddressForChainId(chainId);
}
Expand Down Expand Up @@ -59,7 +59,7 @@ function getAddressForChainId(chainId: number) {
return multicallAddresses[chainId];
}

async function getAddress(provider: ethers.providers.Provider) {
async function getAddress(provider: EthersProvider) {
const { chainId } = await provider.getNetwork();
return getAddressForChainId(chainId);
}
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ethers } from 'ethers';
import {ParamType} from '@ethersproject/abi';

export interface ContractCall {
contract: {
address: string;
};
name: string;
inputs: ethers.utils.ParamType[];
outputs: ethers.utils.ParamType[];
inputs: ParamType[];
outputs: ParamType[];
params: any[];
}
4 changes: 2 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {InfuraProvider} from '@ethersproject/providers';
import { assert } from 'chai';
import { ethers } from 'ethers';
import { Contract, Provider } from '../src';

const provider = new ethers.providers.InfuraProvider('mainnet');
const provider = new InfuraProvider('mainnet');
const ethcallProvider = new Provider(provider, 1);

it('human readable abi', async () => {
Expand Down

0 comments on commit 7b9e684

Please sign in to comment.