3

I have been following the public documentation of polkadot API to deploy contract: https://polkadot.js.org/docs/api-contract/start/code

but I'm getting error Uncaught (in promise) TypeError: code.tx.new is not a function

here is my script:

import { ApiPromise, WsProvider } from "@polkadot/api";
import { CodePromise, ContractPromise } from "@polkadot/api-contract";
import { Keyring } from "@polkadot/keyring";

export const deployContract= async (wasm,abi,providerURL ="wss://rococo-contracts-rpc.polkadot.io",endowment=123456789,gasLimit=5000000n)=>{
    if(!wasm || !abi) {alert('wasm or abi not provided!'); return;}

    const provider = new WsProvider(providerURL);
    const api = await ApiPromise.create({ provider });
    const code = new CodePromise(api, abi, wasm);
    const keyring = new Keyring({ type: "sr25519" });
    const dataJsonAccount = {THIS_IS_JUST_PLACEHOLDER} 
    const tx = code.tx.new({gasLimit,storageDepositLimit:null})
    let address;
    const account = keyring.addFromJson(dataJsonAccount);
    account.decodePkcs8('THIS_IS_JUST_PLACEHOLDER');

    try{
      const unsub = await tx.signAndSend(account, ({ contract, status }) => {
        if (status.isInBlock || status.isFinalized) {
          address = contract.address.toString();
          console.info(address)
          unsub();
        }
      });
  }

  catch(e){
    console.info(e);
  }

 

Later I tried to print what is code.tx and I got result that there is a only construct function.

so I used that one like this:

          const tx = code.tx.construct({gasLimit,storageDepositLimit:null})

and it kinda worked, but I'm never getting my contract deployed for some reason. I'm getting this error:

          TypeError: Cannot read properties of undefined (reading 'address')

which means contract here:

          address = contract.address.toString();

is never deployed.

I also tried using Contracts-UI to deploy contract manually(by just uploading .contract, and it works.

What I'm missing, what I'm doing wrong?

1 Answer 1

3

Actually, this: smart contract function call error solved my question.

It seems that Polkadot documentation is not up to date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.