zkverifyjs
Version:
Submit proofs to zkVerify and query proof state with ease using our npm package.
30 lines • 1.35 kB
JavaScript
/**
* Converts a fee in the smallest unit to the base token unit.
*
* @param {string} feeInSmallestUnit - Fee in the blockchain's smallest unit.
* @param {number} decimals - The number of decimals in the blockchain's base token.
* @returns {string} - The fee in the base token unit.
*/
export function convertFeeToToken(feeInSmallestUnit, decimals) {
const feeInTokens = parseFloat(feeInSmallestUnit) / Math.pow(10, decimals);
return feeInTokens.toFixed(decimals);
}
/**
* Estimates the cost of a given extrinsic for the specified account.
*
* @param {ApiPromise} api - The Polkadot API instance.
* @param {SubmittableExtrinsic<'promise', ISubmittableResult>} extrinsic - The extrinsic to estimate.
* @param {KeyringPair} account - The account to use.
* @returns {Promise<ExtrinsicCostEstimate>} - A promise that resolves to an object containing the estimated fee and extrinsic details.
*/
export async function estimateCost(api, extrinsic, account) {
const paymentInfo = await extrinsic.paymentInfo(account);
const tokenDecimals = api.registry.chainDecimals[0];
const estimatedFeeInTokens = convertFeeToToken(paymentInfo.partialFee.toString(), tokenDecimals);
return {
partialFee: paymentInfo.partialFee.toString(),
estimatedFeeInTokens,
weight: paymentInfo.weight.toString(),
length: extrinsic.length
};
}