UNPKG

@muirglacier/jellyfish-transaction-builder

Version:

A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance for Bitcoin

118 lines 6.11 kB
"use strict"; 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.TxnBuilderICXOrderBook = void 0; const jellyfish_transaction_1 = require("@muirglacier/jellyfish-transaction"); const txn_builder_1 = require("./txn_builder"); const txn_builder_error_1 = require("./txn_builder_error"); class TxnBuilderICXOrderBook extends txn_builder_1.P2WPKHTxnBuilder { /** * Creates ICXCreateOrder transaction. * * @param {ICXCreateOrder} createOrder Create order txn to create * @param {Script} changeScript to send unspent to after deducting the (converted + fees) * @returns {Promise<TransactionSegWit>} */ createOrder(createOrder, changeScript) { return __awaiter(this, void 0, void 0, function* () { if (createOrder.receivePubkey !== undefined) { const bufferLen = Buffer.from(createOrder.receivePubkey, 'hex').length; if (bufferLen !== 33 && bufferLen !== 65) { throw new txn_builder_error_1.TxnBuilderError(txn_builder_error_1.TxnBuilderErrorType.INVALID_PUB_KEY_LENGTH, 'Create order receivePubkey buffer length should be 33 (COMPRESSED_PUBLIC_KEY_SIZE) or 65 (PUBLIC_KEY_SIZE)'); } } if (!createOrder.amountToFill.isEqualTo(createOrder.amountFrom)) { throw new txn_builder_error_1.TxnBuilderError(txn_builder_error_1.TxnBuilderErrorType.INVALID_ICX_CREATE_ORDER_AMOUNT_TO_FILL, 'Create order amountToFill should always equal amountFrom'); } return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_CREATE_ORDER(createOrder), changeScript); }); } /** * Creates ICXMakeOffer transaction. * * @param {ICXMakeOffer} makeOffer txn to create * @param {Script} changeScript to send unspent to after deducting the (converted + fees) * @returns {Promise<TransactionSegWit>} */ makeOffer(makeOffer, changeScript) { return __awaiter(this, void 0, void 0, function* () { if (makeOffer.receivePubkey !== undefined) { const bufferLen = Buffer.from(makeOffer.receivePubkey, 'hex').length; if (bufferLen !== 33 && bufferLen !== 65) { throw new txn_builder_error_1.TxnBuilderError(txn_builder_error_1.TxnBuilderErrorType.INVALID_PUB_KEY_LENGTH, 'Make offer receivePubkey buffer length should be 33 (COMPRESSED_PUBLIC_KEY_SIZE) or 65 (PUBLIC_KEY_SIZE)'); } } return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_MAKE_OFFER(makeOffer), changeScript); }); } /** * Creates ICXCloseOrder transaction. * * @param {ICXCloseOrder} closeOrder txn to create * @param {Script} changeScript to send unspent to after deducting the (converted + fees) * @returns {Promise<TransactionSegWit>} */ closeOrder(closeOrder, changeScript) { return __awaiter(this, void 0, void 0, function* () { return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_CLOSE_ORDER(closeOrder), changeScript); }); } /** * Creates ICXCloseOffer transaction. * * @param {ICXCloseOffer} closeOffer txn to create * @param {Script} changeScript to send unspent to after deducting the (converted + fees) * @returns {Promise<TransactionSegWit>} */ closeOffer(closeOffer, changeScript) { return __awaiter(this, void 0, void 0, function* () { return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_CLOSE_OFFER(closeOffer), changeScript); }); } /** * Creates submitDFCHTLC transaction. * * @param {ICXSubmitDFCHTLC} icxSubmitDFCHTLC txn to create * @param {Script} changeScript to send unspent to after deducting the (transfer value + fees) * @returns {Promise<TransactionSegWit>} */ submitDFCHTLC(icxSubmitDFCHTLC, changeScript) { return __awaiter(this, void 0, void 0, function* () { return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_SUBMIT_DFC_HTLC(icxSubmitDFCHTLC), changeScript); }); } /** * Creates submitEXTHTLC transaction. * * @param {ICXSubmitEXTHTLC} icxSubmitEXTHTLC txn to create * @param {Script} changeScript to send unspent to after deducting the (transfer value + fees) * @returns {Promise<TransactionSegWit>} */ submitEXTHTLC(icxSubmitEXTHTLC, changeScript) { return __awaiter(this, void 0, void 0, function* () { return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_SUBMIT_EXT_HTLC(icxSubmitEXTHTLC), changeScript); }); } /** * Creates claimDFCHTLC transaction. * * @param {ICXClaimDFCHTLC} icxClaimDFCHTLC txn to create * @param {Script} changeScript to send unspent to after deducting the (transfer value + fees) * @returns {Promise<TransactionSegWit>} */ claimDFCHTLC(icxClaimDFCHTLC, changeScript) { return __awaiter(this, void 0, void 0, function* () { return yield this.createDeFiTx(jellyfish_transaction_1.OP_CODES.OP_DEFI_TX_ICX_CLAIM_DFC_HTLC(icxClaimDFCHTLC), changeScript); }); } } exports.TxnBuilderICXOrderBook = TxnBuilderICXOrderBook; //# sourceMappingURL=txn_builder_icxorderbook.js.map