forked from MyCryptoHQ/eth-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.test.ts
75 lines (59 loc) · 2.51 KB
/
http.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { decode, encode, fromHex, toHex } from '@findeth/abi';
import { network, ethers, waffle } from 'hardhat';
import { JsonRpcServer } from 'hardhat/internal/hardhat-network/jsonrpc/server';
import { ETHER_BALANCES_ID, ETHER_BALANCES_TYPE, TOKEN_BALANCES_ID, TOKEN_BALANCES_TYPE } from '../constants';
import { fixture } from '../eth-scan.test';
import { Result } from '../types';
import { withId } from '../utils';
import HttpProvider from './http';
const { createFixtureLoader, provider } = waffle;
const { isProvider, send } = HttpProvider;
const loadFixture = createFixtureLoader(provider.getWallets(), provider);
const server = new JsonRpcServer({
hostname: '127.0.0.1',
port: 8547,
provider: network.provider
});
beforeAll(async () => {
await server.listen();
}, 100000);
afterAll(async () => {
await server.close();
});
describe('isProvider', () => {
it('checks if a provider is an HTTP provider', () => {
expect(isProvider('https://foo')).toBe(true);
expect(
isProvider({
url: 'https://foo'
})
).toBe(true);
expect(isProvider({})).toBe(false);
});
});
describe('send', () => {
it('gets the Ether balances from the contract', async () => {
const { contract, addresses } = await loadFixture(fixture);
const data = withId(ETHER_BALANCES_ID, encode(ETHER_BALANCES_TYPE, [addresses]));
const response = await send<string>('http://127.0.0.1:8547', 'eth_call', [{ to: contract.address, data }]);
const results = decode(['(bool,bytes)[]'], fromHex(response))[0] as Result[];
for (let i = 0; i < addresses.length; i++) {
const balance = await ethers.provider.getBalance(addresses[i]);
const [success, value] = results[i];
expect(success).toBe(true);
expect(toHex(value)).toBe(balance.toHexString().slice(2).padStart(64, '0'));
}
});
it('gets the token balances from the contract', async () => {
const { contract, addresses, token } = await loadFixture(fixture);
await token.mock.balanceOf.returns('1000');
const data = withId(TOKEN_BALANCES_ID, encode(TOKEN_BALANCES_TYPE, [addresses, token.address]));
const response = await send<string>('http://127.0.0.1:8547', 'eth_call', [{ to: contract.address, data }]);
const results = decode(['(bool,bytes)[]'], fromHex(response))[0] as Result[];
for (let i = 0; i < addresses.length; i++) {
const [success, value] = results[i];
expect(success).toBe(true);
expect(toHex(value)).toBe('00000000000000000000000000000000000000000000000000000000000003e8');
}
});
});