@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
365 lines (318 loc) • 13.4 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
import _merge from "ramda/src/merge";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context5; _forEachInstanceProperty(_context5 = ownKeys(Object(source), true)).call(_context5, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context6; _forEachInstanceProperty(_context6 = ownKeys(Object(source))).call(_context6, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/**
* Ae module
* @module @aeternity/aepp-sdk/es/ae
* @export Ae
* @example import { Ae } from '@aeternity/aepp-sdk'
*/
import stampit from '@stamp/it';
import Tx from '../tx';
import Chain from '../chain';
import AccountBase from '../account/base';
import TxBuilder from '../tx/builder';
import BigNumber from 'bignumber.js';
import { AE_AMOUNT_FORMATS } from '../utils/amount-formatter';
/**
* Sign and post a transaction to the chain
* @instance
* @category async
* @rtype (tx: String, options: Object) => Promise[String]
* @param {String} tx - Transaction
* @param {Object} [options={}] options - Options
* @param {Object} [options.verify] verify - Verify transaction before broadcast, throw error if not valid
* @return {Object} Transaction
*/
function send(_x) {
return _send.apply(this, arguments);
}
function _send() {
_send = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(tx) {
var options,
opt,
_yield$this$getAccoun,
gaId,
authFun,
signed,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
opt = _merge(this.Ae.defaults, options);
_context.t0 = this;
_context.next = 5;
return this.address(opt);
case 5:
_context.t1 = _context.sent;
_context.next = 8;
return _context.t0.getAccount.call(_context.t0, _context.t1);
case 8:
_yield$this$getAccoun = _context.sent;
gaId = _yield$this$getAccoun.contractId;
authFun = _yield$this$getAccoun.authFun;
if (!gaId) {
_context.next = 17;
break;
}
_context.next = 14;
return this.signUsingGA(tx, _objectSpread(_objectSpread({}, opt), {}, {
authFun: authFun
}));
case 14:
_context.t2 = _context.sent;
_context.next = 20;
break;
case 17:
_context.next = 19;
return this.signTransaction(tx, opt);
case 19:
_context.t2 = _context.sent;
case 20:
signed = _context.t2;
return _context.abrupt("return", this.sendTransaction(signed, opt));
case 22:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return _send.apply(this, arguments);
}
function signUsingGA(_x2) {
return _signUsingGA.apply(this, arguments);
}
/**
* Send tokens to another account
* @instance
* @category async
* @rtype (amount: Number|String, recipientIdOrName: String, options?: Object) => Promise[String]
* @param {Number|String} amount - Amount to spend
* @param {String} recipientIdOrName - Address or name of recipient account
* @param {Object} [options] - Options
* @return {Object} Transaction
*/
function _signUsingGA() {
_signUsingGA = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(tx) {
var _ref,
authData,
authFun,
options,
_args2 = arguments;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_ref = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
authData = _ref.authData, authFun = _ref.authFun, options = _objectWithoutProperties(_ref, ["authData", "authFun"]);
return _context2.abrupt("return", this.createMetaTx(tx, authData, authFun, options));
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
return _signUsingGA.apply(this, arguments);
}
function spend(_x3, _x4, _x5) {
return _spend.apply(this, arguments);
} // TODO: Rename to spendFraction
/**
* Send a fraction of token balance to another account
* @instance
* @category async
* @rtype (fraction: Number|String, recipientIdOrName: String, options?: Object) => Promise[String]
* @param {Number|String} fraction - Fraction of balance to spend (between 0 and 1)
* @param {String} recipientIdOrName - Address or name of recipient account
* @param {Object} [options] - Options
* @return {Object} Transaction
*/
function _spend() {
_spend = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(amount, recipientIdOrName, options) {
var opt;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
opt = _objectSpread(_objectSpread({}, this.Ae.defaults), options);
_context3.t0 = this;
_context3.t1 = this;
_context3.t2 = _objectSpread;
_context3.t3 = _objectSpread({}, opt);
_context3.t4 = {};
_context3.next = 8;
return this.address(opt);
case 8:
_context3.t5 = _context3.sent;
_context3.next = 11;
return this.resolveName(recipientIdOrName, 'ak', opt);
case 11:
_context3.t6 = _context3.sent;
_context3.t7 = amount;
_context3.t8 = {
senderId: _context3.t5,
recipientId: _context3.t6,
amount: _context3.t7
};
_context3.t9 = (0, _context3.t2)(_context3.t3, _context3.t4, _context3.t8);
_context3.next = 17;
return _context3.t1.spendTx.call(_context3.t1, _context3.t9);
case 17:
_context3.t10 = _context3.sent;
_context3.t11 = opt;
return _context3.abrupt("return", _context3.t0.send.call(_context3.t0, _context3.t10, _context3.t11));
case 20:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
return _spend.apply(this, arguments);
}
function transferFunds(_x6, _x7, _x8) {
return _transferFunds.apply(this, arguments);
}
/**
* Remove all listeners for RPC
* @instance
* @return {void}
*/
function _transferFunds() {
_transferFunds = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(fraction, recipientIdOrName, options) {
var opt, recipientId, senderId, balance, desiredAmount, _TxBuilder$unpackTx, fee, amount;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!(fraction < 0 || fraction > 1)) {
_context4.next = 2;
break;
}
throw new Error("Fraction should be a number between 0 and 1, got ".concat(fraction));
case 2:
opt = _objectSpread(_objectSpread({}, this.Ae.defaults), options);
_context4.next = 5;
return this.resolveName(recipientIdOrName, 'ak', opt);
case 5:
recipientId = _context4.sent;
_context4.next = 8;
return this.address(opt);
case 8:
senderId = _context4.sent;
_context4.t0 = BigNumber;
_context4.next = 12;
return this.balance(senderId);
case 12:
_context4.t1 = _context4.sent;
balance = new _context4.t0(_context4.t1);
desiredAmount = balance.times(fraction).integerValue(BigNumber.ROUND_HALF_UP);
_context4.t2 = TxBuilder;
_context4.next = 18;
return this.spendTx(_objectSpread(_objectSpread({}, opt), {}, {
senderId: senderId,
recipientId: recipientId,
amount: desiredAmount
}));
case 18:
_context4.t3 = _context4.sent;
_TxBuilder$unpackTx = _context4.t2.unpackTx.call(_context4.t2, _context4.t3);
fee = _TxBuilder$unpackTx.tx.fee;
// Reducing of the amount may reduce transaction fee, so this is not completely accurate
amount = desiredAmount.plus(fee).gt(balance) ? balance.minus(fee) : desiredAmount;
_context4.t4 = this;
_context4.next = 25;
return this.spendTx(_objectSpread(_objectSpread({}, opt), {}, {
senderId: senderId,
recipientId: recipientId,
amount: amount
}));
case 25:
_context4.t5 = _context4.sent;
_context4.t6 = opt;
return _context4.abrupt("return", _context4.t4.send.call(_context4.t4, _context4.t5, _context4.t6));
case 28:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
return _transferFunds.apply(this, arguments);
}
function destroyInstance() {
var _this = this;
var destroyMethods = ['destroyClient', 'destroyServer']; // Array with destroy function's
_forEachInstanceProperty(destroyMethods).call(destroyMethods, function (m) {
return _this[m] && typeof _this[m] === 'function' && _this[m]();
});
}
/**
* Basic Ae Stamp
*
* Attempting to create instances from the Stamp without overwriting all
* abstract methods using composition will result in an exception.
*
* Ae objects are the composition of three basic building blocks:
* * {@link module:@aeternity/aepp-sdk/es/tx--Tx}
* * {@link module:@aeternity/aepp-sdk/es/account--Account}
* * {@link module:@aeternity/aepp-sdk/es/chain--Chain}
* Only by providing the joint functionality of those three, most more advanced
* operations, i.e. the ones with actual use value on the chain, become
* available.
* @function
* @alias module:@aeternity/aepp-sdk/es/ae
* @rtype Stamp
* @param {Object} [options={}] - Initializer object
* @return {Object} Ae instance
*/
var Ae = stampit(Tx, AccountBase, Chain, {
methods: {
send: send,
spend: spend,
transferFunds: transferFunds,
destroyInstance: destroyInstance,
signUsingGA: signUsingGA
},
deepProps: {
Ae: {
defaults: {
denomination: AE_AMOUNT_FORMATS.AETTOS
}
}
}
});
export default Ae;
//# sourceMappingURL=index.js.map