11import { Contract , ContractFactory } from 'ethers' ;
22import { Wallet } from '@ethersproject/wallet' ;
3- import { afterAll , describe , expect , it } from 'vitest' ;
3+ import { describe , expect , it } from 'vitest' ;
44import { hexZeroPad , parseEther } from 'ethers/lib/utils' ;
55
66import { AcalaJsonRpcProvider } from '../json-rpc-provider' ;
7- import { sleep } from '../utils' ;
87import echoJson from './abis/Echo.json' ;
98import erc20Json from './abis/IERC20.json' ;
9+ import evmAccounts from './utils/evmAccounts' ;
1010
1111const localEthRpc = process . env . ETH_RPC || 'http://localhost:8545' ;
1212
1313describe ( '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