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

fix and enable eth provider tests #1028

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module.exports = {
exports: 'always-multiline',
functions: 'only-multiline',
}],
'@typescript-eslint/member-delimiter-style': 2,

/* -------------------- warn -------------------- */
semi: [1, 'always'],
Expand Down Expand Up @@ -57,6 +56,7 @@ module.exports = {
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0,
'@typescript-eslint/no-non-null-asserted-optional-chain': 0,
'@typescript-eslint/no-unused-expressions': 0, // short circuit if
'import/no-named-as-default-member': 0,
'import/no-named-as-default': 0,
'no-unused-expressions': 0, // short ciucuit if
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
- name: start test infra
run: docker compose up -d

- name: start rpc adapter for eth-providers
if: matrix.project == 'eth-providers'
run: yarn workspace @acala-network/eth-rpc-adapter run start:coverage

- name: run tests
run: yarn workspace @acala-network/${{ matrix.project }} run test:coverage

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
# TODO: cache does not seem to work
# - ~/.bun/install/cache:/root/.bun/install/cache
command:
bunx @acala-network/chopsticks@latest -c /app/chopsticks/configs/acala.yml -p 9944
bunx @acala-network/chopsticks@0.15.0 -c /app/chopsticks/configs/acala.yml -p 9944
ports:
- 9944:9944
healthcheck:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"clean": "yarn workspaces foreach -pvit --include \"@acala-network/*\" run clean",
"build": "yarn workspaces foreach -pvit --include \"@acala-network/*\" run build",
"lint": "tsc --noEmit --project tsconfig.json && eslint . --ext .js,.ts",
"lint": "tsc --noEmit --project tsconfig.json && eslint . --ext .js,.ts --fix",
"fix": "eslint . --ext .js,.ts --fix",
"test:all": "yarn workspaces foreach -vit run test",
"test": "vitest run",
Expand Down Expand Up @@ -55,12 +55,12 @@
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.6.0",
"typescript": "~5.5.0",
"vite-tsconfig-paths": "^4.0.0",
"vitest": "^2.1.1"
},
"lint-staged": {
"*.{js,ts}": "eslint . --cache --ext .js,.ts --fix"
"*.{js,ts}": "yarn lint"
},
"exports": {
".": "./lib/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/eth-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build": "tsc",
"clean": "rm -rf tsconfig.tsbuildinfo .nyc_output coverage/ lib/",
"gql:typegen": "graphql-codegen --config codegen.yml",
"test": "vitest",
"test:coverage": "vitest --run --coverage"
"test": "vitest --no-file-parallelism",
"test:coverage": "yarn test --run --coverage"
},
"peerDependencies": {
"@acala-network/api": "6.1.3",
Expand Down
19 changes: 7 additions & 12 deletions packages/eth-providers/src/__tests__/getTransactionReceipt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { afterAll, describe, expect, it } from 'vitest';
import ACAABI from '@acala-network/contracts/build/contracts/Token.json';
import ADDRESS from '@acala-network/contracts/utils/AcalaAddress';

import { BigNumber } from 'ethers';
import { EvmRpcProvider } from '../rpc-provider';
import { parseEther } from 'ethers/lib/utils';
import { parseUnits } from 'ethers/lib/utils';
import evmAccounts from './utils/evmAccounts';

describe('TransactionReceipt', async () => {
Expand All @@ -18,24 +17,20 @@ describe('TransactionReceipt', async () => {
await provider.disconnect();
});

it('getTransactionReceipt', async () => {
it('getReceiptAtBlock', async () => {
const wallet1 = new Wallet(evmAccounts[0].privateKey).connect(provider);
const addr1 = wallet1.address;
console.log({ addr1 });
const acaContract = new Contract(ADDRESS.ACA, ACAABI.abi, wallet1);

const tx = await acaContract.transfer(evmAccounts[1].evmAddress, parseEther('10'), {
gasLimit: BigNumber.from(34132001n),
gasPrice: BigNumber.from(200786445289n),
type: 0,
});
const tx = await acaContract.transfer(evmAccounts[1].evmAddress, parseUnits('10', 12));
await tx.wait();

const receipt = await provider.getReceiptAtBlock(tx.hash, tx.blockHash);
expect(receipt).toBeTruthy();
expect(receipt.blockHash).equal(tx.blockHash);
expect(receipt.logs.length).equal(1);
expect(receipt.logs[0].blockNumber).equal(tx.blockNumber);
expect(receipt.logs[0].topics.length).equal(3);
expect(receipt!.blockHash).equal(tx.blockHash);
expect(receipt!.logs.length).equal(1);
expect(receipt!.logs[0].blockNumber).equal(tx.blockNumber);
expect(receipt!.logs[0].topics.length).equal(3);
});
});
48 changes: 22 additions & 26 deletions packages/eth-providers/src/__tests__/json-rpc-provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,77 @@
import { Contract, ContractFactory } from 'ethers';
import { Wallet } from '@ethersproject/wallet';
import { afterAll, describe, expect, it } from 'vitest';
import { describe, expect, it } from 'vitest';
import { hexZeroPad, parseEther } from 'ethers/lib/utils';

import { AcalaJsonRpcProvider } from '../json-rpc-provider';
import { sleep } from '../utils';
import echoJson from './abis/Echo.json';
import erc20Json from './abis/IERC20.json';
import evmAccounts from './utils/evmAccounts';

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

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

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

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

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

describe.concurrent('get chain data', () => {
it('get chain id', async () => {
const network = await provider.getNetwork();
const network = await providerKar.getNetwork();
expect(network.chainId).to.eq(686);
});

it('get block number', async () => {
const blockNumber = await provider.getBlockNumber();
const blockNumber = await providerKar.getBlockNumber();
expect(blockNumber).to.be.gt(0);
});

it('get gas price', async () => {
const gasPrice = await provider.getGasPrice();
const gasPrice = await providerKar.getGasPrice();
expect(gasPrice.gt(0)).to.be.true;
});

it('get balance', async () => {
const balance = await provider.getBalance(someOne);
const balance = await providerKar.getBalance(someOne);
expect(balance.gt(0)).to.be.true;
});

it('get transaction count', async () => {
const transactionCount = await provider.getTransactionCount(wallet.address);
const transactionCount = await providerKar.getTransactionCount(wallet.address);
expect(transactionCount).to.be.gt(0);
});

it('get contract code', async () => {
const bridgeImplAddress = '0xae9d7fe007b3327AA64A32824Aaac52C42a6E624';
const code = await provider.getCode(bridgeImplAddress);
const code = await providerKar.getCode(bridgeImplAddress);
expect(code.length).to.gt(100);
});

it('get transaction by hash', async () => {
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95';
const fetchedTransaction = await provider.getTransaction(txHash);
const fetchedTransaction = await providerKar.getTransaction(txHash);
expect(fetchedTransaction.hash).to.equal(txHash);
});

it('get transaction receipt', async () => {
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95';
const fetchedTransaction = await provider.getTransactionReceipt(txHash);
const fetchedTransaction = await providerKar.getTransactionReceipt(txHash);
expect(fetchedTransaction.transactionHash).to.equal(txHash);
});

it('get block with transactions', async () => {
let data = await provider.getBlockWithTransactions(1818518);
let data = await providerKar.getBlockWithTransactions(1818518);
expect(data.transactions.length).to.eq(1);

data = await provider.getBlockWithTransactions(2449983);
data = await providerKar.getBlockWithTransactions(2449983);
expect(data.transactions.length).to.eq(2);
});

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

const logs = await provider.getLogs(filter);
const logs = await providerKar.getLogs(filter);

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

describe('call', () => {
describe.concurrent('call', () => {
it('estimate gas', async () => {
const to = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e';
const value = parseEther('0.1');
const gasEstimate = await provider.estimateGas({
const gasEstimate = await providerKar.estimateGas({
from: someOne,
to,
value,
Expand Down Expand Up @@ -197,20 +193,20 @@ describe('JsonRpcProvider', async () => {

describe('subscription', () => {
it('subscribe to new block', async () => {
const curBlockNumber = await provider.getBlockNumber();
const curBlockNumber = await providerKar.getBlockNumber();

const blockNumber = await new Promise((resolve, reject) => {
const onBlock = (blockNumber: number) => {
// TODO: is it normal that cb is triggered immediately for current block
if (blockNumber > curBlockNumber) {
provider.off('block', onBlock);
providerKar.off('block', onBlock);
resolve(blockNumber);
}

setTimeout(() => reject('<provider.onBlock> no new block in 30s!'), 30_000);
setTimeout(() => reject('<providerKar.onBlock> no new block in 30s!'), 30_000);
};

provider.on('block', onBlock);
providerKar.on('block', onBlock);
});

expect(blockNumber).to.be.eq(curBlockNumber + 1);
Expand Down
Loading
Loading