1
1
import { Contract , ContractFactory } from 'ethers' ;
2
2
import { Wallet } from '@ethersproject/wallet' ;
3
- import { afterAll , describe , expect , it } from 'vitest' ;
3
+ import { describe , expect , it } from 'vitest' ;
4
4
import { hexZeroPad , parseEther } from 'ethers/lib/utils' ;
5
5
6
6
import { AcalaJsonRpcProvider } from '../json-rpc-provider' ;
7
- import { sleep } from '../utils' ;
8
7
import echoJson from './abis/Echo.json' ;
9
8
import erc20Json from './abis/IERC20.json' ;
9
+ import evmAccounts from './utils/evmAccounts' ;
10
10
11
11
const localEthRpc = process . env . ETH_RPC || 'http://localhost:8545' ;
12
12
13
13
describe ( 'JsonRpcProvider' , async ( ) => {
14
14
/* --------- karura --------- */
15
15
const someOne = '0xf7ABcfa42bF7e7d43d3d53C665deD80fDAfB5244' ;
16
16
17
- const provider = new AcalaJsonRpcProvider ( 'https://eth-rpc-karura.aca-api.network' ) ;
17
+ const providerKar = new AcalaJsonRpcProvider ( 'https://eth-rpc-karura.aca-api.network' ) ;
18
18
const usdcAddr = '0x1F3a10587A20114EA25Ba1b388EE2dD4A337ce27' ;
19
- const usdc = new Contract ( usdcAddr , erc20Json . abi , provider ) ;
19
+ const usdc = new Contract ( usdcAddr , erc20Json . abi , providerKar ) ;
20
20
21
21
/* --------- local --------- */
22
- const testKey = 'a872f6cbd25a0e04a08b1e21098017a9e6194d101d75e13111f71410c59cd57f' ; // 0x75E480dB528101a381Ce68544611C169Ad7EB342
22
+ const testKey = evmAccounts [ 0 ] . privateKey ; // 0x75E480dB528101a381Ce68544611C169Ad7EB342
23
23
const providerLocal = new AcalaJsonRpcProvider ( localEthRpc ) ;
24
24
const wallet = new Wallet ( testKey , providerLocal ) ;
25
25
26
- afterAll ( async ( ) => {
27
- await sleep ( 5000 ) ;
28
- } ) ;
29
-
30
26
describe . concurrent ( 'get chain data' , ( ) => {
31
27
it ( 'get chain id' , async ( ) => {
32
- const network = await provider . getNetwork ( ) ;
28
+ const network = await providerKar . getNetwork ( ) ;
33
29
expect ( network . chainId ) . to . eq ( 686 ) ;
34
30
} ) ;
35
31
36
32
it ( 'get block number' , async ( ) => {
37
- const blockNumber = await provider . getBlockNumber ( ) ;
33
+ const blockNumber = await providerKar . getBlockNumber ( ) ;
38
34
expect ( blockNumber ) . to . be . gt ( 0 ) ;
39
35
} ) ;
40
36
41
37
it ( 'get gas price' , async ( ) => {
42
- const gasPrice = await provider . getGasPrice ( ) ;
38
+ const gasPrice = await providerKar . getGasPrice ( ) ;
43
39
expect ( gasPrice . gt ( 0 ) ) . to . be . true ;
44
40
} ) ;
45
41
46
42
it ( 'get balance' , async ( ) => {
47
- const balance = await provider . getBalance ( someOne ) ;
43
+ const balance = await providerKar . getBalance ( someOne ) ;
48
44
expect ( balance . gt ( 0 ) ) . to . be . true ;
49
45
} ) ;
50
46
51
47
it ( 'get transaction count' , async ( ) => {
52
- const transactionCount = await provider . getTransactionCount ( wallet . address ) ;
48
+ const transactionCount = await providerKar . getTransactionCount ( wallet . address ) ;
53
49
expect ( transactionCount ) . to . be . gt ( 0 ) ;
54
50
} ) ;
55
51
56
52
it ( 'get contract code' , async ( ) => {
57
53
const bridgeImplAddress = '0xae9d7fe007b3327AA64A32824Aaac52C42a6E624' ;
58
- const code = await provider . getCode ( bridgeImplAddress ) ;
54
+ const code = await providerKar . getCode ( bridgeImplAddress ) ;
59
55
expect ( code . length ) . to . gt ( 100 ) ;
60
56
} ) ;
61
57
62
58
it ( 'get transaction by hash' , async ( ) => {
63
59
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95' ;
64
- const fetchedTransaction = await provider . getTransaction ( txHash ) ;
60
+ const fetchedTransaction = await providerKar . getTransaction ( txHash ) ;
65
61
expect ( fetchedTransaction . hash ) . to . equal ( txHash ) ;
66
62
} ) ;
67
63
68
64
it ( 'get transaction receipt' , async ( ) => {
69
65
const txHash = '0xbd273dc63f4e5e1998d0f1e191e7bc5e3a3067a4101771dfd7091a32a8784d95' ;
70
- const fetchedTransaction = await provider . getTransactionReceipt ( txHash ) ;
66
+ const fetchedTransaction = await providerKar . getTransactionReceipt ( txHash ) ;
71
67
expect ( fetchedTransaction . transactionHash ) . to . equal ( txHash ) ;
72
68
} ) ;
73
69
74
70
it ( 'get block with transactions' , async ( ) => {
75
- let data = await provider . getBlockWithTransactions ( 1818518 ) ;
71
+ let data = await providerKar . getBlockWithTransactions ( 1818518 ) ;
76
72
expect ( data . transactions . length ) . to . eq ( 1 ) ;
77
73
78
- data = await provider . getBlockWithTransactions ( 2449983 ) ;
74
+ data = await providerKar . getBlockWithTransactions ( 2449983 ) ;
79
75
expect ( data . transactions . length ) . to . eq ( 2 ) ;
80
76
} ) ;
81
77
@@ -87,7 +83,7 @@ describe('JsonRpcProvider', async () => {
87
83
toBlock : 4128888 ,
88
84
} ;
89
85
90
- const logs = await provider . getLogs ( filter ) ;
86
+ const logs = await providerKar . getLogs ( filter ) ;
91
87
92
88
expect ( logs . length ) . to . eq ( 6 ) ;
93
89
for ( const log of logs ) {
@@ -97,11 +93,11 @@ describe('JsonRpcProvider', async () => {
97
93
} ) ;
98
94
} ) ;
99
95
100
- describe ( 'call' , ( ) => {
96
+ describe . concurrent ( 'call' , ( ) => {
101
97
it ( 'estimate gas' , async ( ) => {
102
98
const to = '0x742d35Cc6634C0532925a3b844Bc454e4438f44e' ;
103
99
const value = parseEther ( '0.1' ) ;
104
- const gasEstimate = await provider . estimateGas ( {
100
+ const gasEstimate = await providerKar . estimateGas ( {
105
101
from : someOne ,
106
102
to,
107
103
value,
@@ -197,20 +193,20 @@ describe('JsonRpcProvider', async () => {
197
193
198
194
describe ( 'subscription' , ( ) => {
199
195
it ( 'subscribe to new block' , async ( ) => {
200
- const curBlockNumber = await provider . getBlockNumber ( ) ;
196
+ const curBlockNumber = await providerKar . getBlockNumber ( ) ;
201
197
202
198
const blockNumber = await new Promise ( ( resolve , reject ) => {
203
199
const onBlock = ( blockNumber : number ) => {
204
200
// TODO: is it normal that cb is triggered immediately for current block
205
201
if ( blockNumber > curBlockNumber ) {
206
- provider . off ( 'block' , onBlock ) ;
202
+ providerKar . off ( 'block' , onBlock ) ;
207
203
resolve ( blockNumber ) ;
208
204
}
209
205
210
- setTimeout ( ( ) => reject ( '<provider .onBlock> no new block in 30s!' ) , 30_000 ) ;
206
+ setTimeout ( ( ) => reject ( '<providerKar .onBlock> no new block in 30s!' ) , 30_000 ) ;
211
207
} ;
212
208
213
- provider . on ( 'block' , onBlock ) ;
209
+ providerKar . on ( 'block' , onBlock ) ;
214
210
} ) ;
215
211
216
212
expect ( blockNumber ) . to . be . eq ( curBlockNumber + 1 ) ;
0 commit comments