1
1
/* global describe, before, it */
2
- import { Web3 , FMT_BYTES , FMT_NUMBER } from 'web3'
3
2
import { Provider } from '../src/index'
4
- const web3 = new Web3 ( )
5
3
import * as assert from 'assert'
4
+ import { ethers , BrowserProvider } from "ethers" ;
6
5
7
6
describe ( 'Accounts' , ( ) => {
7
+ let ethersProvider : BrowserProvider
8
8
before ( async function ( ) {
9
9
const provider = new Provider ( )
10
10
await provider . init ( )
11
- web3 . setProvider ( provider as any )
11
+ ethersProvider = new ethers . BrowserProvider ( provider as any )
12
12
} )
13
13
14
14
describe ( 'eth_getAccounts' , ( ) => {
15
15
it ( 'should get a list of accounts' , async function ( ) {
16
- const accounts : string [ ] = await web3 . eth . getAccounts ( )
16
+ const accounts : string [ ] = await ethersProvider . send ( "eth_requestAccounts" , [ ] )
17
17
assert . notEqual ( accounts . length , 0 )
18
18
} )
19
19
} )
20
20
21
21
describe ( 'eth_getBalance' , ( ) => {
22
22
it ( 'should get an account balance' , async ( ) => {
23
- const accounts : string [ ] = await web3 . eth . getAccounts ( )
24
- const balance0 : string = await web3 . eth . getBalance ( accounts [ 0 ] , undefined , { number : FMT_NUMBER . STR , bytes : FMT_BYTES . HEX } )
25
- const balance1 : string = await web3 . eth . getBalance ( accounts [ 1 ] , undefined , { number : FMT_NUMBER . STR , bytes : FMT_BYTES . HEX } )
26
- const balance2 : string = await web3 . eth . getBalance ( accounts [ 2 ] , undefined , { number : FMT_NUMBER . STR , bytes : FMT_BYTES . HEX } )
23
+ const accounts : string [ ] = await ethersProvider . send ( "eth_requestAccounts" , [ ] )
24
+ const balance0 : bigint = await ethersProvider . getBalance ( accounts [ 0 ] )
25
+ const balance1 : bigint = await ethersProvider . getBalance ( accounts [ 1 ] )
26
+ const balance2 : bigint = await ethersProvider . getBalance ( accounts [ 2 ] )
27
27
28
- assert . deepEqual ( balance0 , '100000000000000000000' )
29
- assert . deepEqual ( balance1 , '100000000000000000000' )
30
- assert . deepEqual ( balance2 , '100000000000000000000' )
28
+ assert . deepEqual ( balance0 . toString ( ) , '100000000000000000000' )
29
+ assert . deepEqual ( balance1 . toString ( ) , '100000000000000000000' )
30
+ assert . deepEqual ( balance2 . toString ( ) , '100000000000000000000' )
31
31
} )
32
32
} )
33
33
34
34
describe ( 'eth_sign' , ( ) => {
35
35
it ( 'should sign payloads' , async ( ) => {
36
- const accounts : string [ ] = await web3 . eth . getAccounts ( )
37
- const signature = await web3 . eth . sign ( web3 . utils . utf8ToHex ( 'Hello world' ) , accounts [ 0 ] )
38
-
36
+ const signer = await ethersProvider . getSigner ( )
37
+ const signature : any = await signer . _legacySignMessage ( 'Hello world' ) // _legacySignMessage uses 'eth_sign' internally
39
38
assert . deepEqual ( typeof signature === 'string' ? signature . length : signature . signature . length , 132 )
40
39
} )
41
40
} )
42
41
43
42
describe ( 'eth_signTypedData' , ( ) => {
44
43
it ( 'should sign typed data' , async ( ) => {
45
- const accounts : string [ ] = await web3 . eth . getAccounts ( )
44
+ const accounts : string [ ] = await ethersProvider . send ( "eth_requestAccounts" , [ ] )
46
45
const typedData = {
47
46
domain : {
48
47
chainId : 1 ,
@@ -68,10 +67,7 @@ describe('Accounts', () => {
68
67
] ,
69
68
} ,
70
69
} ;
71
- const result = await web3 . currentProvider . request ( {
72
- method : 'eth_signTypedData' ,
73
- params : [ accounts [ 0 ] , typedData ]
74
- } )
70
+ const result = await ethersProvider . send ( 'eth_signTypedData' , [ accounts [ 0 ] , typedData ] )
75
71
assert . equal ( result , '0x248d23de0e23231370db8aa21ad5908ca90c33ae2b8c611b906674bda6b1a8b85813f945c2ea896316e240089029619ab3d801a1b098c199bd462dd8026349da1c' )
76
72
} )
77
73
} )
0 commit comments