Skip to content

Commit

Permalink
Remove proxy if proxy type is Any
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Mar 6, 2024
1 parent e61b16a commit b53281a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/components/donate/LazyTxButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
showErrorMessage,
showSuccessMessage,
} from 'src/components/utils/Message'
import config from 'src/config'
import { AnyAccountId } from 'src/types'
import { getCurrentWallet } from '../auth/utils'
import { useResponsiveSize } from '../responsive/ResponsiveContext'
Expand All @@ -39,6 +40,8 @@ type FailedMessage = Message | FailedMessageFn

export type BaseTxButtonProps = Omit<ButtonProps, 'onClick' | 'form'>

const { appName } = config

export type TxButtonProps = BaseTxButtonProps & {
accountId?: AnyAccountId
network: string
Expand Down Expand Up @@ -202,6 +205,7 @@ function LazyTxButton({
} else {
const currentWallet = getCurrentWallet()
const wallet = getWalletBySource(currentWallet)
await wallet?.enable(appName)
signer = wallet?.signer
}

Expand Down
29 changes: 24 additions & 5 deletions src/stores/my-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,24 @@ export const useMyAccount = create<State & Actions>()((set, get) => ({
if (parentProxyAddress) {
set({ parentProxyAddress })
try {
const proxy = await getProxies(parentProxyAddress)
const isProxyValid = proxy.includes(get().address ?? '')
if (!isProxyValid) {
// Remove proxy with type 'Any'
const proxies = await getProxies(parentProxyAddress)
const currentProxy = proxies.find(({ address }) => address === get().address)
if (currentProxy?.proxyType === 'Any') {
async function removeProxy() {
const api = getSubsocialApi()
const substrateApi = await api.substrateApi
await substrateApi.tx.proxy
.proxy(parentProxyAddress!, null, substrateApi.tx.proxy.removeProxies())
.signAndSend(get().signer!)
}
removeProxy()

parentProxyAddressStorage.remove()
set({ parentProxyAddress: undefined })
get().logout()
alert('Sorry we had to remove your proxy, please relogin to use your account again.')
} else if (!currentProxy) {
parentProxyAddressStorage.remove()
set({ parentProxyAddress: undefined })
get().logout()
Expand Down Expand Up @@ -192,11 +207,15 @@ async function getProxies(address: string) {
.map(proxy => {
const proxyData = proxy.toPrimitive()
if (Array.isArray(proxyData)) {
return toSubsocialAddress((proxyData[0] as any)?.delegate)!
const data = proxyData[0] as any
return {
address: toSubsocialAddress(data?.delegate)!,
proxyType: data?.proxyType,
}
}
return null
})
.filter(Boolean) as string[]
.filter(Boolean) as { address: string; proxyType: string }[]
}

async function subscribeEnergy(
Expand Down

0 comments on commit b53281a

Please sign in to comment.