UNPKG

@kadena/kadena-cli

Version:

Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)

85 lines 3.43 kB
import { isNotEmptyString, notEmpty } from '../../../utils/globalHelpers.js'; import { sortChainIds } from './accountHelpers.js'; import { createAndTransferFund } from './createAndTransferFunds.js'; import { getAccountDetails } from './getAccountDetails.js'; import { transferFund } from './transferFund.js'; const formatAccountCreatedMsgs = (msgs) => { if (msgs.length === 0) return null; const [pre, ...chainIds] = msgs; const sortedChainIds = sortChainIds(chainIds); return `${pre} ${sortedChainIds.join(', ')}. So the account will be created on these Chain ID(s).`; }; export async function fund({ accountConfig, amount, networkConfig, chainIds, }) { let status = 'success'; const errors = []; const warnings = []; const accountCreatedMsgs = []; let accountFundsResult = []; try { accountFundsResult = await Promise.all(chainIds.map(async (chainId) => { try { const accountDetailsFromChain = await getAccountDetails({ accountName: accountConfig.name, networkId: networkConfig.networkId, chainId: chainId, networkHost: networkConfig.networkHost, fungible: accountConfig.fungible, }); const result = accountDetailsFromChain ? await transferFund({ accountName: accountConfig.name, config: { contract: accountConfig.fungible, amount, chainId, networkConfig, }, }) : await createAndTransferFund({ account: { name: accountConfig.name, publicKeys: accountConfig.publicKeys, predicate: accountConfig.predicate, }, config: { ...accountConfig, networkConfig, contract: accountConfig.fungible, amount, chainId, }, }); if (!accountDetailsFromChain) { if (accountCreatedMsgs.length === 0) { accountCreatedMsgs.push(`Account "${accountConfig.name}" does not exist on Chain ID(s)`); } accountCreatedMsgs.push(chainId); } return result; } catch (error) { status = 'partial'; warnings.push(`Error on Chain ID ${chainId} - ${error.message}`); return null; } })); } catch (error) { status = 'error'; errors.push(error.message); } const nonEmptyData = accountFundsResult.filter(notEmpty); status = nonEmptyData.length === 0 ? 'error' : status; const formattedAccountMsgs = formatAccountCreatedMsgs(accountCreatedMsgs); if (isNotEmptyString(formattedAccountMsgs)) { warnings.push(formattedAccountMsgs); } return { status, data: nonEmptyData, errors, warnings: warnings.filter(notEmpty), }; } //# sourceMappingURL=fund.js.map