Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor provider #728

Merged
merged 11 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docker/eth-providers-test.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ WORKDIR /app

ENV ENDPOINT_URL=ws://mandala-node:9944
ENV ETH_RPC=http://eth-rpc-adapter-server:8545
CMD yarn e2e:feed-tx; yarn workspace @acala-network/eth-providers run test:CI
CMD yarn e2e:feed-tx; yarn workspace @acala-network/eth-providers run test:e2e
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"start:eth-rpc-adapter": "docker compose up --build -- eth-rpc-adapter-ready",
"e2e:feed-tx": "yarn workspace evm-waffle-example-dex run test",
"e2e:feed-tx-2": "yarn workspace evm-waffle-example-e2e run test",
"e2e:eth-providers": "yarn start:chain; yarn e2e:feed-tx; yarn start:eth-rpc-adapter; yarn workspace @acala-network/eth-providers run test:CI",
"e2e:eth-providers": "yarn start:chain; yarn e2e:feed-tx; yarn start:eth-rpc-adapter; yarn workspace @acala-network/eth-providers run test:e2e",
"e2e:eth-rpc-adapter": "docker compose up --build -- eth-rpc-adapter-with-subql-ready; yarn e2e:feed-tx; yarn e2e:feed-tx-2; yarn workspace @acala-network/eth-rpc-adapter run test:CI",
"e2e:waffle": "yarn start:chain; yarn run test:waffle",
"e2e:truffle": "yarn start:eth-rpc-adapter; cd examples/truffle-tutorials; yarn install --immutable; yarn test:mandala",
Expand Down
2 changes: 1 addition & 1 deletion packages/eth-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"clean": "rm -rf lib tsconfig.tsbuildinfo",
"build": "tsc --build ./tsconfig.json",
"gql:typegen": "graphql-codegen --config codegen.yml",
"test:CI": "vitest --run --config vitest.config.e2e.ts"
"test:e2e": "vitest --run --config vitest.config.e2e.ts"
},
"peerDependencies": {
"@acala-network/api": "~5.0.3-0",
Expand Down
26 changes: 2 additions & 24 deletions packages/eth-providers/src/__tests__/e2e/evm-rpc-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,8 @@ const ACALA_SUBQL = 'https://subql-query-acala.aca-api.network';

describe('connect random', () => {
it('should throw error', async () => {
try {
const provider = EvmRpcProvider.from('ws://192.-');
await provider.isReady();
} catch (e) {
expect((e as any).type).to.equal('error');
}
});
});

describe('initilization', async () => {
const provider = EvmRpcProvider.from(ACALA_NODE_URL);
await provider.isReady();

afterAll(async () => {
await sleep(5000);
await provider.disconnect();
});

it('should already has initial block number and hash', async () => {
expect(provider.latestBlockNumber).to.gt(-1);
expect(provider.latestFinalizedBlockNumber).to.gt(-1);
expect(await provider.getBlockNumber()).to.gt(-1);
expect(provider.latestBlockHash).not.to.equal(DUMMY_BLOCK_HASH);
expect(provider.latestFinalizedBlockHash).not.to.equal(DUMMY_BLOCK_HASH);
const provider = EvmRpcProvider.from('ws://192.-');
await expect(provider.isReady()).rejects.toThrowError();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ describe('TransactionReceipt', async () => {
afterAll(async () => {
await sleep(5000);
await provider.disconnect();
await sleep(1000);
});

it('getTransactionReceipt', async () => {
const account1 = evmAccounts[0];
const account2 = evmAccounts[1];

const account1Wallet = new Wallet(account1.privateKey).connect(provider as any);
const account1Wallet = new Wallet(account1.privateKey).connect(provider);
const acaContract = new Contract(ADDRESS.ACA, ACAABI.abi, account1Wallet);

const pairs = createTestPairs();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Contract, ContractFactory } from 'ethers';
import { Wallet } from '@ethersproject/wallet';
import { afterAll, describe, expect, it } from 'vitest';
import { formatEther, hexZeroPad, parseEther } from 'ethers/lib/utils';
import { hexZeroPad, parseEther } from 'ethers/lib/utils';

import { AcalaJsonRpcProvider } from '../../json-rpc-provider';
import { JsonRpcProvider } from '@ethersproject/providers';
import { sleep } from '../../utils';
import echoJson from '../abis/Echo.json';
import erc20Json from '../abis/IERC20.json';
Expand Down Expand Up @@ -127,7 +126,7 @@ describe('JsonRpcProvider', async () => {
expect(name).to.eq('USD Coin');
expect(symbol).to.eq('USDC');
expect(decimals).to.eq(6);
expect(totalSupply.gt(0)).to.be.true;
expect(totalSupply.gt(0)).toBeTruthy();
});
});

Expand All @@ -149,10 +148,10 @@ describe('JsonRpcProvider', async () => {

expect(await echo.echo()).to.equal('Deployed successfully!');

await (await echo.scream('hello Gogeta!')).wait();
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
await echo.scream('hello Gogeta!');
expect(await echo.echo()).to.equal('hello Gogeta!');

await (await echo.scream('hello Vegito!')).wait();
await echo.scream('hello Vegito!');
expect(await echo.echo()).to.equal('hello Vegito!');
});
});
Expand All @@ -175,7 +174,7 @@ describe('JsonRpcProvider', async () => {
provider.on('block', onBlock);
});

expect(blockNumber).to.be.greaterThan(0);
expect(blockNumber).to.be.eq(curBlockNumber + 1);
});

// TODO: need to setup whole stack
Expand Down
3 changes: 1 addition & 2 deletions packages/eth-providers/src/__tests__/e2e/parseBlock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ describe.concurrent('getAllReceiptsAtBlock', () => {
});

afterAll(async () => {
await sleep(5000);
await sleep(10_000);
await apiK.disconnect();
await apiA.disconnect();
await apiM.disconnect();
await sleep(5000);
});

describe.concurrent('transfer kar', async () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/eth-providers/src/__tests__/e2e/safemode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('safe mode', () => {
safeProvider.getBlockData('latest'),
]);
expect(curBlock.hash).to.equal(curFinalizedBlock.hash);
expect(curBlock.hash).to.equal(safeProvider.latestFinalizedBlockHash);
expect(curBlock.hash).to.equal(await safeProvider.latestFinalizedBlockHash);

// real test
await newBlock(false);
Expand All @@ -62,7 +62,7 @@ describe('safe mode', () => {
safeProvider.getBlockData('latest'),
]);
expect(curBlock.hash).to.equal(curFinalizedBlock.hash);
expect(curBlock.hash).to.equal(safeProvider.latestFinalizedBlockHash);
expect(curBlock.hash).to.equal(await safeProvider.latestFinalizedBlockHash);

// make sure next block is not finalized
await newBlock(false);
Expand All @@ -89,7 +89,7 @@ describe('safe mode', () => {
-------------------------- */
// ①
expect(await safeProvider._ensureSafeModeBlockTagFinalization('latest')).to.equal(
safeProvider.latestFinalizedBlockHash
await safeProvider.latestFinalizedBlockHash
);
expect(await safeProvider._ensureSafeModeBlockTagFinalization('latest')).to.equal(curFinalizedBlock.hash);

Expand Down
5 changes: 4 additions & 1 deletion packages/eth-providers/src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ type MochBlock = {
};
type MochChain = MochBlock[];

export const randHash = (): string => Math.floor(Math.random() * 66666666).toString(16);
export const randHash = (): string => {
const hash = Math.floor(Math.random() * 66666666).toString(16);
return '0x' + hash.padStart(64, '0');
};

export const randReceipt = (blockNumber: number, blockHash: string): FullReceipt =>
({
Expand Down
14 changes: 7 additions & 7 deletions packages/eth-providers/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight,
ethCallTiming,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand All @@ -221,7 +221,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight,
ethCallTiming,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight: curFinalizedHeightBad,
ethCallTiming,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('getHealthResult', () => {
},
curFinalizedHeight,
ethCallTiming,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand All @@ -306,7 +306,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight,
ethCallTiming: ethCallTimingBad,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight,
ethCallTiming: ethCallTimingBad,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand All @@ -360,7 +360,7 @@ describe('getHealthResult', () => {
cacheInfo,
curFinalizedHeight,
ethCallTiming: ethCallTimingBad,
listenersCount: { newHead: 0, logs: 0 },
listenersCount: { newHead: 0, newFinalizedHead: 0, logs: 0 },
});

expect(res).toEqual(expect.objectContaining({
Expand Down
Loading