@@ -46,8 +46,10 @@ async function getProxies(api: TypedApi<typeof devnet>, address: string) {
4646
4747describe ( "Test pure proxy precompile" , ( ) => {
4848 const evmWallet = generateRandomEthersWallet ( ) ;
49+ // only used for edge case and normal proxy
4950 const evmWallet2 = generateRandomEthersWallet ( ) ;
5051 const evmWallet3 = generateRandomEthersWallet ( ) ;
52+ const evmWallet4 = generateRandomEthersWallet ( ) ;
5153 const receiver = getRandomSubstrateKeypair ( ) ;
5254
5355 let api : TypedApi < typeof devnet >
@@ -61,6 +63,7 @@ describe("Test pure proxy precompile", () => {
6163 await forceSetBalanceToEthAddress ( api , evmWallet . address )
6264 await forceSetBalanceToEthAddress ( api , evmWallet2 . address )
6365 await forceSetBalanceToEthAddress ( api , evmWallet3 . address )
66+ await forceSetBalanceToEthAddress ( api , evmWallet4 . address )
6467 } )
6568
6669 it ( "Call createPureProxy, then use proxy to call transfer" , async ( ) => {
@@ -130,34 +133,78 @@ describe("Test pure proxy precompile", () => {
130133 const proxies = await api . query . Proxy . Proxies . getValue ( convertH160ToSS58 ( evmWallet2 . address ) )
131134 const contract = new ethers . Contract ( IPROXY_ADDRESS , IProxyABI , evmWallet2 )
132135
136+ const proxiesFromContract = await contract . getProxies ( convertH160ToPublicKey ( evmWallet2 . address ) )
137+ assert . equal ( proxiesFromContract . length , proxies [ 0 ] . length , "proxies length should be equal" )
138+
133139 const type = 0 ;
134140 const delay = 0 ;
135141
136142 const tx = await contract . addProxy ( convertH160ToPublicKey ( evmWallet3 . address ) , type , delay )
137143 await tx . wait ( )
138144
145+ const proxiesAfterAdd = await api . query . Proxy . Proxies . getValue ( convertH160ToSS58 ( evmWallet2 . address ) )
146+ const proxiesList = proxiesAfterAdd [ 0 ] . map ( proxy => proxy . delegate )
139147
140- const proxiesAfterAdd = await await api . query . Proxy . Proxies . getValue ( convertH160ToSS58 ( evmWallet2 . address ) )
148+ const proxiesFromContractAfterAdd = await contract . getProxies ( convertH160ToPublicKey ( evmWallet2 . address ) )
141149
142- const length = proxiesAfterAdd [ 0 ] . length
143- assert . equal ( length , proxies [ 0 ] . length + 1 , "proxy should be set" )
144- const proxy = proxiesAfterAdd [ 0 ] [ proxiesAfterAdd [ 0 ] . length - 1 ]
150+ assert . equal ( proxiesFromContractAfterAdd . length , proxiesList . length , "proxy length should be equal" )
145151
146- assert . equal ( proxy . delegate , convertH160ToSS58 ( evmWallet3 . address ) , "proxy should be set" )
152+ for ( let index = 0 ; index < proxiesFromContractAfterAdd . length ; index ++ ) {
153+ const proxyInfo = proxiesFromContractAfterAdd [ index ]
154+ let proxySs58 = convertPublicKeyToSs58 ( proxyInfo [ 0 ] )
155+ assert . ok ( proxiesList . includes ( proxySs58 ) , "proxy should be set" )
156+ if ( index === proxiesFromContractAfterAdd . length - 1 ) {
157+ assert . equal ( Number ( proxyInfo [ 1 ] ) , type , "proxy_type should match" )
158+ assert . equal ( Number ( proxyInfo [ 2 ] ) , delay , "delay should match" )
159+ }
160+ }
147161
162+ assert . equal ( proxiesList . length , proxies [ 0 ] . length + 1 , "proxy should be set" )
163+ const proxy = proxiesList [ proxiesList . length - 1 ]
148164
165+ assert . equal ( proxy , convertH160ToSS58 ( evmWallet3 . address ) , "proxy should be set" )
149166 const balance = ( await api . query . System . Account . getValue ( convertPublicKeyToSs58 ( receiver . publicKey ) ) ) . data . free
150-
151167 const amount = 1000000000 ;
152168
153169 const contract2 = new ethers . Contract ( IPROXY_ADDRESS , IProxyABI , evmWallet3 )
154-
155-
156170 const callCode = await getTransferCallCode ( api , receiver , amount )
157171 const tx2 = await contract2 . proxyCall ( convertH160ToPublicKey ( evmWallet2 . address ) , [ type ] , callCode )
158172 await tx2 . wait ( )
159173
160174 const balanceAfter = ( await api . query . System . Account . getValue ( convertPublicKeyToSs58 ( receiver . publicKey ) ) ) . data . free
161175 assert . equal ( balanceAfter , balance + BigInt ( amount ) , "balance should be increased" )
162176 } )
177+
178+ it ( "Call addProxy many times, then check getProxies is correct" , async ( ) => {
179+ const proxies = await api . query . Proxy . Proxies . getValue ( convertH160ToSS58 ( evmWallet4 . address ) )
180+ const contract = new ethers . Contract ( IPROXY_ADDRESS , IProxyABI , evmWallet4 )
181+ assert . equal ( proxies [ 0 ] . length , 0 , "proxies length should be 0" )
182+
183+ const proxiesFromContract = await contract . getProxies ( convertH160ToPublicKey ( evmWallet4 . address ) )
184+ assert . equal ( proxiesFromContract . length , proxies [ 0 ] . length , "proxies length should be equal" )
185+
186+ const type = 1 ;
187+ const delay = 2 ;
188+
189+ for ( let i = 0 ; i < 5 ; i ++ ) {
190+ const evmWallet = generateRandomEthersWallet ( )
191+ const tx = await contract . addProxy ( convertH160ToPublicKey ( evmWallet . address ) , type , delay )
192+ await tx . wait ( )
193+ }
194+
195+ const proxiesAfterAdd = await await api . query . Proxy . Proxies . getValue ( convertH160ToSS58 ( evmWallet4 . address ) )
196+ const proxiesList = proxiesAfterAdd [ 0 ] . map ( proxy => proxy . delegate )
197+
198+ const proxiesFromContractAfterAdd = await contract . getProxies ( convertH160ToPublicKey ( evmWallet4 . address ) )
199+
200+ assert . equal ( proxiesFromContractAfterAdd . length , proxiesList . length , "proxy length should be equal" )
201+
202+ for ( let index = 0 ; index < proxiesFromContractAfterAdd . length ; index ++ ) {
203+ const proxyInfo = proxiesFromContractAfterAdd [ index ]
204+ let proxySs58 = convertPublicKeyToSs58 ( proxyInfo [ 0 ] )
205+ assert . ok ( proxiesList . includes ( proxySs58 ) , "proxy should be set" )
206+ assert . equal ( Number ( proxyInfo [ 1 ] ) , type , "proxy_type should match" )
207+ assert . equal ( Number ( proxyInfo [ 2 ] ) , delay , "delay should match" )
208+ }
209+ } )
163210} ) ;
0 commit comments