Skip to content

Commit

Permalink
Merge pull request #6 from c0rv0s/master
Browse files Browse the repository at this point in the history
Enable human readable abis and include multicall address for goerli
  • Loading branch information
cavanmflynn authored May 3, 2021
2 parents 1f008bc + 6738ba3 commit 3538349
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"chai": "^4.2.0",
"mocha": "^5.2.0",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
"typescript": "^4.2.4"
},
"dependencies": {
"ethers": "^5.0.0"
Expand Down
18 changes: 12 additions & 6 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JsonFragment } from '@ethersproject/abi';
import { Fragment, FunctionFragment, JsonFragment } from '@ethersproject/abi';
import { utils } from 'ethers';

export class Contract {
private _address: string;
private _abi: JsonFragment[];
private _functions: JsonFragment[];
private _abi: Fragment[];
private _functions: FunctionFragment[];

get address() {
return this._address;
Expand All @@ -17,11 +18,12 @@ export class Contract {
return this._functions;
}

constructor(address: string, abi: JsonFragment[]) {
constructor(address: string, abi: JsonFragment[] | string[] | Fragment[]) {
this._address = address;
this._abi = abi;

this._functions = abi.filter(x => x.type === 'function');
this._abi = toFragment(abi);

this._functions = this._abi.filter(x => x.type === 'function').map(x => FunctionFragment.from(x));
const callFunctions = this._functions.filter(x => x.stateMutability === 'pure' || x.stateMutability === 'view');

for (const callFunction of callFunctions) {
Expand All @@ -36,6 +38,10 @@ export class Contract {
[method: string]: any;
}

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

function makeCallFunction(contract: Contract, name: string) {
return (...params: any[]) => {
const { address } = contract;
Expand Down
1 change: 1 addition & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const multicallAddresses = {
1: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
3: '0xF24b01476a55d635118ca848fbc7Dab69d403be3',
4: '0x42ad527de7d4e9d9d011ac45b31d8551f8fe9821',
5: '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e',
42: '0x2cc8688c5f75e365aaeeb4ea8d6a480405a48d2a',
56: '0x1Ee38d535d541c55C9dae27B12edf090C608E6Fb',
100: '0xb5b692a88bdfc81ca69dcb1d924f59f0413a602a',
Expand Down
57 changes: 55 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
describe('noop', () => {
// no tests :(
import { assert } from 'chai';
import { ethers } from 'ethers';
import { Contract, Provider } from '../src';

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

it('human readable abi', async () => {
const abi = ['function totalSupply() public view returns (uint256)'];
const addresses = [
'0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e',
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'
];

const yfiContract = new Contract(addresses[0], abi);
const uniContract = new Contract(addresses[1], abi);

const calls = [yfiContract.totalSupply(), uniContract.totalSupply()];
const [yfiSupply, uniSupply] = await ethcallProvider.all(calls);

assert.equal(yfiSupply.toString(), '36666000000000000000000');
assert.equal(uniSupply.toString(), '1000000000000000000000000000');
});

it('json abi', async () => {
const abi = [
{
constant: true,
inputs: [],
name: 'totalSupply',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
];
const addresses = [
'0x0bc529c00C6401aEF6D220BE8C6Ea1667F6Ad93e',
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'
];

const yfiContract = new Contract(addresses[0], abi);
const uniContract = new Contract(addresses[1], abi);

const calls = [yfiContract.totalSupply(), uniContract.totalSupply()];
const [yfiSupply, uniSupply] = await ethcallProvider.all(calls);

assert.equal(yfiSupply.toString(), '36666000000000000000000');
assert.equal(uniSupply.toString(), '1000000000000000000000000000');
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,10 @@ type-detect@^4.0.0, type-detect@^4.0.5:
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==

typescript@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.2.2.tgz#fe8101c46aa123f8353523ebdcf5730c2ae493e5"
integrity sha512-VCj5UiSyHBjwfYacmDuc/NOk4QQixbE+Wn7MFJuS0nRuPQbof132Pw4u53dm264O8LPc2MVsc7RJNml5szurkg==
typescript@^4.2.4:
version "4.2.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.2.4.tgz#8610b59747de028fda898a8aef0e103f156d0961"
integrity sha512-V+evlYHZnQkaz8TRBuxTA92yZBPotr5H+WhQ7bD3hZUndx5tGOa1fuCgeSjxAzM1RiN5IzvadIXTVefuuwZCRg==

wrappy@1:
version "1.0.2"
Expand Down

0 comments on commit 3538349

Please sign in to comment.