Skip to content

Commit

Permalink
Removed direct references to polytoneProxies and use DAO accounts arr…
Browse files Browse the repository at this point in the history
…ay as source of truth.
  • Loading branch information
NoahSaso committed Oct 17, 2023
1 parent 6d682f1 commit 29bcbed
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ export const InstantiateComponent: ActionComponent<InstantiateOptions> = (
onRemove: props.isCreating
? () => removeCoin(index)
: undefined,
chainId,
} as NativeCoinSelectorProps)}
errors={errors?.funds?.[index]}
fieldNamePrefix={fieldNamePrefix + `funds.${index}.`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Default.args = {
symbol: 'ATOM',
imageUrl:
'https://raw.githubusercontent.com/CosmosContracts/junoswap-asset-list/main/images/atom.png',
source: {
chainId: CHAIN_ID,
denomOrAddress: 'uatom',
},
},
balance: '984129741',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
convertDenomToMicroDenomWithDecimals,
convertMicroDenomToDenomWithDecimals,
decodePolytoneExecuteMsg,
getDaoAccountAddress,
getNativeTokenForChainId,
makeWasmMessage,
maybeMakePolytoneExecuteMessage,
Expand All @@ -53,7 +54,7 @@ const Component: ActionComponent = (props) => {
const {
context,
address,
chain: { chain_id: currentChainId, bech32_prefix: bech32Prefix },
chain: { bech32_prefix: bech32Prefix },
} = useActionOptions()

const { watch, setValue } = useFormContext<Instantiate2Data>()
Expand Down Expand Up @@ -109,9 +110,10 @@ const Component: ActionComponent = (props) => {
setValue((props.fieldNamePrefix + 'funds') as 'funds', [])
setValue(
(props.fieldNamePrefix + 'admin') as 'admin',
chainId === currentChainId
? address
: context.info.polytoneProxies[chainId] ?? ''
getDaoAccountAddress({
accounts: context.info.accounts,
chainId,
}) || ''
)
}}
/>
Expand Down
142 changes: 3 additions & 139 deletions packages/stateful/actions/core/treasury/ConfigureRebalancer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,100 +179,9 @@ const Component: ActionComponent<undefined, ConfigureRebalancerData> = (
const useTransformToCosmos: UseTransformToCosmos<
ConfigureRebalancerData
> = () => {
const {
address,
context,
chain: { chain_id: currentChainId },
} = useActionOptions()

const loadingTokenBalances = useTokenBalances({
allChains: true,
filter: TokenType.Native,
})

return useCallback(
({ chainId, tokens, pid }: ConfigureRebalancerData) => {
return undefined
// if (loadingTokenBalances.loading) {
// return
// }

// const token = loadingTokenBalances.data.find(
// ({ token }) =>
// token.chainId === fromChainId && token.denomOrAddress === denom
// )?.token
// if (!token) {
// throw new Error(`Unknown token: ${denom}`)
// }

// const amount = BigInt(
// convertDenomToMicroDenomWithDecimals(_amount, token.decimals)
// ).toString()

// let msg: CosmosMsgForEmpty | undefined
// if (toChainId !== fromChainId) {
// const { sourceChannel } = getIbcTransferInfoBetweenChains(
// fromChainId,
// toChainId
// )
// const sender =
// fromChainId === currentChainId
// ? address
// : context.type === ActionContextType.Dao
// ? context.info.polytoneProxies[fromChainId]
// : transformBech32Address(address, fromChainId)
// msg = makeStargateMessage({
// stargate: {
// typeUrl: MsgTransfer.typeUrl,
// value: {
// sourcePort: 'transfer',
// sourceChannel,
// token: coin(amount, denom),
// sender,
// receiver: to,
// // Timeout after 1 year. Needs to survive voting period and
// // execution delay.
// timeoutTimestamp: BigInt(
// // Nanoseconds.
// (Date.now() + 1000 * 60 * 60 * 24 * 365) * 1e6
// ),
// memo: '',
// } as MsgTransfer,
// },
// })
// } else if (token.type === TokenType.Native) {
// msg = {
// bank: makeBankMessage(amount, to, denom),
// }
// } else if (token.type === TokenType.Cw20) {
// msg = makeWasmMessage({
// wasm: {
// execute: {
// contract_addr: denom,
// funds: [],
// msg: {
// transfer: {
// recipient: to,
// amount,
// },
// },
// },
// },
// })
// }

// if (!msg) {
// throw new Error(`Unknown token type: ${token.type}`)
// }

// if (chainId === currentChainId) {
// return msg
// } else {
// return makePolytoneExecuteMessage(currentChainId, chainId, msg)
// }
},
[address, context, currentChainId, loadingTokenBalances]
)
return useCallback(({ chainId, tokens, pid }: ConfigureRebalancerData) => {
return undefined
}, [])
}

const useDecodedCosmosMsg: UseDecodedCosmosMsg<ConfigureRebalancerData> = (
Expand All @@ -288,51 +197,6 @@ const useDecodedCosmosMsg: UseDecodedCosmosMsg<ConfigureRebalancerData> = (
return {
match: false,
}

// const isNative =
// objectMatchesStructure(msg, {
// bank: {
// send: {
// amount: {},
// to_address: {},
// },
// },
// }) &&
// msg.bank.send.amount.length === 1 &&
// objectMatchesStructure(msg.bank.send.amount[0], {
// amount: {},
// denom: {},
// })

// const token = useRecoilValue(
// isNative
// ? genericTokenSelector({
// chainId,
// type: TokenType.Native,
// denomOrAddress: msg.bank.send.amount[0].denom,
// })
// : constSelector(undefined)
// )

// if (!token) {
// return {
// match: false,
// }
// }

// return {
// match: true,
// data: {
// fromChainId: chainId,
// toChainId: chainId,
// to: msg.bank.send.to_address,
// amount: convertMicroDenomToDenomWithDecimals(
// msg.bank.send.amount[0].amount,
// token.decimals
// ),
// denom: token.denomOrAddress,
// },
// }
}

export const makeConfigureRebalancerAction: ActionMaker<
Expand Down
10 changes: 5 additions & 5 deletions packages/stateful/actions/core/treasury/Spend/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
convertMicroDenomToDenomWithDecimals,
getChainForChainId,
getChainForChainName,
getDaoAccountAddress,
getImageUrlForChainId,
makeValidateAddress,
toAccessibleImageUrl,
Expand Down Expand Up @@ -120,11 +121,10 @@ export const SpendComponent: ActionComponent<SpendOptions> = ({
// account on the destination chain.
else {
newRecipient =
toChain.chain_id === currentEntity.chainId
? currentEntity.address
: toChain.chain_id in currentEntity.daoInfo.polytoneProxies
? currentEntity.daoInfo.polytoneProxies[toChain.chain_id]
: ''
getDaoAccountAddress({
accounts: currentEntity.daoInfo.accounts,
chainId: toChain.chain_id,
}) || ''
}

if (newRecipient !== recipient) {
Expand Down
6 changes: 5 additions & 1 deletion packages/stateful/actions/core/treasury/Spend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
decodePolytoneExecuteMsg,
getChainForChainId,
getChainForChainName,
getDaoAccountAddress,
getIbcTransferInfoBetweenChains,
getIbcTransferInfoFromChainSource,
isDecodedStargateMsg,
Expand Down Expand Up @@ -169,7 +170,10 @@ const useTransformToCosmos: UseTransformToCosmos<SpendData> = () => {
fromChainId === currentChainId
? address
: context.type === ActionContextType.Dao
? context.info.polytoneProxies[fromChainId]
? getDaoAccountAddress({
accounts: context.info.accounts,
chainId: fromChainId,
})
: transformBech32Address(address, fromChainId)
msg = makeStargateMessage({
stargate: {
Expand Down
13 changes: 3 additions & 10 deletions packages/stateful/command/contexts/generic/dao.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ import { subDaoInfosSelector } from '../../../recoil'

export const makeGenericDaoContext: CommandModalContextMaker<{
dao: CommandModalDaoInfo
}> = ({
dao: { chainId, coreAddress, name, imageUrl, polytoneProxies },
...options
}) => {
}> = ({ dao: { chainId, coreAddress, name, imageUrl }, ...options }) => {
const useSections = () => {
const { t } = useTranslation()
const { getDaoPath, getDaoProposalPath, router } = useDaoNavHelpers()
const { coreVersion } = useDaoInfoContext()
const { coreVersion, accounts } = useDaoInfoContext()
const tabs = useDaoTabs()

const { isFollowing, setFollowing, setUnfollowing, updatingFollowing } =
Expand Down Expand Up @@ -78,8 +75,6 @@ export const makeGenericDaoContext: CommandModalContextMaker<{
routes.forEach((url) => router.prefetch(url))
}, [routes])

const chains = [[chainId, coreAddress], ...Object.entries(polytoneProxies)]

const actionsSection: CommandModalContextSection<
{ href: string } | { onChoose: () => void }
> = {
Expand Down Expand Up @@ -124,7 +119,7 @@ export const makeGenericDaoContext: CommandModalContextMaker<{
following ? setUnfollowing(coreAddress) : setFollowing(coreAddress),
loading: updatingFollowing,
},
...chains.map(([chainId, address]) => ({
...accounts.map(({ chainId, address }) => ({
name:
copied === chainId
? t('info.copiedDaoChainAddress', {
Expand Down Expand Up @@ -183,13 +178,11 @@ export const makeGenericDaoContext: CommandModalContextMaker<{
coreAddress,
name,
imageUrl,
polytoneProxies,
}): CommandModalDaoInfo => ({
chainId,
coreAddress,
name,
imageUrl: imageUrl || getFallbackImage(coreAddress),
polytoneProxies,
})
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
CommandModalContextUseSectionsOptions,
CommandModalDaoInfo,
} from '@dao-dao/types'
import {
getFallbackImage,
getSupportedChains,
polytoneNoteProxyMapToChainIdMap,
} from '@dao-dao/utils'
import { getFallbackImage, getSupportedChains } from '@dao-dao/utils'

import {
useLoadingFeaturedDaoCardInfos,
Expand Down Expand Up @@ -71,16 +67,12 @@ export const useFollowingAndFilteredDaosSections = ({
value: {
config: { name, image_url },
proposalCount,
polytoneProxies,
},
}): CommandModalContextSectionItem<CommandModalDaoInfo> => ({
chainId,
coreAddress: contractAddress,
name,
imageUrl: image_url || getFallbackImage(contractAddress),
polytoneProxies: polytoneProxies
? polytoneNoteProxyMapToChainIdMap(chainId, polytoneProxies)
: {},
// If DAO has no proposals, make it less visible and give it a
// tooltip to indicate that it may not be active.
...(proposalCount === 0 && {
Expand Down
13 changes: 10 additions & 3 deletions packages/stateful/components/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
useChain,
} from '@dao-dao/stateless'
import { AddressInputProps, Entity, EntityType } from '@dao-dao/types'
import { POLYTONE_CONFIG_PER_CHAIN, isValidBech32Address } from '@dao-dao/utils'
import {
POLYTONE_CONFIG_PER_CHAIN,
getDaoAccountAddress,
isValidBech32Address,
} from '@dao-dao/utils'

import { entitySelector } from '../recoil'
import { EntityDisplay } from './EntityDisplay'
Expand Down Expand Up @@ -101,14 +105,17 @@ export const AddressInput = <
const entities = loadingEntities.loading
? []
: // Only show entities that are on the current chain or are DAOs with
// polytone accounts on the current chain.
// accounts (polytone probably) on the current chain.
loadingEntities.data
.filter(
(entity) =>
entity.state === 'hasValue' &&
(entity.contents.chainId === currentChain.chain_id ||
(entity.contents.type === EntityType.Dao &&
entity.contents.daoInfo.polytoneProxies[currentChain.chain_id]))
getDaoAccountAddress({
accounts: entity.contents.daoInfo.accounts,
chainId: currentChain.chain_id,
})))
)
.map((entity) => entity.contents as Entity)

Expand Down
17 changes: 7 additions & 10 deletions packages/stateful/components/dao/DaoFiatDepositModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useTranslation } from 'react-i18next'

import { KadoModal, useDaoInfoContext } from '@dao-dao/stateless'
import { DaoFiatDepositModalProps } from '@dao-dao/types'
import { getDaoAccount } from '@dao-dao/utils'

export const DaoFiatDepositModal = ({
chainId,
Expand All @@ -11,16 +10,14 @@ export const DaoFiatDepositModal = ({
}: DaoFiatDepositModalProps) => {
const { t } = useTranslation()

const daoInfo = useDaoInfoContext()
// Deposit address depends on what the account type is of the token owner.
let depositAddress = getDaoAccount({
daoInfo,
chainId,
accountType,
})
const { chainId: daoChainId, coreAddress, accounts } = useDaoInfoContext()
// Deposit address depends on the account type.
let depositAddress = accounts.find(
(account) => account.chainId === chainId && account.type === accountType
)?.address
// Default to the DAO's native chain and address if no deposit address found.
chainId = depositAddress ? chainId : daoInfo.chainId
depositAddress ||= daoInfo.coreAddress
chainId = depositAddress ? chainId : daoChainId
depositAddress ||= coreAddress

return (
<KadoModal
Expand Down
Loading

0 comments on commit 29bcbed

Please sign in to comment.