MCPcopy Create free account
hub / github.com/bitpay/bitcore / createTransaction

Function createTransaction

packages/bitcore-cli/src/commands/transaction.ts:49–244  ·  view source on GitHub ↗
(
  args: CommonArgs<{
    to?: string;
    amount?: string;
    fee?: string;
    feeRate?: string;
    feeLevel?: string;
    nonce?: number;
    note?: string;
    dryRun?: boolean;
  }>
)

Source from the content-addressed store, hash-verified

47}
48
49export async function createTransaction(
50 args: CommonArgs<{
51 to?: string;
52 amount?: string;
53 fee?: string;
54 feeRate?: string;
55 feeLevel?: string;
56 nonce?: number;
57 note?: string;
58 dryRun?: boolean;
59 }>
60) {
61 const { wallet, opts } = args;
62 let { status } = args;
63 const { chain, network } = wallet.client.credentials;
64
65 if (opts.command) {
66 Object.assign(opts, command(args));
67 }
68
69 if (wallet.isReadOnly()) {
70 throw new Error('Read-only wallets cannot create transactions');
71 }
72
73 let tokenObj: ITokenObj;
74 if (opts.token || opts.tokenAddress) {
75 tokenObj = await wallet.getToken(opts);
76 if (!tokenObj) {
77 throw new Error(`Unknown token "${opts.tokenAddress || opts.token}" on ${chain}:${network}`);
78 }
79 }
80 const nativeCurrency = (await wallet.getNativeCurrency(true)).displayCode;
81
82 if (!status) {
83 status = await wallet.client.getStatus({ tokenAddress: tokenObj?.contractAddress });
84 }
85
86 const { balance } = status;
87 const currency = tokenObj?.displayCode || nativeCurrency;
88 const availableAmount = Utils.amountFromSats(chain, balance.availableAmount, tokenObj);
89
90 if (!balance.availableAmount) {
91 prompt.log.warn(`You have no available balance to send ${currency} on ${chain}:${network}.`);
92 return;
93 }
94
95
96 const to = opts.to || await prompt.text({
97 message: 'Enter the recipient\'s address:',
98 validate: (value) => {
99 if (!Validation.validateAddress(chain, network, value)) {
100 return `Invalid address for ${chain}:${network}`;
101 }
102 return; // valid value
103 }
104 });
105 if (prompt.isCancel(to)) {
106 throw new UserCancelled();

Callers

nothing calls this directly

Calls 15

amountFromSatsMethod · 0.80
warnMethod · 0.80
amountToSatsMethod · 0.80
capitalizeMethod · 0.80
displayFeeRateMethod · 0.80
splitMethod · 0.80
createTxProposalMethod · 0.80
renderAmountMethod · 0.80
publishTxProposalMethod · 0.80
colorTextMethod · 0.80
infoMethod · 0.80
commandFunction · 0.70

Tested by

no test coverage detected