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

Add try catch to toSubsocialAddress calls #671

Merged
merged 3 commits into from
May 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { Skeleton } from '@/components/SkeletonFallback'
import Toast from '@/components/Toast'
import { useSendEvent } from '@/stores/analytics'
import { useMyAccount } from '@/stores/my-account'
import { Signer, truncateAddress } from '@/utils/account'
import { toSubsocialAddress } from '@subsocial/utils'
import {
Signer,
convertAddressToSubsocialAddress,
truncateAddress,
} from '@/utils/account'
import dynamic from 'next/dynamic'
import Image from 'next/image'
import toast from 'react-hot-toast'
Expand Down Expand Up @@ -40,7 +43,7 @@ export default function PolkadotConnectAccountContent({
const avatar = (account as any).avatar
let address = ''
try {
address = toSubsocialAddress(account.address)!
address = convertAddressToSubsocialAddress(account.address)!
} catch {}
if (!address) return null

Expand All @@ -49,7 +52,9 @@ export default function PolkadotConnectAccountContent({
<span className='flex flex-1 items-center justify-between gap-4'>
<span>{account.name || account.address}</span>
<span className='whitespace-nowrap font-mono text-text-muted'>
{truncateAddress(toSubsocialAddress(account.address)!)}
{truncateAddress(
convertAddressToSubsocialAddress(account.address)!
)}
</span>
</span>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { getProxiesQuery } from '@/services/subsocial/proxy/query'
import { useSendEvent } from '@/stores/analytics'
import { useLoginModal } from '@/stores/login-modal'
import { useMyAccount } from '@/stores/my-account'
import { convertAddressToSubsocialAddress } from '@/utils/account'
import { estimatedWaitTime } from '@/utils/network'
import { IdentityProvider } from '@subsocial/data-hub-sdk'
import { toSubsocialAddress } from '@subsocial/utils'
import { useState } from 'react'
import { PolkadotConnectContentProps } from './types'

Expand Down Expand Up @@ -120,7 +120,9 @@ export default function PolkadotConnectConfirmationContent({
className='w-full'
size='lg'
onClick={async () => {
const address = toSubsocialAddress(connectedWallet?.address)
const address = convertAddressToSubsocialAddress(
connectedWallet?.address
)
const signer = connectedWallet?.signer
if (address && signer) {
setIsProcessing(true)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ApiResponse, handlerWrapper } from '@/server/common'
import { generateGetDataFromSquidWithBlockchainFallback } from '@/server/squid'
import { isDatahubAvailable } from '@/services/datahub/utils'
import { getPostsFromSubsocial } from '@/services/subsocial/posts/fetcher'
import { convertAddressToSubsocialAddress } from '@/utils/account'
import { getUrlFromText } from '@/utils/strings'
import { LinkMetadata, PostData } from '@subsocial/api/types'
import { toSubsocialAddress } from '@subsocial/utils'
import { parser } from 'html-metadata-parser'
import { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function getPostsServer(postIds: string[]): Promise<PostData[]> {

const linksToFetch = new Set<string>()
posts.forEach((post) => {
post.struct.ownerId = toSubsocialAddress(post.struct.ownerId)!
post.struct.ownerId = convertAddressToSubsocialAddress(post.struct.ownerId)!
const link = post.content?.link
const shouldLinkBeFetched =
link && link.startsWith('https') && !canRenderEmbed(link)
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SubsocialProfile,
getProfilesFromSubsocial,
} from '@/services/subsocial/profiles/fetcher'
import { toSubsocialAddress } from '@subsocial/utils'
import { convertAddressToSubsocialAddress } from '@/utils/account'
import { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'

Expand Down Expand Up @@ -81,7 +81,7 @@ export const getProfilesServer = generateGetDataFromSquidWithBlockchainFallback(
{ paramToId: (param) => param, responseToId: (response) => response.address },
{
blockchainResponse: (data) => {
data.address = toSubsocialAddress(data.address)!
data.address = convertAddressToSubsocialAddress(data.address)!
},
},
getInvalidatedProfileRedisKey
Expand Down
12 changes: 7 additions & 5 deletions src/providers/ConnectWalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import {
useMyMainAddress,
} from '@/stores/my-account'
import { WalletAccount } from '@/subsocial-query/subsocial/types'
import { Signer } from '@/utils/account'
import { Signer, convertAddressToSubsocialAddress } from '@/utils/account'
import { cx } from '@/utils/class-names'
import { generateManuallyTriggeredPromise } from '@/utils/promise'
import { toSubsocialAddress } from '@subsocial/utils'
import {
ReactNode,
createContext,
Expand Down Expand Up @@ -46,7 +45,9 @@ export function ConnectWalletProvider({ children }: { children: ReactNode }) {
const requestWalletAccount = useCallback(async () => {
const firstCheckAccounts = await enableWalletOnce()
const found = firstCheckAccounts.find(
(account) => toSubsocialAddress(account.address) === getMyMainAddress()
(account) =>
convertAddressToSubsocialAddress(account.address) ===
getMyMainAddress() ?? ''
)

if (found) return { address: found.address, signer: found.signer as Signer }
Expand All @@ -58,7 +59,8 @@ export function ConnectWalletProvider({ children }: { children: ReactNode }) {
await getPromise()
const accounts = await enableWalletOnce()
const foundAcc = accounts.find(
(account) => toSubsocialAddress(account.address) === getMyMainAddress()
(account) =>
convertAddressToSubsocialAddress(account.address) === getMyMainAddress()
)
return foundAcc
? { address: foundAcc.address, signer: foundAcc.signer as Signer }
Expand Down Expand Up @@ -99,7 +101,7 @@ function ConnectWalletModal({ ...props }: ModalFunctionalityProps) {
)

const isFound = accounts?.find(
(account) => toSubsocialAddress(account.address) === myAddress
(account) => convertAddressToSubsocialAddress(account.address) === myAddress
)

return (
Expand Down
10 changes: 6 additions & 4 deletions src/server/opengov/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ProposalStatus from '@/components/opengov/ProposalStatus'
import { getProposalResourceId } from '@/modules/opengov/ProposalDetailPage/context'
import { getDiscussion } from '@/pages/api/discussion'
import { toSubsocialAddress } from '@subsocial/utils'
import { convertAddressToSubsocialAddress } from '@/utils/account'
import { POLKADOT_BLOCK_TIME, getProposalPeriods } from './data'

export type ProposalStatus =
Expand Down Expand Up @@ -220,13 +220,15 @@ export async function mapSubsquareProposalToProposal(
)
const decisionDeposit = proposal.onchainData?.info?.decisionDeposit ?? null
if (decisionDeposit) {
decisionDeposit.who = toSubsocialAddress(decisionDeposit.who)!
decisionDeposit.who = convertAddressToSubsocialAddress(decisionDeposit.who)!
}

const submissionDeposit =
proposal.onchainData?.info?.submissionDeposit ?? null
if (submissionDeposit) {
submissionDeposit.who = toSubsocialAddress(submissionDeposit.who)!
submissionDeposit.who = convertAddressToSubsocialAddress(
submissionDeposit.who
)!
}

const trackInfo = proposal.onchainData.trackInfo ?? null
Expand All @@ -236,7 +238,7 @@ export async function mapSubsquareProposalToProposal(
comments: mapComments(proposal, comments),
chatId,
beneficiary: proposal.onchainData.treasuryInfo?.beneficiary ?? '',
proposer: toSubsocialAddress(proposal.proposer)!,
proposer: convertAddressToSubsocialAddress(proposal.proposer)!,
requested: BigInt(
proposal.onchainData.treasuryInfo?.amount ?? '0'
).toString(),
Expand Down
4 changes: 2 additions & 2 deletions src/services/subsocial/proxy/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createSubsocialQuery,
SubsocialQueryData,
} from '@/subsocial-query/subsocial/query'
import { toSubsocialAddress } from '@subsocial/utils'
import { convertAddressToSubsocialAddress } from '@/utils/account'

async function getProxies({
api,
Expand All @@ -16,7 +16,7 @@ async function getProxies({
if (Array.isArray(proxyData)) {
const data = proxyData[0] as any
return {
address: toSubsocialAddress(data?.delegate)!,
address: convertAddressToSubsocialAddress(data?.delegate)!,
proxyType: data?.proxyType,
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/stores/my-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useParentData } from '@/stores/parent'
import { getSubsocialApi } from '@/subsocial-query/subsocial/connection'
import {
Signer,
convertAddressToSubsocialAddress,
decodeSecretKey,
encodeSecretKey,
generateAccount,
Expand All @@ -26,7 +27,6 @@ import { currentNetwork } from '@/utils/network'
import { wait } from '@/utils/promise'
import { LocalStorage, LocalStorageAndForage } from '@/utils/storage'
import { isWebNotificationsEnabled } from '@/utils/window'
import { toSubsocialAddress } from '@subsocial/utils'
import { Wallet, WalletAccount, getWallets } from '@talismn/connect-wallets'
import dayjs from 'dayjs'
import toast from 'react-hot-toast'
Expand Down Expand Up @@ -194,8 +194,7 @@ const useMyAccountBase = create<State & Actions>()((set, get) => ({
})
},
connectWallet: async (address, signer) => {
const { toSubsocialAddress } = await import('@subsocial/utils')
const parsedAddress = toSubsocialAddress(address)!
const parsedAddress = convertAddressToSubsocialAddress(address)!

set({ connectedWallet: { address: parsedAddress, signer } })
get()._subscribeConnectedWalletEnergy()
Expand All @@ -217,7 +216,6 @@ const useMyAccountBase = create<State & Actions>()((set, get) => ({
isInitialization,
withErrorToast = true,
} = config || {}
const { toSubsocialAddress } = await import('@subsocial/utils')
const analytics = useAnalytics.getState()
let address: string = ''
try {
Expand All @@ -244,7 +242,7 @@ const useMyAccountBase = create<State & Actions>()((set, get) => ({

const signer = await loginWithSecretKey(secretKey)
const encodedSecretKey = encodeSecretKey(secretKey)
address = toSubsocialAddress(signer.address)!
address = convertAddressToSubsocialAddress(signer.address)!

set({
address,
Expand Down Expand Up @@ -447,8 +445,8 @@ async function linkPolkadotIfNotLinked(
) {
const linkedAddress = await getParentProxyAddress(address)
if (
toSubsocialAddress(linkedAddress ?? '')! ===
toSubsocialAddress(parentProxyAddress)!
convertAddressToSubsocialAddress(linkedAddress ?? '')! ===
convertAddressToSubsocialAddress(parentProxyAddress)!
)
return

Expand Down Expand Up @@ -678,7 +676,8 @@ export function useGetCurrentSigner(): () => Promise<WalletSigner | undefined> {
return {
signRaw: ({ address, data }) => {
if (
toSubsocialAddress(signer.address) !== toSubsocialAddress(address)
convertAddressToSubsocialAddress(signer.address) !==
convertAddressToSubsocialAddress(address)
)
throw new Error('Invalid address')

Expand Down
10 changes: 10 additions & 0 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMyAccount } from '@/stores/my-account'
import type { Keyring } from '@polkadot/keyring'
import { GenericAccountId } from '@polkadot/types'
import registry from '@subsocial/api/utils/registry'
import { toSubsocialAddress } from '@subsocial/utils'

export type Signer = ReturnType<Keyring['addFromSeed']>

Expand Down Expand Up @@ -95,3 +96,12 @@ export function convertAddressToGenericAddress(address?: string) {
} catch {}
return genericAccountAddress
}

export function convertAddressToSubsocialAddress(address?: string) {
if (!address) return ''
let subsocialAddress = ''
try {
subsocialAddress = toSubsocialAddress(address ?? '')!
} catch {}
return subsocialAddress
}
Loading