The library follows common api architecture throughout and the APIs are divided into two types -
Read APIs does not publish anything on blockchain, so it does not consume any gas. Example of read APIs are - getBalance
, isWithdrawExited
etc.
Let's see an example of read API -
const erc20 = posClient.erc20('<token address>');
const balance = await erc20.getBalance('<user address>')
read APIs are very simple and returns result directly.
Write APSs publish some data on the blockchain, so it consumes gas. Example of write APIs are - approve
, deposit
etc.
When you are calling a write API - you need two data from the result.
Let's see an example of write API and get the transactionhash and receipt -
const erc20 = posClient.erc20('<token address>');
// send the transaction
const result = await erc20.approve(10);
// get transaction hash
const txHash = await result.getTransactionHash();
// get receipt
const receipt = await result.getReceipt();
There are some configurable options that are available for all API's. These configurations can be passed in parameters.
Available configurations are -
Let's see an example by configuring the gasPrice -
const erc20RootToken = posClient.erc20(<root token address>,true);
// approve 100 amount
const approveResult = await erc20Token.approve(100, {
gasPrice: '4000000000',
});
const txHash = await approveResult.getTransactionHash();
const txReceipt = await approveResult.getReceipt();