@ledgerhq/coin-stacks
Version:
Ledger Stacks Coin integration
41 lines • 2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.estimateMaxSpendable = void 0;
const index_1 = require("@ledgerhq/coin-framework/account/index");
const abandonseed_1 = require("@ledgerhq/cryptoassets/abandonseed");
const transactions_1 = require("@stacks/transactions");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const invariant_1 = __importDefault(require("invariant"));
const api_types_1 = require("../network/api.types");
const createTransaction_1 = require("./createTransaction");
const estimateMaxSpendable = async ({ account, parentAccount, transaction, }) => {
const mainAccount = (0, index_1.getMainAccount)(account, parentAccount);
const { spendableBalance, xpub } = mainAccount;
(0, invariant_1.default)(xpub, "xpub is required");
const dummyTx = {
...(0, createTransaction_1.createTransaction)(account),
...transaction,
recipient: (0, abandonseed_1.getAbandonSeedAddress)(mainAccount.currency.id),
useAllAmount: true,
};
// Compute fees
const { recipient, anchorMode, memo, amount } = dummyTx;
const network = api_types_1.StacksNetwork[dummyTx.network] || api_types_1.StacksNetwork["mainnet"];
const options = {
recipient,
anchorMode,
memo,
network,
publicKey: xpub,
amount: amount.toFixed(),
};
const tx = await (0, transactions_1.makeUnsignedSTXTokenTransfer)(options);
const [feeEst] = await (0, transactions_1.estimateTransaction)(tx.payload, (0, transactions_1.estimateTransactionByteLength)(tx), network);
const diff = spendableBalance.minus(new bignumber_js_1.default(feeEst.fee));
return diff.gte(0) ? diff : new bignumber_js_1.default(0);
};
exports.estimateMaxSpendable = estimateMaxSpendable;
//# sourceMappingURL=estimateMaxSpendable.js.map