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

feat: validate if contract environment is compatible with chain environment #482

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 5 additions & 1 deletion src/ui/components/instantiate/Step1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CodeHash } from './CodeHash';
import { useNonEmptyString } from 'ui/hooks/useNonEmptyString';
import { useApi, useDatabase, useInstantiate } from 'ui/contexts';
import { useDbQuery } from 'ui/hooks';
import { Metadata } from '../metadata';
import { Environment, Metadata } from '../metadata';

export function Step1() {
const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>();
Expand Down Expand Up @@ -65,6 +65,9 @@ export function Step1() {
}

if (step !== 1) return null;
if (metadata) {
console.log(metadata.json.spec.environment);
}

return (
<Loader isLoading={isLoading}>
Expand Down Expand Up @@ -124,6 +127,7 @@ export function Step1() {
</FormField>
)}
</Form>
{metadata && <Environment metadata={metadata} />}

{metadata && (
<>
Expand Down
83 changes: 83 additions & 0 deletions src/ui/components/metadata/Environment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2023 @paritytech/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { classes } from 'helpers';
import { useMemo } from 'react';
import { Abi } from 'types';
import { useApi } from '../../contexts/ApiContext';

interface Props extends React.HTMLAttributes<HTMLDivElement> {
metadata: Abi;
}

interface EnvironmentValue<T extends string> {
displayName: [T];
type: number;
}

const isEnvironmentValue = (value: unknown): value is EnvironmentValue<string> => {
if (typeof value !== 'object') return false;
if ('displayName' in (value as object)) {
return true;
}
return false;
};

interface ContractEnvironment {
accountId: EnvironmentValue<'AccountId'>;
balance: EnvironmentValue<'Balance'>;
blockNumber: EnvironmentValue<'BlockNumber'>;
chainExtension: EnvironmentValue<'ChainExtension'>;
hash: EnvironmentValue<'Hash'>;
maxEventTopics: number;
timestamp: EnvironmentValue<'Timestamp'>;
}

export function Environment({ metadata, className = '', ...restOfProps }: Props) {
const { api } = useApi();

const environment = useMemo(() => {
if (
!metadata.json['spec'] ||
!(metadata.json['spec'] as Record<string, unknown>)['environment']
)
return undefined;
return (metadata.json['spec'] as Record<string, unknown>)['environment'] as ContractEnvironment;
}, [metadata]);

const isCompatible = useMemo(() => {
if (!environment || !api) return undefined;

Object.entries(environment).forEach(([key, value]) => {
if (isEnvironmentValue(value)) {
try {
const abiTypeCreateType = metadata.registry.createType(value.displayName[0]);
const abiType = metadata.registry.lookup.getTypeDef(value.type).type;
const chainType = api.registry.createType(value.displayName[0]);
metadata.json;
console.log({
displayName: value.displayName[0],
abiType: abiType,
abiTypeCreateType: abiTypeCreateType.toRawType(),
chainType: chainType.toRawType(),
primitive: chainType.toPrimitive(),
});
} catch (exception) {
console.log(`Unable to create type ${value.displayName[0]}`);
}
}
});
}, [api, environment]);

return (
<div
className={classes(
'grid-cols-2 gap-2 p-3 border dark:bg-elevation-1 dark:border-gray-700 border-gray-200 inline-flex items-center rounded grid text-sm dark:text-white text-gray-900',
className
)}
{...restOfProps}
>
Environment
</div>
);
}
1 change: 1 addition & 0 deletions src/ui/components/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2023 @paritytech/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

export * from './Environment';
export * from './Metadata';