Skip to content

Commit

Permalink
Update Abstract registry queries
Browse files Browse the repository at this point in the history
  • Loading branch information
adairrr committed Dec 21, 2024
1 parent 54f6a9c commit bae70dd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/formulas/formulas/account/abstract.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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.
Expand Down Expand Up @@ -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,
}),
}))
)
},
}
69 changes: 65 additions & 4 deletions src/formulas/formulas/contract/abstract/registry.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -21,7 +22,7 @@ const RegistryStorageKeys = {
REV_NAMESPACES: 'ck',
}

export const listRegisteredModules: ContractFormula<Array<Module>> = {
export const registeredModules: ContractFormula<Array<Module>> = {
docs: {
description: 'Lists registered modules in registry',
},
Expand Down Expand Up @@ -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)

Check failure on line 195 in src/formulas/formulas/contract/abstract/registry.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

'registeredModules' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

Check failure on line 195 in src/formulas/formulas/contract/abstract/registry.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

Block-scoped variable 'registeredModules' used before its declaration.

const filteredModules = registeredModules.filter(
({ info: { name, namespace } }) => {

Check failure on line 198 in src/formulas/formulas/contract/abstract/registry.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

Binding element 'name' implicitly has an 'any' type.

Check failure on line 198 in src/formulas/formulas/contract/abstract/registry.ts

View workflow job for this annotation

GitHub Actions / Lint & Test

Binding element 'namespace' implicitly has an 'any' type.
Expand Down Expand Up @@ -238,7 +239,7 @@ export const module: ContractFormula<
},
}

export const listLocalAccounts: ContractFormula<RegistryTypes.AccountListResponse> =
export const localAccounts: ContractFormula<RegistryTypes.AccountListResponse> =
{
docs: {
description: 'Lists local accounts on chain',
Expand Down Expand Up @@ -272,3 +273,63 @@ export const listLocalAccounts: ContractFormula<RegistryTypes.AccountListRespons
return { accounts }
},
}

/**
* Get all contracts with the account governance owner set to this address.
*/
export const accountsByModule: ContractFormula<
{ id: AccountTypes.AccountId | null; address: string }[],
{
/**
* Filter by module id.
*/
id?: string
/**
* Filter by module version.
*/
version?: string
/**
* Filter by module address.
*/
address?: string
}
> = {
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 }))
},
}
46 changes: 46 additions & 0 deletions static/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit bae70dd

Please sign in to comment.