s.Takahashi
s.Takahashi

Reputation: 479

How to deploy contract with polkadot.js

  1. I implement a front end app with polkadot.js.
  2. I want to deploy contract from front end app.
  3. I implemented the code as following, but it's does not work and I got an Error.
  4. Please help me.

code is:

  const deployContract = async () => {
    const contractWasm = contract_file.source.wasm;
    const contract = new ContractPromise(api,abi,contractWasm);
    const initValue = true;
    console.log("contract is :", contract);
    const tx = await contract.tx.new({ gasLimit, storageDepositLimit }, initValue)
    setContractAddress(contract.address.toString());
  }

error is:

Unhandled Runtime Error
TypeError: contract.tx.new is not a function

Upvotes: 0

Views: 512

Answers (1)

s.Takahashi
s.Takahashi

Reputation: 479

I'm sorry. It was a simple mistake. It was just that the correct answer was to use "CodePromise" instead of "ContractPromise".

Fixed code:

  const deployContract = async () => {
    const { web3FromSource } = await import("@polkadot/extension-dapp");
    const contractWasm = contract_file.source.wasm;
    const contract = new CodePromise(api, abi, contractWasm);
    const initValue = true;
    console.log("contract is :", contract);
    const performingAccount = accounts[0];
    const injector = await web3FromSource(performingAccount.meta.source);
    const tx = contract.tx.new({ gasLimit, storageDepositLimit }, initValue);
    let address = "";
    const unsub = await tx.signAndSend(
      actingAddress,
      { signer: injector.signer },
      ({ contract, status }) => {
        if (status.isInBlock) {
          setResult("in a block");
        } else if (status.isFinalized) {
          setResult("finalized");
          address = contract.address.toString();
          setContractAddress(address);
          unsub();
        }
      }
    );
  };

Upvotes: 0

Related Questions