meteor-nem-library
Version:
Meteor NIS1 Library
129 lines • 6.06 kB
JavaScript
;
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 NEM
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Mosaic_1 = require("../../models/mosaic/Mosaic");
const XEM_1 = require("../../models/mosaic/XEM");
const TransferTransaction_1 = require("../../models/transaction/TransferTransaction");
const bAddress_1 = require("./bAddress");
const bMosaic_1 = require("./bMosaic");
const bPlainMessage_1 = require("./bPlainMessage");
const bTimeWindow_1 = require("./bTimeWindow");
var ExpirationType;
(function (ExpirationType) {
ExpirationType[ExpirationType["oneHour"] = 1] = "oneHour";
ExpirationType[ExpirationType["twoHour"] = 2] = "twoHour";
ExpirationType[ExpirationType["sixHour"] = 6] = "sixHour";
ExpirationType[ExpirationType["twelveHour"] = 12] = "twelveHour";
})(ExpirationType = exports.ExpirationType || (exports.ExpirationType = {}));
class BTransferTransaction extends TransferTransaction_1.TransferTransaction {
/**
* @internal
* @param recipient
* @param amount
* @param timeWindow
* @param version
* @param fee
* @param message
* @param signature
* @param mosaic
* @param sender
* @param transactionInfo
*/
constructor(recipient, amount, timeWindow, version, fee, message, signature, mosaic, sender, transactionInfo) {
super(recipient, amount, timeWindow, version, fee, message, signature, mosaic, sender, transactionInfo);
/**
* returns mosaic array of received mosaics
* @returns {MosaicTransferable[]}
*/
this.mosaicDetails = () => {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
if (this.containsMosaics()) {
resolve(yield Promise.all(this.mosaics().map((mosaic) => __awaiter(this, void 0, void 0, function* () {
const bMosaic = new bMosaic_1.BMosaic(mosaic.mosaicId, mosaic.quantity);
return yield bMosaic.getMosaicDetails();
}))));
}
else {
resolve([this.xem()]);
}
}
catch (err) {
reject(err);
}
}));
};
}
}
/**
* Create a CacheTransferTransaction object
* @param recipient
* @param mosaic
* @param message
* @param timeWindow - 2 hours default, can't exceed 23 hours
* @returns {TransferTransaction}
*/
BTransferTransaction.createTX = (recipient, mosaic, message, timeWindow) => {
if (mosaic.mosaicId.namespaceId === 'nem' && mosaic.mosaicId.name === 'xem') {
return TransferTransaction_1.TransferTransaction.create(timeWindow, recipient, XEM_1.XEM.fromAbsolute(mosaic.quantity), message);
}
else {
return TransferTransaction_1.TransferTransaction.createWithMosaics(timeWindow, recipient, [mosaic], message);
}
};
/**
* Create a TransferTransaction from object
* @param transferTransaction - transferTransaction object from outside source
* @returns {BTransferTransaction}
*/
BTransferTransaction.castToTransferTransaction = (transferTransaction) => {
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
const recipient = bAddress_1.BAddress.castToAddress(transferTransaction.recipient);
const message = bPlainMessage_1.BPlainMessage.castToPlainMessage(transferTransaction.message);
const xem = XEM_1.XEM.fromAbsolute(transferTransaction._xem.quantity);
const timeWindow = yield bTimeWindow_1.BTimeWindow.useNodeToCreateDeadline();
if (transferTransaction._mosaics) {
resolve(new BTransferTransaction(recipient, xem, timeWindow, 2, transferTransaction.fee, message, undefined, transferTransaction._mosaics.map((_) => { return new Mosaic_1.Mosaic(_.mosaicId, _.quantity); })));
}
else {
resolve(new BTransferTransaction(recipient, xem, timeWindow, 1, transferTransaction.fee, message, undefined, undefined));
}
}
catch (err) {
reject(err);
}
}));
};
exports.BTransferTransaction = BTransferTransaction;
//# sourceMappingURL=bTransferTransaction.js.map