Skip to content

Commit

Permalink
Added config object to DAO account type and allow filtering valence a…
Browse files Browse the repository at this point in the history
…ccount by rebalanced tokens only.
  • Loading branch information
NoahSaso committed Oct 4, 2023
1 parent fcb3ae9 commit e15951b
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 118 deletions.
85 changes: 61 additions & 24 deletions packages/state/recoil/selectors/contracts/DaoCore.v2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { selectorFamily, waitForAll, waitForAny } from 'recoil'

import {
ChainId,
DaoAccount,
DaoAccountType,
DaoValenceAccountConfig,
GenericTokenBalance,
PolytoneProxies,
TokenType,
Expand Down Expand Up @@ -1235,9 +1238,9 @@ export const coreAddressForPolytoneProxySelector = selectorFamily<
),
})

// Get the valence account address from the DAO's items.
// Get the valence account address from the DAO's items and its config.
export const valenceAccountSelector = selectorFamily<
string | undefined,
DaoAccount | undefined,
QueryClientParams & {
// The chain ID the valence account exists on.
targetChainId: string
Expand All @@ -1246,20 +1249,65 @@ export const valenceAccountSelector = selectorFamily<
key: 'daoCoreV2ValenceAccount',
get:
({ targetChainId, ...queryClientParams }) =>
({ get }) =>
({ 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.
get(
getItemSelector({
...queryClientParams,
params: [

// TODO(rebalancer): Get config
const config: DaoValenceAccountConfig = {
rebalancer: {
targets: [
{
key: VALENCE_ACCOUNT_ITEM_KEY_PREFIX + targetChainId,
source: {
chainId: ChainId.NeutronMainnet,
denomOrAddress: 'untrn',
},
targets: [
{
timestamp: new Date(0),
target: 0.75,
},
],
},
{
source: {
chainId: 'axelar-dojo-1',
denomOrAddress: 'uusdc',
},
targets: [
{
timestamp: new Date(0),
target: 0.25,
},
],
},
],
})
).item || undefined,
},
}

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

// Get all accounts controlled by this DAO, including its native chain, all
Expand All @@ -1281,14 +1329,14 @@ export const allAccountsSelector = selectorFamily<
{
chainId: queryClientParams.chainId,
address: queryClientParams.contractAddress,
type: 'native',
type: DaoAccountType.Native,
},
// Polytone.
...polytoneProxies.map(
([chainId, address]): DaoAccount => ({
chainId,
address,
type: 'polytone',
type: DaoAccountType.Polytone,
})
),
]
Expand All @@ -1306,18 +1354,7 @@ export const allAccountsSelector = selectorFamily<
)

// Add valence accounts.
allAccounts.push(
...allAccounts.flatMap(({ chainId }, idx): DaoAccount | [] => {
const address = valenceAccounts[idx]
return address
? {
chainId,
address,
type: 'valence',
}
: []
})
)
allAccounts.push(...valenceAccounts.flatMap((account) => account || []))

return allAccounts
},
Expand Down
35 changes: 24 additions & 11 deletions packages/stateful/components/dao/DaoTreasuryHistoryGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
useDaoInfoContext,
useNamedThemeColor,
} from '@dao-dao/stateless'
import { DaoTreasuryHistoryGraphProps } from '@dao-dao/types'
import { DaoAccountType, DaoTreasuryHistoryGraphProps } from '@dao-dao/types'
import {
DISTRIBUTION_COLORS,
formatDate,
Expand All @@ -45,8 +45,8 @@ ChartJS.register(

// TODO: add way to set base price denom to use instead of USD
export const DaoTreasuryHistoryGraph = ({
filter,
targets,
account,
showRebalancer = false,
className,
}: DaoTreasuryHistoryGraphProps) => {
const { t } = useTranslation()
Expand All @@ -68,7 +68,10 @@ export const DaoTreasuryHistoryGraph = ({
chainId,
coreAddress,
precision,
filter,
filter: account && {
account,
rebalancerOnly: showRebalancer,
},
startSecondsAgo,
})
)
Expand Down Expand Up @@ -96,10 +99,19 @@ export const DaoTreasuryHistoryGraph = ({
}
)

const targetValues =
treasuryValueHistory.loading || treasuryValueHistory.errored
? []
: (targets || []).flatMap(({ source, targets }) => {
const showTargets =
!!account &&
account.type === DaoAccountType.Valence &&
!!account.config.rebalancer?.targets.length &&
showRebalancer &&
!treasuryValueHistory.loading &&
!treasuryValueHistory.errored

// Show targets if rebalancer configured account.
const targetValues = !showTargets
? []
: (account.config.rebalancer?.targets || []).flatMap(
({ source, targets }) => {
if (targets.length === 0) {
return []
}
Expand All @@ -109,7 +121,7 @@ export const DaoTreasuryHistoryGraph = ({
// Find first target that is after this timestamp so we can
// choose the most recent target before it.
let nextTargetIndex = targets.findIndex(
(target) => target.timestamp > _timestamp
(target) => target.timestamp > _timestamp.getTime()
)
const targetIndex =
nextTargetIndex === -1
Expand Down Expand Up @@ -168,7 +180,8 @@ export const DaoTreasuryHistoryGraph = ({
pointHitRadius: 0,
borderWidth: 2.5,
}
})
}
)

const totalValues =
treasuryValueHistory.loading || treasuryValueHistory.errored
Expand Down Expand Up @@ -363,7 +376,7 @@ export const DaoTreasuryHistoryGraph = ({
})}
</p>

{index > 0 && !!tooltipTotalValue && (
{index > 0 && !!tooltipTotalValue && showTargets && (
<>
<p className="caption-text">
{formatPercentOf100(
Expand Down
46 changes: 36 additions & 10 deletions packages/stateful/recoil/selectors/treasury.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@dao-dao/state'
import {
DaoAccount,
DaoAccountType,
GenericToken,
LoadingTokens,
TokenCardInfo,
Expand Down Expand Up @@ -216,6 +217,12 @@ export const treasuryTokenCardInfosForDaoSelector = selectorFamily<
},
})

const ACCOUNT_FILTER_PROPERTIES: (keyof DaoAccount)[] = [
'type',
'chainId',
'address',
]

export const daoTreasuryValueHistorySelector = selectorFamily<
{
timestamps: Date[]
Expand All @@ -237,8 +244,13 @@ export const daoTreasuryValueHistorySelector = selectorFamily<
coreAddress: string
precision: OsmosisHistoricalPriceChartPrecision
startSecondsAgo: number
// Filter by any of the account properties.
filter?: Partial<DaoAccount>
filter?: {
// Filter by any of the account properties.
account?: Partial<DaoAccount>
// If `account` is a valence account and `rebalancerOnly` is true, only
// show valence account assets that are being rebalanced.
rebalancerOnly?: boolean
}
}>
>({
key: 'daoTreasuryValueHistory',
Expand All @@ -258,12 +270,15 @@ export const daoTreasuryValueHistorySelector = selectorFamily<
})
)

// Filter accounts.
if (filter) {
// Filter by account fields.
if (filter?.account) {
allAccounts = allAccounts.filter((account) =>
Object.entries(filter).every(([key, value]) => {
return account[key as keyof DaoAccount] === value
})
ACCOUNT_FILTER_PROPERTIES.every(
(key) =>
!filter.account ||
!(key in filter.account) ||
account[key] === filter.account[key]
)
)
}

Expand Down Expand Up @@ -339,9 +354,20 @@ export const daoTreasuryValueHistorySelector = selectorFamily<
const tokens = [
...historicalBalancesByToken.map(({ token }) => token),
...currentBalances.map(({ token }) => token),
]
// Can only compute price if token decimals loaded correctly.
.filter(({ decimals }) => decimals > 0)
].filter(
(token) =>
// Can only compute price if token decimals loaded correctly.
token.decimals > 0 &&
// Filter by rebalancer tokens in valence account.
(!filter ||
!filter.rebalancerOnly ||
filter.account?.type !== DaoAccountType.Valence ||
!filter.account.config?.rebalancer ||
filter.account.config.rebalancer.targets.some(
({ source }) =>
serializeTokenSource(source) === serializeTokenSource(token)
))
)

// Unique token sources.
const uniqueTokenSources = [
Expand Down
42 changes: 39 additions & 3 deletions packages/stateful/server/makeGetDaoStaticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import {
queryIndexer,
} from '@dao-dao/state'
import {
ChainId,
CommonProposalInfo,
ContractVersion,
ContractVersionInfo,
DaoAccount,
DaoAccountType,
DaoPageMode,
DaoParentInfo,
DaoValenceAccountConfig,
IndexerDumpState,
InfoResponse,
PolytoneProxies,
Expand Down Expand Up @@ -234,14 +237,14 @@ export const makeGetDaoStaticProps: GetDaoStaticPropsMaker =
{
chainId,
address: coreAddress,
type: 'native',
type: DaoAccountType.Native,
},
// Polytone.
...Object.entries(polytoneProxies).map(
([chainId, address]): DaoAccount => ({
chainId,
address,
type: 'polytone',
type: DaoAccountType.Polytone,
})
),
// Valence.
Expand All @@ -254,10 +257,43 @@ export const makeGetDaoStaticProps: GetDaoStaticPropsMaker =
const chainId = key.slice(VALENCE_ACCOUNT_ITEM_KEY_PREFIX.length)
const address = value

// 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 {
chainId,
address,
type: 'valence',
type: DaoAccountType.Valence,
config,
}
}),
]
Expand Down
3 changes: 2 additions & 1 deletion packages/stateful/server/makeGetGovStaticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { serverSideTranslationsWithServerT } from '@dao-dao/i18n/serverSideTrans
import { cosmos } from '@dao-dao/protobuf'
import {
ContractVersion,
DaoAccountType,
GovProposalVersion,
GovProposalWithDecodedContent,
} from '@dao-dao/types'
Expand Down Expand Up @@ -130,7 +131,7 @@ export const makeGetGovStaticProps: GetGovStaticPropsMaker =
polytoneProxies: {},
accounts: [
{
type: 'native',
type: DaoAccountType.Native,
chainId: chain.chain_id,
address: supportedChain.name,
},
Expand Down
Loading

0 comments on commit e15951b

Please sign in to comment.