Skip to content

Commit 841a728

Browse files
committed
using type guards with 2 non hydration lists
1 parent f774003 commit 841a728

File tree

9 files changed

+160
-179
lines changed

9 files changed

+160
-179
lines changed

packages/ui/src/components/CallInfo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Expander from './Expander'
22
import { styled } from '@mui/material/styles'
33
import { ReactNode, useMemo } from 'react'
4-
import { IApiContext, useApi } from '../contexts/ApiContext'
4+
import { ApiDescriptors, IApiContext, useApi } from '../contexts/ApiContext'
55
import { getExtrinsicName } from '../utils/getExtrinsicName'
66
import { isProxyCall } from '../utils/isProxyCall'
77
import { formatBigIntBalance } from '../utils/formatBnBalance'
@@ -29,7 +29,7 @@ interface CreateTreeParams {
2929
decimals: number
3030
unit: string
3131
name?: string
32-
api: IApiContext['api']
32+
api: IApiContext<ApiDescriptors>['api']
3333
chainInfo?: ChainInfoHuman
3434
}
3535

packages/ui/src/components/modals/ChangeMultisig.tsx

Lines changed: 96 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import AccountDisplay from '../AccountDisplay/AccountDisplay'
1414
import ThresholdSelection from '../../pages/Creation/ThresholdSelection'
1515
import SignatorySelection from '../select/SignatorySelection'
1616
import Summary from '../../pages/Creation/Summary'
17-
import { useApi } from '../../contexts/ApiContext'
17+
import {
18+
isContextIn,
19+
isContextOf,
20+
noHydrationKeys_1,
21+
noHydrationKeys_2,
22+
useApi
23+
} from '../../contexts/ApiContext'
1824
import { useAccounts } from '../../contexts/AccountsContext'
1925
import { useSigningCallback } from '../../hooks/useSigningCallback'
2026
import { AccountBadge } from '../../types'
@@ -44,7 +50,8 @@ type Step = 'selection' | 'summary' | 'call1' | 'call2'
4450
const ChangeMultisig = ({ onClose, className }: Props) => {
4551
const { selectedNetwork } = useNetwork()
4652
const modalRef = useRef<HTMLDivElement | null>(null)
47-
const { api, chainInfo, compatibilityToken, apiDescriptor } = useApi()
53+
const ctx = useApi()
54+
const { api, chainInfo, compatibilityToken } = ctx
4855
const { selectedMultiProxy, getMultisigAsAccountBaseInfo, getMultisigByAddress } = useMultiProxy()
4956
const signCallBack2 = useSigningCallback({
5057
onSuccess: onClose,
@@ -89,7 +96,7 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
8996
const [callError, setCallError] = useState('')
9097

9198
const secondCall = useMemo(() => {
92-
if (!api || !compatibilityToken || !selectedNetwork) {
99+
if (!ctx.api || !compatibilityToken || !selectedNetwork) {
93100
// console.error('api is not ready')
94101
return
95102
}
@@ -122,79 +129,47 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
122129
selectedMultisig.signatories.filter((sig) => sig !== selectedAccount.address)
123130
)
124131

125-
const addProxyTx =
126-
apiDescriptor === 'hydration'
127-
? api.tx.Proxy.add_proxy({
128-
delegate: newMultisigAddress,
132+
const addProxyTx = isContextOf(ctx, 'hydration')
133+
? ctx.api.tx.Proxy.add_proxy({
134+
delegate: newMultisigAddress,
135+
proxy_type: Enum('Any'),
136+
delay: 0
137+
})
138+
: isContextIn(ctx, noHydrationKeys_1)
139+
? ctx.api.tx.Proxy.add_proxy({
140+
delegate: MultiAddress.Id(newMultisigAddress),
129141
proxy_type: Enum('Any'),
130142
delay: 0
131143
})
132-
: apiDescriptor === 'acala'
133-
? api.tx.Proxy.add_proxy({
134-
delegate: MultiAddress.Id(newMultisigAddress),
135-
proxy_type: Enum('Any'),
136-
delay: 0
137-
})
138-
: apiDescriptor === 'khala'
139-
? api.tx.Proxy.add_proxy({
140-
delegate: MultiAddress.Id(newMultisigAddress),
141-
proxy_type: Enum('Any'),
142-
delay: 0
143-
})
144-
: apiDescriptor === 'kusama'
145-
? api.tx.Proxy.add_proxy({
146-
delegate: MultiAddress.Id(newMultisigAddress),
147-
proxy_type: Enum('Any'),
148-
delay: 0
149-
})
150-
: apiDescriptor === 'phala'
151-
? api.tx.Proxy.add_proxy({
152-
delegate: MultiAddress.Id(newMultisigAddress),
153-
proxy_type: Enum('Any'),
154-
delay: 0
155-
})
156-
: api.tx.Proxy.add_proxy({
157-
delegate: MultiAddress.Id(newMultisigAddress),
158-
proxy_type: Enum('Any'),
159-
delay: 0
160-
})
161-
162-
const proxyTx =
163-
apiDescriptor === 'hydration'
164-
? api.tx.Proxy.proxy({
165-
real: selectedMultiProxy?.proxy,
144+
: isContextIn(ctx, noHydrationKeys_2) &&
145+
ctx.api.tx.Proxy.add_proxy({
146+
delegate: MultiAddress.Id(newMultisigAddress),
147+
proxy_type: Enum('Any'),
148+
delay: 0
149+
})
150+
151+
if (!addProxyTx) return
152+
153+
const proxyTx = isContextOf(ctx, 'hydration')
154+
? ctx.api.tx.Proxy.proxy({
155+
real: selectedMultiProxy?.proxy,
156+
force_proxy_type: undefined,
157+
call: addProxyTx.decodedCall
158+
})
159+
: isContextIn(ctx, noHydrationKeys_1)
160+
? ctx.api.tx.Proxy.proxy({
161+
real: MultiAddress.Id(selectedMultiProxy?.proxy),
162+
force_proxy_type: undefined,
163+
call: addProxyTx.decodedCall
164+
})
165+
: isContextIn(ctx, noHydrationKeys_2) &&
166+
ctx.api.tx.Proxy.proxy({
167+
real: MultiAddress.Id(selectedMultiProxy?.proxy),
166168
force_proxy_type: undefined,
167169
call: addProxyTx.decodedCall
168170
})
169-
: apiDescriptor === 'acala'
170-
? api.tx.Proxy.proxy({
171-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
172-
force_proxy_type: undefined,
173-
call: addProxyTx.decodedCall
174-
})
175-
: apiDescriptor === 'khala'
176-
? api.tx.Proxy.proxy({
177-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
178-
force_proxy_type: undefined,
179-
call: addProxyTx.decodedCall
180-
})
181-
: apiDescriptor === 'kusama'
182-
? api.tx.Proxy.proxy({
183-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
184-
force_proxy_type: undefined,
185-
call: addProxyTx.decodedCall
186-
})
187-
: apiDescriptor === 'phala'
188-
? api.tx.Proxy.proxy({
189-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
190-
force_proxy_type: undefined,
191-
call: addProxyTx.decodedCall
192-
})
193-
: api.tx.Proxy.proxy({
194-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
195-
force_proxy_type: undefined,
196-
call: addProxyTx.decodedCall
197-
})
171+
172+
if (!proxyTx) return
198173

199174
// call with the old multisig to delete the new one
200175
return getAsMultiTx({
@@ -205,22 +180,22 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
205180
compatibilityToken
206181
})
207182
}, [
208-
api,
209-
chainInfo,
183+
ctx,
210184
compatibilityToken,
211-
getSortAddress,
212-
newMultisigAddress,
213-
newThreshold,
214-
oldThreshold,
185+
selectedNetwork,
215186
selectedAccount,
216187
selectedMultiProxy?.proxy,
188+
chainInfo,
217189
selectedMultisig,
218-
selectedNetwork,
219-
apiDescriptor
190+
oldThreshold,
191+
newThreshold,
192+
newMultisigAddress,
193+
getSortAddress,
194+
api
220195
])
221196

222197
const firstCall = useMemo(() => {
223-
if (!api || !compatibilityToken) {
198+
if (!ctx?.api || !api || !compatibilityToken) {
224199
// console.error('api is not ready')
225200
return
226201
}
@@ -248,79 +223,48 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
248223
const otherNewSignatories = getSortAddress(
249224
newSignatories.filter((sig) => sig !== selectedAccount.address)
250225
)
251-
const removeProxyTx =
252-
apiDescriptor === 'hydration'
253-
? api.tx.Proxy.remove_proxy({
254-
delegate: selectedMultisig?.address,
226+
227+
const removeProxyTx = isContextOf(ctx, 'hydration')
228+
? ctx.api.tx.Proxy.remove_proxy({
229+
delegate: selectedMultisig?.address,
230+
proxy_type: Enum('Any'),
231+
delay: 0
232+
})
233+
: isContextIn(ctx, noHydrationKeys_1)
234+
? ctx.api.tx.Proxy.remove_proxy({
235+
delegate: MultiAddress.Id(selectedMultisig?.address),
236+
proxy_type: Enum('Any'),
237+
delay: 0
238+
})
239+
: isContextIn(ctx, noHydrationKeys_2) &&
240+
ctx.api.tx.Proxy.remove_proxy({
241+
delegate: MultiAddress.Id(selectedMultisig?.address),
255242
proxy_type: Enum('Any'),
256243
delay: 0
257244
})
258-
: apiDescriptor === 'acala'
259-
? api.tx.Proxy.remove_proxy({
260-
delegate: MultiAddress.Id(selectedMultisig?.address),
261-
proxy_type: Enum('Any'),
262-
delay: 0
263-
})
264-
: apiDescriptor === 'khala'
265-
? api.tx.Proxy.remove_proxy({
266-
delegate: MultiAddress.Id(selectedMultisig?.address),
267-
proxy_type: Enum('Any'),
268-
delay: 0
269-
})
270-
: apiDescriptor === 'kusama'
271-
? api.tx.Proxy.remove_proxy({
272-
delegate: MultiAddress.Id(selectedMultisig?.address),
273-
proxy_type: Enum('Any'),
274-
delay: 0
275-
})
276-
: apiDescriptor === 'phala'
277-
? api.tx.Proxy.remove_proxy({
278-
delegate: MultiAddress.Id(selectedMultisig?.address),
279-
proxy_type: Enum('Any'),
280-
delay: 0
281-
})
282-
: api.tx.Proxy.remove_proxy({
283-
delegate: MultiAddress.Id(selectedMultisig?.address),
284-
proxy_type: Enum('Any'),
285-
delay: 0
286-
})
287-
288-
const proxyTx =
289-
apiDescriptor === 'hydration'
290-
? api.tx.Proxy.proxy({
291-
real: selectedMultiProxy?.proxy,
245+
246+
if (!removeProxyTx) return
247+
248+
const proxyTx = isContextOf(ctx, 'hydration')
249+
? ctx.api.tx.Proxy.proxy({
250+
real: selectedMultiProxy?.proxy,
251+
force_proxy_type: undefined,
252+
call: removeProxyTx.decodedCall
253+
})
254+
: isContextIn(ctx, noHydrationKeys_1)
255+
? ctx.api.tx.Proxy.proxy({
256+
real: MultiAddress.Id(selectedMultiProxy?.proxy),
292257
force_proxy_type: undefined,
293258
call: removeProxyTx.decodedCall
294259
})
295-
: apiDescriptor === 'acala'
296-
? api.tx.Proxy.proxy({
297-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
298-
force_proxy_type: undefined,
299-
call: removeProxyTx.decodedCall
300-
})
301-
: apiDescriptor === 'khala'
302-
? api.tx.Proxy.proxy({
303-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
304-
force_proxy_type: undefined,
305-
call: removeProxyTx.decodedCall
306-
})
307-
: apiDescriptor === 'kusama'
308-
? api.tx.Proxy.proxy({
309-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
310-
force_proxy_type: undefined,
311-
call: removeProxyTx.decodedCall
312-
})
313-
: apiDescriptor === 'phala'
314-
? api.tx.Proxy.proxy({
315-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
316-
force_proxy_type: undefined,
317-
call: removeProxyTx.decodedCall
318-
})
319-
: api.tx.Proxy.proxy({
320-
real: MultiAddress.Id(selectedMultiProxy?.proxy),
321-
force_proxy_type: undefined,
322-
call: removeProxyTx.decodedCall
323-
})
260+
: isContextIn(ctx, noHydrationKeys_2) &&
261+
api.tx.Proxy.proxy({
262+
real: MultiAddress.Id(selectedMultiProxy?.proxy),
263+
force_proxy_type: undefined,
264+
call: removeProxyTx.decodedCall
265+
})
266+
267+
if (!proxyTx) return
324268

325269
return getAsMultiTx({
326270
api,
@@ -330,16 +274,16 @@ const ChangeMultisig = ({ onClose, className }: Props) => {
330274
compatibilityToken
331275
})
332276
}, [
277+
ctx,
333278
api,
279+
compatibilityToken,
280+
selectedAccount,
281+
selectedMultisig?.address,
282+
selectedMultiProxy?.proxy,
334283
chainInfo,
335-
getSortAddress,
336284
newSignatories,
337285
newThreshold,
338-
selectedAccount,
339-
selectedMultiProxy,
340-
selectedMultisig,
341-
compatibilityToken,
342-
apiDescriptor
286+
getSortAddress
343287
])
344288

345289
const { multisigProposalNeededFunds: firstCallNeededFunds, reserved: firstCallReserved } =

0 commit comments

Comments
 (0)