Skip to content

Commit

Permalink
get all proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Oct 16, 2023
1 parent a87ebd1 commit acd05c9
Show file tree
Hide file tree
Showing 2 changed files with 44 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

0 comments on commit acd05c9

Please sign in to comment.