Skip to main content
Last updated

Send a request to the blockchain

The @kadena/client provides a createClient function with some utility functions. these helpers call the Pact API under the hood Pactjs API .

  • submit
  • pollStatus
  • getStatus
  • pollSpv
  • getSpv
  • local,
  • preflight
  • dirtyRead
  • signatureVerification

createClient accepts the host url or the host url generator function that handles url generating as pact is a multi chain network we need to change the url based on that.

ts
// we only want to send request to the chain 1 one the mainnetconst hostUrl = 'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/1/pact';const client = createClient(hostUrl);// we need more flexibility to call different chains or even networks, then functions// extract networkId and chainId from the cmd part of the transaction and use the hostUrlGenerator to generate the urlconst hostUrlGenerator = ({ networkId, chainId }) =>  `https://api.chainweb.com/chainweb/0.0/${networkId}/chain/${chainId}/pact`;const { local, submit, getStatus, pollStatus, getSpv, pollSpv } =  createClient(hostUrlGenerator);
ts
// we only want to send request to the chain 1 one the mainnetconst hostUrl = 'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/1/pact';const client = createClient(hostUrl);// we need more flexibility to call different chains or even networks, then functions// extract networkId and chainId from the cmd part of the transaction and use the hostUrlGenerator to generate the urlconst hostUrlGenerator = ({ networkId, chainId }) =>  `https://api.chainweb.com/chainweb/0.0/${networkId}/chain/${chainId}/pact`;const { local, submit, getStatus, pollStatus, getSpv, pollSpv } =  createClient(hostUrlGenerator);

Probably the simplest call you can make is describe-module, but as this is not on the coin contract, we have to trick Typescript a little:

ts
const res = await local({  payload: {    exec: {      code: Pact.modules.coin['get-balance']('albert'),    },  },});console.log(JSON.stringify(res, null, 2));
ts
const res = await local({  payload: {    exec: {      code: Pact.modules.coin['get-balance']('albert'),    },  },});console.log(JSON.stringify(res, null, 2));

A more elaborate example that includes signing, sending and polling can be found in example-contract/transfer.ts