Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy fixes #1405

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/stateful/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useChains } from '@cosmos-kit/react-lite'
import { useTranslation } from 'react-i18next'

import {
ConnectWalletProps,
ConnectWallet as StatelessConnectWallet,
Tooltip,
useChainContextIfAvailable,
} from '@dao-dao/stateless'

import { useWallet } from '../hooks/useWallet'
import { getSupportedChains } from '@dao-dao/utils'

export type StatefulConnectWalletProps = Omit<
ConnectWalletProps,
Expand All @@ -15,7 +16,19 @@ export type StatefulConnectWalletProps = Omit<

export const ConnectWallet = (props: StatefulConnectWalletProps) => {
const { t } = useTranslation()
const { connect, disconnect, isWalletConnecting } = useWallet()

const {
chain: { chain_name: currentChainName } = { chain_name: undefined },
} = useChainContextIfAvailable() ?? {}
const chainNames = getSupportedChains().map(({ chain }) => chain.chain_name)
const { connect, disconnect, isWalletConnecting } =
useChains(chainNames)[
// Use current chain if available, or just use first chain. Should not
// matter because connect/disconnect will sync to all chains, but in case
// the user only approves some chains and not others, we want to make sure
// the current chain is priority.
currentChainName || chainNames[0]
]

return (
<Tooltip
Expand Down
15 changes: 2 additions & 13 deletions packages/stateful/components/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { wallets as leapWallets } from '@cosmos-kit/leap'
import { wallets as leapMetamaskWallets } from '@cosmos-kit/leap-metamask-cosmos-snap'
import { wallets as okxWallets } from '@cosmos-kit/okxwallet'
import { wallets as omniWallets } from '@cosmos-kit/omni'
import { ChainProvider, walletContext } from '@cosmos-kit/react-lite'
import { ChainProvider } from '@cosmos-kit/react-lite'
import { wallets as shellWallets } from '@cosmos-kit/shell'
import { wallets as stationWallets } from '@cosmos-kit/station'
import { wallets as trustWallets } from '@cosmos-kit/trust'
Expand All @@ -24,7 +24,6 @@ import {
PropsWithChildren,
ReactNode,
SetStateAction,
useContext,
useEffect,
useMemo,
} from 'react'
Expand Down Expand Up @@ -213,17 +212,7 @@ export const WalletProvider = ({
const InnerWalletProvider = ({ children }: PropsWithChildren<{}>) => {
useSyncWalletSigner()

const { isWalletDisconnected, chain, walletRepo } = useWallet()
// Re-run account restore logic on wallet chain switch to ensure connected.
const { walletManager } = useContext(walletContext)
useEffect(() => {
if (isWalletDisconnected) {
// @ts-ignore
walletManager._restoreAccounts()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chain.chain_id, walletManager])

const { isWalletDisconnected, walletRepo } = useWallet()
// Auto-connect to Keplr mobile web if in that context.
const isKeplrMobileWeb = useRecoilValue(isKeplrMobileWebAtom)
useEffect(() => {
Expand Down
7 changes: 3 additions & 4 deletions packages/stateful/hooks/useCfWorkerAuthPostRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useWallet } from './useWallet'
// Cloudflare KV is slow to update, so keep track of the last successful nonce
// that worked so we don't have to wait for the nonce query to update. Make this
// a global variable so it persists across all hook uses.
const lastSuccessfulNonceForType: Record<string, number | undefined> = {}
const lastSuccessfulNonceForApi: Record<string, number | undefined> = {}

export const useCfWorkerAuthPostRequest = (
apiBase: string,
Expand Down Expand Up @@ -57,8 +57,7 @@ export const useCfWorkerAuthPostRequest = (

// If nonce was already used, manually increment.
let nonce = nonceResponse.nonce
const lastSuccessfulNonce =
lastSuccessfulNonceForType[signatureType] ?? -1
const lastSuccessfulNonce = lastSuccessfulNonceForApi[apiBase] ?? -1
if (nonce <= lastSuccessfulNonce) {
nonce = lastSuccessfulNonce + 1
}
Expand Down Expand Up @@ -134,7 +133,7 @@ export const useCfWorkerAuthPostRequest = (
}

// If succeeded, store nonce.
lastSuccessfulNonceForType[signatureType] = nonce
lastSuccessfulNonceForApi[apiBase] = nonce

// If response OK, return response body.
return await response.json()
Expand Down
5 changes: 2 additions & 3 deletions packages/stateful/recoil/selectors/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
STARGAZE_NAMES_CONTRACT,
getChainForChainId,
getFallbackImage,
toBech32Hash,
transformBech32Address,
} from '@dao-dao/utils'

Expand Down Expand Up @@ -100,7 +99,7 @@ export const makeDefaultWalletProfileData = (
loading,
address,
profile: { ...EMPTY_WALLET_PROFILE },
backupImageUrl: getFallbackImage(),
backupImageUrl: getFallbackImage(address),
})

// This selector returns the profile for a wallet with some helpful metadata,
Expand Down Expand Up @@ -135,7 +134,7 @@ export const walletProfileDataSelector = selectorFamily<
profile.nft = pfpkProfile.nft
}

const backupImageUrl = getFallbackImage(toBech32Hash(address))
const backupImageUrl = getFallbackImage(address)

// Set `imageUrl` to PFPK image, defaulting to fallback image.
profile.imageUrl = pfpkProfile?.nft?.imageUrl || backupImageUrl
Expand Down
3 changes: 1 addition & 2 deletions packages/stateless/components/EntityDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
concatAddressStartEnd,
getFallbackImage,
toAccessibleImageUrl,
toBech32Hash,
} from '@dao-dao/utils'

import { useChainContext, useDaoNavHelpers, useDetectTruncate } from '../hooks'
Expand Down Expand Up @@ -102,7 +101,7 @@ export const EntityDisplay = ({
style={{
backgroundImage: `url(${
loadingEntity.loading
? getFallbackImage(toBech32Hash(address))
? getFallbackImage(address)
: toAccessibleImageUrl(loadingEntity.data.imageUrl)
})`,
width: imageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { VoteDisplay } from '@dao-dao/stateful/proposal-module-adapter/adapters/
import { CHAIN_ID } from '@dao-dao/storybook'
import { EntityType } from '@dao-dao/types'
import { Vote } from '@dao-dao/types/contracts/DaoProposalSingle.common'
import { getFallbackImage, toBech32Hash } from '@dao-dao/utils'
import { getFallbackImage } from '@dao-dao/utils'

import { EntityDisplay } from '../EntityDisplay'
import { ProposalVotes, ProposalVotesProps } from './ProposalVotes'
Expand Down Expand Up @@ -45,7 +45,7 @@ export const makeProps = (): ProposalVotesProps<Vote> => ({
chainId: CHAIN_ID,
address: props.address,
name: null,
imageUrl: getFallbackImage(toBech32Hash(props.address)),
imageUrl: getFallbackImage(props.address),
},
}}
{...props}
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/getFallbackImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ChainId } from '@dao-dao/types'

import { getImageUrlForChainId } from './chain'
import { NEUTRON_GOVERNANCE_DAO } from './constants'
import { toBech32Hash } from './conversion'

// fallback images in the public/placeholders directory.
export const getFallbackImage = (identifier = '') => {
Expand All @@ -13,6 +14,10 @@ export const getFallbackImage = (identifier = '') => {
return getImageUrlForChainId(ChainId.NeutronMainnet)
}

// If identitifer is an address, get its bech32 data, so it's consistent
// across chains.
identifier = toBech32Hash(identifier) || identifier

const hashed = identifier.split('').reduce((p, n) => n.charCodeAt(0) + p, 0)
const index = (hashed % 5) + 1
return `/placeholders/${index}.svg`
Expand Down
Loading