Skip to content

Commit bf8be0f

Browse files
authored
Add Abstract Indexer definitions
Abstract Indexer
2 parents c3c251d + 5331d3a commit bf8be0f

File tree

13 files changed

+1772
-0
lines changed

13 files changed

+1772
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
src/protobuf/codegen/
2+
src/formulas/contract/abstract/types

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@types/node": "^18.11.10",
6161
"@types/prettier": "^2.7.1",
6262
"@types/redis-info": "^3.0.1",
63+
"@types/semver": "^7.5.8",
6364
"@types/supertest": "^2.0.12",
6465
"@types/validator": "^13.7.10",
6566
"@types/ws": "^8.5.5",
@@ -118,6 +119,7 @@
118119
"pg-hstore": "^2.3.4",
119120
"pusher": "^5.1.2",
120121
"reflect-metadata": "^0.1.13",
122+
"semver": "^7.6.2",
121123
"sequelize": "^6.26.0",
122124
"sequelize-typescript": "^2.1.5",
123125
"supertest": "^6.3.3",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Op } from 'sequelize'
2+
3+
import { AccountFormula } from '@/types'
4+
5+
import { AccountTypes } from '../contract/abstract/types'
6+
7+
/**
8+
* Get all contracts with the account governance owner set to this address.
9+
*/
10+
export const accountsOwnedBy: AccountFormula<
11+
string[],
12+
{
13+
/**
14+
* Optionally filter by code ID key.
15+
*/
16+
key?: string
17+
}
18+
> = {
19+
docs: {
20+
description:
21+
'retrieves account (that use abstract governance to manage ownership) where the account is the owner',
22+
args: [
23+
{
24+
name: 'key',
25+
description: 'optional code ID key to filter by',
26+
required: false,
27+
schema: {
28+
type: 'string',
29+
},
30+
},
31+
],
32+
},
33+
compute: async (env) => {
34+
const {
35+
args: { key },
36+
address,
37+
getTransformationMatches,
38+
getCodeIdsForKeys,
39+
} = env
40+
41+
const owned =
42+
(
43+
await getTransformationMatches(
44+
undefined,
45+
'owner',
46+
{
47+
[Op.or]: [
48+
{
49+
monarchy: {
50+
monarch: address,
51+
},
52+
} satisfies AccountTypes.GovernanceDetailsForString,
53+
{
54+
sub_account: {
55+
account: address,
56+
},
57+
} satisfies AccountTypes.GovernanceDetailsForString,
58+
{
59+
abstract_account: {
60+
address: address,
61+
},
62+
} satisfies AccountTypes.GovernanceDetailsForString,
63+
],
64+
},
65+
key ? getCodeIdsForKeys(key) : undefined
66+
)
67+
)?.map(({ contractAddress }) => contractAddress) || []
68+
69+
return owned
70+
},
71+
}

src/formulas/formulas/account/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * as abstract from './abstract'
12
export * as bank from './bank'
23
export * as contract from './contract'
34
export * as daos from './daos'
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { ContractFormula } from '@/types'
2+
3+
import { makeSimpleContractFormula } from '../../utils'
4+
import * as Common from '../common'
5+
import { AccountTypes } from './types'
6+
import { Addr, GovernanceDetailsForString } from './types/account'
7+
8+
const AccountStorageKeys = {
9+
SUSPENSION_STATUS: 'aa',
10+
INFO: 'ab',
11+
ACCOUNT_MODULES: 'ac',
12+
DEPENDENTS: 'ad',
13+
SUB_ACCOUNTS: 'ae',
14+
WHITELISTED_MODULES: 'af',
15+
ACCOUNT_ID: 'ag',
16+
OWNER: 'ownership',
17+
}
18+
19+
export const owner = makeSimpleContractFormula<
20+
{ owner: GovernanceDetailsForString },
21+
GovernanceDetailsForString | null
22+
>({
23+
docs: {
24+
description: 'Get the owner of the account',
25+
},
26+
transformation: AccountStorageKeys.OWNER,
27+
fallbackKeys: [AccountStorageKeys.OWNER],
28+
transform: (data) => data.owner,
29+
fallback: null,
30+
})
31+
32+
export const accountId =
33+
makeSimpleContractFormula<AccountTypes.AccountId | null>({
34+
docs: {
35+
description: 'Get accountId of the account',
36+
},
37+
key: AccountStorageKeys.ACCOUNT_ID,
38+
fallback: null,
39+
})
40+
41+
export const suspensionStatus = makeSimpleContractFormula<boolean | null>({
42+
docs: {
43+
description: 'Get suspension status of the account',
44+
},
45+
key: AccountStorageKeys.SUSPENSION_STATUS,
46+
fallback: null,
47+
})
48+
49+
export const info = makeSimpleContractFormula<AccountTypes.AccountInfo | null>({
50+
docs: {
51+
description: 'Get the account info',
52+
},
53+
key: AccountStorageKeys.INFO,
54+
fallback: null,
55+
})
56+
57+
export const whitelistedModules = makeSimpleContractFormula<Addr[] | null>({
58+
docs: {
59+
description: 'Get a list of whitelisted modules',
60+
},
61+
key: AccountStorageKeys.WHITELISTED_MODULES,
62+
fallback: null,
63+
})
64+
65+
export const subAccountIds: ContractFormula<AccountTypes.AccountId[]> = {
66+
docs: {
67+
description: 'Get sub-accounts owned by this account',
68+
},
69+
compute: async ({ contractAddress, getMap }) => {
70+
const subAccountsMap =
71+
(await getMap<number, {}>(
72+
contractAddress,
73+
AccountStorageKeys.SUB_ACCOUNTS,
74+
{
75+
keyType: 'number',
76+
}
77+
)) ?? {}
78+
79+
return Object.keys(subAccountsMap).map((seq) => ({
80+
trace: 'local',
81+
seq: Number(seq),
82+
}))
83+
},
84+
}
85+
86+
export const moduleInfos: ContractFormula<
87+
Array<
88+
Omit<
89+
AccountTypes.ModuleInfosResponse['module_infos'][number],
90+
'version'
91+
> & { version: string | undefined }
92+
>
93+
> = {
94+
docs: {
95+
description: 'Get module infos that are installed on this account',
96+
},
97+
compute: async (env) => {
98+
const { contractAddress, getMap } = env
99+
100+
const moduleAddressesMap =
101+
(await getMap<string, AccountTypes.Addr>(
102+
contractAddress,
103+
AccountStorageKeys.ACCOUNT_MODULES
104+
)) ?? {}
105+
106+
// Query the info from the address of the module
107+
return await Promise.all(
108+
Object.entries(moduleAddressesMap).map(async ([moduleId, address]) => {
109+
const contractInfo = await Common.info.compute({
110+
...env,
111+
contractAddress: address,
112+
})
113+
114+
return {
115+
id: contractInfo?.contract ?? moduleId,
116+
address,
117+
version: contractInfo?.version,
118+
}
119+
})
120+
)
121+
},
122+
}
123+
124+
// TODO: account txs
125+
// export const accountTxs: ContractFormula<any> = {
126+
// docs: {
127+
// description: '',
128+
// },
129+
// compute: async (env) => {
130+
// const { contractAddress, getTxEvents } = env
131+
// const events = await getTxEvents(contractAddress)
132+
// return events || []
133+
// },
134+
// }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * as account from './account'
2+
export * as registry from './registry'

0 commit comments

Comments
 (0)