eos-common
Advanced tools
Comparing version 0.6.0 to 0.6.2
@@ -17,2 +17,9 @@ import { Name } from "./name"; | ||
* @param con - The name of the contract | ||
* @example | ||
* | ||
* // string | ||
* extended_symbol("EOS,4", "eosio.token") | ||
* | ||
* // class | ||
* new ExtendedSymbol(new Sym("EOS", 4), new Name("eosio.token")) | ||
*/ | ||
@@ -19,0 +26,0 @@ constructor(sym?: Sym | string, contract?: Name | string); |
@@ -7,3 +7,1 @@ import { Sym } from "./symbol"; | ||
export declare function number_to_asset(amount: number, sym: Sym): Asset; | ||
export declare function number_to_bigint(num: number): bigint; | ||
export declare function isNull(value: any): boolean; |
@@ -17,1 +17,7 @@ /** | ||
export declare function vote2stake(vote: number): number; | ||
/** | ||
* Calculate producer vpay | ||
* | ||
* @return {bigint} producer pay as int64t | ||
*/ | ||
export declare function calculate_producer_per_vote_pay(total_votes: bigint, pervote_bucket: bigint, total_producer_vote_weight: bigint): bigint; |
@@ -1,4 +0,5 @@ | ||
import { Asset, Symbol } from "../" | ||
import { asset } from "../index" | ||
const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
console.log(quantity.to_string()); | ||
const quantity = asset("1.0000 EOS"); | ||
console.log(quantity.amount); | ||
console.log(quantity.symbol.precision()); |
@@ -1,2 +0,1 @@ | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/check.hpp | ||
/** | ||
@@ -22,5 +21,5 @@ * Assert if the predicate fails and use the supplied message. | ||
*/ | ||
class SymbolCode { | ||
var SymbolCode = /** @class */ (function () { | ||
// constructor() | ||
constructor(str) { | ||
function SymbolCode(str) { | ||
this.value = BigInt(0); | ||
@@ -31,7 +30,8 @@ if (typeof str == "number" || typeof str == 'bigint') { | ||
else if (str) { | ||
let value = BigInt(0); | ||
var value = BigInt(0); | ||
if (str.length > 7) { | ||
check(false, "string is too long to be a valid symbol_code"); | ||
} | ||
for (const itr of str.split("").reverse().join("")) { | ||
for (var _i = 0, _a = str.split("").reverse().join(""); _i < _a.length; _i++) { | ||
var itr = _a[_i]; | ||
if (itr < 'A' || itr > 'Z') { | ||
@@ -46,18 +46,22 @@ check(false, "only uppercase letters allowed in symbol_code string"); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'symbol_code'; | ||
} | ||
typeof() { return 'symbol_code'; } | ||
raw() { | ||
Object.defineProperty(SymbolCode.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'symbol_code'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
SymbolCode.prototype.typeof = function () { return 'symbol_code'; }; | ||
SymbolCode.prototype.raw = function () { | ||
return this.value; | ||
} | ||
isTruthy() { | ||
}; | ||
SymbolCode.prototype.isTruthy = function () { | ||
return this.value != BigInt(0); | ||
} | ||
isFalsy() { | ||
}; | ||
SymbolCode.prototype.isFalsy = function () { | ||
return this.value == BigInt(0); | ||
} | ||
length() { | ||
let sym = BigInt(this.value); | ||
let len = 0; | ||
}; | ||
SymbolCode.prototype.length = function () { | ||
var sym = BigInt(this.value); | ||
var len = 0; | ||
while (Number(sym) & 0xFF && len <= 7) { | ||
@@ -68,10 +72,10 @@ len++; | ||
return len; | ||
} | ||
to_string() { | ||
const mask = BigInt(0x00000000000000FF); | ||
}; | ||
SymbolCode.prototype.to_string = function () { | ||
var mask = BigInt(0x00000000000000FF); | ||
if (this.value == BigInt(0)) | ||
return ''; | ||
let begin = ""; | ||
let v = BigInt(this.value); | ||
for (let i = 0; i < 7; ++i, v >>= BigInt(8)) { | ||
var begin = ""; | ||
var v = BigInt(this.value); | ||
for (var i = 0; i < 7; ++i, v >>= BigInt(8)) { | ||
if (v == BigInt(0)) | ||
@@ -82,7 +86,7 @@ return begin; | ||
return begin; | ||
} | ||
is_valid() { | ||
let sym = BigInt(this.value); | ||
for (let i = BigInt(0); i < 7; i++) { | ||
const c = String.fromCharCode(Number(BigInt(sym) & BigInt(0xFF))); | ||
}; | ||
SymbolCode.prototype.is_valid = function () { | ||
var sym = BigInt(this.value); | ||
for (var i = BigInt(0); i < 7; i++) { | ||
var c = String.fromCharCode(Number(BigInt(sym) & BigInt(0xFF))); | ||
if (!("A" <= c && c <= "Z")) | ||
@@ -101,3 +105,3 @@ return false; | ||
return true; | ||
} | ||
}; | ||
/** | ||
@@ -108,5 +112,5 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
isEqual(comparison) { | ||
SymbolCode.prototype.isEqual = function (comparison) { | ||
return comparison.value === this.value; | ||
} | ||
}; | ||
/** | ||
@@ -117,5 +121,5 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
isNotEqual(comparison) { | ||
SymbolCode.prototype.isNotEqual = function (comparison) { | ||
return comparison.value !== this.value; | ||
} | ||
}; | ||
/** | ||
@@ -126,6 +130,7 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
isLessThan(comparison) { | ||
SymbolCode.prototype.isLessThan = function (comparison) { | ||
return this.value < comparison.value; | ||
} | ||
} | ||
}; | ||
return SymbolCode; | ||
}()); | ||
function symbol_code(str) { | ||
@@ -135,27 +140,6 @@ return new SymbolCode(str); | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return BigInt(quantity.amount) / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function bigint_to_asset(amount, sym) { | ||
return new Asset(BigInt(amount) * BigInt(Math.pow(10, sym.precision())), sym); | ||
} | ||
function asset_to_number(quantity) { | ||
if (Number(quantity.amount) == 0) | ||
return 0.0; | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function number_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
} | ||
function number_to_bigint(num) { | ||
return BigInt(num.toFixed(0)); | ||
} | ||
function isNull(value) { | ||
return value == undefined || value == null; | ||
} | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp | ||
class Sym { | ||
var Sym = /** @class */ (function () { | ||
/** | ||
@@ -174,3 +158,3 @@ * Symbol | ||
*/ | ||
constructor(sc, precision) { | ||
function Sym(sc, precision) { | ||
this.value = BigInt(0); | ||
@@ -184,9 +168,24 @@ if (isNull(sc) && isNull(precision)) { | ||
else if (typeof sc == "string") { | ||
check(!isNull(precision), "[precision] is required"); | ||
const symcode = new SymbolCode(sc).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision); | ||
// "precision,symbol_code" (ex: "4,EOS") | ||
if (sc.includes(",")) { | ||
var _a = sc.split(","), precision_str = _a[0], symcode_str = _a[1]; | ||
var precision_1 = Number(precision_str); | ||
check(!isNaN(precision_1), "[precision] must be number type"); | ||
check(!isNull(precision_1), "[precision] is required"); | ||
check(!isNull(symcode_str), "[symcode] is required"); | ||
var symcode = new SymbolCode(symcode_str).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision_1 || Number(precision_str || "")); | ||
// "symbol_code" + @param: precision | ||
} | ||
else { | ||
check(!isNaN(Number(precision)), "[precision] must be number type"); | ||
check(!isNull(precision), "[precision] is required"); | ||
check(!isNull(sc), "[symcode] is required"); | ||
var symcode = new SymbolCode(sc).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision); | ||
} | ||
} | ||
else if (typeof sc == "object") { | ||
check(!isNull(precision), "[precision] is required"); | ||
const symcode = sc; | ||
var symcode = sc; | ||
this.value = BigInt(symcode.raw()) << BigInt(8) | BigInt(precision); | ||
@@ -198,30 +197,34 @@ } | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'symbol'; | ||
} | ||
typeof() { return 'symbol'; } | ||
Object.defineProperty(Sym.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'symbol'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Sym.prototype.typeof = function () { return 'symbol'; }; | ||
/** | ||
* Is this symbol valid | ||
*/ | ||
is_valid() { | ||
Sym.prototype.is_valid = function () { | ||
return this.code().is_valid(); | ||
} | ||
}; | ||
/** | ||
* This symbol's precision | ||
*/ | ||
precision() { | ||
Sym.prototype.precision = function () { | ||
return Number(this.value & BigInt(0x00000000000000FF)); | ||
} | ||
}; | ||
/** | ||
* Returns representation of symbol name | ||
*/ | ||
code() { | ||
Sym.prototype.code = function () { | ||
return new SymbolCode(this.value >> BigInt(8)); | ||
} | ||
}; | ||
/** | ||
* Returns uint64_t repreresentation of the symbol | ||
*/ | ||
raw() { | ||
Sym.prototype.raw = function () { | ||
return this.value; | ||
} | ||
}; | ||
/** | ||
@@ -232,9 +235,10 @@ * Explicit cast to bool of the symbol | ||
*/ | ||
bool() { | ||
Sym.prototype.bool = function () { | ||
return this.value != BigInt(0); | ||
} | ||
}; | ||
/** | ||
* %Print the symbol | ||
*/ | ||
print(show_precision = true) { | ||
Sym.prototype.print = function (show_precision) { | ||
if (show_precision === void 0) { show_precision = true; } | ||
if (show_precision) { | ||
@@ -244,3 +248,3 @@ process.stdout.write(String(this.precision()) + ","); | ||
process.stdout.write(this.code().to_string()); | ||
} | ||
}; | ||
/** | ||
@@ -251,8 +255,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
Sym.isEqual = function (a, b) { | ||
return a.raw() == b.raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
Sym.prototype.isEqual = function (a) { | ||
return a.raw() == this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -263,8 +267,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
Sym.isNotEqual = function (a, b) { | ||
return a.raw() != b.raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Sym.prototype.isNotEqual = function (a) { | ||
return a.raw() != this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -275,9 +279,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
Sym.isLessThan = function (a, b) { | ||
return a.raw() < b.raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
Sym.prototype.isLessThan = function (a) { | ||
return this.raw() < a.raw(); | ||
} | ||
} | ||
}; | ||
return Sym; | ||
}()); | ||
function symbol(sc, precision) { | ||
@@ -296,6 +301,7 @@ return new Sym(sc, precision); | ||
function write_decimal(number, num_decimal_places, negative) { | ||
let str = ""; | ||
let num_digits = 0; | ||
let isNegative = false; | ||
for (const num of number.toString().split("").reverse()) { | ||
var str = ""; | ||
var num_digits = 0; | ||
var isNegative = false; | ||
for (var _i = 0, _a = number.toString().split("").reverse(); _i < _a.length; _i++) { | ||
var num = _a[_i]; | ||
if (num == "-") { | ||
@@ -321,3 +327,8 @@ isNegative = true; | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp | ||
function number_to_bigint(num) { | ||
return BigInt(num.toFixed(0)); | ||
} | ||
function isNull$1(value) { | ||
return value == undefined || value == null; | ||
} | ||
/** | ||
@@ -337,3 +348,3 @@ * Asset | ||
*/ | ||
class Asset { | ||
var Asset = /** @class */ (function () { | ||
/** | ||
@@ -345,3 +356,3 @@ * Construct a new asset given the symbol name and the amount | ||
*/ | ||
constructor(amount, sym) { | ||
function Asset(amount, sym) { | ||
/** | ||
@@ -355,8 +366,8 @@ * {int64_t} The amount of the asset | ||
this.symbol = symbol(); | ||
if (isNull(amount) && isNull(sym)) { | ||
if (isNull$1(amount) && isNull$1(sym)) { | ||
return; | ||
} | ||
else if (typeof amount == "string") { | ||
const [amount_str, symbol_str] = amount.split(" "); | ||
const precision = (amount_str.split(".")[1] || []).length; | ||
var _a = amount.split(" "), amount_str = _a[0], symbol_str = _a[1]; | ||
var precision = (amount_str.split(".")[1] || []).length; | ||
this.amount = number_to_bigint(Number(amount_str) * Math.pow(10, precision)); | ||
@@ -375,6 +386,10 @@ this.symbol = new Sym(symbol_str, precision); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'asset'; | ||
} | ||
typeof() { return 'asset'; } | ||
Object.defineProperty(Asset.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'asset'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Asset.prototype.typeof = function () { return 'asset'; }; | ||
/** | ||
@@ -386,5 +401,5 @@ * Check if the amount doesn't exceed the max amount | ||
*/ | ||
is_amount_within_range() { | ||
Asset.prototype.is_amount_within_range = function () { | ||
return -BigInt(Asset.max_amount) <= BigInt(this.amount) && this.amount <= Asset.max_amount; | ||
} | ||
}; | ||
/** | ||
@@ -396,5 +411,5 @@ * Check if the asset is valid. %A valid asset has its amount <= max_amount and its symbol name valid | ||
*/ | ||
is_valid() { | ||
Asset.prototype.is_valid = function () { | ||
return this.is_amount_within_range() && this.symbol.is_valid(); | ||
} | ||
}; | ||
/** | ||
@@ -405,6 +420,6 @@ * Set the amount of the asset | ||
*/ | ||
set_amount(amount) { | ||
Asset.prototype.set_amount = function (amount) { | ||
this.amount = BigInt(amount); | ||
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
} | ||
}; | ||
/** | ||
@@ -417,3 +432,3 @@ * Subtraction assignment operator | ||
*/ | ||
minus(a) { | ||
Asset.prototype.minus = function (a) { | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -429,3 +444,3 @@ this.amount -= BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -438,3 +453,3 @@ * Addition Assignment operator | ||
*/ | ||
plus(a) { | ||
Asset.prototype.plus = function (a) { | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -450,3 +465,3 @@ this.amount += BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -459,7 +474,7 @@ * Addition operator | ||
*/ | ||
static plus(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.plus = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.plus(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -472,7 +487,7 @@ * Subtraction operator | ||
*/ | ||
static minus(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.minus = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.minus(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -486,4 +501,4 @@ * Multiplication assignment operator, with a number | ||
*/ | ||
times(a) { | ||
let amount; | ||
Asset.prototype.times = function (a) { | ||
var amount; | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -496,3 +511,3 @@ amount = BigInt(a); | ||
} | ||
const tmp = this.amount * amount; | ||
var tmp = this.amount * amount; | ||
check(tmp <= Asset.max_amount, "multiplication overflow"); | ||
@@ -502,3 +517,3 @@ check(tmp >= -Asset.max_amount, "multiplication underflow"); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -512,7 +527,7 @@ * Multiplication operator, with a number proceeding | ||
*/ | ||
static times(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.times = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.times(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -526,4 +541,4 @@ * @brief Division assignment operator, with a number | ||
*/ | ||
div(a) { | ||
let amount; | ||
Asset.prototype.div = function (a) { | ||
var amount; | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -540,3 +555,3 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -549,7 +564,7 @@ * Division operator, with a number proceeding | ||
*/ | ||
static div(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.div = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.div(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -564,10 +579,10 @@ * Equality operator | ||
*/ | ||
static isEqual(a, b) { | ||
Asset.isEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount == b.amount; | ||
} | ||
isEqual(a) { | ||
}; | ||
Asset.prototype.isEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount == this.amount; | ||
} | ||
}; | ||
/** | ||
@@ -582,8 +597,8 @@ * Inequality operator | ||
*/ | ||
static isNotEqual(a, b) { | ||
Asset.isNotEqual = function (a, b) { | ||
return !(a == b); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Asset.prototype.isNotEqual = function (a) { | ||
return !(a == this); | ||
} | ||
}; | ||
/** | ||
@@ -598,10 +613,10 @@ * Less than operator | ||
*/ | ||
static isLessThan(a, b) { | ||
Asset.isLessThan = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount < b.amount; | ||
} | ||
isLessThan(a) { | ||
}; | ||
Asset.prototype.isLessThan = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount < a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -616,10 +631,10 @@ * Less or equal to operator | ||
*/ | ||
static isLessThanOrEqual(a, b) { | ||
Asset.isLessThanOrEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount <= b.amount; | ||
} | ||
isLessThanOrEqual(a) { | ||
}; | ||
Asset.prototype.isLessThanOrEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount <= a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -634,10 +649,10 @@ * Greater than operator | ||
*/ | ||
static isGreaterThan(a, b) { | ||
Asset.isGreaterThan = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount > b.amount; | ||
} | ||
isGreaterThan(a) { | ||
}; | ||
Asset.prototype.isGreaterThan = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount > a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -652,15 +667,15 @@ * Greater or equal to operator | ||
*/ | ||
static isGreaterThanOrEqual(a, b) { | ||
Asset.isGreaterThanOrEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount >= b.amount; | ||
} | ||
isGreaterThanOrEqual(a) { | ||
}; | ||
Asset.prototype.isGreaterThanOrEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount >= a.amount; | ||
} | ||
to_string() { | ||
const amount = write_decimal(this.amount, this.symbol.precision(), true); | ||
const symcode = this.symbol.code().to_string(); | ||
return `${amount} ${symcode}`; | ||
} | ||
}; | ||
Asset.prototype.to_string = function () { | ||
var amount = write_decimal(this.amount, this.symbol.precision(), true); | ||
var symcode = this.symbol.code().to_string(); | ||
return amount + " " + symcode; | ||
}; | ||
/** | ||
@@ -671,10 +686,11 @@ * %Print the asset | ||
*/ | ||
print() { | ||
Asset.prototype.print = function () { | ||
process.stdout.write(this.to_string()); | ||
} | ||
} | ||
/** | ||
* {constexpr int64_t} Maximum amount possible for this asset. It's capped to 2^62 - 1 | ||
*/ | ||
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1); | ||
}; | ||
/** | ||
* {constexpr int64_t} Maximum amount possible for this asset. It's capped to 2^62 - 1 | ||
*/ | ||
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1); | ||
return Asset; | ||
}()); | ||
function asset(amount, sym) { | ||
@@ -688,6 +704,6 @@ return new Asset(amount, sym); | ||
*/ | ||
class Name { | ||
constructor(str) { | ||
var Name = /** @class */ (function () { | ||
function Name(str) { | ||
this.value = BigInt(0); | ||
let value = BigInt(0); | ||
var value = BigInt(0); | ||
if (typeof str == "number" || typeof str == "bigint") { | ||
@@ -701,4 +717,4 @@ this.value = BigInt(str); | ||
return; | ||
const n = Math.min(str.length, 12); | ||
for (let i = 0; i < n; ++i) { | ||
var n = Math.min(str.length, 12); | ||
for (var i = 0; i < n; ++i) { | ||
value <<= BigInt(5); | ||
@@ -709,3 +725,3 @@ value |= BigInt(Name.char_to_value(str[i])); | ||
if (str.length == 13) { | ||
const v = Name.char_to_value(str[12]); | ||
var v = Name.char_to_value(str[12]); | ||
if (v > 0x0F) { | ||
@@ -718,6 +734,10 @@ check(false, "thirteenth character in name cannot be a letter that comes after j"); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'name'; | ||
} | ||
typeof() { return 'name'; } | ||
Object.defineProperty(Name.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'name'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Name.prototype.typeof = function () { return 'name'; }; | ||
/** | ||
@@ -729,3 +749,3 @@ * Converts a %name Base32 symbol into its corresponding value | ||
*/ | ||
static char_to_value(c) { | ||
Name.char_to_value = function (c) { | ||
if (c == '.') | ||
@@ -740,13 +760,13 @@ return 0; | ||
return 0; // control flow will never reach here; just added to suppress warning | ||
} | ||
}; | ||
/** | ||
* Returns the length of the %name | ||
*/ | ||
length() { | ||
const mask = BigInt(0xF800000000000000); | ||
Name.prototype.length = function () { | ||
var mask = BigInt(0xF800000000000000); | ||
if (this.value == BigInt(0)) | ||
return 0; | ||
let l = 0; | ||
let i = 0; | ||
for (let v = this.value; i < 13; ++i, v <<= BigInt(5)) { | ||
var l = 0; | ||
var i = 0; | ||
for (var v = this.value; i < 13; ++i, v <<= BigInt(5)) { | ||
if ((v & mask) > 0) { | ||
@@ -757,12 +777,12 @@ l = i; | ||
return l + 1; | ||
} | ||
}; | ||
/** | ||
* Returns the suffix of the %name | ||
*/ | ||
suffix() { | ||
let remaining_bits_after_last_actual_dot = BigInt(0); | ||
let tmp = BigInt(0); | ||
for (let remaining_bits = BigInt(59); remaining_bits >= BigInt(4); remaining_bits -= BigInt(5)) { // Note: remaining_bits must remain signed integer | ||
Name.prototype.suffix = function () { | ||
var remaining_bits_after_last_actual_dot = BigInt(0); | ||
var tmp = BigInt(0); | ||
for (var remaining_bits = BigInt(59); remaining_bits >= BigInt(4); remaining_bits -= BigInt(5)) { // Note: remaining_bits must remain signed integer | ||
// Get characters one-by-one in name in order from left to right (not including the 13th character) | ||
const c = (this.value >> remaining_bits) & BigInt(0x1F); | ||
var c = (this.value >> remaining_bits) & BigInt(0x1F); | ||
if (!c) { // if this character is a dot | ||
@@ -775,3 +795,3 @@ tmp = remaining_bits; | ||
} | ||
const thirteenth_character = this.value & BigInt(0x0F); | ||
var thirteenth_character = this.value & BigInt(0x0F); | ||
if (thirteenth_character) { // if 13th character is not a dot | ||
@@ -784,12 +804,12 @@ remaining_bits_after_last_actual_dot = tmp; | ||
// Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character). | ||
const mask = (BigInt(1) << remaining_bits_after_last_actual_dot) - BigInt(16); | ||
const shift = BigInt(64) - remaining_bits_after_last_actual_dot; | ||
var mask = (BigInt(1) << remaining_bits_after_last_actual_dot) - BigInt(16); | ||
var shift = BigInt(64) - remaining_bits_after_last_actual_dot; | ||
return new Name(((this.value & mask) << shift) + (thirteenth_character << (shift - BigInt(1)))); | ||
} | ||
}; | ||
/** | ||
* Returns uint64_t repreresentation of the name | ||
*/ | ||
raw() { | ||
Name.prototype.raw = function () { | ||
return this.value; | ||
} | ||
}; | ||
/** | ||
@@ -800,5 +820,5 @@ * Explicit cast to bool of the name | ||
*/ | ||
bool() { | ||
Name.prototype.bool = function () { | ||
return this.value != BigInt(0); | ||
} | ||
}; | ||
/** | ||
@@ -809,9 +829,9 @@ * Returns the name as a string. | ||
*/ | ||
to_string() { | ||
const charmap = ".12345abcdefghijklmnopqrstuvwxyz"; | ||
const mask = BigInt(0xF800000000000000); | ||
let begin = ""; | ||
let v = this.value; | ||
const actual_end = this.length(); | ||
for (let i = 0; i < 13; ++i, v <<= BigInt(5)) { | ||
Name.prototype.to_string = function () { | ||
var charmap = ".12345abcdefghijklmnopqrstuvwxyz"; | ||
var mask = BigInt(0xF800000000000000); | ||
var begin = ""; | ||
var v = this.value; | ||
var actual_end = this.length(); | ||
for (var i = 0; i < 13; ++i, v <<= BigInt(5)) { | ||
if (v == BigInt(0)) | ||
@@ -821,7 +841,7 @@ return begin; | ||
return begin; | ||
const indx = (v & mask) >> (i == 12 ? BigInt(60) : BigInt(59)); | ||
var indx = (v & mask) >> (i == 12 ? BigInt(60) : BigInt(59)); | ||
begin += charmap[Number(indx)]; | ||
} | ||
return begin; | ||
} | ||
}; | ||
/** | ||
@@ -832,8 +852,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
Name.isEqual = function (a, b) { | ||
return a.raw() == b.raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
Name.prototype.isEqual = function (a) { | ||
return a.raw() == this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -844,8 +864,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
Name.isNotEqual = function (a, b) { | ||
return a.raw() != b.raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Name.prototype.isNotEqual = function (a) { | ||
return a.raw() != this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -856,9 +876,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
Name.isLessThan = function (a, b) { | ||
return a.raw() < b.raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
Name.prototype.isLessThan = function (a) { | ||
return this.raw() < a.raw(); | ||
} | ||
} | ||
}; | ||
return Name; | ||
}()); | ||
function name(str) { | ||
@@ -872,3 +893,3 @@ return new Name(str); | ||
*/ | ||
class ExtendedSymbol { | ||
var ExtendedSymbol = /** @class */ (function () { | ||
/** | ||
@@ -879,4 +900,11 @@ * Construct a new symbol_code object initialising symbol and contract with the passed in symbol and name | ||
* @param con - The name of the contract | ||
* @example | ||
* | ||
* // string | ||
* extended_symbol("EOS,4", "eosio.token") | ||
* | ||
* // class | ||
* new ExtendedSymbol(new Sym("EOS", 4), new Name("eosio.token")) | ||
*/ | ||
constructor(sym, contract) { | ||
function ExtendedSymbol(sym, contract) { | ||
this.sym = new Sym(); | ||
@@ -889,6 +917,10 @@ this.contract = new Name(); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'extended_symbol'; | ||
} | ||
typeof() { return 'extended_symbol'; } | ||
Object.defineProperty(ExtendedSymbol.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'extended_symbol'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ExtendedSymbol.prototype.typeof = function () { return 'extended_symbol'; }; | ||
/** | ||
@@ -899,3 +931,3 @@ * Returns the symbol in the extended_contract | ||
*/ | ||
get_symbol() { return this.sym; } | ||
ExtendedSymbol.prototype.get_symbol = function () { return this.sym; }; | ||
/** | ||
@@ -906,3 +938,3 @@ * Returns the name of the contract in the extended_symbol | ||
*/ | ||
get_contract() { return this.contract; } | ||
ExtendedSymbol.prototype.get_contract = function () { return this.contract; }; | ||
/** | ||
@@ -913,6 +945,7 @@ * %Print the extended symbol | ||
*/ | ||
print(show_precision = true) { | ||
ExtendedSymbol.prototype.print = function (show_precision) { | ||
if (show_precision === void 0) { show_precision = true; } | ||
this.sym.print(show_precision); | ||
process.stdout.write("@" + this.contract.to_string()); | ||
} | ||
}; | ||
/** | ||
@@ -923,8 +956,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
ExtendedSymbol.isEqual = function (a, b) { | ||
return a.get_contract().raw() == b.get_contract().raw() && a.get_symbol().raw() == b.get_symbol().raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
ExtendedSymbol.prototype.isEqual = function (a) { | ||
return a.get_contract().raw() == this.get_contract().raw() && a.get_symbol().raw() == this.get_symbol().raw(); | ||
} | ||
}; | ||
/** | ||
@@ -935,8 +968,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
ExtendedSymbol.isNotEqual = function (a, b) { | ||
return a.get_contract().raw() != b.get_contract().raw() || a.get_symbol().raw() != b.get_symbol().raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
ExtendedSymbol.prototype.isNotEqual = function (a) { | ||
return a.get_contract().raw() != this.get_contract().raw() || a.get_symbol().raw() != this.get_symbol().raw(); | ||
} | ||
}; | ||
/** | ||
@@ -947,9 +980,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
ExtendedSymbol.isLessThan = function (a, b) { | ||
return a.get_contract().raw() < b.get_contract().raw() || a.get_symbol().raw() < b.get_symbol().raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
ExtendedSymbol.prototype.isLessThan = function (a) { | ||
return this.get_contract().raw() < a.get_contract().raw() || this.get_symbol().raw() < a.get_symbol().raw(); | ||
} | ||
} | ||
}; | ||
return ExtendedSymbol; | ||
}()); | ||
function extended_symbol(sym, contract) { | ||
@@ -963,3 +997,3 @@ return new ExtendedSymbol(sym, contract); | ||
*/ | ||
class ExtendedAsset { | ||
var ExtendedAsset = /** @class */ (function () { | ||
/** | ||
@@ -979,3 +1013,4 @@ * Construct a new symbol_code object initialising symbol and contract with the passed in symbol and name | ||
// extended_asset( asset a, name c ):quantity(a),contract(c){} | ||
constructor(options = {}) { | ||
function ExtendedAsset(options) { | ||
if (options === void 0) { options = {}; } | ||
/** | ||
@@ -1004,6 +1039,10 @@ * The asset | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'extended_asset'; | ||
} | ||
typeof() { return 'extended_asset'; } | ||
Object.defineProperty(ExtendedAsset.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'extended_asset'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ExtendedAsset.prototype.typeof = function () { return 'extended_asset'; }; | ||
/** | ||
@@ -1014,14 +1053,14 @@ * Get the extended symbol of the asset | ||
*/ | ||
get_extended_symbol() { return new ExtendedSymbol(this.quantity.symbol, this.contract); } | ||
ExtendedAsset.prototype.get_extended_symbol = function () { return new ExtendedSymbol(this.quantity.symbol, this.contract); }; | ||
/** | ||
* %Print the extended asset | ||
*/ | ||
print() { | ||
ExtendedAsset.prototype.print = function () { | ||
this.quantity.print(); | ||
process.stdout.write("@" + this.contract.to_string()); | ||
} | ||
}; | ||
/// @cond OPERATORS | ||
// Multiplication assignment operator | ||
times(a) { | ||
let amount; | ||
ExtendedAsset.prototype.times = function (a) { | ||
var amount; | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1036,13 +1075,13 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
static times(a, b) { | ||
}; | ||
ExtendedAsset.times = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.times(b); | ||
return result; | ||
} | ||
}; | ||
// Division operator | ||
div(a) { | ||
let amount; | ||
ExtendedAsset.prototype.div = function (a) { | ||
var amount; | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1057,12 +1096,12 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
static div(a, b) { | ||
}; | ||
ExtendedAsset.div = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.div(b); | ||
return result; | ||
} | ||
}; | ||
// Subtraction operator | ||
minus(a) { | ||
ExtendedAsset.prototype.minus = function (a) { | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1076,12 +1115,12 @@ this.quantity.minus(a); | ||
return this; | ||
} | ||
static minus(a, b) { | ||
}; | ||
ExtendedAsset.minus = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.minus(b); | ||
return result; | ||
} | ||
}; | ||
// Addition operator | ||
plus(a) { | ||
ExtendedAsset.prototype.plus = function (a) { | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1095,68 +1134,89 @@ this.quantity.plus(a); | ||
return this; | ||
} | ||
static plus(a, b) { | ||
}; | ||
ExtendedAsset.plus = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.plus(b); | ||
return result; | ||
} | ||
}; | ||
/// Less than operator | ||
isLessThan(a) { | ||
ExtendedAsset.prototype.isLessThan = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isLessThan(a.quantity); | ||
} | ||
static isLessThan(a, b) { | ||
}; | ||
ExtendedAsset.isLessThan = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isLessThan(b.quantity); | ||
} | ||
}; | ||
/// Comparison operator | ||
isEqual(a) { | ||
ExtendedAsset.prototype.isEqual = function (a) { | ||
return this.quantity.isEqual(a.quantity) && this.contract.isEqual(a.contract); | ||
} | ||
static isEqual(a, b) { | ||
}; | ||
ExtendedAsset.isEqual = function (a, b) { | ||
return a.quantity.isEqual(b.quantity) && a.contract.isEqual(b.contract); | ||
} | ||
}; | ||
/// Comparison operator | ||
isNotEqual(a) { | ||
ExtendedAsset.prototype.isNotEqual = function (a) { | ||
return this.quantity.isNotEqual(a.quantity) || this.contract.isNotEqual(a.contract); | ||
} | ||
static isNotEqual(a, b) { | ||
}; | ||
ExtendedAsset.isNotEqual = function (a, b) { | ||
return a.quantity.isNotEqual(b.quantity) || a.contract.isNotEqual(b.contract); | ||
} | ||
}; | ||
/// Comparison operator | ||
isLessThanOrEqual(a) { | ||
ExtendedAsset.prototype.isLessThanOrEqual = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isLessThanOrEqual(a.quantity); | ||
} | ||
static isLessThanOrEqual(a, b) { | ||
}; | ||
ExtendedAsset.isLessThanOrEqual = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isLessThanOrEqual(b.quantity); | ||
} | ||
}; | ||
/// Comparison operator | ||
isGreaterThanOrEqual(a) { | ||
ExtendedAsset.prototype.isGreaterThanOrEqual = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isGreaterThanOrEqual(a.quantity); | ||
} | ||
static isGreaterThanOrEqual(a, b) { | ||
}; | ||
ExtendedAsset.isGreaterThanOrEqual = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isGreaterThanOrEqual(b.quantity); | ||
} | ||
} | ||
function extended_asset(options = {}) { | ||
}; | ||
return ExtendedAsset; | ||
}()); | ||
function extended_asset(options) { | ||
if (options === void 0) { options = {}; } | ||
return new ExtendedAsset(options); | ||
} | ||
class Time { | ||
sec_since_epoch() { | ||
var Time = /** @class */ (function () { | ||
function Time() { | ||
} | ||
Time.prototype.sec_since_epoch = function () { | ||
return Date.now(); | ||
} | ||
} | ||
}; | ||
return Time; | ||
}()); | ||
function current_time_point() { | ||
return new Time(); | ||
} | ||
const block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
const seconds_per_day = 60 * 60 * 24; | ||
var block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
var seconds_per_day = 60 * 60 * 24; | ||
// https://github.com/EOSIO/eosio.contracts/blob/master/contracts/eosio.system/src/voting.cpp | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return BigInt(quantity.amount) / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function bigint_to_asset(amount, sym) { | ||
return new Asset(BigInt(amount) * BigInt(Math.pow(10, sym.precision())), sym); | ||
} | ||
function asset_to_number(quantity) { | ||
if (Number(quantity.amount) == 0) | ||
return 0.0; | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function number_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
} | ||
/** | ||
@@ -1166,4 +1226,4 @@ * voteWeightToday computes the stake2vote weight for EOS, in order to compute the decaying value. | ||
function voteWeightToday() { | ||
const seconds_per_day = 86400; | ||
const block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
var seconds_per_day = 86400; | ||
var block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
return Math.floor((Date.now() - block_timestamp_epoch) / 1000 / (seconds_per_day * 7)) / 52; | ||
@@ -1187,4 +1247,12 @@ } | ||
} | ||
/** | ||
* Calculate producer vpay | ||
* | ||
* @return {bigint} producer pay as int64t | ||
*/ | ||
function calculate_producer_per_vote_pay(total_votes, pervote_bucket, total_producer_vote_weight) { | ||
return (pervote_bucket * total_votes) / total_producer_vote_weight; | ||
} | ||
export { Asset, ExtendedAsset, ExtendedSymbol, Name, Sym, SymbolCode, asset, asset_to_bigint, asset_to_number, bigint_to_asset, block_timestamp_epoch, check, current_time_point, extended_asset, extended_symbol, isNull, name, number_to_asset, number_to_bigint, seconds_per_day, stake2vote, symbol, symbol_code, vote2stake, voteWeightToday, write_decimal }; | ||
export { Asset, ExtendedAsset, ExtendedSymbol, Name, Sym, SymbolCode, asset, asset_to_bigint, asset_to_number, bigint_to_asset, block_timestamp_epoch, calculate_producer_per_vote_pay, check, current_time_point, extended_asset, extended_symbol, name, number_to_asset, seconds_per_day, stake2vote, symbol, symbol_code, vote2stake, voteWeightToday, write_decimal }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -5,3 +5,2 @@ 'use strict'; | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/check.hpp | ||
/** | ||
@@ -27,5 +26,5 @@ * Assert if the predicate fails and use the supplied message. | ||
*/ | ||
class SymbolCode { | ||
var SymbolCode = /** @class */ (function () { | ||
// constructor() | ||
constructor(str) { | ||
function SymbolCode(str) { | ||
this.value = BigInt(0); | ||
@@ -36,7 +35,8 @@ if (typeof str == "number" || typeof str == 'bigint') { | ||
else if (str) { | ||
let value = BigInt(0); | ||
var value = BigInt(0); | ||
if (str.length > 7) { | ||
check(false, "string is too long to be a valid symbol_code"); | ||
} | ||
for (const itr of str.split("").reverse().join("")) { | ||
for (var _i = 0, _a = str.split("").reverse().join(""); _i < _a.length; _i++) { | ||
var itr = _a[_i]; | ||
if (itr < 'A' || itr > 'Z') { | ||
@@ -51,18 +51,22 @@ check(false, "only uppercase letters allowed in symbol_code string"); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'symbol_code'; | ||
} | ||
typeof() { return 'symbol_code'; } | ||
raw() { | ||
Object.defineProperty(SymbolCode.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'symbol_code'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
SymbolCode.prototype.typeof = function () { return 'symbol_code'; }; | ||
SymbolCode.prototype.raw = function () { | ||
return this.value; | ||
} | ||
isTruthy() { | ||
}; | ||
SymbolCode.prototype.isTruthy = function () { | ||
return this.value != BigInt(0); | ||
} | ||
isFalsy() { | ||
}; | ||
SymbolCode.prototype.isFalsy = function () { | ||
return this.value == BigInt(0); | ||
} | ||
length() { | ||
let sym = BigInt(this.value); | ||
let len = 0; | ||
}; | ||
SymbolCode.prototype.length = function () { | ||
var sym = BigInt(this.value); | ||
var len = 0; | ||
while (Number(sym) & 0xFF && len <= 7) { | ||
@@ -73,10 +77,10 @@ len++; | ||
return len; | ||
} | ||
to_string() { | ||
const mask = BigInt(0x00000000000000FF); | ||
}; | ||
SymbolCode.prototype.to_string = function () { | ||
var mask = BigInt(0x00000000000000FF); | ||
if (this.value == BigInt(0)) | ||
return ''; | ||
let begin = ""; | ||
let v = BigInt(this.value); | ||
for (let i = 0; i < 7; ++i, v >>= BigInt(8)) { | ||
var begin = ""; | ||
var v = BigInt(this.value); | ||
for (var i = 0; i < 7; ++i, v >>= BigInt(8)) { | ||
if (v == BigInt(0)) | ||
@@ -87,7 +91,7 @@ return begin; | ||
return begin; | ||
} | ||
is_valid() { | ||
let sym = BigInt(this.value); | ||
for (let i = BigInt(0); i < 7; i++) { | ||
const c = String.fromCharCode(Number(BigInt(sym) & BigInt(0xFF))); | ||
}; | ||
SymbolCode.prototype.is_valid = function () { | ||
var sym = BigInt(this.value); | ||
for (var i = BigInt(0); i < 7; i++) { | ||
var c = String.fromCharCode(Number(BigInt(sym) & BigInt(0xFF))); | ||
if (!("A" <= c && c <= "Z")) | ||
@@ -106,3 +110,3 @@ return false; | ||
return true; | ||
} | ||
}; | ||
/** | ||
@@ -113,5 +117,5 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
isEqual(comparison) { | ||
SymbolCode.prototype.isEqual = function (comparison) { | ||
return comparison.value === this.value; | ||
} | ||
}; | ||
/** | ||
@@ -122,5 +126,5 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
isNotEqual(comparison) { | ||
SymbolCode.prototype.isNotEqual = function (comparison) { | ||
return comparison.value !== this.value; | ||
} | ||
}; | ||
/** | ||
@@ -131,6 +135,7 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
isLessThan(comparison) { | ||
SymbolCode.prototype.isLessThan = function (comparison) { | ||
return this.value < comparison.value; | ||
} | ||
} | ||
}; | ||
return SymbolCode; | ||
}()); | ||
function symbol_code(str) { | ||
@@ -140,27 +145,6 @@ return new SymbolCode(str); | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return BigInt(quantity.amount) / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function bigint_to_asset(amount, sym) { | ||
return new Asset(BigInt(amount) * BigInt(Math.pow(10, sym.precision())), sym); | ||
} | ||
function asset_to_number(quantity) { | ||
if (Number(quantity.amount) == 0) | ||
return 0.0; | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function number_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
} | ||
function number_to_bigint(num) { | ||
return BigInt(num.toFixed(0)); | ||
} | ||
function isNull(value) { | ||
return value == undefined || value == null; | ||
} | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp | ||
class Sym { | ||
var Sym = /** @class */ (function () { | ||
/** | ||
@@ -179,3 +163,3 @@ * Symbol | ||
*/ | ||
constructor(sc, precision) { | ||
function Sym(sc, precision) { | ||
this.value = BigInt(0); | ||
@@ -189,9 +173,24 @@ if (isNull(sc) && isNull(precision)) { | ||
else if (typeof sc == "string") { | ||
check(!isNull(precision), "[precision] is required"); | ||
const symcode = new SymbolCode(sc).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision); | ||
// "precision,symbol_code" (ex: "4,EOS") | ||
if (sc.includes(",")) { | ||
var _a = sc.split(","), precision_str = _a[0], symcode_str = _a[1]; | ||
var precision_1 = Number(precision_str); | ||
check(!isNaN(precision_1), "[precision] must be number type"); | ||
check(!isNull(precision_1), "[precision] is required"); | ||
check(!isNull(symcode_str), "[symcode] is required"); | ||
var symcode = new SymbolCode(symcode_str).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision_1 || Number(precision_str || "")); | ||
// "symbol_code" + @param: precision | ||
} | ||
else { | ||
check(!isNaN(Number(precision)), "[precision] must be number type"); | ||
check(!isNull(precision), "[precision] is required"); | ||
check(!isNull(sc), "[symcode] is required"); | ||
var symcode = new SymbolCode(sc).raw(); | ||
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision); | ||
} | ||
} | ||
else if (typeof sc == "object") { | ||
check(!isNull(precision), "[precision] is required"); | ||
const symcode = sc; | ||
var symcode = sc; | ||
this.value = BigInt(symcode.raw()) << BigInt(8) | BigInt(precision); | ||
@@ -203,30 +202,34 @@ } | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'symbol'; | ||
} | ||
typeof() { return 'symbol'; } | ||
Object.defineProperty(Sym.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'symbol'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Sym.prototype.typeof = function () { return 'symbol'; }; | ||
/** | ||
* Is this symbol valid | ||
*/ | ||
is_valid() { | ||
Sym.prototype.is_valid = function () { | ||
return this.code().is_valid(); | ||
} | ||
}; | ||
/** | ||
* This symbol's precision | ||
*/ | ||
precision() { | ||
Sym.prototype.precision = function () { | ||
return Number(this.value & BigInt(0x00000000000000FF)); | ||
} | ||
}; | ||
/** | ||
* Returns representation of symbol name | ||
*/ | ||
code() { | ||
Sym.prototype.code = function () { | ||
return new SymbolCode(this.value >> BigInt(8)); | ||
} | ||
}; | ||
/** | ||
* Returns uint64_t repreresentation of the symbol | ||
*/ | ||
raw() { | ||
Sym.prototype.raw = function () { | ||
return this.value; | ||
} | ||
}; | ||
/** | ||
@@ -237,9 +240,10 @@ * Explicit cast to bool of the symbol | ||
*/ | ||
bool() { | ||
Sym.prototype.bool = function () { | ||
return this.value != BigInt(0); | ||
} | ||
}; | ||
/** | ||
* %Print the symbol | ||
*/ | ||
print(show_precision = true) { | ||
Sym.prototype.print = function (show_precision) { | ||
if (show_precision === void 0) { show_precision = true; } | ||
if (show_precision) { | ||
@@ -249,3 +253,3 @@ process.stdout.write(String(this.precision()) + ","); | ||
process.stdout.write(this.code().to_string()); | ||
} | ||
}; | ||
/** | ||
@@ -256,8 +260,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
Sym.isEqual = function (a, b) { | ||
return a.raw() == b.raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
Sym.prototype.isEqual = function (a) { | ||
return a.raw() == this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -268,8 +272,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
Sym.isNotEqual = function (a, b) { | ||
return a.raw() != b.raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Sym.prototype.isNotEqual = function (a) { | ||
return a.raw() != this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -280,9 +284,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
Sym.isLessThan = function (a, b) { | ||
return a.raw() < b.raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
Sym.prototype.isLessThan = function (a) { | ||
return this.raw() < a.raw(); | ||
} | ||
} | ||
}; | ||
return Sym; | ||
}()); | ||
function symbol(sc, precision) { | ||
@@ -301,6 +306,7 @@ return new Sym(sc, precision); | ||
function write_decimal(number, num_decimal_places, negative) { | ||
let str = ""; | ||
let num_digits = 0; | ||
let isNegative = false; | ||
for (const num of number.toString().split("").reverse()) { | ||
var str = ""; | ||
var num_digits = 0; | ||
var isNegative = false; | ||
for (var _i = 0, _a = number.toString().split("").reverse(); _i < _a.length; _i++) { | ||
var num = _a[_i]; | ||
if (num == "-") { | ||
@@ -326,3 +332,8 @@ isNegative = true; | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp | ||
function number_to_bigint(num) { | ||
return BigInt(num.toFixed(0)); | ||
} | ||
function isNull$1(value) { | ||
return value == undefined || value == null; | ||
} | ||
/** | ||
@@ -342,3 +353,3 @@ * Asset | ||
*/ | ||
class Asset { | ||
var Asset = /** @class */ (function () { | ||
/** | ||
@@ -350,3 +361,3 @@ * Construct a new asset given the symbol name and the amount | ||
*/ | ||
constructor(amount, sym) { | ||
function Asset(amount, sym) { | ||
/** | ||
@@ -360,8 +371,8 @@ * {int64_t} The amount of the asset | ||
this.symbol = symbol(); | ||
if (isNull(amount) && isNull(sym)) { | ||
if (isNull$1(amount) && isNull$1(sym)) { | ||
return; | ||
} | ||
else if (typeof amount == "string") { | ||
const [amount_str, symbol_str] = amount.split(" "); | ||
const precision = (amount_str.split(".")[1] || []).length; | ||
var _a = amount.split(" "), amount_str = _a[0], symbol_str = _a[1]; | ||
var precision = (amount_str.split(".")[1] || []).length; | ||
this.amount = number_to_bigint(Number(amount_str) * Math.pow(10, precision)); | ||
@@ -380,6 +391,10 @@ this.symbol = new Sym(symbol_str, precision); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'asset'; | ||
} | ||
typeof() { return 'asset'; } | ||
Object.defineProperty(Asset.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'asset'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Asset.prototype.typeof = function () { return 'asset'; }; | ||
/** | ||
@@ -391,5 +406,5 @@ * Check if the amount doesn't exceed the max amount | ||
*/ | ||
is_amount_within_range() { | ||
Asset.prototype.is_amount_within_range = function () { | ||
return -BigInt(Asset.max_amount) <= BigInt(this.amount) && this.amount <= Asset.max_amount; | ||
} | ||
}; | ||
/** | ||
@@ -401,5 +416,5 @@ * Check if the asset is valid. %A valid asset has its amount <= max_amount and its symbol name valid | ||
*/ | ||
is_valid() { | ||
Asset.prototype.is_valid = function () { | ||
return this.is_amount_within_range() && this.symbol.is_valid(); | ||
} | ||
}; | ||
/** | ||
@@ -410,6 +425,6 @@ * Set the amount of the asset | ||
*/ | ||
set_amount(amount) { | ||
Asset.prototype.set_amount = function (amount) { | ||
this.amount = BigInt(amount); | ||
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
} | ||
}; | ||
/** | ||
@@ -422,3 +437,3 @@ * Subtraction assignment operator | ||
*/ | ||
minus(a) { | ||
Asset.prototype.minus = function (a) { | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -434,3 +449,3 @@ this.amount -= BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -443,3 +458,3 @@ * Addition Assignment operator | ||
*/ | ||
plus(a) { | ||
Asset.prototype.plus = function (a) { | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -455,3 +470,3 @@ this.amount += BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -464,7 +479,7 @@ * Addition operator | ||
*/ | ||
static plus(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.plus = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.plus(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -477,7 +492,7 @@ * Subtraction operator | ||
*/ | ||
static minus(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.minus = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.minus(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -491,4 +506,4 @@ * Multiplication assignment operator, with a number | ||
*/ | ||
times(a) { | ||
let amount; | ||
Asset.prototype.times = function (a) { | ||
var amount; | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -501,3 +516,3 @@ amount = BigInt(a); | ||
} | ||
const tmp = this.amount * amount; | ||
var tmp = this.amount * amount; | ||
check(tmp <= Asset.max_amount, "multiplication overflow"); | ||
@@ -507,3 +522,3 @@ check(tmp >= -Asset.max_amount, "multiplication underflow"); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -517,7 +532,7 @@ * Multiplication operator, with a number proceeding | ||
*/ | ||
static times(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.times = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.times(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -531,4 +546,4 @@ * @brief Division assignment operator, with a number | ||
*/ | ||
div(a) { | ||
let amount; | ||
Asset.prototype.div = function (a) { | ||
var amount; | ||
if (typeof a == "number" || typeof a == "bigint") { | ||
@@ -545,3 +560,3 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
}; | ||
/** | ||
@@ -554,7 +569,7 @@ * Division operator, with a number proceeding | ||
*/ | ||
static div(a, b) { | ||
const result = new Asset(a.amount, a.symbol); | ||
Asset.div = function (a, b) { | ||
var result = new Asset(a.amount, a.symbol); | ||
result.div(b); | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -569,10 +584,10 @@ * Equality operator | ||
*/ | ||
static isEqual(a, b) { | ||
Asset.isEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount == b.amount; | ||
} | ||
isEqual(a) { | ||
}; | ||
Asset.prototype.isEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount == this.amount; | ||
} | ||
}; | ||
/** | ||
@@ -587,8 +602,8 @@ * Inequality operator | ||
*/ | ||
static isNotEqual(a, b) { | ||
Asset.isNotEqual = function (a, b) { | ||
return !(a == b); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Asset.prototype.isNotEqual = function (a) { | ||
return !(a == this); | ||
} | ||
}; | ||
/** | ||
@@ -603,10 +618,10 @@ * Less than operator | ||
*/ | ||
static isLessThan(a, b) { | ||
Asset.isLessThan = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount < b.amount; | ||
} | ||
isLessThan(a) { | ||
}; | ||
Asset.prototype.isLessThan = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount < a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -621,10 +636,10 @@ * Less or equal to operator | ||
*/ | ||
static isLessThanOrEqual(a, b) { | ||
Asset.isLessThanOrEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount <= b.amount; | ||
} | ||
isLessThanOrEqual(a) { | ||
}; | ||
Asset.prototype.isLessThanOrEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount <= a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -639,10 +654,10 @@ * Greater than operator | ||
*/ | ||
static isGreaterThan(a, b) { | ||
Asset.isGreaterThan = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount > b.amount; | ||
} | ||
isGreaterThan(a) { | ||
}; | ||
Asset.prototype.isGreaterThan = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount > a.amount; | ||
} | ||
}; | ||
/** | ||
@@ -657,15 +672,15 @@ * Greater or equal to operator | ||
*/ | ||
static isGreaterThanOrEqual(a, b) { | ||
Asset.isGreaterThanOrEqual = function (a, b) { | ||
check(a.symbol.isEqual(b.symbol), "comparison of assets with different symbols is not allowed"); | ||
return a.amount >= b.amount; | ||
} | ||
isGreaterThanOrEqual(a) { | ||
}; | ||
Asset.prototype.isGreaterThanOrEqual = function (a) { | ||
check(a.symbol.isEqual(this.symbol), "comparison of assets with different symbols is not allowed"); | ||
return this.amount >= a.amount; | ||
} | ||
to_string() { | ||
const amount = write_decimal(this.amount, this.symbol.precision(), true); | ||
const symcode = this.symbol.code().to_string(); | ||
return `${amount} ${symcode}`; | ||
} | ||
}; | ||
Asset.prototype.to_string = function () { | ||
var amount = write_decimal(this.amount, this.symbol.precision(), true); | ||
var symcode = this.symbol.code().to_string(); | ||
return amount + " " + symcode; | ||
}; | ||
/** | ||
@@ -676,10 +691,11 @@ * %Print the asset | ||
*/ | ||
print() { | ||
Asset.prototype.print = function () { | ||
process.stdout.write(this.to_string()); | ||
} | ||
} | ||
/** | ||
* {constexpr int64_t} Maximum amount possible for this asset. It's capped to 2^62 - 1 | ||
*/ | ||
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1); | ||
}; | ||
/** | ||
* {constexpr int64_t} Maximum amount possible for this asset. It's capped to 2^62 - 1 | ||
*/ | ||
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1); | ||
return Asset; | ||
}()); | ||
function asset(amount, sym) { | ||
@@ -693,6 +709,6 @@ return new Asset(amount, sym); | ||
*/ | ||
class Name { | ||
constructor(str) { | ||
var Name = /** @class */ (function () { | ||
function Name(str) { | ||
this.value = BigInt(0); | ||
let value = BigInt(0); | ||
var value = BigInt(0); | ||
if (typeof str == "number" || typeof str == "bigint") { | ||
@@ -706,4 +722,4 @@ this.value = BigInt(str); | ||
return; | ||
const n = Math.min(str.length, 12); | ||
for (let i = 0; i < n; ++i) { | ||
var n = Math.min(str.length, 12); | ||
for (var i = 0; i < n; ++i) { | ||
value <<= BigInt(5); | ||
@@ -714,3 +730,3 @@ value |= BigInt(Name.char_to_value(str[i])); | ||
if (str.length == 13) { | ||
const v = Name.char_to_value(str[12]); | ||
var v = Name.char_to_value(str[12]); | ||
if (v > 0x0F) { | ||
@@ -723,6 +739,10 @@ check(false, "thirteenth character in name cannot be a letter that comes after j"); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'name'; | ||
} | ||
typeof() { return 'name'; } | ||
Object.defineProperty(Name.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'name'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Name.prototype.typeof = function () { return 'name'; }; | ||
/** | ||
@@ -734,3 +754,3 @@ * Converts a %name Base32 symbol into its corresponding value | ||
*/ | ||
static char_to_value(c) { | ||
Name.char_to_value = function (c) { | ||
if (c == '.') | ||
@@ -745,13 +765,13 @@ return 0; | ||
return 0; // control flow will never reach here; just added to suppress warning | ||
} | ||
}; | ||
/** | ||
* Returns the length of the %name | ||
*/ | ||
length() { | ||
const mask = BigInt(0xF800000000000000); | ||
Name.prototype.length = function () { | ||
var mask = BigInt(0xF800000000000000); | ||
if (this.value == BigInt(0)) | ||
return 0; | ||
let l = 0; | ||
let i = 0; | ||
for (let v = this.value; i < 13; ++i, v <<= BigInt(5)) { | ||
var l = 0; | ||
var i = 0; | ||
for (var v = this.value; i < 13; ++i, v <<= BigInt(5)) { | ||
if ((v & mask) > 0) { | ||
@@ -762,12 +782,12 @@ l = i; | ||
return l + 1; | ||
} | ||
}; | ||
/** | ||
* Returns the suffix of the %name | ||
*/ | ||
suffix() { | ||
let remaining_bits_after_last_actual_dot = BigInt(0); | ||
let tmp = BigInt(0); | ||
for (let remaining_bits = BigInt(59); remaining_bits >= BigInt(4); remaining_bits -= BigInt(5)) { // Note: remaining_bits must remain signed integer | ||
Name.prototype.suffix = function () { | ||
var remaining_bits_after_last_actual_dot = BigInt(0); | ||
var tmp = BigInt(0); | ||
for (var remaining_bits = BigInt(59); remaining_bits >= BigInt(4); remaining_bits -= BigInt(5)) { // Note: remaining_bits must remain signed integer | ||
// Get characters one-by-one in name in order from left to right (not including the 13th character) | ||
const c = (this.value >> remaining_bits) & BigInt(0x1F); | ||
var c = (this.value >> remaining_bits) & BigInt(0x1F); | ||
if (!c) { // if this character is a dot | ||
@@ -780,3 +800,3 @@ tmp = remaining_bits; | ||
} | ||
const thirteenth_character = this.value & BigInt(0x0F); | ||
var thirteenth_character = this.value & BigInt(0x0F); | ||
if (thirteenth_character) { // if 13th character is not a dot | ||
@@ -789,12 +809,12 @@ remaining_bits_after_last_actual_dot = tmp; | ||
// Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character). | ||
const mask = (BigInt(1) << remaining_bits_after_last_actual_dot) - BigInt(16); | ||
const shift = BigInt(64) - remaining_bits_after_last_actual_dot; | ||
var mask = (BigInt(1) << remaining_bits_after_last_actual_dot) - BigInt(16); | ||
var shift = BigInt(64) - remaining_bits_after_last_actual_dot; | ||
return new Name(((this.value & mask) << shift) + (thirteenth_character << (shift - BigInt(1)))); | ||
} | ||
}; | ||
/** | ||
* Returns uint64_t repreresentation of the name | ||
*/ | ||
raw() { | ||
Name.prototype.raw = function () { | ||
return this.value; | ||
} | ||
}; | ||
/** | ||
@@ -805,5 +825,5 @@ * Explicit cast to bool of the name | ||
*/ | ||
bool() { | ||
Name.prototype.bool = function () { | ||
return this.value != BigInt(0); | ||
} | ||
}; | ||
/** | ||
@@ -814,9 +834,9 @@ * Returns the name as a string. | ||
*/ | ||
to_string() { | ||
const charmap = ".12345abcdefghijklmnopqrstuvwxyz"; | ||
const mask = BigInt(0xF800000000000000); | ||
let begin = ""; | ||
let v = this.value; | ||
const actual_end = this.length(); | ||
for (let i = 0; i < 13; ++i, v <<= BigInt(5)) { | ||
Name.prototype.to_string = function () { | ||
var charmap = ".12345abcdefghijklmnopqrstuvwxyz"; | ||
var mask = BigInt(0xF800000000000000); | ||
var begin = ""; | ||
var v = this.value; | ||
var actual_end = this.length(); | ||
for (var i = 0; i < 13; ++i, v <<= BigInt(5)) { | ||
if (v == BigInt(0)) | ||
@@ -826,7 +846,7 @@ return begin; | ||
return begin; | ||
const indx = (v & mask) >> (i == 12 ? BigInt(60) : BigInt(59)); | ||
var indx = (v & mask) >> (i == 12 ? BigInt(60) : BigInt(59)); | ||
begin += charmap[Number(indx)]; | ||
} | ||
return begin; | ||
} | ||
}; | ||
/** | ||
@@ -837,8 +857,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
Name.isEqual = function (a, b) { | ||
return a.raw() == b.raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
Name.prototype.isEqual = function (a) { | ||
return a.raw() == this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -849,8 +869,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
Name.isNotEqual = function (a, b) { | ||
return a.raw() != b.raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
Name.prototype.isNotEqual = function (a) { | ||
return a.raw() != this.raw(); | ||
} | ||
}; | ||
/** | ||
@@ -861,9 +881,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
Name.isLessThan = function (a, b) { | ||
return a.raw() < b.raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
Name.prototype.isLessThan = function (a) { | ||
return this.raw() < a.raw(); | ||
} | ||
} | ||
}; | ||
return Name; | ||
}()); | ||
function name(str) { | ||
@@ -877,3 +898,3 @@ return new Name(str); | ||
*/ | ||
class ExtendedSymbol { | ||
var ExtendedSymbol = /** @class */ (function () { | ||
/** | ||
@@ -884,4 +905,11 @@ * Construct a new symbol_code object initialising symbol and contract with the passed in symbol and name | ||
* @param con - The name of the contract | ||
* @example | ||
* | ||
* // string | ||
* extended_symbol("EOS,4", "eosio.token") | ||
* | ||
* // class | ||
* new ExtendedSymbol(new Sym("EOS", 4), new Name("eosio.token")) | ||
*/ | ||
constructor(sym, contract) { | ||
function ExtendedSymbol(sym, contract) { | ||
this.sym = new Sym(); | ||
@@ -894,6 +922,10 @@ this.contract = new Name(); | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'extended_symbol'; | ||
} | ||
typeof() { return 'extended_symbol'; } | ||
Object.defineProperty(ExtendedSymbol.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'extended_symbol'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ExtendedSymbol.prototype.typeof = function () { return 'extended_symbol'; }; | ||
/** | ||
@@ -904,3 +936,3 @@ * Returns the symbol in the extended_contract | ||
*/ | ||
get_symbol() { return this.sym; } | ||
ExtendedSymbol.prototype.get_symbol = function () { return this.sym; }; | ||
/** | ||
@@ -911,3 +943,3 @@ * Returns the name of the contract in the extended_symbol | ||
*/ | ||
get_contract() { return this.contract; } | ||
ExtendedSymbol.prototype.get_contract = function () { return this.contract; }; | ||
/** | ||
@@ -918,6 +950,7 @@ * %Print the extended symbol | ||
*/ | ||
print(show_precision = true) { | ||
ExtendedSymbol.prototype.print = function (show_precision) { | ||
if (show_precision === void 0) { show_precision = true; } | ||
this.sym.print(show_precision); | ||
process.stdout.write("@" + this.contract.to_string()); | ||
} | ||
}; | ||
/** | ||
@@ -928,8 +961,8 @@ * Equivalency operator. Returns true if a == b (are the same) | ||
*/ | ||
static isEqual(a, b) { | ||
ExtendedSymbol.isEqual = function (a, b) { | ||
return a.get_contract().raw() == b.get_contract().raw() && a.get_symbol().raw() == b.get_symbol().raw(); | ||
} | ||
isEqual(a) { | ||
}; | ||
ExtendedSymbol.prototype.isEqual = function (a) { | ||
return a.get_contract().raw() == this.get_contract().raw() && a.get_symbol().raw() == this.get_symbol().raw(); | ||
} | ||
}; | ||
/** | ||
@@ -940,8 +973,8 @@ * Inverted equivalency operator. Returns true if a != b (are different) | ||
*/ | ||
static isNotEqual(a, b) { | ||
ExtendedSymbol.isNotEqual = function (a, b) { | ||
return a.get_contract().raw() != b.get_contract().raw() || a.get_symbol().raw() != b.get_symbol().raw(); | ||
} | ||
isNotEqual(a) { | ||
}; | ||
ExtendedSymbol.prototype.isNotEqual = function (a) { | ||
return a.get_contract().raw() != this.get_contract().raw() || a.get_symbol().raw() != this.get_symbol().raw(); | ||
} | ||
}; | ||
/** | ||
@@ -952,9 +985,10 @@ * Less than operator. Returns true if a < b. | ||
*/ | ||
static isLessThan(a, b) { | ||
ExtendedSymbol.isLessThan = function (a, b) { | ||
return a.get_contract().raw() < b.get_contract().raw() || a.get_symbol().raw() < b.get_symbol().raw(); | ||
} | ||
isLessThan(a) { | ||
}; | ||
ExtendedSymbol.prototype.isLessThan = function (a) { | ||
return this.get_contract().raw() < a.get_contract().raw() || this.get_symbol().raw() < a.get_symbol().raw(); | ||
} | ||
} | ||
}; | ||
return ExtendedSymbol; | ||
}()); | ||
function extended_symbol(sym, contract) { | ||
@@ -968,3 +1002,3 @@ return new ExtendedSymbol(sym, contract); | ||
*/ | ||
class ExtendedAsset { | ||
var ExtendedAsset = /** @class */ (function () { | ||
/** | ||
@@ -984,3 +1018,4 @@ * Construct a new symbol_code object initialising symbol and contract with the passed in symbol and name | ||
// extended_asset( asset a, name c ):quantity(a),contract(c){} | ||
constructor(options = {}) { | ||
function ExtendedAsset(options) { | ||
if (options === void 0) { options = {}; } | ||
/** | ||
@@ -1009,6 +1044,10 @@ * The asset | ||
} | ||
get [Symbol.toStringTag]() { | ||
return 'extended_asset'; | ||
} | ||
typeof() { return 'extended_asset'; } | ||
Object.defineProperty(ExtendedAsset.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return 'extended_asset'; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
ExtendedAsset.prototype.typeof = function () { return 'extended_asset'; }; | ||
/** | ||
@@ -1019,14 +1058,14 @@ * Get the extended symbol of the asset | ||
*/ | ||
get_extended_symbol() { return new ExtendedSymbol(this.quantity.symbol, this.contract); } | ||
ExtendedAsset.prototype.get_extended_symbol = function () { return new ExtendedSymbol(this.quantity.symbol, this.contract); }; | ||
/** | ||
* %Print the extended asset | ||
*/ | ||
print() { | ||
ExtendedAsset.prototype.print = function () { | ||
this.quantity.print(); | ||
process.stdout.write("@" + this.contract.to_string()); | ||
} | ||
}; | ||
/// @cond OPERATORS | ||
// Multiplication assignment operator | ||
times(a) { | ||
let amount; | ||
ExtendedAsset.prototype.times = function (a) { | ||
var amount; | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1041,13 +1080,13 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
static times(a, b) { | ||
}; | ||
ExtendedAsset.times = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.times(b); | ||
return result; | ||
} | ||
}; | ||
// Division operator | ||
div(a) { | ||
let amount; | ||
ExtendedAsset.prototype.div = function (a) { | ||
var amount; | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1062,12 +1101,12 @@ amount = BigInt(a); | ||
return this; | ||
} | ||
static div(a, b) { | ||
}; | ||
ExtendedAsset.div = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.div(b); | ||
return result; | ||
} | ||
}; | ||
// Subtraction operator | ||
minus(a) { | ||
ExtendedAsset.prototype.minus = function (a) { | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1081,12 +1120,12 @@ this.quantity.minus(a); | ||
return this; | ||
} | ||
static minus(a, b) { | ||
}; | ||
ExtendedAsset.minus = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.minus(b); | ||
return result; | ||
} | ||
}; | ||
// Addition operator | ||
plus(a) { | ||
ExtendedAsset.prototype.plus = function (a) { | ||
if (typeof a == "bigint" || typeof a == "number") { | ||
@@ -1100,68 +1139,89 @@ this.quantity.plus(a); | ||
return this; | ||
} | ||
static plus(a, b) { | ||
}; | ||
ExtendedAsset.plus = function (a, b) { | ||
if (typeof b != "bigint" && typeof b != "number") | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
const result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
var result = new ExtendedAsset({ quantity: a.quantity, contract: a.contract }); | ||
result.plus(b); | ||
return result; | ||
} | ||
}; | ||
/// Less than operator | ||
isLessThan(a) { | ||
ExtendedAsset.prototype.isLessThan = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isLessThan(a.quantity); | ||
} | ||
static isLessThan(a, b) { | ||
}; | ||
ExtendedAsset.isLessThan = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isLessThan(b.quantity); | ||
} | ||
}; | ||
/// Comparison operator | ||
isEqual(a) { | ||
ExtendedAsset.prototype.isEqual = function (a) { | ||
return this.quantity.isEqual(a.quantity) && this.contract.isEqual(a.contract); | ||
} | ||
static isEqual(a, b) { | ||
}; | ||
ExtendedAsset.isEqual = function (a, b) { | ||
return a.quantity.isEqual(b.quantity) && a.contract.isEqual(b.contract); | ||
} | ||
}; | ||
/// Comparison operator | ||
isNotEqual(a) { | ||
ExtendedAsset.prototype.isNotEqual = function (a) { | ||
return this.quantity.isNotEqual(a.quantity) || this.contract.isNotEqual(a.contract); | ||
} | ||
static isNotEqual(a, b) { | ||
}; | ||
ExtendedAsset.isNotEqual = function (a, b) { | ||
return a.quantity.isNotEqual(b.quantity) || a.contract.isNotEqual(b.contract); | ||
} | ||
}; | ||
/// Comparison operator | ||
isLessThanOrEqual(a) { | ||
ExtendedAsset.prototype.isLessThanOrEqual = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isLessThanOrEqual(a.quantity); | ||
} | ||
static isLessThanOrEqual(a, b) { | ||
}; | ||
ExtendedAsset.isLessThanOrEqual = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isLessThanOrEqual(b.quantity); | ||
} | ||
}; | ||
/// Comparison operator | ||
isGreaterThanOrEqual(a) { | ||
ExtendedAsset.prototype.isGreaterThanOrEqual = function (a) { | ||
check(a.contract.raw() == this.contract.raw(), "type mismatch"); | ||
return this.quantity.isGreaterThanOrEqual(a.quantity); | ||
} | ||
static isGreaterThanOrEqual(a, b) { | ||
}; | ||
ExtendedAsset.isGreaterThanOrEqual = function (a, b) { | ||
check(a.contract.raw() == b.contract.raw(), "type mismatch"); | ||
return a.quantity.isGreaterThanOrEqual(b.quantity); | ||
} | ||
} | ||
function extended_asset(options = {}) { | ||
}; | ||
return ExtendedAsset; | ||
}()); | ||
function extended_asset(options) { | ||
if (options === void 0) { options = {}; } | ||
return new ExtendedAsset(options); | ||
} | ||
class Time { | ||
sec_since_epoch() { | ||
var Time = /** @class */ (function () { | ||
function Time() { | ||
} | ||
Time.prototype.sec_since_epoch = function () { | ||
return Date.now(); | ||
} | ||
} | ||
}; | ||
return Time; | ||
}()); | ||
function current_time_point() { | ||
return new Time(); | ||
} | ||
const block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
const seconds_per_day = 60 * 60 * 24; | ||
var block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
var seconds_per_day = 60 * 60 * 24; | ||
// https://github.com/EOSIO/eosio.contracts/blob/master/contracts/eosio.system/src/voting.cpp | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return BigInt(quantity.amount) / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function bigint_to_asset(amount, sym) { | ||
return new Asset(BigInt(amount) * BigInt(Math.pow(10, sym.precision())), sym); | ||
} | ||
function asset_to_number(quantity) { | ||
if (Number(quantity.amount) == 0) | ||
return 0.0; | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function number_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
} | ||
/** | ||
@@ -1171,4 +1231,4 @@ * voteWeightToday computes the stake2vote weight for EOS, in order to compute the decaying value. | ||
function voteWeightToday() { | ||
const seconds_per_day = 86400; | ||
const block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
var seconds_per_day = 86400; | ||
var block_timestamp_epoch = new Date(Date.UTC(2000, 0, 1, 0, 0, 0, 0)).getTime(); | ||
return Math.floor((Date.now() - block_timestamp_epoch) / 1000 / (seconds_per_day * 7)) / 52; | ||
@@ -1192,2 +1252,10 @@ } | ||
} | ||
/** | ||
* Calculate producer vpay | ||
* | ||
* @return {bigint} producer pay as int64t | ||
*/ | ||
function calculate_producer_per_vote_pay(total_votes, pervote_bucket, total_producer_vote_weight) { | ||
return (pervote_bucket * total_votes) / total_producer_vote_weight; | ||
} | ||
@@ -1205,2 +1273,3 @@ exports.Asset = Asset; | ||
exports.block_timestamp_epoch = block_timestamp_epoch; | ||
exports.calculate_producer_per_vote_pay = calculate_producer_per_vote_pay; | ||
exports.check = check; | ||
@@ -1210,6 +1279,4 @@ exports.current_time_point = current_time_point; | ||
exports.extended_symbol = extended_symbol; | ||
exports.isNull = isNull; | ||
exports.name = name; | ||
exports.number_to_asset = number_to_asset; | ||
exports.number_to_bigint = number_to_bigint; | ||
exports.seconds_per_day = seconds_per_day; | ||
@@ -1216,0 +1283,0 @@ exports.stake2vote = stake2vote; |
{ | ||
"name": "eos-common", | ||
"version": "0.6.0", | ||
"version": "0.6.2", | ||
"description": "EOSIO Smart Contract common library used for Typescript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -39,5 +39,7 @@ # `eos-common` | ||
const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
quantity.toString() //=> "1.0000 EOS"; | ||
// or | ||
const quantity = new Asset('1.0000 EOS'); | ||
quantity.to_string() //=> "1.0000 EOS"; | ||
quantity.symbol.code() //=> "EOS" | ||
quantity.symbol.precision //=> 4 | ||
quantity.symbol.precision() //=> 4 | ||
``` | ||
@@ -70,5 +72,2 @@ | ||
- [voteWeightToday](#voteweighttoday) | ||
- [split](#split) | ||
- [Parameters](#parameters-5) | ||
- [Examples](#examples-3) | ||
@@ -88,3 +87,3 @@ ### Asset | ||
const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
quantity.toString() //=> "1.0000 EOS"; | ||
quantity.to_string() //=> "1.0000 EOS"; | ||
quantity.symbol.code() //=> "EOS" | ||
@@ -152,4 +151,6 @@ quantity.symbol.precision //=> 4 | ||
const sym = new Symbol("EOS", 4); | ||
// or | ||
const sym = new Symbol("4,EOS"); | ||
sym.code() //=> "EOS" | ||
sym.precision //=> 4 | ||
sym.precision() //=> 4 | ||
``` | ||
@@ -180,20 +181,1 @@ | ||
voteWeightToday computes the stake2vote weight for EOS, in order to compute the decaying value. | ||
### split | ||
Split quantity string | ||
#### Parameters | ||
- `quantity` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Quantity string | ||
#### Examples | ||
```javascript | ||
const quantity = split("1.0000 EOS"); | ||
quantity.amount //=> 10000 | ||
quantity.symbol.precision //=> 4 | ||
quantity.symbol.code() //=> "EOS" | ||
``` | ||
Returns **[Asset](#asset)** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
116639
2925
177