@muirglacier/jellyfish-transaction-builder
Version:
A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance for Bitcoin
78 lines • 3.38 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxnBuilderUtxo = void 0;
const txn_builder_1 = require("./txn_builder");
const jellyfish_transaction_1 = require("@muirglacier/jellyfish-transaction");
class TxnBuilderUtxo extends txn_builder_1.P2WPKHTxnBuilder {
/**
* Send all UTXO provided by prevoutProvider.all() to script.
*
* @param {Script} toScript to send output to
*/
sendAll(toScript) {
return __awaiter(this, void 0, void 0, function* () {
const { prevouts, vin, total } = yield this.allPrevouts();
const to = {
value: total,
script: toScript,
tokenId: 0x00
};
const txn = {
version: jellyfish_transaction_1.DeFiTransactionConstants.Version,
vin: vin,
vout: [to],
lockTime: 0x00000000
};
const fee = yield this.calculateFee(txn);
to.value = total.minus(fee);
return yield this.sign(txn, prevouts);
});
}
/**
* Send a specific amount of UTXO provided by prevoutProvider.collect(amount, fee) to script.
* If you are not sending the full amount via sendAll, you will need at least 0.001 DFI more
* than the specific amount for sending. This will also evidently merge small prevout during
* the operation.
*
* @param {BigNumber} amount of UTXO to send to script
* @param {Script} toScript to send UTXO to
* @param {Script} changeScript to send unspent to after deducting the fees
*/
send(amount, toScript, changeScript) {
return __awaiter(this, void 0, void 0, function* () {
const minAmount = amount.plus(0.001);
const { prevouts, vin, total } = yield this.collectPrevouts(minAmount);
const changeAmount = total.minus(amount);
const to = {
value: amount,
script: toScript,
tokenId: 0x00
};
const change = {
value: changeAmount,
script: changeScript,
tokenId: 0x00
};
const txn = {
version: jellyfish_transaction_1.DeFiTransactionConstants.Version,
vin: vin,
vout: [to, change],
lockTime: 0x00000000
};
const fee = yield this.calculateFee(txn);
change.value = changeAmount.minus(fee);
return yield this.sign(txn, prevouts);
});
}
}
exports.TxnBuilderUtxo = TxnBuilderUtxo;
//# sourceMappingURL=txn_builder_utxo.js.map