-
Notifications
You must be signed in to change notification settings - Fork 44
/
useMetadata.ts
150 lines (122 loc) · 3.46 KB
/
useMetadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2022 @paritytech/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only
import { useEffect, useState } from 'react';
import { useApi } from 'ui/contexts/ApiContext';
import { Abi, ApiPromise, FileState, MetadataState, UseMetadata, Validation, VoidFn } from 'types';
type OnChange = (_: FileState | undefined, __?: Record<string, unknown>) => void;
type OnRemove = VoidFn;
interface Options {
isWasmRequired?: boolean;
}
interface DeriveOptions extends Options {
name?: string;
}
interface Callbacks {
onChange?: OnChange;
onRemove?: OnRemove;
}
function deriveFromJson(
options: DeriveOptions,
source?: Record<string, unknown>,
api?: ApiPromise | null
): MetadataState {
if (!source) {
return EMPTY;
}
let value: Abi | undefined = undefined;
try {
value = new Abi(source, api?.registry.getChainProperties());
const name = options.name || value.info.contract.name.toString();
return {
source,
name,
value,
isSupplied: true,
...validate(value, options),
};
} catch (e) {
console.error(e);
return {
source,
name: '',
value,
isSupplied: true,
...validate(value, options),
};
}
}
const EMPTY: MetadataState = {
isError: false,
isSupplied: false,
isValid: false,
name: '',
message: null,
};
function validate(metadata: Abi | undefined, { isWasmRequired }: Options): Validation {
if (!metadata) {
return {
isValid: false,
isError: true,
message:
'Invalid contract file format. Please upload the generated .contract bundle for your smart contract.',
};
}
const wasm = metadata.info.source.wasm;
const isWasmEmpty = wasm.isEmpty;
const isWasmInvalid = !WebAssembly.validate(wasm);
if (isWasmRequired && (isWasmEmpty || isWasmInvalid)) {
return {
isValid: false,
isError: true,
message: 'This contract bundle has an empty or invalid WASM field.',
};
}
return {
isValid: true,
isError: false,
isSuccess: true,
message: isWasmRequired ? 'Valid contract bundle!' : 'Valid metadata file!',
};
}
const utf8decoder = new TextDecoder();
export function useMetadata(
initialValue?: Record<string, unknown>,
options: Options & Callbacks = {}
): UseMetadata {
const { api } = useApi();
const { isWasmRequired = false, ...callbacks } = options;
const [state, setState] = useState<MetadataState>(() =>
deriveFromJson({ isWasmRequired }, initialValue, api)
);
function onChange(file: FileState): void {
try {
const json = JSON.parse(utf8decoder.decode(file.data)) as Record<string, unknown>;
const name = file.name.replace('.contract', '').replace('.json', '').replace('_', ' ');
const newState = deriveFromJson({ isWasmRequired, name }, json, api);
setState(newState);
callbacks.onChange && callbacks.onChange(file, json);
} catch (error) {
console.error(error);
setState({
...EMPTY,
message: 'This contract file is not in a valid format.',
isError: true,
isSupplied: true,
isValid: false,
});
}
}
function onRemove(): void {
setState(EMPTY);
callbacks.onChange && callbacks.onChange(undefined);
callbacks.onRemove && callbacks.onRemove();
}
useEffect((): void => {
setState(deriveFromJson({ isWasmRequired }, initialValue, api));
}, [api, initialValue, isWasmRequired]);
return {
...state,
onChange,
onRemove,
};
}