Teleport on Polkadot 9110 (#6271)

* Teleport on Polkadot 9110

* Adjust type introspection

* Param type formatting

* Reformat
This commit is contained in:
Jaco 2021-10-12 11:18:30 +03:00 committed by GitHub
parent a0f896c349
commit c11bf0da53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 11 deletions

View File

@ -4,6 +4,7 @@
import type BN from 'bn.js';
import type { LinkOption } from '@polkadot/apps-config/endpoints/types';
import type { Option } from '@polkadot/apps-config/settings/types';
import type { XcmVersionedMultiLocation } from '@polkadot/types/lookup';
import React, { useMemo, useState } from 'react';
@ -79,19 +80,29 @@ function Teleport ({ onClose }: Props): React.ReactElement<Props> | null {
const params = useMemo(
() => {
const src = isParaTeleport
// From Polkadot runtime 9110 (no destination weight)
// Get first item, it should have V0, V1, ...
const firstType = api.createType<XcmVersionedMultiLocation>(call.meta.args[0].type.toString());
const isCurrent = firstType.defKeys.includes('V1');
const dst = isParaTeleport
? { X1: 'Parent' }
: { X1: { ParaChain: recipientParaId } };
const dst = { X1: { AccountId32: { id: recipientId, network: 'Any' } } };
const tkn = isParaTeleport
const acc = { X1: { AccountId32: { id: recipientId, network: 'Any' } } };
const ass = isParaTeleport
? [{ ConcreteFungible: { amount, id: { X1: 'Parent' } } }]
: [{ ConcreteFungible: { amount, id: 'Here' } }];
// forgo id - 'Here' for 9100, 'Null' for 9110 (both is the default enum value)
: [{ ConcreteFungible: { amount } }];
return call.meta.args.length === 5
? [{ V0: src }, { V0: dst }, { V0: tkn }, 0, destWeight]
: [src, dst, tkn, destWeight];
return isCurrent
? call.meta.args.length === 5
// Polkadot 9100
? [{ V0: dst }, { V0: acc }, { V0: ass }, 0, destWeight]
// Polkadot 9110
: [{ V0: dst }, { V0: acc }, { V0: ass }, 0]
: [dst, acc, ass, destWeight];
},
[amount, call, destWeight, isParaTeleport, recipientId, recipientParaId]
[api, amount, call, destWeight, isParaTeleport, recipientId, recipientParaId]
);
const hasAvailable = !!amount && amount.gte(weightFee);

View File

@ -11,6 +11,17 @@ import { isUndefined } from '@polkadot/util';
import findComponent from './findComponent';
import Static from './Static';
function formatJSON (input: string): string {
return input
.replace(/"/g, '')
.replace(/\\/g, '')
.replace(/:Null/g, '')
.replace(/:/g, ': ')
// .replace(/{/g, '{ ')
// .replace(/}/g, ' }')
.replace(/,/g, ', ');
}
function Param ({ className = '', defaultValue, isDisabled, isInOption, isOptional, name, onChange, onEnter, onEscape, overrides, registry, type }: Props): React.ReactElement<Props> | null {
const Component = useMemo(
() => findComponent(registry, type, overrides),
@ -18,9 +29,11 @@ function Param ({ className = '', defaultValue, isDisabled, isInOption, isOption
);
const label = useMemo(
() => isUndefined(name)
? `${isDisabled && isInOption ? 'Option<' : ''}${encodeTypeDef(registry, type)}${isDisabled && isInOption ? '>' : ''}`
: `${name}: ${isDisabled && isInOption ? 'Option<' : ''}${encodeTypeDef(registry, type)}${isDisabled && isInOption ? '>' : ''}`,
() => formatJSON(
isUndefined(name)
? `${isDisabled && isInOption ? 'Option<' : ''}${encodeTypeDef(registry, type)}${isDisabled && isInOption ? '>' : ''}`
: `${name}: ${isDisabled && isInOption ? 'Option<' : ''}${encodeTypeDef(registry, type)}${isDisabled && isInOption ? '>' : ''}`
),
[isDisabled, isInOption, name, registry, type]
);