Skip to content

Commit

Permalink
Added transformers and formulas to get vesting contracts with a speci…
Browse files Browse the repository at this point in the history
…fic admin.
  • Loading branch information
NoahSaso committed Nov 20, 2023
1 parent 606d2ce commit 6076049
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/data/formulas/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * as nft from './nft'
export * as proposals from './proposals'
export * as tokens from './tokens'
export * as valence from './valence'
export * as vesting from './vesting'
50 changes: 50 additions & 0 deletions src/data/formulas/wallet/vesting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Op } from 'sequelize'

import { WalletFormula } from '@/core'

export const ownerOf: WalletFormula<string[]> = {
compute: async (env) => {
const { walletAddress, getTransformationMatches, getCodeIdsForKeys } = env

const cwVestingCodeIds = getCodeIdsForKeys('cw-vesting')
if (!cwVestingCodeIds.length) {
throw new Error('missing cw-vesting code IDs')
}

// Get all cw1-whitelist contracts with this wallet as an admin.
const cw1WhitelistCodeIds = getCodeIdsForKeys('cw1-whitelist')
const cw1WhitelistContracts = cw1WhitelistCodeIds.length
? (await getTransformationMatches(
undefined,
'admins',
{
[Op.contains]: walletAddress,
},
cw1WhitelistCodeIds
)) ?? []
: []

// Get all cw-vesting contracts with this wallet as the owner or a
// cw1-whitelist contract where this wallet is an admin.
const cwVestingContracts = (
await Promise.all([
getTransformationMatches(
undefined,
`owner:${walletAddress}`,
undefined,
cwVestingCodeIds
),
...cw1WhitelistContracts.map(({ contractAddress }) =>
getTransformationMatches(
undefined,
`owner:${contractAddress}`,
undefined,
cwVestingCodeIds
)
),
])
).flatMap((m) => m ?? [])

return cwVestingContracts.map(({ contractAddress }) => contractAddress)
},
}
14 changes: 13 additions & 1 deletion src/data/transformers/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Transformer } from '@/core'
import { dbKeyForKeys } from '@/core/utils'

import { makeTransformer } from './utils'

// Transform for all contracts.
const info: Transformer = makeTransformer([], 'info', 'contract_info')

export default [info]
// Transform for all contracts. cw-ownable ownership
const KEY_OWNERSHIP = dbKeyForKeys('ownership')
const owner: Transformer = {
filter: {
codeIdsKeys: [],
matches: (event) => event.key === KEY_OWNERSHIP,
},
name: 'owner',
getValue: (event) => event.valueJson.owner,
}

export default [info, owner]
17 changes: 17 additions & 0 deletions src/data/transformers/external/cw1Whitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Transformer } from '@/core/types'
import { dbKeyForKeys } from '@/core/utils'

const CODE_IDS_KEYS: string[] = ['cw1-whitelist']

const KEY_ADMIN_LIST = dbKeyForKeys('admin_list')

const admins: Transformer = {
filter: {
codeIdsKeys: CODE_IDS_KEYS,
matches: (event) => event.key === KEY_ADMIN_LIST,
},
name: 'admins',
getValue: (event) => event.valueJson.admins,
}

export default [admins]
3 changes: 2 additions & 1 deletion src/data/transformers/external/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cw1Whitelist from './cw1Whitelist'
import cw20 from './cw20'
import cw4Group from './cw4Group'
import cw721 from './cw721'

export default [...cw20, ...cw4Group, ...cw721]
export default [...cw1Whitelist, ...cw20, ...cw4Group, ...cw721]

0 comments on commit 6076049

Please sign in to comment.