jcc-bizain-utils
Version:
Toolkit of crossing chain from Bizain chain to SWTC chain
205 lines • 9.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var bignumber_js_1 = require("bignumber.js");
var jcc_jingtum_lib_1 = require("jcc_jingtum_lib");
var jtWallet = require("jcc_wallet/lib/jingtum");
var validator_1 = require("./validator");
var BizainFingate = /** @class */ (function () {
function BizainFingate(server) {
this._remote = null;
this._localSign = true;
this._fee = 10;
this._currency = "BIZ";
this._issuer = "bf42S78serP2BeSx7HGtwQR2QASYaHVqyb";
this._server = server;
}
Object.defineProperty(BizainFingate.prototype, "localSign", {
set: function (v) {
this._localSign = v;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BizainFingate.prototype, "fee", {
set: function (v) {
this._fee = v;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BizainFingate.prototype, "currency", {
set: function (v) {
this._currency = v;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BizainFingate.prototype, "remote", {
get: function () {
return this._remote;
},
enumerable: true,
configurable: true
});
BizainFingate.isValidAddress = function (address) {
return jtWallet.isValidAddress(address, BizainFingate._token);
};
BizainFingate.isValidSecret = function (secret) {
return jtWallet.isValidSecret(secret, BizainFingate._token);
};
BizainFingate.getAddress = function (secret) {
return jtWallet.getAddress(secret, BizainFingate._token);
};
BizainFingate.prototype.init = function () {
var _server = {
local_sign: this._localSign,
server: this._server,
token: BizainFingate._token
};
this._remote = new jcc_jingtum_lib_1.Remote(_server);
return this;
};
BizainFingate.prototype.connect = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
_this._remote.connect(function (error) {
if (error) {
return reject(error);
}
return resolve();
});
})];
});
});
};
BizainFingate.prototype.disconnect = function () {
this._remote.disconnect();
};
/**
* request balance of currency
*
* @param {string} address
* @returns {Promise<string>}
* @memberof BizainFingate
*/
BizainFingate.prototype.balanceOf = function (address) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._remote.requestAccountRelations({
account: address,
type: "trust"
}).submit(function (err, results) {
if (err) {
return reject(err);
}
try {
var lines = results.lines;
var currencyInfo = lines.find(function (line) { return line.currency.toUpperCase() === _this._currency.toUpperCase(); });
return resolve(currencyInfo.balance);
}
catch (error) {
return resolve("0");
}
});
});
};
/**
* transfer token
*
* @param {string} secret
* @param {string} destination
* @param {string} value amount
* @param {IMemo} memo
* @returns {Promise<string>}
* @memberof BizainFingate
*/
BizainFingate.prototype.transfer = function (secret, destination, value, memo) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
var address = BizainFingate.getAddress(secret);
var tx = {
account: address,
amount: {
currency: _this._currency.toUpperCase(),
issuer: "",
value: new bignumber_js_1.default(value).toNumber()
},
to: destination
};
if (tx.amount.currency !== BizainFingate._token) {
tx.amount.issuer = _this._issuer;
}
var transaction = _this._remote.buildPaymentTx(tx);
transaction.setTransferRate(_this._fee);
transaction.setSecret(secret);
transaction.addMemo(JSON.stringify(memo));
transaction.submit(function (error, res) {
if (error) {
return reject(error);
}
if (res.engine_result === "tesSUCCESS") {
return resolve(res.tx_json.hash);
}
return reject(new Error(res.engine_result_message));
});
})];
});
});
};
BizainFingate._token = "bwt";
__decorate([
validator_1.validate,
__param(0, validator_1.isValidBizainSecret), __param(1, validator_1.isValidBizainAddress), __param(2, validator_1.isValidAmount), __param(3, validator_1.isValidMemo)
], BizainFingate.prototype, "transfer", null);
return BizainFingate;
}());
exports.default = BizainFingate;
//# sourceMappingURL=bizainFingate.js.map