diff --git a/src/formulas/formulas/account/abstract.ts b/src/formulas/formulas/account/abstract.ts index 0712663..b6106e1 100644 --- a/src/formulas/formulas/account/abstract.ts +++ b/src/formulas/formulas/account/abstract.ts @@ -1,5 +1,6 @@ import { Op } from 'sequelize' +import { accountId } from '@/formulas/formulas/contract/abstract/account' import { AccountFormula } from '@/types' import { AccountTypes } from '../contract/abstract/types' @@ -8,7 +9,7 @@ import { AccountTypes } from '../contract/abstract/types' * Get all contracts with the account governance owner set to this address. */ export const accountsOwnedBy: AccountFormula< - string[], + { id: AccountTypes.AccountId | null; address: AccountTypes.Addr }[], { /** * Optionally filter by code ID key. @@ -66,6 +67,14 @@ export const accountsOwnedBy: AccountFormula< ) )?.map(({ contractAddress }) => contractAddress) || [] - return owned + return await Promise.all( + owned.map(async (account) => ({ + address: account, + id: await accountId.compute({ + ...env, + contractAddress: account, + }), + })) + ) }, } diff --git a/src/formulas/formulas/contract/abstract/registry.ts b/src/formulas/formulas/contract/abstract/registry.ts index b9427dd..eb70d3d 100644 --- a/src/formulas/formulas/contract/abstract/registry.ts +++ b/src/formulas/formulas/contract/abstract/registry.ts @@ -1,10 +1,11 @@ import semver from 'semver/preload' +import { moduleInfos } from '@/formulas/formulas/contract/abstract/account' import { Module } from '@/formulas/formulas/contract/abstract/types/registry' import { ContractFormula } from '@/types' import { dbKeyForKeys, dbKeyToKeys } from '@/utils' -import { RegistryTypes } from './types' +import { AccountTypes, RegistryTypes } from './types' const RegistryStorageKeys = { CONFIG: 'cfg', @@ -21,7 +22,7 @@ const RegistryStorageKeys = { REV_NAMESPACES: 'ck', } -export const listRegisteredModules: ContractFormula> = { +export const registeredModules: ContractFormula> = { docs: { description: 'Lists registered modules in registry', }, @@ -191,7 +192,7 @@ export const module: ContractFormula< if (!args || !args.namespace || !args.name) return undefined const moduleParam = args as ModuleInfoParameter - const registeredModules = await listRegisteredModules.compute(env) + const registeredModules = await registeredModules.compute(env) const filteredModules = registeredModules.filter( ({ info: { name, namespace } }) => { @@ -238,7 +239,7 @@ export const module: ContractFormula< }, } -export const listLocalAccounts: ContractFormula = +export const localAccounts: ContractFormula = { docs: { description: 'Lists local accounts on chain', @@ -272,3 +273,63 @@ export const listLocalAccounts: ContractFormula = { + docs: { + description: + 'Filters accounts by module id, version, or address. Returns account id and address', + args: [], + }, + compute: async (env) => { + const { + args: { id: moduleId, address: moduleAddress, version: moduleVersion }, + } = env + + if (!moduleId && !moduleAddress) { + throw new Error('accountsByModule requires id or address') + } + + const { accounts: allAccounts } = await localAccounts.compute(env) + + const allAccountsWithModules = await Promise.all( + allAccounts.map(async ([id, address]) => ({ + address, + id, + modules: await moduleInfos.compute({ + ...env, + contractAddress: address, + }), + })) + ) + + return allAccountsWithModules + .filter(({ modules }) => + modules.some( + (module) => + (!moduleId || moduleId === module.id) && + (!moduleVersion || moduleVersion === module.version) && + (!moduleAddress || moduleAddress === module.address) + ) + ) + .map(({ id, address }) => ({ id, address })) + }, +} diff --git a/static/openapi.json b/static/openapi.json index cde22be..027d3d9 100644 --- a/static/openapi.json +++ b/static/openapi.json @@ -270,6 +270,52 @@ } } }, + "/{chainId}/contract/{address}/abstract/registry/accountsByModule": { + "get": { + "tags": [ + "contract" + ], + "summary": "abstract > registry > accountsByModule", + "description": "retrieves account (that use abstract governance to manage ownership) where the account is the owner", + "parameters": [ + { + "name": "chainId", + "in": "path", + "description": "chain ID", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "address", + "in": "path", + "description": "contract address", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "key", + "description": "optional code ID key to filter by", + "required": false, + "schema": { + "type": "string" + }, + "in": "query" + } + ], + "responses": { + "200": { + "description": "success" + }, + "400": { + "description": "missing required arguments" + } + } + } + }, "/{chainId}/contract/{address}/abstract/registry/listLocalAccounts": { "get": { "tags": [