Version: 24.3.0
RPC
Written by Claude Barde
You can use the RPC client package to query the RPC API of your chosen node. The higher-level @taquito/taquito package builds on this RPC package.
In general, you won’t need to use this package directly, but it is available for use should you want specific data and need to bypass the higher-level abstractions in Taquito.
Methods in the RPC package map one-to-one to the corresponding Tezos RPC API endpoints. All responses from the RPC are returned with TypeScript types. No other parsing or compositions are done at this level.
Examples
The RpcClient constructor takes the URL of the node you want to use and the chain ID.
// Initializing the RPC client
import { RpcClient } from '@taquito/rpc';
const client = new RpcClient(' https://shadownet.tezos.ecadinfra.com/', 'NetXLH1uAxK7CCh');
The balance is returned as a BigNumber and must be converted to a number to output it. Please note that the returned value is in mutez (micro ꜩ), so if you need the balance in ꜩ, you can divide it by 1000000.
/* Fetching the spendable balance of an account
* using the client set up above */
const balance = await client.getBalance('tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb');
console.log('Balance:', balance.toNumber());
You can use the RPC client to get the head block information.
// gets head block
const block = await client.getBlock();
console.log('Head block:', block);
If it’s the head block hash you are looking for, you can easily get it with the getBlockHash method.
// gets head block hash
const blockHash = await client.getBlockHash();
console.log('Head block hash:', blockHash);
This method returns some blockchain constants pertaining for example to the gas limits or the block information.
// gets constants
const constants = await client.getConstants();
console.log('Constants:', constants);
This method returns the balance of the contract, the code, and the storage.
// gets contract
const contractExample = 'KT1J4E79F1qL6kGBSQ3yXBdXmuq5j4FNThK2';
const contract = await client.getContract(contractExample);
console.log('Contract:', contract);
You can also get a list of the contract entry points as an object whose keys are the entry point names and whose values are the expected parameters in JSON format.
// gets contract entrypoints
const entrypoints = await client.getEntrypoints(contractExample);
console.log('Entrypoints:', entrypoints);
The getScript method returns the contract’s script as a 3 key/value pair object: a key for the parameter, a key for the storage, and a key for the code.
// gets contract script
const script = await client.getScript(contractExample);
console.log('Contract script:', script);
You also have access to the storage of the contract using the getStorage method.
// gets contract storage
const storage = await client.getStorage(contractExample);
console.log('Contract storage:', storage);
You can simulate the PACK instruction from Michelson with the packData method.
// packs data
const packedData = await client.packData({ data: { string: 'test' }, type: { prim: 'string' } });
console.log('Packed data:', packedData);
This function will execute TZIP-4 views normally referred to as ‘Lambda Views’. You can learn more about TZIP-4 here
// runs view
const view = await client.runView({
contract: 'contractAddress',
entrypoint: 'contractEntrypoint',
chain_id: 'chainId',
input: {
string: 'testInput'
}
});
Full documentation
Typedoc information is available here.