eos-common
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -20,7 +20,7 @@ import { Symbol } from "./symbol"; | ||
*/ | ||
static max_amount: number; | ||
static max_amount: bigint; | ||
/** | ||
* {int64_t} The amount of the asset | ||
*/ | ||
amount: number; | ||
amount: bigint; | ||
/** | ||
@@ -30,3 +30,3 @@ * {symbol} The symbol name of the asset | ||
symbol: Symbol; | ||
constructor(amount: number, sym: Symbol); | ||
constructor(amount?: string | number | BigInt, sym?: Symbol); | ||
/** | ||
@@ -47,3 +47,3 @@ * Check if the amount doesn't exceed the max amount | ||
to_string(): string; | ||
to_double(): number; | ||
} | ||
export declare function asset(amount?: number | BigInt, sym?: Symbol): Asset; |
@@ -17,3 +17,3 @@ import { SymbolCode } from "./symbol_code"; | ||
*/ | ||
constructor(sc?: string | SymbolCode | number | BigInt, precision?: number); | ||
constructor(sc: string | SymbolCode | number | BigInt, precision: number | BigInt); | ||
/** | ||
@@ -56,2 +56,2 @@ * Is this symbol valid | ||
} | ||
export declare function symbol(sc?: number | string | SymbolCode, precision?: number): Symbol; | ||
export declare function symbol(sc: number | string | SymbolCode, precision: number | BigInt): Symbol; |
import { Symbol } from "./symbol"; | ||
import { Asset } from "./asset"; | ||
/** | ||
* Split quantity string | ||
* | ||
* @param {string} quantity Quantity string | ||
* @returns {Asset} | ||
* @example | ||
* | ||
* const quantity = split("1.0000 EOS"); | ||
* quantity.amount //=> 10000 | ||
* quantity.symbol.precision //=> 4 | ||
* quantity.symbol.code() //=> "EOS" | ||
*/ | ||
export declare function split(quantity: string): Asset; | ||
export declare function asset_to_double(quantity: Asset): number; | ||
export declare function double_to_asset(amount: number, sym: Symbol): Asset; | ||
export declare function asset_to_bigint(quantity: Asset): BigInt; | ||
export declare function bigint_to_asset(amount: number | BigInt, sym: Symbol): Asset; | ||
export declare function asset_to_number(quantity: Asset): number; | ||
export declare function number_to_asset(amount: number | BigInt, sym: Symbol): Asset; |
@@ -241,28 +241,17 @@ // https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/core/eosio/check.hpp | ||
// import { Decimal } from "decimal.js"; | ||
/** | ||
* Split quantity string | ||
* | ||
* @param {string} quantity Quantity string | ||
* @returns {Asset} | ||
* @example | ||
* | ||
* const quantity = split("1.0000 EOS"); | ||
* quantity.amount //=> 10000 | ||
* quantity.symbol.precision //=> 4 | ||
* quantity.symbol.code() //=> "EOS" | ||
*/ | ||
function split(quantity) { | ||
const [amount, symbol] = quantity.split(" "); | ||
const precision = (amount.split(".")[1] || []).length; | ||
const amount_uint64 = Number(amount) * Math.pow(10, precision); | ||
return new Asset(amount_uint64, new Symbol(symbol, precision)); | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return quantity.amount / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function asset_to_double(quantity) { | ||
if (quantity.amount == 0) | ||
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 quantity.amount / Math.pow(10, quantity.symbol.precision()); | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function double_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
function number_to_asset(amount, sym) { | ||
return new Asset(Number(amount) * Math.pow(10, sym.precision()), sym); | ||
} | ||
@@ -290,7 +279,18 @@ | ||
*/ | ||
this.amount = 0; | ||
this.amount = amount; | ||
this.symbol = sym; | ||
this.amount = BigInt(0); | ||
if (typeof amount == "string") { | ||
const [amount_str, symbol_str] = amount.split(" "); | ||
const precision = (amount_str.split(".")[1] || []).length; | ||
this.amount = BigInt(Number(amount_str)) * BigInt(Math.pow(10, precision)); | ||
this.symbol = new Symbol(symbol_str, precision); | ||
} | ||
else if (sym) { | ||
this.amount = BigInt(amount); | ||
this.symbol = sym; | ||
} | ||
else { | ||
throw new Error("[sym] is required"); | ||
} | ||
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
// check( this.symbol.is_valid(), "invalid symbol name" ); | ||
check(this.symbol.is_valid(), "invalid symbol name"); | ||
} | ||
@@ -313,12 +313,8 @@ /** | ||
is_valid() { | ||
// return this.is_amount_within_range() && this.symbol.is_valid(); | ||
return true; | ||
return this.is_amount_within_range() && this.symbol.is_valid(); | ||
} | ||
to_string() { | ||
const amount = this.to_double().toFixed(this.symbol.precision()); | ||
return `${amount} ${this.symbol.code().to_string()}`; | ||
const fixed = asset_to_number(this).toFixed(this.symbol.precision()); | ||
return `${fixed} ${this.symbol.code().to_string()}`; | ||
} | ||
to_double() { | ||
return asset_to_double(this); | ||
} | ||
} | ||
@@ -328,3 +324,10 @@ /** | ||
*/ | ||
Asset.max_amount = 2 ** 62 - 1; | ||
Asset.max_amount = BigInt(2 ** 62 - 1); | ||
function asset(amount, sym) { | ||
return new Asset(amount, sym); | ||
} | ||
// (() => { | ||
// const quantity = new Asset("1.0000 EOS"); | ||
// console.log(quantity); | ||
// })() | ||
@@ -368,3 +371,3 @@ class Time { | ||
export { Asset, Symbol, SymbolCode, asset_to_double, block_timestamp_epoch, check, current_time_point, double_to_asset, seconds_per_day, split, 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, number_to_asset, seconds_per_day, stake2vote, symbol, symbol_code, vote2stake, voteWeightToday }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -245,28 +245,17 @@ 'use strict'; | ||
// import { Decimal } from "decimal.js"; | ||
/** | ||
* Split quantity string | ||
* | ||
* @param {string} quantity Quantity string | ||
* @returns {Asset} | ||
* @example | ||
* | ||
* const quantity = split("1.0000 EOS"); | ||
* quantity.amount //=> 10000 | ||
* quantity.symbol.precision //=> 4 | ||
* quantity.symbol.code() //=> "EOS" | ||
*/ | ||
function split(quantity) { | ||
const [amount, symbol] = quantity.split(" "); | ||
const precision = (amount.split(".")[1] || []).length; | ||
const amount_uint64 = Number(amount) * Math.pow(10, precision); | ||
return new Asset(amount_uint64, new Symbol(symbol, precision)); | ||
function asset_to_bigint(quantity) { | ||
if (quantity.amount == BigInt(0)) | ||
return BigInt(0.0); | ||
return quantity.amount / BigInt(Math.pow(10, quantity.symbol.precision())); | ||
} | ||
function asset_to_double(quantity) { | ||
if (quantity.amount == 0) | ||
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 quantity.amount / Math.pow(10, quantity.symbol.precision()); | ||
return Number(quantity.amount) / Math.pow(10, quantity.symbol.precision()); | ||
} | ||
function double_to_asset(amount, sym) { | ||
return new Asset(amount * Math.pow(10, sym.precision()), sym); | ||
function number_to_asset(amount, sym) { | ||
return new Asset(Number(amount) * Math.pow(10, sym.precision()), sym); | ||
} | ||
@@ -294,7 +283,18 @@ | ||
*/ | ||
this.amount = 0; | ||
this.amount = amount; | ||
this.symbol = sym; | ||
this.amount = BigInt(0); | ||
if (typeof amount == "string") { | ||
const [amount_str, symbol_str] = amount.split(" "); | ||
const precision = (amount_str.split(".")[1] || []).length; | ||
this.amount = BigInt(Number(amount_str)) * BigInt(Math.pow(10, precision)); | ||
this.symbol = new Symbol(symbol_str, precision); | ||
} | ||
else if (sym) { | ||
this.amount = BigInt(amount); | ||
this.symbol = sym; | ||
} | ||
else { | ||
throw new Error("[sym] is required"); | ||
} | ||
check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
// check( this.symbol.is_valid(), "invalid symbol name" ); | ||
check(this.symbol.is_valid(), "invalid symbol name"); | ||
} | ||
@@ -317,12 +317,8 @@ /** | ||
is_valid() { | ||
// return this.is_amount_within_range() && this.symbol.is_valid(); | ||
return true; | ||
return this.is_amount_within_range() && this.symbol.is_valid(); | ||
} | ||
to_string() { | ||
const amount = this.to_double().toFixed(this.symbol.precision()); | ||
return `${amount} ${this.symbol.code().to_string()}`; | ||
const fixed = asset_to_number(this).toFixed(this.symbol.precision()); | ||
return `${fixed} ${this.symbol.code().to_string()}`; | ||
} | ||
to_double() { | ||
return asset_to_double(this); | ||
} | ||
} | ||
@@ -332,3 +328,10 @@ /** | ||
*/ | ||
Asset.max_amount = 2 ** 62 - 1; | ||
Asset.max_amount = BigInt(2 ** 62 - 1); | ||
function asset(amount, sym) { | ||
return new Asset(amount, sym); | ||
} | ||
// (() => { | ||
// const quantity = new Asset("1.0000 EOS"); | ||
// console.log(quantity); | ||
// })() | ||
@@ -375,9 +378,11 @@ class Time { | ||
exports.SymbolCode = SymbolCode; | ||
exports.asset_to_double = asset_to_double; | ||
exports.asset = asset; | ||
exports.asset_to_bigint = asset_to_bigint; | ||
exports.asset_to_number = asset_to_number; | ||
exports.bigint_to_asset = bigint_to_asset; | ||
exports.block_timestamp_epoch = block_timestamp_epoch; | ||
exports.check = check; | ||
exports.current_time_point = current_time_point; | ||
exports.double_to_asset = double_to_asset; | ||
exports.number_to_asset = number_to_asset; | ||
exports.seconds_per_day = seconds_per_day; | ||
exports.split = split; | ||
exports.stake2vote = stake2vote; | ||
@@ -384,0 +389,0 @@ exports.symbol = symbol; |
{ | ||
"name": "eos-common", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"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
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
38204
924