@@ -65,26 +65,7 @@ export const issueFirstClassCitizenship = async (
65
65
} )
66
66
) ;
67
67
68
- const sbtcBalancesMap = new Map (
69
- simnetAddresses . map ( ( address ) => {
70
- try {
71
- const { result : getBalanceResult } = simnet . callReadOnlyFn (
72
- "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token" ,
73
- "get-balance" ,
74
- [ Cl . principal ( address ) ] ,
75
- address
76
- ) ;
77
-
78
- // If the previous read-only call works, the user is working with
79
- // sBTC. This means we can proceed with restoring sBTC balances.
80
- const sbtcBalance = cvToJSON ( getBalanceResult ) . value . value ;
81
-
82
- return [ address , sbtcBalance ] ;
83
- } catch ( e ) {
84
- return [ address , 0 ] ;
85
- }
86
- } )
87
- ) ;
68
+ const sbtcBalancesMap = getSbtcBalancesFromSimnet ( simnet ) ;
88
69
89
70
await simnet . initEmptySession ( remoteDataSettings ) ;
90
71
@@ -386,6 +367,43 @@ export function scheduleRendezvous(
386
367
return `${ targetContractSource } \n\n${ context } \n\n${ tests } ` ;
387
368
}
388
369
370
+ /**
371
+ * Maps the simnet accounts to their sBTC balances. The function tries to call
372
+ * the `get-balance` function of the `sbtc-token` contract for each address. If
373
+ * the call fails, it returns a balance of 0 for that address. The call fails
374
+ * if the user is not working with sBTC.
375
+ * @param simnet The simnet instance.
376
+ * @returns A map of addresses to their sBTC balances.
377
+ */
378
+ export const getSbtcBalancesFromSimnet = (
379
+ simnet : Simnet
380
+ ) : Map < string , number > =>
381
+ new Map (
382
+ [ ...simnet . getAccounts ( ) . values ( ) ] . map ( ( address ) => {
383
+ try {
384
+ const { result : getBalanceResult } = simnet . callReadOnlyFn (
385
+ "SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token" ,
386
+ "get-balance" ,
387
+ [ Cl . principal ( address ) ] ,
388
+ address
389
+ ) ;
390
+
391
+ // If the previous read-only call works, the user is working with
392
+ // sBTC. This means we can proceed with restoring sBTC balances.
393
+ const sbtcBalanceJSON = cvToJSON ( getBalanceResult ) ;
394
+
395
+ // The `get-balance` function returns a response containing the uint
396
+ // balance of the address. In the JSON representation, the balance is
397
+ // represented as a string. We need to parse it to an integer.
398
+ const sbtcBalance = parseInt ( sbtcBalanceJSON . value . value , 10 ) ;
399
+
400
+ return [ address , sbtcBalance ] ;
401
+ } catch ( e ) {
402
+ return [ address , 0 ] ;
403
+ }
404
+ } )
405
+ ) ;
406
+
389
407
/**
390
408
* Utility function that restores the test wallets' initial sBTC balances in
391
409
* the re-initialized first-class citizenship simnet.
0 commit comments