@kadena/kadena-cli
Version:
Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)
104 lines • 4.25 kB
JavaScript
import { createClient } from '@kadena/client';
import { describeModule } from '@kadena/client-utils';
import { notEmpty } from '../../../utils/globalHelpers.js';
import { log } from '../../../utils/logger.js';
import deployDevNetFaucet from '../../devnet/faucet/deploy/index.js';
import { getTransactionExplorerUrl } from './accountHelpers.js';
function isSuccessOrPartialSuccess(result) {
return result.status === 'success' || result.status === 'partial';
}
export function logTransactionExplorerUrls(result, networkExplorerUrl) {
if (isSuccessOrPartialSuccess(result) &&
'data' in result &&
result.data.length > 0) {
log.info(log.color.green('Transaction explorer URL for '));
for (const { chainId, requestKey } of result.data) {
const explorerUrl = getTransactionExplorerUrl(networkExplorerUrl, requestKey);
log.info(log.color.green(`Chain ID "${chainId}" : ${explorerUrl}`));
}
}
}
export async function fetchTransactionDetails(transaction, networkHost, networkId) {
const { requestKey, chainId } = transaction;
try {
const { pollStatus } = createClient(`${networkHost}/chainweb/0.0/${networkId}/chain/${chainId}/pact`);
const response = await pollStatus(transaction);
const transactionResult = response[requestKey];
if (typeof transactionResult !== 'string' &&
transactionResult.result.status === 'failure') {
throw transactionResult.result.error;
}
return transactionResult;
}
catch (e) {
if (e instanceof Error) {
throw new Error(e.message);
}
throw new Error(e);
}
}
export async function getTxDetails(data, networkHost, networkId) {
const txErrors = [];
const txResultsPromises = data.map(async (transaction) => {
const { requestKey, chainId } = transaction;
try {
const transactionResult = await fetchTransactionDetails(transaction, networkHost, networkId);
if (transactionResult !== null) {
return { [chainId]: transactionResult };
}
}
catch (e) {
txErrors.push(`ChainID: "${chainId}" - requestKey: ${requestKey} - ${e.message}`);
}
});
const txResults = (await Promise.all(txResultsPromises)).filter(notEmpty);
return { txResults, txErrors };
}
export function logAccountFundingTxResults(txResults, txErrors, accountName, fungible, amount, networkId) {
if (txResults.length > 0) {
const chainIds = txResults
.map((obj) => Object.keys(obj))
.flat()
.join(', ');
log.info(log.color.green(`\nAccount "${accountName}" funded with ${amount} ${fungible}(s) on Chain ID(s) "${chainIds}" in ${networkId} network.`));
log.info(log.color.green(`Use "kadena account details" command to check the balance.`));
}
if (txErrors.length > 0) {
log.error('Failed to fund account on following:');
txErrors.forEach((error) => {
log.error(error);
});
}
}
export async function findMissingModuleDeployments(moduleName, network, targetChainIds) {
const undeployedChainIds = [];
await Promise.all(targetChainIds.map(async (chainId) => {
const moduleDeployed = await describeModule(moduleName, {
host: network.networkHost,
defaults: {
networkId: network.networkId,
meta: { chainId },
},
}).catch(() => false);
if (moduleDeployed === false) {
undeployedChainIds.push(chainId);
}
}));
return undeployedChainIds;
}
export async function deployFaucetsToChains(chainIds) {
const succeededDeployments = [];
const failedDeployments = [];
await Promise.all(chainIds.map(async (chainId) => {
try {
await deployDevNetFaucet(chainId);
succeededDeployments.push(chainId);
}
catch (error) {
const message = error instanceof Error ? error.message : '';
failedDeployments.push({ message: message, chainId });
}
}));
return [succeededDeployments, failedDeployments];
}
//# sourceMappingURL=fundHelpers.js.map