I want to query a smart contract which I deployed on the Aleph Zero testnet. The Polkadot.js documentation is pretty clear and easy to follow regarding connecting to the chain and querying general chain information. But I get a bit confused when it comes to querying information regarding deployed smart contracts.

Here is what I have so far:

// Required imports
import { ApiPromise, WsProvider } from '@polkadot/api';
import { ContractPromise } from '@polkadot/api-contract';

// Initialise the provider to connect to the local node
const provider = new WsProvider('wss://ws.test.azero.dev');

// Create the API and wait until ready
const api = await ApiPromise.create({ provider });
const contract = new ContractPromise(api, metadata, wasm);

I am a bit confused about what the metadata and wasm are in this context. The documentation just sends me in a circle of links here.

Also, when I have established the connection to the right deployed smart contract, how can I query or execute methods to get information about it?

1 Answer 1


Following the Polkadot JS docs, I don't see where wasm is used tbh

import { ContractPromise } from '@polkadot/api-contract';

// The address is the actual on-chain address as ss58 or AccountId object.
const contract = new ContractPromise(api, metadata, address);

But anyways, the alephZero docs also mention the following aspects in this section:

The resulting files will be placed in mytoken/target/ink/ folder. If the compilation is successful you will find there the following 3 files:

  • mytoken.wasm is a binary WASM file with the compiled contract
  • metadata.json containing our contracts ABI (Application Binary Interface)
  • mytoken.contract which bundles the above two for more convenient interaction with the chain explorer

So, once you compile your smart contract, and because you mentioned that you've deployed it, I assume you have a metadata.json file, which is the one that you have to add here new ContractPromise(api, metadata, address);

Instead of a wasm file, the third argument address is the actual address of your smart contract deployed on the testnet, its identifier.

So let's assume you have a directory src where you store the metadata.json file and the address of your smart contract is 5DewKiJMRAaeRuYH48wmyXiTwzxedkq6zziRNr2EmirZATK3. You need to create the ContractPromise like:

... //snip
// Create the API and wait until ready
const api = await ApiPromise.create({ provider });
const scAddress = '5DewKiJMRAaeRuYH48wmyXiTwzxedkq6zziRNr2EmirZATK3';

const contract = new ContractPromise(api, 'src/metadata.json', scAddress);

Finally, in order to interact with your smart contract, you can follow this guide: https://polkadot.js.org/docs/api-contract/start/contract.read#reading-contract-values

  • I successfully loaded the contract. Thank you for your answer. However, when trying to interact with the smart contract I always encounter this error. "Uncaught TypeError: contract.query.balanceOf is not a function". Do you have an idea what could be the reason for this?
    – rajohs
    Aug 8, 2022 at 16:58
  • If your smart contract does not have that function implemented, then it will throw that error. You can execute queries over your items defined under the macro#[ink(storage)], but I'm not sure at all.
    – andresvsm
    Aug 9, 2022 at 7:58
  • 1
    That was partially correct. I am implementing a PSP34 contract and it looks like you cannot query the methods implemented in the original PSP34 contract, but only the ones you defined yourself using #[ink(message)]
    – rajohs
    Aug 9, 2022 at 12:14

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.