Skip to content

Commit

Permalink
Overhauled account type system to be more general.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Oct 20, 2023
1 parent 86718ba commit 581f138
Show file tree
Hide file tree
Showing 35 changed files with 585 additions and 628 deletions.
163 changes: 44 additions & 119 deletions packages/state/recoil/selectors/contracts/DaoCore.v2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { selectorFamily, waitForAll, waitForAny } from 'recoil'

import {
ChainId,
DaoAccount,
DaoAccountType,
DaoValenceAccountConfig,
Account,
AccountType,
GenericTokenBalance,
PolytoneProxies,
TokenType,
Expand Down Expand Up @@ -35,7 +33,6 @@ import {
import {
CW721_WORKAROUND_ITEM_KEY_PREFIX,
POLYTONE_CW721_ITEM_KEY_PREFIX,
VALENCE_ACCOUNT_ITEM_KEY_PREFIX,
getSupportedChainConfig,
polytoneNoteProxyMapToChainIdMap,
} from '@dao-dao/utils'
Expand All @@ -57,6 +54,7 @@ import {
import { cosmWasmClientForChainSelector } from '../chain'
import { queryContractIndexerSelector } from '../indexer'
import { genericTokenSelector } from '../token'
import { valenceAccountsSelector } from '../valence'
import * as Cw20BaseSelectors from './Cw20Base'

type QueryClientParams = WithChainId<{
Expand Down Expand Up @@ -1238,124 +1236,51 @@ export const coreAddressForPolytoneProxySelector = selectorFamily<
),
})

// Get the valence account address from the DAO's items and its config.
export const valenceAccountSelector = selectorFamily<
DaoAccount | undefined,
QueryClientParams & {
// The chain ID the valence account exists on.
targetChainId: string
}
>({
key: 'daoCoreV2ValenceAccount',
get:
({ targetChainId, ...queryClientParams }) =>
({ get }) => {
const valenceAccountAddress =
get(
getItemSelector({
...queryClientParams,
params: [
{
key: VALENCE_ACCOUNT_ITEM_KEY_PREFIX + targetChainId,
},
],
})
).item || undefined
if (!valenceAccountAddress) {
return
}

// TODO(rebalancer): Verify that the address value is set to a valence account
// contract owned by this DAO, so DAOs cannot set their valence account to
// any address to render it in their treasury.

// TODO(rebalancer): Get config
const config: DaoValenceAccountConfig = {
rebalancer: {
targets: [
{
source: {
chainId: ChainId.NeutronMainnet,
denomOrAddress: 'untrn',
},
targets: [
{
timestamp: 0,
target: 0.75,
},
],
},
{
source: {
chainId: 'axelar-dojo-1',
denomOrAddress: 'uusdc',
},
targets: [
{
timestamp: 0,
target: 0.25,
},
],
},
],
},
}

return {
type: DaoAccountType.Valence,
chainId: targetChainId,
address: valenceAccountAddress,
config,
}
},
})

// Get all accounts controlled by this DAO, including its native chain, all
// polytone proxies, and all valence accounts.
export const allAccountsSelector = selectorFamily<
DaoAccount[],
QueryClientParams
>({
key: 'daoCoreV2AllAccounts',
get:
(queryClientParams) =>
({ get }) => {
const polytoneProxies = Object.entries(
get(polytoneProxiesSelector(queryClientParams))
)

const allAccounts: DaoAccount[] = [
// Current chain.
{
chainId: queryClientParams.chainId,
address: queryClientParams.contractAddress,
type: DaoAccountType.Native,
},
// Polytone.
...polytoneProxies.map(
([chainId, address]): DaoAccount => ({
chainId,
address,
type: DaoAccountType.Polytone,
})
),
]
export const allAccountsSelector = selectorFamily<Account[], QueryClientParams>(
{
key: 'daoCoreV2AllAccounts',
get:
(queryClientParams) =>
({ get }) => {
const polytoneProxies = Object.entries(
get(polytoneProxiesSelector(queryClientParams))
)

// Get valence accounts on each potential chain.
const valenceAccounts = get(
waitForAll(
allAccounts.map(({ chainId }) =>
valenceAccountSelector({
...queryClientParams,
targetChainId: chainId,
const allAccounts: Account[] = [
// Current chain.
{
chainId: queryClientParams.chainId,
address: queryClientParams.contractAddress,
type: AccountType.Native,
},
// Polytone.
...polytoneProxies.map(
([chainId, address]): Account => ({
chainId,
address,
type: AccountType.Polytone,
})
),
]

// Get valence accounts on each potential chain.
const valenceAccounts = get(
waitForAll(
allAccounts.map(({ chainId, address }) =>
valenceAccountsSelector({
address,
chainId,
})
)
)
)
)
).flat()

// Add valence accounts.
allAccounts.push(...valenceAccounts.flatMap((account) => account || []))
// Add valence accounts.
allAccounts.push(...valenceAccounts)

return allAccounts
},
})
return allAccounts
},
}
)
80 changes: 80 additions & 0 deletions packages/state/recoil/selectors/valence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { selectorFamily } from 'recoil'

import {
AccountType,
ChainId,
ValenceAccount,
ValenceAccountConfig,
WithChainId,
} from '@dao-dao/types'

import { queryWalletIndexerSelector } from './indexer'

// Retrieve the valence accounts for a given address.
export const valenceAccountsSelector = selectorFamily<
ValenceAccount[],
WithChainId<{ address: string }>
>({
key: 'valenceAccounts',
get:
({ address, chainId }) =>
async ({ get }) => {
// Get valence accounts from indexer.
const valenceAccountAddresses = get(
queryWalletIndexerSelector({
chainId,
walletAddress: address,
formula: 'valence/accounts',
required: true,
})
)
if (!valenceAccountAddresses || !Array.isArray(valenceAccountAddresses)) {
return []
}

const accounts = valenceAccountAddresses.map(
(address): ValenceAccount => {
// TODO(rebalancer): Get config
const config: ValenceAccountConfig = {
rebalancer: {
targets: [
{
source: {
chainId: ChainId.NeutronMainnet,
denomOrAddress: 'untrn',
},
targets: [
{
timestamp: 0,
target: 0.75,
},
],
},
{
source: {
chainId: 'axelar-dojo-1',
denomOrAddress: 'uusdc',
},
targets: [
{
timestamp: 0,
target: 0.25,
},
],
},
],
},
}

return {
type: AccountType.Valence,
chainId,
address,
config,
}
}
)

return accounts
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
convertDenomToMicroDenomWithDecimals,
convertMicroDenomToDenomWithDecimals,
decodePolytoneExecuteMsg,
getDaoAccountAddress,
getAccountAddress,
getNativeTokenForChainId,
makeWasmMessage,
maybeMakePolytoneExecuteMessage,
Expand Down Expand Up @@ -110,7 +110,7 @@ const Component: ActionComponent = (props) => {
setValue((props.fieldNamePrefix + 'funds') as 'funds', [])
setValue(
(props.fieldNamePrefix + 'admin') as 'admin',
getDaoAccountAddress({
getAccountAddress({
accounts: context.info.accounts,
chainId,
}) || ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ Default.args = {
},
],
},
icaRemoteAddressLoading: {
loading: false,
errored: false,
data: 'icaRemote',
},
},
isCreating: true,
errors: {},
Expand Down
Loading

0 comments on commit 581f138

Please sign in to comment.