types#InstantiateState TypeScript Examples

The following examples show how to use types#InstantiateState. 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: instantiate.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
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 #2
Source File: instantiate.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
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 #3
Source File: InstantiateContext.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
initialState = {
  data: initialData,
  currentStep: 1,
  tx: null,
  onError: NOOP,
  onSuccess: NOOP,
  onInstantiate: () => Promise.resolve(),
} as unknown as InstantiateState
Example #4
Source File: InstantiateContext.tsx    From contracts-ui with GNU General Public License v3.0 4 votes vote down vote up
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>;
}