Skip to content

Commit

Permalink
best and compatibility token
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut committed Oct 9, 2024
1 parent 7798af0 commit 5804978
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 99 deletions.
2 changes: 0 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
"@mui/styled-engine": "^5.15.14",
"@polkadot-api/descriptors": "portal:.papi/descriptors",
"@polkadot/api": "^13.1.1",
"@polkadot/api-augment": "^13.1.1",
"@polkadot/extension-dapp": "^0.52.3",
"@polkadot/react-identicon": "^3.10.1",
"@polkadot/types": "^13.1.1",
"@polkadot/util-crypto": "^13.1.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/components/modals/ProposalSigning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ const ProposalSigning = ({
// In case the tx has been approved between the last couple blocks
// and the tx in the indexer hasn't been updated we should query the latest state
// right before sending the tx to have the right amount of signers.
const callStorage = await api.query.Multisig.Multisigs.getEntries(multisig.address)
const callStorage = await api.query.Multisig.Multisigs.getEntries(multisig.address, {
at: 'best'
})

callStorage.some((storage) => {
const hash = storage.keyArgs[1].asHex()
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/components/modals/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ const Send = ({ onClose, className, onSuccess, onFinalized, preselected }: Props

setIsSubmitting(true)

multisigTx.signSubmitAndWatch(selectedAccount.polkadotSigner).subscribe(signCallback)
multisigTx
.signSubmitAndWatch(selectedAccount.polkadotSigner, { at: 'best' })
.subscribe(signCallback)
}, [threshold, api, selectedOrigin, extrinsicToCall, multisigTx, selectedAccount, signCallback])

const onChangeEasySetupOption: (event: SelectChangeEvent<unknown>) => void = useCallback(
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/src/components/modals/WalletConnectSigning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ const ProposalSigning = ({ onClose, className, request, onSuccess }: SigningModa

setIsSubmitting(true)

multisigTx.signSubmitAndWatch(selectedAccount.polkadotSigner).subscribe(signCallback)
multisigTx
.signSubmitAndWatch(selectedAccount.polkadotSigner, { at: 'best' })
.subscribe(signCallback)
}, [threshold, api, originAddress, extrinsicToCall, multisigTx, selectedAccount, signCallback])

const handleMultisigSelection = useCallback(
Expand Down
7 changes: 3 additions & 4 deletions packages/ui/src/contexts/ApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react'
import { useState, useEffect, createContext, useContext } from 'react'
import { useNetwork } from './NetworkContext'
import { ethereumChains } from '../utils/ethereumChains'
import '@polkadot/api-augment'
import { createClient, PolkadotClient, TypedApi } from 'polkadot-api'
import { getWsProvider } from 'polkadot-api/ws-provider/web'
import {
Expand Down Expand Up @@ -138,9 +137,9 @@ const ApiContextProvider = ({ children }: ApiContextProps) => {
if (!client || !api) return

client?.getChainSpecData().then(async ({ properties, name }) => {
if (!properties) return
if (!properties || !compatibilityToken) return

const ss58prefix = await api.constants.System.SS58Prefix()
const ss58prefix = api.constants.System.SS58Prefix(compatibilityToken)
const tokenDecimals = Array.isArray(properties?.tokenDecimals)
? properties?.tokenDecimals[0]
: properties?.tokenDecimals
Expand All @@ -159,7 +158,7 @@ const ApiContextProvider = ({ children }: ApiContextProps) => {
isEthereum
})
})
}, [client, api])
}, [client, api, compatibilityToken])

useEffect(() => {
if (!api) return
Expand Down
26 changes: 18 additions & 8 deletions packages/ui/src/contexts/PeopleChainApiContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { useState, useEffect, createContext, useContext } from 'react'
import { useNetwork } from './NetworkContext'
import { createClient, PolkadotClient, TypedApi } from 'polkadot-api'
import { CompatibilityToken, createClient, PolkadotClient, TypedApi } from 'polkadot-api'
import { getWsProvider } from 'polkadot-api/ws-provider/web'
import { dotPpl, ksmPpl, pasPpl, wesPpl } from '@polkadot-api/descriptors'

Expand All @@ -14,7 +14,8 @@ type ApiContextProps = {
export interface IApiContext {
pplApi?: false | PplApiType
pplChainInfo?: ChainInfoHuman
client?: PolkadotClient
pplClient?: PolkadotClient
pplCompatibilityToken?: CompatibilityToken
}

export interface ChainInfoHuman {
Expand All @@ -35,7 +36,15 @@ const PplApiContextProvider = ({ children }: ApiContextProps) => {
const { selectedNetworkInfo } = useNetwork()
const [pplChainInfo, setPplChainInfo] = useState<ChainInfoHuman | undefined>()
const [pplApi, setPplApi] = useState<IApiContext['pplApi']>()
const [client, setClient] = useState<IApiContext['client']>()
const [client, setClient] = useState<IApiContext['pplClient']>()
const [compatibilityToken, setCompatibilityToken] =
useState<IApiContext['pplCompatibilityToken']>()

useEffect(() => {
if (!pplApi) return

pplApi.compatibilityToken.then(setCompatibilityToken).catch(console.error)
}, [pplApi])

useEffect(() => {
if (!selectedNetworkInfo?.pplChainRpcUrl) return
Expand Down Expand Up @@ -72,9 +81,9 @@ const PplApiContextProvider = ({ children }: ApiContextProps) => {
if (!client || !pplApi) return

client?.getChainSpecData().then(async ({ properties }) => {
if (!properties) return
if (!properties || !compatibilityToken) return

const ss58prefix = await pplApi.constants.System.SS58Prefix()
const ss58prefix = pplApi.constants.System.SS58Prefix(compatibilityToken)
const tokenDecimals = Array.isArray(properties?.tokenDecimals)
? properties?.tokenDecimals[0]
: properties?.tokenDecimals
Expand All @@ -90,14 +99,15 @@ const PplApiContextProvider = ({ children }: ApiContextProps) => {
tokenSymbol: tokensymbol || ''
})
})
}, [client, pplApi])
}, [client, compatibilityToken, pplApi])

return (
<PplApiContext.Provider
value={{
client,
pplClient: client,
pplApi,
pplChainInfo: pplChainInfo
pplChainInfo,
pplCompatibilityToken: compatibilityToken
}}
>
{children}
Expand Down
23 changes: 6 additions & 17 deletions packages/ui/src/hooks/useMultisigProposalNeededFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,22 @@ interface Props {
}

export const useMultisigProposalNeededFunds = ({ threshold, signatories, call }: Props) => {
const { api, chainInfo } = useApi()
const { api, chainInfo, compatibilityToken } = useApi()
const [min, setMin] = useState(0n)
const [reserved, setReserved] = useState(0n)
const [multisigDepositFactor, setMultisigDepositFactor] = useState<bigint | undefined>(undefined)
const [multisigDepositBase, setMultisigDepositBase] = useState<bigint | undefined>(undefined)

useEffect(() => {
if (!api) return

api.constants.Multisig.DepositBase().then(setMultisigDepositBase).catch(console.error)
}, [api])

useEffect(() => {
if (!api) return

api.constants.Multisig.DepositFactor().then(setMultisigDepositFactor).catch(console.error)
}, [api])

useEffect(() => {
if (!api || !signatories || signatories.length < 2) return
if (!api || !signatories || signatories.length < 2 || !compatibilityToken) return

if (!chainInfo?.tokenDecimals) return

if (!threshold) return

if (!call) return

const multisigDepositBase = api.constants.Multisig.DepositBase(compatibilityToken)
const multisigDepositFactor = api.constants.Multisig.DepositFactor(compatibilityToken)

if (!multisigDepositFactor || !multisigDepositBase) return

call
Expand All @@ -46,7 +35,7 @@ export const useMultisigProposalNeededFunds = ({ threshold, signatories, call }:
setReserved(reservedTemp)
})
.catch(console.error)
}, [api, call, chainInfo, multisigDepositBase, multisigDepositFactor, signatories, threshold])
}, [api, call, chainInfo, compatibilityToken, signatories, threshold])

return { multisigProposalNeededFunds: min, reserved }
}
2 changes: 1 addition & 1 deletion packages/ui/src/hooks/usePendingTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export const usePendingTx = (multisigAddresses: string[], skipProxyCheck = false
const pendingMultisigTxs: PendingTx[] = []

const callsPromises = multisigAddresses.map((address) =>
api.query.Multisig.Multisigs.getEntries(address)
api.query.Multisig.Multisigs.getEntries(address, { at: 'best' })
)

await Promise.all(callsPromises)
Expand Down
49 changes: 15 additions & 34 deletions packages/ui/src/hooks/usePureProxyCreationNeededFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,24 @@ import { TypedApi } from 'polkadot-api'
import { dot } from '@polkadot-api/descriptors'

export const usePureProxyCreationNeededFunds = () => {
const { api, chainInfo } = useApi()
const { api, chainInfo, compatibilityToken } = useApi()
const [min, setMin] = useState(0n)
const [reserved, setReserved] = useState(0n)
const [depositFactor, setDepositFactor] = useState<bigint | undefined>(undefined)
const [depositBase, setDepositBase] = useState<bigint | undefined>(undefined)
const [existentialDeposit, setExistentialDeposit] = useState<bigint | undefined>(undefined)

useEffect(() => {
if (!api) return

if (!(api as TypedApi<typeof dot>).constants?.Proxy?.ProxyDepositFactor) return
;(api as TypedApi<typeof dot>).constants.Proxy.ProxyDepositFactor()
.then(setDepositFactor)
.catch(console.error)
}, [api])

useEffect(() => {
if (!api) return

if (!(api as TypedApi<typeof dot>).constants?.Proxy?.ProxyDepositBase) return
;(api as TypedApi<typeof dot>).constants.Proxy.ProxyDepositBase()
.then(setDepositBase)
.catch(console.error)
}, [api])

useEffect(() => {
if (!api) return

if (!(api as TypedApi<typeof dot>).constants?.Balances.ExistentialDeposit) return
;(api as TypedApi<typeof dot>).constants.Balances.ExistentialDeposit()
.then(setExistentialDeposit)
.catch(console.error)
}, [api])

useEffect(() => {
if (!api || !existentialDeposit || !depositBase || !depositFactor) return

if (!api || !compatibilityToken) return

const existentialDeposit = (api as TypedApi<typeof dot>).constants.Balances.ExistentialDeposit(
compatibilityToken
)
const depositBase = (api as TypedApi<typeof dot>).constants.Proxy.ProxyDepositBase(
compatibilityToken
)
const depositFactor = (api as TypedApi<typeof dot>).constants.Proxy.ProxyDepositFactor(
compatibilityToken
)

if (!existentialDeposit || !depositBase || !depositFactor) return
// if (!chainInfo?.tokenDecimals) return

// we only create one proxy here
Expand All @@ -53,7 +34,7 @@ export const usePureProxyCreationNeededFunds = () => {
setReserved(reserved)
setMin(reserved + survive)
// console.log('reserved Pure Creation', formatBnBalance(reserved.add(survive), chainInfo.tokenDecimals, { tokenSymbol: chainInfo?.tokenSymbol, numberAfterComma: 3 }))
}, [api, chainInfo, depositBase, depositFactor, existentialDeposit])
}, [api, chainInfo, compatibilityToken])

return { pureProxyCreationNeededFunds: min, reserved }
}
42 changes: 12 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3462,7 +3462,7 @@ __metadata:
languageName: node
linkType: hard

"@polkadot/api-augment@npm:13.1.1, @polkadot/api-augment@npm:^13.1.1":
"@polkadot/api-augment@npm:13.1.1":
version: 13.1.1
resolution: "@polkadot/api-augment@npm:13.1.1"
dependencies:
Expand Down Expand Up @@ -3589,23 +3589,25 @@ __metadata:
languageName: node
linkType: hard

"@polkadot/extension-dapp@npm:^0.52.3":
version: 0.52.3
resolution: "@polkadot/extension-dapp@npm:0.52.3"
"@polkadot/extension-inject@npm:^0.48.1":
version: 0.48.2
resolution: "@polkadot/extension-inject@npm:0.48.2"
dependencies:
"@polkadot/extension-inject": 0.52.3
"@polkadot/util": ^13.0.2
"@polkadot/util-crypto": ^13.0.2
"@polkadot/api": ^12.0.2
"@polkadot/rpc-provider": ^12.0.2
"@polkadot/types": ^12.0.2
"@polkadot/util": ^12.6.2
"@polkadot/util-crypto": ^12.6.2
"@polkadot/x-global": ^12.6.2
tslib: ^2.6.2
peerDependencies:
"@polkadot/api": "*"
"@polkadot/util": "*"
"@polkadot/util-crypto": "*"
checksum: fa6930f35764abb5e443ffeca833854d5dabf7e3bba1c3f7551f40ba02dd9229f88c7b61dd52774876e81ed36f9516245a899098a2c7a179859728bc3124ca9e
checksum: c8281942bf30f31c9b5388475483197e023a113da839b9144d8c558124728e7edd48b305e9f2d7f9674a3f0b6517571b6993eb0f4d40157a796bd772094e7ca3
languageName: node
linkType: hard

"@polkadot/extension-inject@npm:0.52.3, @polkadot/extension-inject@npm:^0.52.3":
"@polkadot/extension-inject@npm:^0.52.3":
version: 0.52.3
resolution: "@polkadot/extension-inject@npm:0.52.3"
dependencies:
Expand All @@ -3623,24 +3625,6 @@ __metadata:
languageName: node
linkType: hard

"@polkadot/extension-inject@npm:^0.48.1":
version: 0.48.2
resolution: "@polkadot/extension-inject@npm:0.48.2"
dependencies:
"@polkadot/api": ^12.0.2
"@polkadot/rpc-provider": ^12.0.2
"@polkadot/types": ^12.0.2
"@polkadot/util": ^12.6.2
"@polkadot/util-crypto": ^12.6.2
"@polkadot/x-global": ^12.6.2
tslib: ^2.6.2
peerDependencies:
"@polkadot/api": "*"
"@polkadot/util": "*"
checksum: c8281942bf30f31c9b5388475483197e023a113da839b9144d8c558124728e7edd48b305e9f2d7f9674a3f0b6517571b6993eb0f4d40157a796bd772094e7ca3
languageName: node
linkType: hard

"@polkadot/keyring@npm:^12.6.2":
version: 12.6.2
resolution: "@polkadot/keyring@npm:12.6.2"
Expand Down Expand Up @@ -10877,8 +10861,6 @@ __metadata:
"@mui/styled-engine": ^5.15.14
"@polkadot-api/descriptors": "portal:.papi/descriptors"
"@polkadot/api": ^13.1.1
"@polkadot/api-augment": ^13.1.1
"@polkadot/extension-dapp": ^0.52.3
"@polkadot/extension-inject": ^0.52.3
"@polkadot/react-identicon": ^3.10.1
"@polkadot/typegen": ^13.1.1
Expand Down

0 comments on commit 5804978

Please sign in to comment.