import BigNumber from "bignumber.js"; import type { BitcoinInput, BitcoinOutput, Transaction } from "./types"; import { buildTransaction } from "./buildTransaction"; import { perCoinLogic } from "./logic"; import type { Account } from "@ledgerhq/types-live"; import type { BitcoinCoinConfig } from "./config"; import type { Logger } from "@ledgerhq/coin-module-framework/config"; const getFeesForTransaction = async ( config: BitcoinCoinConfig, logger: Logger, { account, transaction, }: { account: Account; transaction: Transaction; }, ): Promise<{ fees: BigNumber; txInputs: BitcoinInput[]; txOutputs: BitcoinOutput[] }> => { const walletTx = await buildTransaction(config, logger, account, transaction); const fees = new BigNumber(walletTx.fee).integerValue(); let txInputs = walletTx.inputs.map(i => { return { address: i.address, value: new BigNumber(i.value), previousTxHash: i.output_hash, previousOutputIndex: i.output_index, }; }); let txOutputs = walletTx.outputs.map(o => { return { outputIndex: walletTx.outputs.indexOf(o), address: o.address, isChange: o.isChange, value: new BigNumber(o.value), hash: "", blockHeight: undefined, rbf: transaction.rbf, }; }); const perCoin = perCoinLogic[account.currency.id]; if (perCoin) { const { syncReplaceAddress } = perCoin; if (syncReplaceAddress) { txInputs = txInputs.map(i => ({ ...i, address: i.address && syncReplaceAddress(i.address), })); txOutputs = txOutputs.map(o => ({ ...o, address: o.address && syncReplaceAddress(o.address), })); } } return { fees, txInputs, txOutputs, }; }; export default getFeesForTransaction;