Skip to content

Commit

Permalink
feat: call multicall v3 contract, add unit test for amoy, base, polyg…
Browse files Browse the repository at this point in the history
…on chain
  • Loading branch information
galvin96 committed Jul 16, 2024
1 parent fb84bcc commit c495864
Show file tree
Hide file tree
Showing 8 changed files with 1,464 additions and 244 deletions.
1,166 changes: 1,166 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethers-multicall",
"version": "0.2.3",
"name": "@ixswap1/ethers-multicall",
"version": "0.1.0",
"description": "Make multiple Ethereum network requests in a single HTTP query. ethcall for ethers v5.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -20,6 +20,7 @@
"@ts-tools/node": "^0.8.3",
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/node": "^20.14.10",
"chai": "^4.2.0",
"mocha": "^5.2.0",
"tslint": "^5.12.0",
Expand Down
30 changes: 14 additions & 16 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,20 @@ export class Provider {
}

const multicallAddresses = {
1: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
3: '0xF24b01476a55d635118ca848fbc7Dab69d403be3',
4: '0x42ad527de7d4e9d9d011ac45b31d8551f8fe9821',
5: '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e',
42: '0x2cc8688c5f75e365aaeeb4ea8d6a480405a48d2a',
56: '0x1Ee38d535d541c55C9dae27B12edf090C608E6Fb',
66: '0x94fEadE0D3D832E4A05d459eBeA9350c6cDd3bCa',
97: '0x3A09ad1B8535F25b48e6Fa0CFd07dB6B017b31B2',
100: '0xb5b692a88bdfc81ca69dcb1d924f59f0413a602a',
128: '0x2C55D51804CF5b436BA5AF37bD7b8E5DB70EBf29',
137: '0x11ce4B23bD875D7F5C6a31084f55fDe1e9A87507',
250: '0x0118EF741097D0d3cc88e46233Da1e407d9ac139',
1337: '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e',
42161: '0x813715eF627B01f4931d8C6F8D2459F26E19137E',
43114: '0x7f3aC7C283d7E6662D886F494f7bc6F1993cDacf',
80001: '0x08411ADd0b5AA8ee47563b146743C13b3556c9Cc',
1: '0xcA11bde05977b3631167028862bE2a173976CA11',
5: '0xcA11bde05977b3631167028862bE2a173976CA11',
56: '0xcA11bde05977b3631167028862bE2a173976CA11',
97: '0xcA11bde05977b3631167028862bE2a173976CA11',
100: '0xcA11bde05977b3631167028862bE2a173976CA11',
128: '0xcA11bde05977b3631167028862bE2a173976CA11',
137: '0xcA11bde05977b3631167028862bE2a173976CA11', // Polygon
250: '0xcA11bde05977b3631167028862bE2a173976CA11',
8453: '0xca11bde05977b3631167028862be2a173976ca11', // BASE
42161: '0xcA11bde05977b3631167028862bE2a173976CA11',
43114: '0xcA11bde05977b3631167028862bE2a173976CA11',
80001: '0xcA11bde05977b3631167028862bE2a173976CA11',
80002: '0xcA11bde05977b3631167028862bE2a173976CA11',
84532: '0xcA11bde05977b3631167028862bE2a173976CA11', // BASE Sepolia
};

export function setMulticallAddress(chainId: number, address: string) {
Expand Down
26 changes: 26 additions & 0 deletions test/amoy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ethers } from 'ethers';
import { assert } from 'chai';
import { Contract, Provider } from '../src';

const rpcUrl = 'https://polygon-amoy.drpc.org'
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const ethcallProvider = new Provider(provider, 80002);

describe('amoy network', () => {
it('should retrieve totalSupply both of the tokens successfully', async () => {
const abi = ['function totalSupply() public view returns (uint256)'];
const addresses = [
'0x0Fd9e8d3aF1aaee056EB9e802c3A762a667b1904',
'0x6EEBe75caf9c579B3FBA9030760B84050283b50a'
];

const linkContract = new Contract(addresses[0], abi);
const usdcContract = new Contract(addresses[1], abi);

const calls = [linkContract.totalSupply(), usdcContract.totalSupply()];
const [linkSupply, usdcSupply] = await ethcallProvider.all(calls);

assert.equal(linkSupply.toString(), '10001000000000000000000000');
assert.equal(usdcSupply.toString(), '100000000000000000000000000');
})
})
26 changes: 26 additions & 0 deletions test/base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ethers } from 'ethers';
import { assert } from 'chai';
import { Contract, Provider } from '../src';

const rpcUrl = 'https://base-pokt.nodies.app'
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const ethcallProvider = new Provider(provider, 8453);

describe('base network', () => {
it('should retrieve totalSupply both of the tokens successfully', async () => {
const abi = ['function totalSupply() public view returns (uint256)'];
const addresses = [
'0xfA980cEd6895AC314E7dE34Ef1bFAE90a5AdD21b',
'0x1C7a460413dD4e964f96D8dFC56E7223cE88CD85'
];

const primeContract = new Contract(addresses[0], abi);
const seamContract = new Contract(addresses[1], abi);

const calls = [primeContract.totalSupply(), seamContract.totalSupply()];
const [primeSupply, seamSupply] = await ethcallProvider.all(calls);

assert.equal(primeSupply.toString(), '655283967592417997806328');
assert.equal(seamSupply.toString(), '100000000000000000000000000');
})
})
78 changes: 40 additions & 38 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
import {InfuraProvider} from '@ethersproject/providers';
import { InfuraProvider } from '@ethersproject/providers';
import { assert } from 'chai';
import { Contract, Provider } from '../src';

const provider = new 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'
];
describe('ethereum network', () => {
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 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);
const calls = [yfiContract.totalSupply(), uniContract.totalSupply()];
const [yfiSupply, uniSupply] = await ethcallProvider.all(calls);

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

it('json abi', async () => {
const abi = [
{
it('json abi', async () => {
const abi = [
{
constant: true,
inputs: [],
name: 'totalSupply',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256'
}
{
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');
});
}
];
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');
})
})
26 changes: 26 additions & 0 deletions test/polygon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ethers } from 'ethers';
import { assert } from 'chai';
import { Contract, Provider } from '../src';

const rpcUrl = 'https://polygon-pokt.nodies.app'
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const ethcallProvider = new Provider(provider, 137);

describe('polygon network', () => {
it('should retrieve totalSupply both of the tokens successfully', async () => {
const abi = ['function totalSupply() public view returns (uint256)'];
const addresses = [
'0x0000000000000000000000000000000000001010',
'0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619'
];

const maticContract = new Contract(addresses[0], abi);
const wethContract = new Contract(addresses[1], abi);

const calls = [maticContract.totalSupply(), wethContract.totalSupply()];
const [maticSupply, wethSupply] = await ethcallProvider.all(calls);

assert.equal(maticSupply.toString(), '10000000000000000000000000000');
assert.equal(wethSupply.toString(), '151625134652898175725399');
})
})
Loading

0 comments on commit c495864

Please sign in to comment.