(
args: CommonArgs<{
to?: string;
amount?: string;
fee?: string;
feeRate?: string;
feeLevel?: string;
nonce?: number;
note?: string;
dryRun?: boolean;
}>
)
| 47 | } |
| 48 | |
| 49 | export 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(); |
nothing calls this directly
no test coverage detected