Send a request to the blockchain
The @kadena/client
provides a getClient
function with some utility
functions. this helpers calls pact api under the hood
Pactjs API .
local
,submit
andpollStatus
.getStatus
pollSpv
getSpv
getClient
accepts the host url or the host url generator function that handel
url generating as pact is a multi chian network we need to change the url based
on that.
// we only want to send request to the chain 1 one the mainnet
const hostUrl = 'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/1/pact';
const client = getClient(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 url
const hostUrlGenerator = ({ networkId, chainId }) =>
`https://api.chainweb.com/chainweb/0.0/${networkId}/chain/${chainId}/pact`;
const { local, submit, getStatus, pollStatus, getSpv, pollSpv } =
getClient(hostUrlGenerator);
// we only want to send request to the chain 1 one the mainnet
const hostUrl = 'https://api.chainweb.com/chainweb/0.0/mainnet01/chain/1/pact';
const client = getClient(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 url
const hostUrlGenerator = ({ networkId, chainId }) =>
`https://api.chainweb.com/chainweb/0.0/${networkId}/chain/${chainId}/pact`;
const { local, submit, getStatus, pollStatus, getSpv, pollSpv } =
getClient(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:
Also see example-contract/get-balance.ts .
const res = await local({
payload: {
exec: {
code: Pact.modules.coin['get-balance']('albert'),
},
},
});
console.log(JSON.stringify(res, null, 2));
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