Skip to content

Commit 12cc4ed

Browse files
authored
fix and enable eth provider tests (#1028)
* fix and enable eth provider tests * polish * add back dev pkgs * test
1 parent 6312437 commit 12cc4ed

File tree

11 files changed

+113
-89
lines changed

11 files changed

+113
-89
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ module.exports = {
2525
exports: 'always-multiline',
2626
functions: 'only-multiline',
2727
}],
28-
'@typescript-eslint/member-delimiter-style': 2,
2928

3029
/* -------------------- warn -------------------- */
3130
semi: [1, 'always'],
@@ -57,6 +56,7 @@ module.exports = {
5756
'@typescript-eslint/explicit-module-boundary-types': 0,
5857
'@typescript-eslint/ban-ts-comment': 0,
5958
'@typescript-eslint/no-non-null-asserted-optional-chain': 0,
59+
'@typescript-eslint/no-unused-expressions': 0, // short circuit if
6060
'import/no-named-as-default-member': 0,
6161
'import/no-named-as-default': 0,
6262
'no-unused-expressions': 0, // short ciucuit if

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ jobs:
3838
- name: start test infra
3939
run: docker compose up -d
4040

41+
- name: start rpc adapter for eth-providers
42+
if: matrix.project == 'eth-providers'
43+
run: yarn workspace @acala-network/eth-rpc-adapter run start:coverage
44+
4145
- name: run tests
4246
run: yarn workspace @acala-network/${{ matrix.project }} run test:coverage
4347

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
# TODO: cache does not seem to work
1212
# - ~/.bun/install/cache:/root/.bun/install/cache
1313
command:
14-
bunx @acala-network/chopsticks@latest -c /app/chopsticks/configs/acala.yml -p 9944
14+
bunx @acala-network/chopsticks@0.15.0 -c /app/chopsticks/configs/acala.yml -p 9944
1515
ports:
1616
- 9944:9944
1717
healthcheck:

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"scripts": {
1515
"clean": "yarn workspaces foreach -pvit --include \"@acala-network/*\" run clean",
1616
"build": "yarn workspaces foreach -pvit --include \"@acala-network/*\" run build",
17-
"lint": "tsc --noEmit --project tsconfig.json && eslint . --ext .js,.ts",
17+
"lint": "tsc --noEmit --project tsconfig.json && eslint . --ext .js,.ts --fix",
1818
"fix": "eslint . --ext .js,.ts --fix",
1919
"test:all": "yarn workspaces foreach -vit run test",
2020
"test": "vitest run",
@@ -55,12 +55,12 @@
5555
"ts-node": "^10.9.1",
5656
"ts-node-dev": "^2.0.0",
5757
"tsconfig-paths": "^4.2.0",
58-
"typescript": "^5.6.0",
58+
"typescript": "~5.5.0",
5959
"vite-tsconfig-paths": "^4.0.0",
6060
"vitest": "^2.1.1"
6161
},
6262
"lint-staged": {
63-
"*.{js,ts}": "eslint . --cache --ext .js,.ts --fix"
63+
"*.{js,ts}": "yarn lint"
6464
},
6565
"exports": {
6666
".": "./lib/index.js",

packages/eth-providers/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"build": "tsc",
88
"clean": "rm -rf tsconfig.tsbuildinfo .nyc_output coverage/ lib/",
99
"gql:typegen": "graphql-codegen --config codegen.yml",
10-
"test": "vitest",
11-
"test:coverage": "vitest --run --coverage"
10+
"test": "vitest --no-file-parallelism",
11+
"test:coverage": "yarn test --run --coverage"
1212
},
1313
"peerDependencies": {
1414
"@acala-network/api": "6.1.3",

packages/eth-providers/src/__tests__/getTransactionReceipt.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { afterAll, describe, expect, it } from 'vitest';
44
import ACAABI from '@acala-network/contracts/build/contracts/Token.json';
55
import ADDRESS from '@acala-network/contracts/utils/AcalaAddress';
66

7-
import { BigNumber } from 'ethers';
87
import { EvmRpcProvider } from '../rpc-provider';
9-
import { parseEther } from 'ethers/lib/utils';
8+
import { parseUnits } from 'ethers/lib/utils';
109
import evmAccounts from './utils/evmAccounts';
1110

1211
describe('TransactionReceipt', async () => {
@@ -18,24 +17,20 @@ describe('TransactionReceipt', async () => {
1817
await provider.disconnect();
1918
});
2019

21-
it('getTransactionReceipt', async () => {
20+
it('getReceiptAtBlock', async () => {
2221
const wallet1 = new Wallet(evmAccounts[0].privateKey).connect(provider);
2322
const addr1 = wallet1.address;
2423
console.log({ addr1 });
2524
const acaContract = new Contract(ADDRESS.ACA, ACAABI.abi, wallet1);
2625

27-
const tx = await acaContract.transfer(evmAccounts[1].evmAddress, parseEther('10'), {
28-
gasLimit: BigNumber.from(34132001n),
29-
gasPrice: BigNumber.from(200786445289n),
30-
type: 0,
31-
});
26+
const tx = await acaContract.transfer(evmAccounts[1].evmAddress, parseUnits('10', 12));
3227
await tx.wait();
3328

3429
const receipt = await provider.getReceiptAtBlock(tx.hash, tx.blockHash);
3530
expect(receipt).toBeTruthy();
36-
expect(receipt.blockHash).equal(tx.blockHash);
37-
expect(receipt.logs.length).equal(1);
38-
expect(receipt.logs[0].blockNumber).equal(tx.blockNumber);
39-
expect(receipt.logs[0].topics.length).equal(3);
31+
expect(receipt!.blockHash).equal(tx.blockHash);
32+
expect(receipt!.logs.length).equal(1);
33+
expect(receipt!.logs[0].blockNumber).equal(tx.blockNumber);
34+
expect(receipt!.logs[0].topics.length).equal(3);
4035
});
4136
});

packages/eth-providers/src/__tests__/json-rpc-provider.test.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,77 @@
11
import { Contract, ContractFactory } from 'ethers';
22
import { Wallet } from '@ethersproject/wallet';
3-
import { afterAll, describe, expect, it } from 'vitest';
3+
import { describe, expect, it } from 'vitest';
44
import { hexZeroPad, parseEther } from 'ethers/lib/utils';
55

66
import { AcalaJsonRpcProvider } from '../json-rpc-provider';
7-
import { sleep } from '../utils';
87
import echoJson from './abis/Echo.json';
98
import erc20Json from './abis/IERC20.json';
9+
import evmAccounts from './utils/evmAccounts';
1010

1111
const localEthRpc = process.env.ETH_RPC || 'http://localhost:8545';
1212

1313
describe('JsonRpcProvider', async () => {
1414
/* --------- karura --------- */
1515
const someOne = '0xf7ABcfa42bF7e7d43d3d53C665deD80fDAfB5244';
1616

17-
const provider = new AcalaJsonRpcProvider('https://eth-rpc-karura.aca-api.network');
17+
const providerKar = new AcalaJsonRpcProvider('https://eth-rpc-karura.aca-api.network');
1818
const usdcAddr = '0x1F3a10587A20114EA25Ba1b388EE2dD4A337ce27';
19-
const usdc = new Contract(usdcAddr, erc20Json.abi, provider);
19+
const usdc = new Contract(usdcAddr, erc20Json.abi, providerKar);
2020

2121
/* --------- local --------- */
22-
const testKey = 'a872f6cbd25a0e04a08b1e21098017a9e6194d101d75e13111f71410c59cd57f'; // 0x75E480dB528101a381Ce68544611C169Ad7EB342
22+
const testKey = evmAccounts[0].privateKey; // 0x75E480dB528101a381Ce68544611C169Ad7EB342
2323
const providerLocal = new AcalaJsonRpcProvider(localEthRpc);
2424
const wallet = new Wallet(testKey, providerLocal);
2525

26-
afterAll(async () => {
27-
await sleep(5000);
28-
});
29-
3026
describe.concurrent('get chain data', () => {
3127
it('get chain id', async () => {
32-
const network = await provider.getNetwork();
28+
const network = await providerKar.getNetwork();
3329
expect(network.chainId).to.eq(686);
3430
});
3531

3632
it('get block number', async () => {
37-
const blockNumber = await provider.getBlockNumber();
33+
const blockNumber = await providerKar.getBlockNumber();
3834
expect(blockNumber).to.be.gt(0);
3935
});
4036

4137
it('get gas price', async () => {
42-
const gasPrice = await provider.getGasPrice();
38+
const gasPrice = await providerKar.getGasPrice();
4339
expect(gasPrice.gt(0)).to.be.true;
4440
});
4541

4642
it('get balance', async () => {
47-
const balance = await provider.getBalance(someOne);
43+
const balance = await providerKar.getBalance(someOne);
4844
expect(balance.gt(0)).to.be.true;
4945
});
5046

5147
it('get transaction count', async () => {
52-
const transactionCount = await provider.getTransactionCount(wallet.address);
48+
const transactionCount = await providerKar.getTransactionCount(wallet.address);
5349
expect(transactionCount).to.be.gt(0);
5450
});
5551

5652
it('get contract code', async () => {
5753
const bridgeImplAddress = '0xae9d7fe007b3327AA64A32824Aaac52C42a6E624';
58-
const code = await provider.getCode(bridgeImplAddress);
54+
const code = await providerKar.getCode(bridgeImplAddress);
5955
expect(code.length).to.gt(100);
6056
});
6157

6258
it('get transaction by hash', async () => {
6359
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95';
64-
const fetchedTransaction = await provider.getTransaction(txHash);
60+
const fetchedTransaction = await providerKar.getTransaction(txHash);
6561
expect(fetchedTransaction.hash).to.equal(txHash);
6662
});
6763

6864
it('get transaction receipt', async () => {
6965
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95';
70-
const fetchedTransaction = await provider.getTransactionReceipt(txHash);
66+
const fetchedTransaction = await providerKar.getTransactionReceipt(txHash);
7167
expect(fetchedTransaction.transactionHash).to.equal(txHash);
7268
});
7369

7470
it('get block with transactions', async () => {
75-
let data = await provider.getBlockWithTransactions(1818518);
71+
let data = await providerKar.getBlockWithTransactions(1818518);
7672
expect(data.transactions.length).to.eq(1);
7773

78-
data = await provider.getBlockWithTransactions(2449983);
74+
data = await providerKar.getBlockWithTransactions(2449983);
7975
expect(data.transactions.length).to.eq(2);
8076
});
8177

@@ -87,7 +83,7 @@ describe('JsonRpcProvider', async () => {
8783
toBlock: 4128888,
8884
};
8985

90-
const logs = await provider.getLogs(filter);
86+
const logs = await providerKar.getLogs(filter);
9187

9288
expect(logs.length).to.eq(6);
9389
for (const log of logs) {
@@ -97,11 +93,11 @@ describe('JsonRpcProvider', async () => {
9793
});
9894
});
9995

100-
describe('call', () => {
96+
describe.concurrent('call', () => {
10197
it('estimate gas', async () => {
10298
const to = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
10399
const value = parseEther('0.1');
104-
const gasEstimate = await provider.estimateGas({
100+
const gasEstimate = await providerKar.estimateGas({
105101
from: someOne,
106102
to,
107103
value,
@@ -197,20 +193,20 @@ describe('JsonRpcProvider', async () => {
197193

198194
describe('subscription', () => {
199195
it('subscribe to new block', async () => {
200-
const curBlockNumber = await provider.getBlockNumber();
196+
const curBlockNumber = await providerKar.getBlockNumber();
201197

202198
const blockNumber = await new Promise((resolve, reject) => {
203199
const onBlock = (blockNumber: number) => {
204200
// TODO: is it normal that cb is triggered immediately for current block
205201
if (blockNumber > curBlockNumber) {
206-
provider.off('block', onBlock);
202+
providerKar.off('block', onBlock);
207203
resolve(blockNumber);
208204
}
209205

210-
setTimeout(() => reject('<provider.onBlock> no new block in 30s!'), 30_000);
206+
setTimeout(() => reject('<providerKar.onBlock> no new block in 30s!'), 30_000);
211207
};
212208

213-
provider.on('block', onBlock);
209+
providerKar.on('block', onBlock);
214210
});
215211

216212
expect(blockNumber).to.be.eq(curBlockNumber + 1);

0 commit comments

Comments
 (0)