@@ -4,9 +4,10 @@ import fs from 'node:fs/promises';
44import toml from 'toml' ;
55import Ajv from 'ajv' ;
66import addFormats from 'ajv-formats' ;
7+ import { Coin , StargateClient } from '@cosmjs/stargate' ;
78
8- import { readSubDirectories , getWorkspaceRoot , bold , green , red } from '@/utils' ;
9- import { Contract } from '@/types' ;
9+ import { readSubDirectories , getWorkspaceRoot , bold , green , red , darkGreen , yellow } from '@/utils' ;
10+ import { AccountBalancesJSON , Contract } from '@/types' ;
1011import { DEFAULT , REPOSITORIES } from '@/GlobalConfig' ;
1112import { Cargo , Deployments } from '@/domain' ;
1213import { ErrorCodes , ExecuteError , InstantiateError , NotFoundError , QueryError } from '@/exceptions' ;
@@ -320,9 +321,9 @@ export class Contracts {
320321 *
321322 * @param contractName - Name of the contract to search by
322323 * @param chainId - Chain id to search by
323- * @returns Promise containing an instance of {@link StoreDeployment} or undefined if not found
324+ * @returns An instance of {@link StoreDeployment} or undefined if not found
324325 */
325- async findStoreDeployment ( contractName : string , chainId : string ) : Promise < StoreDeployment | undefined > {
326+ findStoreDeployment ( contractName : string , chainId : string ) : StoreDeployment | undefined {
326327 const contract = this . assertGetContractByName ( contractName ) ;
327328
328329 return contract . deployments . find ( item => {
@@ -339,9 +340,9 @@ export class Contracts {
339340 *
340341 * @param contractName - Name of the contract to search by
341342 * @param chainId - Chain id to search by
342- * @returns Promise containing an instance of {@link InstantiateDeployment} or undefined if not found
343+ * @returns An instance of {@link InstantiateDeployment} or undefined if not found
343344 */
344- async findInstantiateDeployment ( contractName : string , chainId : string ) : Promise < InstantiateDeployment | undefined > {
345+ findInstantiateDeployment ( contractName : string , chainId : string ) : InstantiateDeployment | undefined {
345346 const contract = this . assertGetContractByName ( contractName ) ;
346347
347348 return contract . deployments . find ( item => {
@@ -354,6 +355,62 @@ export class Contracts {
354355 ) ;
355356 } ) as InstantiateDeployment | undefined ;
356357 }
358+
359+ /**
360+ * Returns a list of all the last instantiated deployments of all contracts
361+ *
362+ * @param chainId - Chain id to search by
363+ * @returns An array of instances of {@link InstantiateDeployment}
364+ */
365+ getAllInstantiateDeployments ( chainId : string ) : InstantiateDeployment [ ] {
366+ const instantiatedDeployments = this . _data . map (
367+ contract =>
368+ contract . deployments . find ( item => {
369+ const pastDeploy = item as InstantiateDeployment ;
370+
371+ return (
372+ pastDeploy . contract . version === contract . version &&
373+ pastDeploy . action === DeploymentAction . INSTANTIATE &&
374+ pastDeploy . chainId === chainId
375+ ) ;
376+ } ) as InstantiateDeployment | undefined
377+ ) ;
378+
379+ return instantiatedDeployments . filter ( item => item !== undefined ) as InstantiateDeployment [ ] ;
380+ }
381+
382+ /**
383+ * Query the balance of contracts
384+ *
385+ * @param client - Stargate client to use when querying
386+ * @param instantiateDeployements - An array of instances of {@link InstantiateDeployment} to query
387+ * @returns Promise containing the balances result
388+ */
389+ async queryAllBalances ( client : StargateClient , instantiatedDeployments : InstantiateDeployment [ ] ) : Promise < AccountBalancesJSON [ ] > {
390+ const balances = await Promise . all ( instantiatedDeployments . map ( item => client . getAllBalances ( item . contract . address ) ) ) ;
391+
392+ return instantiatedDeployments . map ( ( item , index ) => ( {
393+ account : {
394+ name : item . contract . name ,
395+ address : item . contract . address ,
396+ balances : balances [ index ] as Coin [ ] ,
397+ } ,
398+ } ) ) ;
399+ }
400+
401+ /**
402+ * Get a formatted version of a contract balance
403+ *
404+ * @param balance - Contract balance data
405+ * @returns Formatted contract address balance
406+ */
407+ prettyPrintBalances ( balance : AccountBalancesJSON ) : string {
408+ let result = `Balances for contract ${ green ( balance . account . name ) } (${ darkGreen ( balance . account . address ) } )\n\n` ;
409+ if ( balance . account . balances . length === 0 ) result += `- ${ yellow ( 'Empty balance' ) } \n` ;
410+ for ( const item of balance . account . balances ) result += `- ${ bold ( item . amount ) } ${ item . denom } \n` ;
411+
412+ return result ;
413+ }
357414}
358415
359416/**
0 commit comments