1
1
import {
2
- InvalidAddressError ,
2
+ IMPLICIT_INITIAL_PROTOCOL_VERSION ,
3
3
getInterfaceId ,
4
4
getProtocolVersion ,
5
5
} from '../../src' ;
6
6
import { ADDRESS_ONE , TEST_HTTP_URI } from '../constants' ;
7
- import { mockContractProtocolVersion , mockJSONRPCProvider } from '../mocks' ;
7
+ import { mockJSONRPCProvider , mockContractProtocolVersion } from '../mocks' ;
8
8
import { Interface } from '@ethersproject/abi' ;
9
+ import { Contract } from '@ethersproject/contracts' ;
10
+ import { JsonRpcProvider } from '@ethersproject/providers' ;
9
11
10
12
describe ( 'introspection' , ( ) => {
11
13
describe ( 'getInterfaceId' , ( ) => {
@@ -21,33 +23,40 @@ describe('introspection', () => {
21
23
} ) ;
22
24
23
25
describe ( 'getProtocolVersion' , ( ) => {
24
- it ( 'should return the correct protocol version' , async ( ) => {
25
- const expectedVersion : [ number , number , number ] = [ 1 , 3 , 0 ] ;
26
- // mock call to the contract
26
+ let iface : Interface ;
27
+ let contract : Contract ;
28
+ let provider : JsonRpcProvider ;
29
+ beforeAll ( ( ) => {
30
+ // mock JSONRPCProvider to return chainId 1 and blockNumber 1
27
31
mockJSONRPCProvider ( ) ;
28
- // mock the call to the contract
29
- mockContractProtocolVersion ( expectedVersion ) ;
30
- const version = await getProtocolVersion ( TEST_HTTP_URI , ADDRESS_ONE ) ;
31
- expect ( version ) . toEqual ( expectedVersion ) ;
32
32
} ) ;
33
- it ( 'should fail when an invalid address is passed' , async ( ) => {
33
+ it ( 'should return the correct protocol version' , async ( ) => {
34
+ // Expected protocol version
34
35
const expectedVersion : [ number , number , number ] = [ 1 , 3 , 0 ] ;
35
- // mock call to the contract
36
- mockJSONRPCProvider ( ) ;
37
- // mock the call to the contract
36
+ // mock Contract to return the expected protocol version
38
37
mockContractProtocolVersion ( expectedVersion ) ;
39
- await expect ( ( ) =>
40
- getProtocolVersion ( TEST_HTTP_URI , '0x' )
41
- ) . rejects . toThrow ( new InvalidAddressError ( '0x' ) ) ;
38
+ // Initialize the contract
39
+ provider = new JsonRpcProvider ( TEST_HTTP_URI ) ;
40
+ iface = new Interface ( [
41
+ 'function protocolVersion() public pure returns (uint8[3] memory)' ,
42
+ ] ) ;
43
+ contract = new Contract ( ADDRESS_ONE , iface , provider ) ;
44
+ // Call getProtocolVersion
45
+ const version = await getProtocolVersion ( contract ) ;
46
+ expect ( version ) . toEqual ( expectedVersion ) ;
42
47
} ) ;
43
48
it ( 'should return [1,0,0] when the call throws an error' , async ( ) => {
44
- const expectedVersion : [ number , number , number ] = [ 1 , 0 , 0 ] ;
45
- // mock call to the contract
46
- mockJSONRPCProvider ( ) ;
47
- // mock the call to the contract
48
- mockContractProtocolVersion ( expectedVersion , true ) ;
49
- const version = await getProtocolVersion ( TEST_HTTP_URI , ADDRESS_ONE ) ;
50
- expect ( version ) . toEqual ( expectedVersion ) ;
49
+ // mock Contract to throw an error
50
+ mockContractProtocolVersion ( IMPLICIT_INITIAL_PROTOCOL_VERSION , true ) ;
51
+ // Initialize the contract
52
+ const iface = new Interface ( [
53
+ 'function protocolVersion() public pure returns (uint8[3] memory)' ,
54
+ ] ) ;
55
+ const provider = new JsonRpcProvider ( TEST_HTTP_URI ) ;
56
+ const contract = new Contract ( ADDRESS_ONE , iface , provider ) ;
57
+ // Call getProtocolVersion
58
+ const version = await getProtocolVersion ( contract ) ;
59
+ expect ( version ) . toEqual ( IMPLICIT_INITIAL_PROTOCOL_VERSION ) ;
51
60
} ) ;
52
61
} ) ;
53
62
} ) ;
0 commit comments