Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eos-common

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eos-common - npm Package Compare versions

Comparing version 0.4.8 to 0.5.0

dist/eosiolib/eosiolib.d.ts

16

CHANGES.md
## TO-DO
- add support for `new Asset("1.0000 EOS")`
- add test cases for `Symbol`
## 2020-03-06
- Add operators to `asset`
- max_amount
- plus
- minus
- times
- div
- isEqual
- isNotEqual
- isLessThan
- isLessThanOrEqual
- isGreaterThan
- isGreaterThanOrEqual
## 2020-02-25

@@ -7,0 +21,0 @@ - add `symbol_code` unit tests from C++ tests

@@ -29,2 +29,8 @@ import { Symbol } from "./symbol";

symbol: Symbol;
/**
* Construct a new asset given the symbol name and the amount
*
* @param amount - The amount of the asset
* @param sym - The name of the symbol
*/
constructor(amount?: string | number | bigint, sym?: Symbol);

@@ -45,4 +51,143 @@ /**

is_valid(): boolean;
/**
* Set the amount of the asset
*
* @param a - New amount for the asset
*/
set_amount(amount: bigint | number): void;
/**
* Subtraction assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is subtracted by the amount of asset a
*/
minus(a: Asset | number | bigint): Asset;
/**
* Addition Assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is added with the amount of asset a
*/
plus(a: Asset | number | bigint): Asset;
/**
* Addition operator
*
* @param a - The first asset to be added
* @param b - The second asset to be added
* @return asset - New asset as the result of addition
*/
static plus(a: Asset, b: Asset): Asset;
/**
* Subtraction operator
*
* @param a - The asset to be subtracted
* @param b - The asset used to subtract
* @return asset - New asset as the result of subtraction of a with b
*/
static minus(a: Asset, b: Asset): Asset;
/**
* Multiplication assignment operator, with a number
*
* @details Multiplication assignment operator. Multiply the amount of this asset with a number and then assign the value to itself.
* @param a - The multiplier for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is multiplied by a
*/
times(a: number | bigint | Asset): Asset;
/**
* Multiplication operator, with a number proceeding
*
* @brief Multiplication operator, with a number proceeding
* @param a - The asset to be multiplied
* @param b - The multiplier for the asset's amount
* @return asset - New asset as the result of multiplication
*/
static times(a: Asset, b: number | bigint | Asset): Asset;
/**
* @brief Division assignment operator, with a number
*
* @details Division assignment operator. Divide the amount of this asset with a number and then assign the value to itself.
* @param a - The divisor for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is divided by a
*/
div(a: number | bigint | Asset): Asset;
/**
* Division operator, with a number proceeding
*
* @param a - The asset to be divided
* @param b - The divisor for the asset's amount
* @return asset - New asset as the result of division
*/
static div(a: Asset, b: number | bigint | Asset): Asset;
/**
* Equality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset has the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isEqual(a: Asset, b: Asset): boolean;
isEqual(a: Asset): boolean;
/**
* Inequality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset doesn't have the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isNotEqual(a: Asset, b: Asset): boolean;
isNotEqual(a: Asset): boolean;
/**
* Less than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThan(a: Asset, b: Asset): boolean;
isLessThan(a: Asset): boolean;
/**
* Less or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThanOrEqual(a: Asset, b: Asset): boolean;
isLessThanOrEqual(a: Asset): boolean;
/**
* Greater than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThan(a: Asset, b: Asset): boolean;
isGreaterThan(a: Asset): boolean;
/**
* Greater or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThanOrEqual(a: Asset, b: Asset): boolean;
isGreaterThanOrEqual(a: Asset): boolean;
to_string(): string;
}
export declare function asset(amount?: string | number | bigint, sym?: Symbol): Asset;

4

dist/eosiolib/symbol.d.ts

@@ -17,3 +17,3 @@ import { SymbolCode } from "./symbol_code";

*/
constructor(sc: string | SymbolCode | number | bigint, precision: number | bigint);
constructor(sc?: string | Symbol | SymbolCode | number | bigint, precision?: number | bigint);
/**

@@ -56,2 +56,2 @@ * Is this symbol valid

}
export declare function symbol(sc: number | string | SymbolCode, precision: number | bigint): Symbol;
export declare function symbol(sc?: string | SymbolCode | number | bigint, precision?: number | bigint): Symbol;

@@ -8,1 +8,2 @@ import { Symbol } from "./symbol";

export declare function number_to_bigint(num: number): bigint;
export declare function isNull(value: any): boolean;

@@ -8,1 +8,2 @@ export * from "./eosiolib/asset";

export * from "./eosiolib/voting";
export * from "./eosiolib/eosiolib";

@@ -134,2 +134,32 @@ // https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/check.hpp

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;
}
// console.log(isNull(undefined))
// console.log(isNull(null))
// console.log(isNull("foo"))
// console.log(isNull(0))
// (() => {
// const asset = number_to_asset(3.6587120707054996, new Symbol("EOS", 4));
// })();
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp

@@ -152,7 +182,4 @@ class Symbol {

this.value = BigInt(0);
if (sc != undefined)
check(precision != undefined, "[precision] is required");
if (typeof sc == "string" && precision) {
const symcode = new SymbolCode(sc).raw();
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision);
if (isNull(sc) && isNull(precision)) {
this.value = BigInt(0);
}

@@ -162,6 +189,15 @@ else if (typeof sc == "number" || typeof sc == "bigint") {

}
else if (typeof sc == typeof SymbolCode) {
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);
}
else if (typeof sc == "object") {
check(!isNull(precision), "[precision] is required");
const symcode = sc;
this.value = BigInt(symcode.raw()) << BigInt(8) | BigInt(precision);
}
else {
check(false, "invalid symbol parameters");
}
}

@@ -244,24 +280,34 @@ /**

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()));
/**
* Writes a number as a string
*
* @brief Writes number x 10^(-num_decimal_places) (optionally negative) as a string
* @param number - The number to print before shifting the decimal point to the left by num_decimal_places.
* @param num_decimal_places - The number of decimal places to shift the decimal point.
* @param negative - Whether to print a minus sign in the front.
*/
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()) {
if (num == "-") {
isNegative = true;
continue;
}
if (num_decimal_places != 0 && num_decimal_places == num_digits)
str = "." + str;
str = num + str;
num_digits += 1;
}
if (num_digits == num_decimal_places)
str = "0." + str;
else if (num_digits < num_decimal_places)
str = "0." + "0".repeat(num_decimal_places - num_digits) + str;
else if (str[0] == ".")
str = "0" + str;
if (negative && isNegative)
str = "-" + str;
return str;
}
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));
}
// (() => {
// const asset = number_to_asset(3.6587120707054996, new Symbol("EOS", 4));
// })();

@@ -284,2 +330,8 @@ // https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp

class Asset {
/**
* Construct a new asset given the symbol name and the amount
*
* @param amount - The amount of the asset
* @param sym - The name of the symbol
*/
constructor(amount, sym) {

@@ -290,3 +342,10 @@ /**

this.amount = BigInt(0);
if (typeof amount == "string") {
/**
* {symbol} The symbol name of the asset
*/
this.symbol = symbol();
if (isNull(amount) && isNull(sym)) {
return;
}
else if (typeof amount == "string") {
const [amount_str, symbol_str] = amount.split(" ");

@@ -325,5 +384,247 @@ const precision = (amount_str.split(".")[1] || []).length;

}
/**
* Set the amount of the asset
*
* @param a - New amount for the asset
*/
set_amount(amount) {
this.amount = BigInt(amount);
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62");
}
/**
* Subtraction assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is subtracted by the amount of asset a
*/
minus(a) {
if (typeof a == "number" || typeof a == "bigint") {
this.amount -= BigInt(a);
}
else {
check(a.symbol == this.symbol, "attempt to subtract asset with different symbol");
this.amount -= a.amount;
}
check(-Asset.max_amount <= this.amount, "subtraction underflow");
check(this.amount <= Asset.max_amount, "subtraction overflow");
return this;
}
/**
* Addition Assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is added with the amount of asset a
*/
plus(a) {
if (typeof a == "number" || typeof a == "bigint") {
this.amount += BigInt(a);
}
else {
check(a.symbol == this.symbol, "attempt to add asset with different symbol");
this.amount += a.amount;
}
check(-Asset.max_amount <= this.amount, "addition underflow");
check(this.amount <= Asset.max_amount, "addition overflow");
return this;
}
/**
* Addition operator
*
* @param a - The first asset to be added
* @param b - The second asset to be added
* @return asset - New asset as the result of addition
*/
static plus(a, b) {
const result = new Asset(a.amount, a.symbol);
result.plus(b);
return result;
}
/**
* Subtraction operator
*
* @param a - The asset to be subtracted
* @param b - The asset used to subtract
* @return asset - New asset as the result of subtraction of a with b
*/
static minus(a, b) {
const result = new Asset(a.amount, a.symbol);
result.minus(b);
return result;
}
/**
* Multiplication assignment operator, with a number
*
* @details Multiplication assignment operator. Multiply the amount of this asset with a number and then assign the value to itself.
* @param a - The multiplier for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is multiplied by a
*/
times(a) {
let amount;
if (typeof a == "number" || typeof a == "bigint") {
amount = BigInt(a);
}
else {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
amount = a.amount;
}
const tmp = this.amount * amount;
check(tmp <= Asset.max_amount, "multiplication overflow");
check(tmp >= -Asset.max_amount, "multiplication underflow");
this.amount = tmp;
return this;
}
/**
* Multiplication operator, with a number proceeding
*
* @brief Multiplication operator, with a number proceeding
* @param a - The asset to be multiplied
* @param b - The multiplier for the asset's amount
* @return asset - New asset as the result of multiplication
*/
static times(a, b) {
const result = new Asset(a.amount, a.symbol);
result.times(b);
return result;
}
/**
* @brief Division assignment operator, with a number
*
* @details Division assignment operator. Divide the amount of this asset with a number and then assign the value to itself.
* @param a - The divisor for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is divided by a
*/
div(a) {
let amount;
if (typeof a == "number" || typeof a == "bigint") {
amount = BigInt(a);
}
else {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
amount = a.amount;
}
check(amount != BigInt(0), "divide by zero");
check(!(this.amount == -Asset.max_amount && amount == BigInt(-1)), "signed division overflow");
this.amount /= amount;
return this;
}
/**
* Division operator, with a number proceeding
*
* @param a - The asset to be divided
* @param b - The divisor for the asset's amount
* @return asset - New asset as the result of division
*/
static div(a, b) {
const result = new Asset(a.amount, a.symbol);
result.div(b);
return result;
}
/**
* Equality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset has the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount == b.amount;
}
isEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return a.amount == this.amount;
}
/**
* Inequality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset doesn't have the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isNotEqual(a, b) {
return !(a == b);
}
isNotEqual(a) {
return !(a == this);
}
/**
* Less than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThan(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount < b.amount;
}
isLessThan(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount < a.amount;
}
/**
* Less or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThanOrEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount <= b.amount;
}
isLessThanOrEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount <= a.amount;
}
/**
* Greater than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThan(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount > b.amount;
}
isGreaterThan(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount > a.amount;
}
/**
* Greater or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThanOrEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount >= b.amount;
}
isGreaterThanOrEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount >= a.amount;
}
to_string() {
const fixed = asset_to_number(this).toFixed(this.symbol.precision());
return `${fixed} ${this.symbol.code().to_string()}`;
const amount = write_decimal(this.amount, this.symbol.precision(), true);
const symcode = this.symbol.code().to_string();
return `${amount} ${symcode}`;
}

@@ -334,6 +635,16 @@ }

*/
Asset.max_amount = BigInt(2 ** 62 - 1);
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1);
function asset(amount, sym) {
return new Asset(amount, sym);
}
// const asset_mask: bigint = (BigInt(1) << BigInt(62)) - BigInt(1);
// const asset_min: bigint = -asset_mask; // -4611686018427387903
// const asset_max: bigint = asset_mask; // 4611686018427387903
// const sym_no_prec = symbol("SYMBOLL", 0n); // Symbol with no precision
// const sym_prec = symbol("SYMBOLL", 63n); // Symbol with precision
// console.log( "asset_min", asset_min );
// console.log( asset( asset_min, sym_no_prec ).to_string() )
// .toBe( "-4611686018427387903 SYMBOLL" )
// asset( asset_min - 1n, symbol() )
// console.log( asset( asset_min, sym_no_prec ).minus( 1 ).amount );
// (() => {

@@ -381,3 +692,3 @@ // const quantity = new Asset(9643, new Symbol("USD", 4));

export { Asset, Symbol, SymbolCode, asset, asset_to_bigint, asset_to_number, bigint_to_asset, block_timestamp_epoch, check, current_time_point, number_to_asset, number_to_bigint, seconds_per_day, stake2vote, symbol, symbol_code, vote2stake, voteWeightToday };
export { Asset, Symbol, SymbolCode, asset, asset_to_bigint, asset_to_number, bigint_to_asset, block_timestamp_epoch, check, current_time_point, isNull, number_to_asset, number_to_bigint, seconds_per_day, stake2vote, symbol, symbol_code, vote2stake, voteWeightToday, write_decimal };
//# sourceMappingURL=index.es.js.map

@@ -138,2 +138,32 @@ 'use strict';

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;
}
// console.log(isNull(undefined))
// console.log(isNull(null))
// console.log(isNull("foo"))
// console.log(isNull(0))
// (() => {
// const asset = number_to_asset(3.6587120707054996, new Symbol("EOS", 4));
// })();
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp

@@ -156,7 +186,4 @@ class Symbol {

this.value = BigInt(0);
if (sc != undefined)
check(precision != undefined, "[precision] is required");
if (typeof sc == "string" && precision) {
const symcode = new SymbolCode(sc).raw();
this.value = BigInt(symcode) << BigInt(8) | BigInt(precision);
if (isNull(sc) && isNull(precision)) {
this.value = BigInt(0);
}

@@ -166,6 +193,15 @@ else if (typeof sc == "number" || typeof sc == "bigint") {

}
else if (typeof sc == typeof SymbolCode) {
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);
}
else if (typeof sc == "object") {
check(!isNull(precision), "[precision] is required");
const symcode = sc;
this.value = BigInt(symcode.raw()) << BigInt(8) | BigInt(precision);
}
else {
check(false, "invalid symbol parameters");
}
}

@@ -248,24 +284,34 @@ /**

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()));
/**
* Writes a number as a string
*
* @brief Writes number x 10^(-num_decimal_places) (optionally negative) as a string
* @param number - The number to print before shifting the decimal point to the left by num_decimal_places.
* @param num_decimal_places - The number of decimal places to shift the decimal point.
* @param negative - Whether to print a minus sign in the front.
*/
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()) {
if (num == "-") {
isNegative = true;
continue;
}
if (num_decimal_places != 0 && num_decimal_places == num_digits)
str = "." + str;
str = num + str;
num_digits += 1;
}
if (num_digits == num_decimal_places)
str = "0." + str;
else if (num_digits < num_decimal_places)
str = "0." + "0".repeat(num_decimal_places - num_digits) + str;
else if (str[0] == ".")
str = "0" + str;
if (negative && isNegative)
str = "-" + str;
return str;
}
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));
}
// (() => {
// const asset = number_to_asset(3.6587120707054996, new Symbol("EOS", 4));
// })();

@@ -288,2 +334,8 @@ // https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp

class Asset {
/**
* Construct a new asset given the symbol name and the amount
*
* @param amount - The amount of the asset
* @param sym - The name of the symbol
*/
constructor(amount, sym) {

@@ -294,3 +346,10 @@ /**

this.amount = BigInt(0);
if (typeof amount == "string") {
/**
* {symbol} The symbol name of the asset
*/
this.symbol = symbol();
if (isNull(amount) && isNull(sym)) {
return;
}
else if (typeof amount == "string") {
const [amount_str, symbol_str] = amount.split(" ");

@@ -329,5 +388,247 @@ const precision = (amount_str.split(".")[1] || []).length;

}
/**
* Set the amount of the asset
*
* @param a - New amount for the asset
*/
set_amount(amount) {
this.amount = BigInt(amount);
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62");
}
/**
* Subtraction assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is subtracted by the amount of asset a
*/
minus(a) {
if (typeof a == "number" || typeof a == "bigint") {
this.amount -= BigInt(a);
}
else {
check(a.symbol == this.symbol, "attempt to subtract asset with different symbol");
this.amount -= a.amount;
}
check(-Asset.max_amount <= this.amount, "subtraction underflow");
check(this.amount <= Asset.max_amount, "subtraction overflow");
return this;
}
/**
* Addition Assignment operator
*
* @param a - Another asset to subtract this asset with
* @return asset& - Reference to this asset
* @post The amount of this asset is added with the amount of asset a
*/
plus(a) {
if (typeof a == "number" || typeof a == "bigint") {
this.amount += BigInt(a);
}
else {
check(a.symbol == this.symbol, "attempt to add asset with different symbol");
this.amount += a.amount;
}
check(-Asset.max_amount <= this.amount, "addition underflow");
check(this.amount <= Asset.max_amount, "addition overflow");
return this;
}
/**
* Addition operator
*
* @param a - The first asset to be added
* @param b - The second asset to be added
* @return asset - New asset as the result of addition
*/
static plus(a, b) {
const result = new Asset(a.amount, a.symbol);
result.plus(b);
return result;
}
/**
* Subtraction operator
*
* @param a - The asset to be subtracted
* @param b - The asset used to subtract
* @return asset - New asset as the result of subtraction of a with b
*/
static minus(a, b) {
const result = new Asset(a.amount, a.symbol);
result.minus(b);
return result;
}
/**
* Multiplication assignment operator, with a number
*
* @details Multiplication assignment operator. Multiply the amount of this asset with a number and then assign the value to itself.
* @param a - The multiplier for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is multiplied by a
*/
times(a) {
let amount;
if (typeof a == "number" || typeof a == "bigint") {
amount = BigInt(a);
}
else {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
amount = a.amount;
}
const tmp = this.amount * amount;
check(tmp <= Asset.max_amount, "multiplication overflow");
check(tmp >= -Asset.max_amount, "multiplication underflow");
this.amount = tmp;
return this;
}
/**
* Multiplication operator, with a number proceeding
*
* @brief Multiplication operator, with a number proceeding
* @param a - The asset to be multiplied
* @param b - The multiplier for the asset's amount
* @return asset - New asset as the result of multiplication
*/
static times(a, b) {
const result = new Asset(a.amount, a.symbol);
result.times(b);
return result;
}
/**
* @brief Division assignment operator, with a number
*
* @details Division assignment operator. Divide the amount of this asset with a number and then assign the value to itself.
* @param a - The divisor for the asset's amount
* @return asset - Reference to this asset
* @post The amount of this asset is divided by a
*/
div(a) {
let amount;
if (typeof a == "number" || typeof a == "bigint") {
amount = BigInt(a);
}
else {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
amount = a.amount;
}
check(amount != BigInt(0), "divide by zero");
check(!(this.amount == -Asset.max_amount && amount == BigInt(-1)), "signed division overflow");
this.amount /= amount;
return this;
}
/**
* Division operator, with a number proceeding
*
* @param a - The asset to be divided
* @param b - The divisor for the asset's amount
* @return asset - New asset as the result of division
*/
static div(a, b) {
const result = new Asset(a.amount, a.symbol);
result.div(b);
return result;
}
/**
* Equality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset has the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount == b.amount;
}
isEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return a.amount == this.amount;
}
/**
* Inequality operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if both asset doesn't have the same amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isNotEqual(a, b) {
return !(a == b);
}
isNotEqual(a) {
return !(a == this);
}
/**
* Less than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThan(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount < b.amount;
}
isLessThan(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount < a.amount;
}
/**
* Less or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is less or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isLessThanOrEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount <= b.amount;
}
isLessThanOrEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount <= a.amount;
}
/**
* Greater than operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater than the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThan(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount > b.amount;
}
isGreaterThan(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount > a.amount;
}
/**
* Greater or equal to operator
*
* @param a - The first asset to be compared
* @param b - The second asset to be compared
* @return true - if the first asset's amount is greater or equal to the second asset amount
* @return false - otherwise
* @pre Both asset must have the same symbol
*/
static isGreaterThanOrEqual(a, b) {
check(a.symbol == b.symbol, "comparison of assets with different symbols is not allowed");
return a.amount >= b.amount;
}
isGreaterThanOrEqual(a) {
check(a.symbol == this.symbol, "comparison of assets with different symbols is not allowed");
return this.amount >= a.amount;
}
to_string() {
const fixed = asset_to_number(this).toFixed(this.symbol.precision());
return `${fixed} ${this.symbol.code().to_string()}`;
const amount = write_decimal(this.amount, this.symbol.precision(), true);
const symcode = this.symbol.code().to_string();
return `${amount} ${symcode}`;
}

@@ -338,6 +639,16 @@ }

*/
Asset.max_amount = BigInt(2 ** 62 - 1);
Asset.max_amount = (BigInt(1) << BigInt(62)) - BigInt(1);
function asset(amount, sym) {
return new Asset(amount, sym);
}
// const asset_mask: bigint = (BigInt(1) << BigInt(62)) - BigInt(1);
// const asset_min: bigint = -asset_mask; // -4611686018427387903
// const asset_max: bigint = asset_mask; // 4611686018427387903
// const sym_no_prec = symbol("SYMBOLL", 0n); // Symbol with no precision
// const sym_prec = symbol("SYMBOLL", 63n); // Symbol with precision
// console.log( "asset_min", asset_min );
// console.log( asset( asset_min, sym_no_prec ).to_string() )
// .toBe( "-4611686018427387903 SYMBOLL" )
// asset( asset_min - 1n, symbol() )
// console.log( asset( asset_min, sym_no_prec ).minus( 1 ).amount );
// (() => {

@@ -395,2 +706,3 @@ // const quantity = new Asset(9643, new Symbol("USD", 4));

exports.current_time_point = current_time_point;
exports.isNull = isNull;
exports.number_to_asset = number_to_asset;

@@ -404,2 +716,3 @@ exports.number_to_bigint = number_to_bigint;

exports.voteWeightToday = voteWeightToday;
exports.write_decimal = write_decimal;
//# sourceMappingURL=index.js.map
{
"name": "eos-common",
"version": "0.4.8",
"version": "0.5.0",
"description": "EOSIO Smart Contract common library used for Typescript",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc