1- import { multicall1 , multicall2 , multicall3 } from '../src/abis' ;
21import { networks } from '../src/networks' ;
32import { Address , Mapping } from '../src/types' ;
4- import { constructWithAddress } from '../src' ;
3+ import {
4+ abiMap ,
5+ constructWithAddress ,
6+ networkToMapping ,
7+ } from '../src/AbiMapper' ;
8+ import { multicall1 , multicall2 , multicall3 } from '../src/abis' ;
9+ import { ethers } from 'ethers' ;
10+
11+ // IMMUTABLES
12+ const MULTICALL_3_ADDRESS : Address =
13+ '0xcA11bde05977b3631167028862bE2a173976CA11' ;
14+ const GOERLI_MULTICALL_ADDRESS : Address =
15+ '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e' ;
16+ const DEFAULT_MAPPING : Mapping = {
17+ found : false ,
18+ address : MULTICALL_3_ADDRESS ,
19+ network : 1 ,
20+ interface : new ethers . utils . Interface ( multicall3 ) ,
21+ abi : multicall3 ,
22+ } ;
523
624describe ( 'Constructs Mapping with Address' , ( ) => {
725 it ( 'Non-existent address returns default mapping' , ( ) => {
826 const zero : Address = '0x0000000000000000000000000000000000000000' ;
927 const mapping : Mapping = constructWithAddress ( zero ) ;
10-
11- expect ( mapping . found ) . toBe ( false ) ;
12- expect ( mapping . address ) . toEqual ( networks [ '1' ] [ 'multicall3' ] ) ;
13- expect ( mapping . network ) . toEqual ( 1 ) ;
14- expect ( mapping . abi ) . toEqual ( multicall3 ) ;
28+ expect ( mapping ) . toEqual ( DEFAULT_MAPPING ) ;
1529 } ) ;
1630
1731 it ( 'Existing Multicall 3 properly construct mappings' , ( ) => {
@@ -20,7 +34,8 @@ describe('Constructs Mapping with Address', () => {
2034
2135 expect ( mapping . found ) . toBe ( true ) ;
2236 expect ( mapping . address ) . toEqual ( goerliMulticall3 ) ;
23- expect ( mapping . network ) . toEqual ( 5 ) ;
37+ // Since all the multicall3 addresses are the same, we will get back network 1
38+ expect ( mapping . network ) . toEqual ( 1 ) ;
2439 expect ( mapping . abi ) . toEqual ( multicall3 ) ;
2540 } ) ;
2641
@@ -30,12 +45,13 @@ describe('Constructs Mapping with Address', () => {
3045
3146 expect ( mapping . found ) . toBe ( true ) ;
3247 expect ( mapping . address ) . toEqual ( rinkebyMulticall2 ) ;
33- expect ( mapping . network ) . toEqual ( 4 ) ;
48+ // MakerDAO deployed five instances of multicall2 to the same address so we'll get back network 1
49+ expect ( mapping . network ) . toEqual ( 1 ) ;
3450 expect ( mapping . abi ) . toEqual ( multicall2 ) ;
3551 } ) ;
3652
3753 it ( 'Existing Multicall 1 properly construct mappings' , ( ) => {
38- const ropstenMulticall1 : Address = networks [ '3' ] [ 'multicall1 ' ] ;
54+ const ropstenMulticall1 : Address = networks [ '3' ] [ 'multicall ' ] ;
3955 const mapping : Mapping = constructWithAddress ( ropstenMulticall1 ) ;
4056
4157 expect ( mapping . found ) . toBe ( true ) ;
@@ -44,3 +60,121 @@ describe('Constructs Mapping with Address', () => {
4460 expect ( mapping . abi ) . toEqual ( multicall1 ) ;
4561 } ) ;
4662} ) ;
63+
64+ describe ( 'Maps Network to Mapping Object' , ( ) => {
65+ it ( 'Constructs Correct Mapping with Zero' , ( ) => {
66+ const zero : Address = '0x0000000000000000000000000000000000000000' ;
67+ const mapping : Mapping = networkToMapping ( 5 , networks [ '5' ] , zero ) ;
68+ expect ( mapping ) . toEqual ( DEFAULT_MAPPING ) ;
69+ } ) ;
70+
71+ it ( 'Constructs Correct Mapping with Multicall Address' , ( ) => {
72+ const m1addr : Address = '0x77dca2c955b15e9de4dbbcf1246b4b85b651e50e' ;
73+ const mapping : Mapping = networkToMapping ( 5 , networks [ '5' ] , m1addr ) ;
74+
75+ expect ( mapping . found ) . toBe ( true ) ;
76+ expect ( mapping . address ) . toEqual ( m1addr ) ;
77+ expect ( mapping . network ) . toEqual ( 5 ) ;
78+ expect ( mapping . abi ) . toEqual ( multicall1 ) ;
79+ } ) ;
80+
81+ it ( 'Constructs Correct Mapping with Multicall 2 Address' , ( ) => {
82+ const m2addr : Address = '0x5ba1e12693dc8f9c48aad8770482f4739beed696' ;
83+ const mapping : Mapping = networkToMapping ( 5 , networks [ '5' ] , m2addr ) ;
84+
85+ expect ( mapping . found ) . toBe ( true ) ;
86+ expect ( mapping . address ) . toEqual ( m2addr ) ;
87+ expect ( mapping . network ) . toEqual ( 5 ) ;
88+ expect ( mapping . abi ) . toEqual ( multicall2 ) ;
89+ } ) ;
90+
91+ it ( 'Constructs Correct Mapping with Multicall 3 Address' , ( ) => {
92+ const mapping : Mapping = networkToMapping (
93+ 5 ,
94+ networks [ '5' ] ,
95+ MULTICALL_3_ADDRESS
96+ ) ;
97+
98+ expect ( mapping . found ) . toBe ( true ) ;
99+ expect ( mapping . address ) . toEqual ( MULTICALL_3_ADDRESS ) ;
100+ expect ( mapping . network ) . toEqual ( 5 ) ;
101+ expect ( mapping . abi ) . toEqual ( multicall3 ) ;
102+ } ) ;
103+ } ) ;
104+
105+ describe ( 'Abi Mapper' , ( ) => {
106+ it ( 'Returns Default Mapping with no Options' , ( ) => {
107+ const mapping : Mapping = abiMap ( ) ;
108+ expect ( mapping ) . toEqual ( DEFAULT_MAPPING ) ;
109+ } ) ;
110+
111+ it ( 'returns default mapping for invalid network' , ( ) => {
112+ const mapping : Mapping = abiMap ( { network : - 1 } ) ;
113+ expect ( mapping ) . toEqual ( DEFAULT_MAPPING ) ;
114+ } ) ;
115+
116+ it ( 'returns default mapping for an out of bounds network' , ( ) => {
117+ const mapping : Mapping = abiMap ( { network : 10e6 } ) ;
118+ expect ( mapping ) . toEqual ( DEFAULT_MAPPING ) ;
119+ } ) ;
120+
121+ it ( 'returns the network mapping for invalid address' , ( ) => {
122+ const mapping : Mapping = abiMap ( { network : 5 , address : '0x' } ) ;
123+ expect ( mapping . found ) . toBe ( true ) ;
124+ expect ( mapping . address ) . toEqual ( MULTICALL_3_ADDRESS ) ;
125+ expect ( mapping . network ) . toEqual ( 5 ) ;
126+ expect ( mapping . abi ) . toEqual ( multicall3 ) ;
127+ } ) ;
128+
129+ it ( 'returns the network mapping for invalid network but valid address' , ( ) => {
130+ const mapping : Mapping = abiMap ( {
131+ network : - 1 ,
132+ address : GOERLI_MULTICALL_ADDRESS ,
133+ } ) ;
134+ expect ( mapping . found ) . toBe ( true ) ;
135+ expect ( mapping . address ) . toEqual ( GOERLI_MULTICALL_ADDRESS ) ;
136+ expect ( mapping . network ) . toEqual ( 5 ) ;
137+ expect ( mapping . abi ) . toEqual ( multicall1 ) ;
138+ } ) ;
139+
140+ it ( 'returns the network mapping for an out of bounds network but valid address' , ( ) => {
141+ const mapping : Mapping = abiMap ( {
142+ network : 10e6 ,
143+ address : GOERLI_MULTICALL_ADDRESS ,
144+ } ) ;
145+ expect ( mapping . found ) . toBe ( true ) ;
146+ expect ( mapping . address ) . toEqual ( GOERLI_MULTICALL_ADDRESS ) ;
147+ expect ( mapping . network ) . toEqual ( 5 ) ;
148+ expect ( mapping . abi ) . toEqual ( multicall1 ) ;
149+ } ) ;
150+
151+ it ( 'returns the network mapping for valid network' , ( ) => {
152+ const mapping : Mapping = abiMap ( { network : 5 } ) ;
153+ expect ( mapping . found ) . toBe ( true ) ;
154+ expect ( mapping . address ) . toEqual ( MULTICALL_3_ADDRESS ) ;
155+ expect ( mapping . network ) . toEqual ( 5 ) ;
156+ expect ( mapping . abi ) . toEqual ( multicall3 ) ;
157+ } ) ;
158+
159+ it ( 'returns the network mapping for both valid network and valid address' , ( ) => {
160+ const mapping : Mapping = abiMap ( {
161+ network : 5 ,
162+ address : GOERLI_MULTICALL_ADDRESS ,
163+ } ) ;
164+ expect ( mapping . found ) . toBe ( true ) ;
165+ expect ( mapping . address ) . toEqual ( GOERLI_MULTICALL_ADDRESS ) ;
166+ expect ( mapping . network ) . toEqual ( 5 ) ;
167+ expect ( mapping . abi ) . toEqual ( multicall1 ) ;
168+ } ) ;
169+
170+ it ( 'prioritizes address over network' , ( ) => {
171+ const mapping : Mapping = abiMap ( {
172+ network : 4 ,
173+ address : GOERLI_MULTICALL_ADDRESS ,
174+ } ) ;
175+ expect ( mapping . found ) . toBe ( true ) ;
176+ expect ( mapping . address ) . toEqual ( GOERLI_MULTICALL_ADDRESS ) ;
177+ expect ( mapping . network ) . toEqual ( 5 ) ;
178+ expect ( mapping . abi ) . toEqual ( multicall1 ) ;
179+ } ) ;
180+ } ) ;
0 commit comments