Customs extrinsics & queries

Ternoa-js provides the capability to create custom calls to the blockchain. If existing helpers do not meet your requirements, you can still perform custom operations thanks to the library's atomic architecture.

You should not have to install directly the Polkadot{js} library as the ternoa js might cover all your needs.

If you are not familiar with the process of creating a transaction on the Ternoa chain, we highly suggest first reading the Getting Started section of the documentation with a focus on:

If you have explored the SDK code repository, you would have noticed that the functions are interdependent to maintain modularity and enhance the customized usage of these functions.

Performing a custom extrinsic transaction

Under the hood, extrinsics helpers provided on the SDK rely on this function: createTxHex() which creates a transaction in hex format:

getRawApi().tx[txPallet][txExtrinsic](...txArgs)

The createTxHex() function expects three params:

  • txPallet is a string corresponding to the name of the pallet. Example for an NFT mint: "nft"

  • txExtrinsic is a string corresponding to the name of the extrinsic to execute. Example for an NFT mint: "createNft"

  • txArgs is an optional array of arguments needed to execute the extrinsic. Example for an NFT mint: [offchainData, royalty, collectionId, isSoulbound ]

Feel free to look at the Polkadot portal to see the available queries, extrinsics, or constants and make your custom requests.

Example: A bridge deposit

Assuming you want to execute a transaction that is not natively proposed as a helper in the SDK, for example, a bridge deposit. Your atomic function could look like this:

export const bridgeDepositTx = async (
  amount: number,
  recipient: string
  destId: number,
): Promise<TransactionHashType> => {
  const formattedAmount = typeof amount === "number" ? numberToBalance(amount) : amount
  return await createTxHex("bridge, "deposit", [
    formattedAmount,
    recipient,
    destId,
  ])
}

or

export const bridgeDepositTx = async (
  amount: number,
  recipient: string
  destId: number,
): Promise<TransactionHashType> => {

  const formattedAmount = typeof amount === "number" ? numberToBalance(amount) : amount
  const api = getRawApi()
  return await api.tx["bridge]["deposit"]([
    formattedAmount,
    recipient,
    destId,
  ])
}

Performing a custom query

Performing custom queries works the same, except that instead of using the createTxHex() function, it uses the query()function that executes under the hood, the following code:

await getRawApi().query[module][call](...args)

The query() function expects three params:

  • module is a string corresponding to the name of the pallet. Example to query the NFT mint fee: "nft"

  • call is a string corresponding to the name of the call to execute. Example to query the NFT mint fee: "nftMintFee"

  • args is an optional array of arguments needed to execute the query. Example to query the NFT mint fee: [ ] as no argument is expected for this query.

Same as the custom extrinsic, you can perform the desired custom query by specifying the arguments of your choice.

Feel free to look at the Polkadot portal to see the available queries, extrinsics, or constants and make your custom requests.

Last updated