Skip to content

Commit

Permalink
Centrifuge App: Get all proxies (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored and gpmayorga committed Nov 23, 2023
1 parent c185323 commit c13979f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
7 changes: 6 additions & 1 deletion centrifuge-js/src/modules/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export function getProxiesModule(inst: CentrifugeBase) {
const $api = inst.getApi()
const $events = inst.getEvents().pipe(
filter(({ api, events }) => {
const event = events.find(({ event }) => api.events.proxy.PureCreated.is(event))
const event = events.find(
({ event }) =>
api.events.proxy.PureCreated.is(event) ||
api.events.proxy.PureAdded.is(event) ||
api.events.proxy.PureRemoved.is(event)
)
return !!event
})
)
Expand Down
83 changes: 38 additions & 45 deletions centrifuge-react/src/components/WalletProvider/WalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as React from 'react'
import { useQuery } from 'react-query'
import { firstValueFrom, map, switchMap } from 'rxjs'
import { ReplacedError, useAsyncCallback } from '../../hooks/useAsyncCallback'
import { useCentrifugeQuery } from '../../hooks/useCentrifugeQuery'
import { useCentrifuge, useCentrifugeApi, useCentrifugeConsts } from '../CentrifugeProvider'
import { EvmChains, getAddChainParameters, getEvmUrls } from './evm/chains'
import { EvmConnectorMeta, getEvmConnectors } from './evm/connectors'
Expand Down Expand Up @@ -191,56 +192,46 @@ export function WalletProvider({
},
[]
)

/*
['allProxies'],
() =>
firstValueFrom(cent.proxies.getAllProxies()).then((proxies) => {
return Object.fromEntries(
Object.entries(proxies).map(([delegatee, ps]) => [
utils.formatAddress(delegatee),
ps.map((p) => ({ ...p, delegator: utils.formatAddress(p.delegator) })),
])
)
}),
*/
const evmSubstrateAccounts = isEvmOnSubstrate
? state.evm.accounts?.map((addr) => ({
address: evmToSubstrateAddress(addr, centEvmChainId!),
source: state.evm.selectedWallet!.id,
wallet: state.evm.selectedWallet as any,
}))
: null
const { data: proxies, isLoading: proxiesAreLoading } = useQuery(
[
'proxies',
state.substrate.accounts?.map((acc) => acc.address),
state.substrate.multisigs.map((m) => m.address),
evmSubstrateAccounts?.map((acc) => acc.address),
],
() =>
firstValueFrom(
cent.proxies.getMultiUserProxies([
(state.substrate.accounts || [])
.map((acc) => acc.address)
.concat(state.substrate.multisigs.map((m) => m.address))
.concat(evmSubstrateAccounts?.map((acc) => acc.address) || []),
])
),
{
staleTime: Infinity,
}
)

const delegatees = [...new Set(Object.values(proxies ?? {})?.flatMap((p) => p.map((d) => d.delegator)))]
const { data: nestedProxies, isLoading: nestedProxiesAreLoading } = useQuery(
['nestedProxies', delegatees],
() => firstValueFrom(cent.proxies.getMultiUserProxies([delegatees])),
{
enabled: !!Object.keys(proxies ?? {})?.length,
staleTime: Infinity,
}
)
// const { data: proxies, isLoading: proxiesAreLoading } = useQuery(
// [
// 'proxies',
// state.substrate.accounts?.map((acc) => acc.address),
// state.substrate.multisigs.map((m) => m.address),
// evmSubstrateAccounts?.map((acc) => acc.address),
// ],
// () =>
// firstValueFrom(
// cent.proxies.getMultiUserProxies([
// (state.substrate.accounts || [])
// .map((acc) => acc.address)
// .concat(state.substrate.multisigs.map((m) => m.address))
// .concat(evmSubstrateAccounts?.map((acc) => acc.address) || []),
// ])
// ),
// {
// staleTime: Infinity,
// }
// )

// const delegatees = [...new Set(Object.values(proxies ?? {})?.flatMap((p) => p.map((d) => d.delegator)))]
// const { data: nestedProxies, isLoading: nestedProxiesAreLoading } = useQuery(
// ['nestedProxies', delegatees],
// () => firstValueFrom(cent.proxies.getMultiUserProxies([delegatees])),
// {
// enabled: !!Object.keys(proxies ?? {})?.length,
// staleTime: Infinity,
// }
// )

const [proxies] = useCentrifugeQuery(['allProxies'], (cent) => cent.proxies.getAllProxies())

function setFilteredAccounts(accounts: SubstrateAccount[]) {
const mappedAccounts = accounts
Expand Down Expand Up @@ -369,7 +360,9 @@ export function WalletProvider({
const [scopedNetworks, setScopedNetworks] = React.useState<WalletContextType['scopedNetworks']>(null)

const ctx: WalletContextType = React.useMemo(() => {
const combinedProxies = { ...proxies, ...nestedProxies }
const combinedProxies = {
...proxies,
}
const combinedSubstrateAccounts =
(evmSubstrateAccounts || state.substrate.accounts)?.flatMap((account) => {
const { address } = account
Expand Down Expand Up @@ -455,7 +448,7 @@ export function WalletProvider({
selectedProxies: selectedCombinedAccount?.proxies || null,
selectedMultisig: selectedCombinedAccount?.multisig || null,
proxies: combinedProxies,
proxiesAreLoading: nestedProxiesAreLoading || proxiesAreLoading,
proxiesAreLoading: !proxies,
subscanUrl,
},
evm: {
Expand All @@ -465,7 +458,7 @@ export function WalletProvider({
chains: evmChains,
},
}
}, [connect, disconnect, selectAccount, proxies, nestedProxies, state, isConnectError, isConnecting])
}, [connect, disconnect, selectAccount, proxies, state, isConnectError, isConnecting])

return (
<WalletContext.Provider value={ctx}>
Expand Down
3 changes: 3 additions & 0 deletions centrifuge-react/src/hooks/useCentrifugeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { PalletError } from '../utils/errors'

export type CentrifugeTransactionOptions = Pick<TransactionOptions, 'createType'> & {
account?: CombinedSubstrateAccount
// If a transaction can be done via a proxy other than Any, pass the allowed types here,
// to make sure the transaction selects the right one.
// Otherwise by default it will try the transaction with the Any proxy type
forceProxyType?: string | string[]
}

Expand Down

0 comments on commit c13979f

Please sign in to comment.