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

Centrifuge App: Get all proxies #1637

Merged
merged 2 commits into from
Oct 16, 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
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