UNPKG

eosplayer

Version:

eosplayer is the glue layer of eosjs, which is packaged based on eosjs and provides better usability for the application layer. It can be used on browsers already installed scatter or in Dapp wallets.

352 lines 17.5 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 asset_1 = require("./model/asset"); var log_1 = require("./utils/log"); var chain_1 = require("./helpers/chain"); var kh_1 = require("./helpers/kh"); var eosProvider_1 = require("./model/eosProvider"); var eventHandler_1 = require("./utils/eventHandler"); var log = log_1.createLogger('chain'); var packageJson = require('../package.json'); /** * event names supported in player * @author kinghand@foxmail.com * @type {{ERR_TRANSCAL_FAILED: string}} */ var EVENT_NAMES = { ERR_TRANSFER_FAILED: 'ERR_TRANSFER_FAILED', ERR_TRANSCAL_FAILED: 'ERR_TRANSCAL_FAILED', ERR_TRANSEND_FAILED: 'ERR_TRANSEND_FAILED', }; /** * Player */ var Player = /** @class */ (function (_super) { __extends(Player, _super); function Player() { var _this = _super.call(this) || this; _this._events = new eventHandler_1.default(); _this.events.enableEvents(EVENT_NAMES); return _this; } Object.defineProperty(Player.prototype, "events", { get: function () { return this._events || (this._events = new eventHandler_1.default()); }, enumerable: true, configurable: true }); Object.defineProperty(Player.prototype, "chain", { get: function () { return new chain_1.default(this.eosClient); }, enumerable: true, configurable: true }); Object.defineProperty(Player.prototype, "kh", { get: function () { return new kh_1.default(this.chain); }, enumerable: true, configurable: true }); /** * get account info of any user, if the account name not given, account info of current identity will return * @param account_name * @return {Promise<{AccountInfo}>} */ Player.prototype.getAccountInfo = function (account_name) { return __awaiter(this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = this.chain).getAccountInfo; _c = account_name; if (_c) return [3 /*break*/, 2]; return [4 /*yield*/, this.getIdentity()]; case 1: _c = (_d.sent()).name; _d.label = 2; case 2: return [4 /*yield*/, _b.apply(_a, [_c])]; case 3: return [2 /*return*/, _d.sent()]; } }); }); }; /** * get balance of specific account * @param code - Account of the currency contract. The default code is "eosio.token", which is the currency code of eos * @param account_name - user's account name, name of cur identity by default * @param symbolName - the token's symbol name * @return {Promise<string|undefined>} asset format '1.0000 EOS' */ Player.prototype.getBalance = function (account_name, code, symbolName) { if (code === void 0) { code = 'eosio.token'; } return __awaiter(this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = this.chain).getBalance; _c = account_name; if (_c) return [3 /*break*/, 2]; return [4 /*yield*/, this.getIdentity()]; case 1: _c = (_d.sent()).name; _d.label = 2; case 2: return [2 /*return*/, _b.apply(_a, [_c, code, symbolName])]; } }); }); }; /** * get balances list of specific account * @param code - Account of the currency contract. The default code is "eosio.token" * @param account_name - user's account name, name of cur identity by default * @return {Promise<Array.<string>>} asset format '1.0000 EOS' */ Player.prototype.getBalances = function (account_name, code) { if (code === void 0) { code = 'eosio.token'; } return __awaiter(this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = this.chain).getBalances; _c = account_name; if (_c) return [3 /*break*/, 2]; return [4 /*yield*/, this.getIdentity()]; case 1: _c = (_d.sent()).name; _d.label = 2; case 2: return [2 /*return*/, _b.apply(_a, [_c, code])]; } }); }); }; /** * get balance value of specific account * @param code - Account of the currency contract. The default code is "eosio.token", which is the currency code of eos * @param account_name - user's account name, name of cur identity by default * @return {Promise<Asset>} */ Player.prototype.getBalanceAsset = function (account_name, code) { if (code === void 0) { code = 'eosio.token'; } return __awaiter(this, void 0, void 0, function () { var strAsset; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getBalance(account_name, code)]; case 1: strAsset = _a.sent(); return [2 /*return*/, asset_1.default.parse(strAsset)]; } }); }); }; /** * transfer * @param {string} target - eos account, can be user or contract * @param {string} quantity - eos asset format, e.p. "1.0000 EOS" * @param {string} memo - memo * @param {string} tokenAccount - name of token account contract, default is 'eosio.token' * @return {Promise<Object>} transactionData */ Player.prototype.transfer = function (target, quantity, memo, tokenAccount) { if (memo === void 0) { memo = ''; } return __awaiter(this, void 0, void 0, function () { var _a, _b; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: _b = (_a = this.chain).transfer; return [4 /*yield*/, this.getIdentity()]; case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent(), target, quantity, memo, function (err) { return _this.events.emitEvent(EVENT_NAMES.ERR_TRANSFER_FAILED, err); }, tokenAccount])]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; /** * call kh contract with transfer (match eoskit) * @param {string} target - eos account, can be user or contract * @param {string} quantity - eos asset format, e.p. "1.0000 EOS" * @param {string} func - function name * @param {Array} args * @return {Promise<Object>} transactionData */ Player.prototype.transcal = function (target, quantity, func) { var args = []; for (var _i = 3; _i < arguments.length; _i++) { args[_i - 3] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var _a, _b; var _this = this; return __generator(this, function (_c) { switch (_c.label) { case 0: _b = (_a = this.kh).transcal; return [4 /*yield*/, this.getIdentity()]; case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent(), target, quantity, func, args, function (err) { return _this.events.emitEvent(EVENT_NAMES.ERR_TRANSCAL_FAILED, err); }])]; case 2: return [2 /*return*/, _c.sent()]; } }); }); }; /** * send action to a contract * @param {string} code - account of contract * @param {string} func - function name * @param {*} jsonData - data * @return {Promise<*>} - transaction */ Player.prototype.call = function (code, func, jsonData) { return __awaiter(this, void 0, void 0, function () { var account, trx; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getIdentity()]; case 1: account = _a.sent(); return [4 /*yield*/, this.chain.call(code, func, jsonData, { actor: account.name, permission: account.authority, })]; case 2: trx = _a.sent(); if (trx) { log.info("call operation dealed, txID: " + trx.transaction_id); } return [2 /*return*/, trx]; } }); }); }; /** * create a account with public key * @param name * @param activeKey * @param ownerKey * @return {Promise<void>} */ Player.prototype.newAccount = function (name, activeKey, ownerKey) { return __awaiter(this, void 0, void 0, function () { var creator, eosClient; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!activeKey) { throw new Error('newAccount : active key error '); } if (!ownerKey) { ownerKey = activeKey; } return [4 /*yield*/, this.getIdentity()]; case 1: creator = _a.sent(); eosClient = this.eosClient; if (!eosClient) { throw new Error('eosClient is not exist'); } return [4 /*yield*/, this.eosClient.transaction(function (tr) { tr.newaccount({ creator: creator.name, name: name, owner: ownerKey, active: activeKey, }); tr.buyrambytes({ payer: creator.name, receiver: name, bytes: 8192, }); tr.delegatebw({ from: creator.name, receiver: name, stake_net_quantity: '1.0000 EOS', stake_cpu_quantity: '1.0000 EOS', transfer: 0, }); })]; case 2: return [2 /*return*/, _a.sent()]; } }); }); }; /** * get version */ Player.prototype.version = function () { return packageJson.name + " # " + packageJson.version; }; /** * get help info */ Player.prototype.help = function () { return "\n```js\n =============================================================\n\n ----- ------ ------ -------\n ----- ----- ------ -------\n ----- ----- ------ -------\n ----- ----- ------ -------\n ---------- ----- ---- ------\n ----- ----- ----- ---- ------\n ----- ----- ------ -------\n ----- ------ ------ -------\n ------ ------- ------ -------\n -------- --------- ------ -------\n\n===========================================================\n```\n---\n\n# eosplayer " + this.version() + "\n\n## Usage of eosplayer\n\n### Events\n\n`ERR_TRANSFER_FAILED`\n`ERR_TRANSCAL_FAILED`\n`ERR_TRANSEND_FAILED`\n\n### APIs\n\n```js\n{String} get help // get help info of usage\n{String} get version // get the version info\n{ChainHelper} get chain // get the chain helper\n{KhHelper} get kh // get the kh contract helper\n\n{Void} eosplayer.event.setEvent(event, fnCallback, context) //listen to a event\n\n{Eos} get eosplayer.eosClient // get eos instance\n{Identity} async eosplayer.getIdentity() // get identity\n\n{AccountInfo} async eosplayer.getAccountInfo(account_name = identity.name)\n // get account info for any user\n\n{String} async eosplayer.getBalance(account_name = undefined, code = \"eosio.token\", symbolName = undefined)\n // get balance string of a account. ex. \"1.0000 EOS\", null means that the account dosen't have any token,\n\n{Array.<String>} async getBalances(account_name = undefined, code = \"eosio.token\")\n // get balances\n\n{String} async eosplayer.getBalanceAsset(account_name = undefined, code = \"eosio.token\")\n // get balance structure of a account. ex. {val:1, sym:\"EOS\", decimal:4}\n\n{Tx} async eosplayer.transfer(target, quantity, memo = \"\")\n // transfer tokens to target\n\n{Tx} async eosplayer.transcal(code, quantity, func, ...args)\n // send a action of transcal to contract\n\n{Tx} async eosplayer.call(code, func, jsonData)\n // send a action to contract\n\n{Tx} async eosplayer.newAccount(name, activeKey, ownerKey)\n // create a account with public key\n```\n\n" + chain_1.default.help(); }; return Player; }(eosProvider_1.default)); exports.Player = Player; //# sourceMappingURL=player.js.map