types#InstantiateData TypeScript Examples
The following examples show how to use
types#InstantiateData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createInstantiateTx.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
export function createInstantiateTx(
api: ApiPromise,
{
argValues,
codeHash,
constructorIndex,
weight: gasLimit,
value,
metadata,
salt,
storageDepositLimit,
}: InstantiateData
): SubmittableExtrinsic<'promise'> | null {
const wasm = metadata?.info.source.wasm;
const isValid = codeHash || !!wasm;
if (isValid && metadata && isNumber(constructorIndex) && metadata && argValues) {
const constructor = metadata.findConstructor(constructorIndex);
const options = {
gasLimit,
salt: salt ? encodeSalt(salt) : null,
storageDepositLimit: storageDepositLimit || undefined,
value: value && constructor.isPayable ? api.registry.createType('Balance', value) : undefined,
};
const codeOrBlueprint = codeHash
? new BlueprintPromise(api, metadata, codeHash)
: new CodePromise(api, metadata, wasm && wasm.toU8a());
const transformed = transformUserInput(api.registry, constructor.args, argValues);
return constructor.args.length > 0
? codeOrBlueprint.tx[constructor.method](options, ...transformed)
: codeOrBlueprint.tx[constructor.method](options);
} else {
throw new Error('Unknown error');
}
}
Example #2
Source File: instantiate.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
export function onInsantiateFromHash(
{ db, identity }: DbState,
{ accountId, codeHash, name }: InstantiateData,
onSuccess: InstantiateState['onSuccess']
): OnInstantiateSuccess$Hash {
return async function ({ contract, status }): Promise<void> {
if (accountId && codeHash && contract && (status.isInBlock || status.isFinalized)) {
await createContract(db, identity, {
abi: contract.abi.json,
address: contract.address.toString(),
creator: accountId,
codeHash,
name: name,
tags: [],
});
onSuccess && onSuccess(contract);
}
};
}
Example #3
Source File: instantiate.ts From contracts-ui with GNU General Public License v3.0 | 6 votes |
export function onInstantiateFromCode(
{ db, identity }: DbState,
{ accountId, name }: InstantiateData,
onSuccess: InstantiateState['onSuccess']
): OnInstantiateSuccess$Code {
return async function (result): Promise<void> {
try {
const { blueprint, contract, status } = result;
if (accountId && contract && (status.isInBlock || status.isFinalized)) {
await createContract(db, identity, {
abi: contract.abi.json,
address: contract.address.toString(),
creator: accountId,
codeHash: blueprint?.codeHash.toHex() || contract.abi.info.source.wasmHash.toHex(),
name: name,
tags: [],
});
onSuccess && onSuccess(contract, blueprint);
}
} catch (e) {
console.error(e);
}
};
}
Example #4
Source File: InstantiateContext.tsx From contracts-ui with GNU General Public License v3.0 | 5 votes |
initialData: InstantiateData = {
constructorIndex: 0,
value: BN_THOUSAND,
name: '',
weight: BN_THOUSAND,
}
Example #5
Source File: InstantiateContext.tsx From contracts-ui with GNU General Public License v3.0 | 4 votes |
export function InstantiateContextProvider({
children,
}: React.PropsWithChildren<Partial<InstantiateProps>>) {
const navigate = useNavigate();
const dbState = useDatabase();
const apiState = useApi();
const NOOP = () => Promise.resolve();
const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>();
const [currentStep, stepForward, stepBackward, setStep] = useStepper(initialState.currentStep);
const [data, setData] = useState<InstantiateData>(initialState.data);
const [[tx, onInstantiate], setTx] = useState<TxState>([null, NOOP, null]);
const [dryRunResult, setDryRunResult] = useState<ContractInstantiateResult>();
const onSuccess = useCallback(
(contract: ContractPromise, _?: BlueprintPromise | undefined) => {
navigate(`/contract/${contract.address}`);
dbState.refreshUserData();
},
[dbState, navigate]
);
const onFinalize = (formData: Partial<InstantiateData>) => {
const newData = { ...data, ...formData };
try {
const tx = createInstantiateTx(apiState.api, newData);
const onInstantiate = (codeHashUrlParam ? onInsantiateFromHash : onInstantiateFromCode)(
dbState,
newData,
onSuccess
);
setTx([tx, onInstantiate, null]);
setData(newData);
stepForward();
} catch (e) {
console.error(e);
setTx([null, NOOP, 'Error creating transaction']);
}
};
const onFormChange = useCallback(
async (formData: Step2FormData) => {
try {
const constructor = data.metadata?.findConstructor(formData.constructorIndex);
const inputData = constructor?.toU8a(
transformUserInput(apiState.api.registry, constructor.args, formData.argValues)
);
const params = {
origin: data.accountId,
gasLimit: formData.weight || maximumBlockWeight(apiState.api),
storageDepositLimit: formData.storageDepositLimit,
code: codeHashUrlParam
? { Existing: codeHashUrlParam }
: { Upload: data.metadata?.info.source.wasm },
data: inputData,
salt: formData.salt || undefined,
value: formData.value
? apiState.api.registry.createType('Balance', formData.value)
: null,
};
if (params.origin) {
const result = await apiState.api.rpc.contracts.instantiate(params);
setDryRunResult(result);
}
} catch (e) {
console.error(e);
}
},
[apiState.api, codeHashUrlParam, data.accountId, data.metadata]
);
function onUnFinalize() {
setTx([null, NOOP, null]);
setStep(2);
}
const value: InstantiateState = {
data,
setData,
currentStep,
dryRunResult,
setStep,
stepForward,
stepBackward,
onSuccess,
onUnFinalize,
onFinalize,
onFormChange,
tx,
onInstantiate,
onError: NOOP,
};
return <InstantiateContext.Provider value={value}>{children}</InstantiateContext.Provider>;
}