eos-common
Advanced tools
Comparing version 0.1.36 to 0.2.0
import { Decimal } from "decimal.js"; | ||
import { Symbol } from "./symbol"; | ||
/** | ||
* Asset | ||
* | ||
* @name Asset | ||
* @param {number} amount The amount of the asset | ||
* @param {Symbol} sym The name of the symbol | ||
* @returns {Asset} Asset | ||
* @example | ||
* | ||
* const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
* quantity.toString() //=> "1.0000 EOS"; | ||
* quantity.symbol.code() //=> "EOS" | ||
* quantity.symbol.precision //=> 4 | ||
*/ | ||
export declare class Asset { | ||
@@ -16,15 +30,2 @@ /** | ||
symbol: Symbol; | ||
/** | ||
* Asset Class | ||
* | ||
* @param {number} a The amount of the asset | ||
* @param {Symbol} sym The name of the symbol | ||
* @returns {Asset} Asset | ||
* @example | ||
* | ||
* const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
* quantity.toString() //=> "1.0000 EOS"; | ||
* quantity.symbol.code() //=> "EOS" | ||
* quantity.symbol.precision //=> 4 | ||
*/ | ||
constructor(amount: number, sym: Symbol); | ||
@@ -49,14 +50,1 @@ /** | ||
} | ||
/** | ||
* 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; |
"use strict"; | ||
// https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const decimal_js_1 = require("decimal.js"); | ||
const symbol_1 = require("./symbol"); | ||
const eosio = __importStar(require("./core/eosio")); | ||
const check_1 = require("./check"); | ||
/** | ||
* Asset | ||
* | ||
* @name Asset | ||
* @param {number} amount The amount of the asset | ||
* @param {Symbol} sym The name of the symbol | ||
* @returns {Asset} Asset | ||
* @example | ||
* | ||
* const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
* quantity.toString() //=> "1.0000 EOS"; | ||
* quantity.symbol.code() //=> "EOS" | ||
* quantity.symbol.precision //=> 4 | ||
*/ | ||
class Asset { | ||
/** | ||
* Asset Class | ||
* | ||
* @param {number} a The amount of the asset | ||
* @param {Symbol} sym The name of the symbol | ||
* @returns {Asset} Asset | ||
* @example | ||
* | ||
* const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
* quantity.toString() //=> "1.0000 EOS"; | ||
* quantity.symbol.code() //=> "EOS" | ||
* quantity.symbol.precision //=> 4 | ||
*/ | ||
constructor(amount, sym) { | ||
@@ -35,4 +28,4 @@ /** | ||
this.symbol = sym; | ||
eosio.check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
// eosio.check( this.symbol.is_valid(), "invalid symbol name" ); | ||
check_1.check(this.is_amount_within_range(), "magnitude of asset amount must be less than 2^62"); | ||
// check( this.symbol.is_valid(), "invalid symbol name" ); | ||
} | ||
@@ -74,20 +67,1 @@ /** | ||
Asset.max_amount = Math.pow(2, 62) - 1; | ||
/** | ||
* 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 = new decimal_js_1.Decimal(amount).times(new decimal_js_1.Decimal(10).pow(precision)).toNumber(); | ||
return new Asset(amount_uint64, new symbol_1.Symbol(symbol, precision)); | ||
} | ||
exports.split = split; |
@@ -12,4 +12,5 @@ /** | ||
/** | ||
* Symbol Class | ||
* Symbol | ||
* | ||
* @name Symbol | ||
* @param {string} code Symbol Code | ||
@@ -16,0 +17,0 @@ * @param {number} precision Precision |
@@ -16,4 +16,5 @@ "use strict"; | ||
/** | ||
* Symbol Class | ||
* Symbol | ||
* | ||
* @name Symbol | ||
* @param {string} code Symbol Code | ||
@@ -36,5 +37,5 @@ * @param {number} precision Precision | ||
isEqual(comparison) { | ||
return comparison.code() === this.code() && comparison.precision === this.precision; | ||
return comparison.code === this.code && comparison.precision === this.precision; | ||
} | ||
} | ||
exports.Symbol = Symbol; |
export * from "./eosiolib/asset"; | ||
export * from "./eosiolib/symbol"; | ||
export * from "./eosiolib/time"; | ||
export * from "./eosiolib/voting"; | ||
export * from "./eosiolib/utils"; |
@@ -8,1 +8,4 @@ "use strict"; | ||
__export(require("./eosiolib/symbol")); | ||
__export(require("./eosiolib/time")); | ||
__export(require("./eosiolib/voting")); | ||
__export(require("./eosiolib/utils")); |
{ | ||
"name": "eos-common", | ||
"version": "0.1.36", | ||
"version": "0.2.0", | ||
"description": "EOSIO Smart Contract common library used for Typescript", | ||
@@ -19,3 +19,3 @@ "main": "build/index.js", | ||
"posttest": "tslint -p .", | ||
"docs": "tsc && documentation readme index.js -s API" | ||
"docs": "tsc && documentation readme build/index.js -s API" | ||
}, | ||
@@ -22,0 +22,0 @@ "repository": "git@github.com:EOS-Nation/eos-common.git", |
128
README.md
@@ -13,7 +13,7 @@ # `eos-common` | ||
- [Asset](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp) | ||
- [Symbol](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
- [SymbolCode](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
- [ExtendedAsset](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp) | ||
- [ExtendedSymbol](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
- [Asset](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp) | ||
- [Symbol](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
- [SymbolCode](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
- [ExtendedAsset](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/asset.hpp) | ||
- [ExtendedSymbol](https://github.com/EOSIO/eosio.cdt/blob/master/libraries/eosiolib/symbol.hpp) | ||
@@ -37,6 +37,8 @@ ## Installation | ||
```ts | ||
import { split } from "eos-common" | ||
import { Asset, Symbol } from "eos-common" | ||
const { amount } = split("1.0000 EOS"); | ||
amount //=> 10000 | ||
const quantity = new Asset(10000, new Symbol("EOS", 4)); | ||
quantity.toString() //=> "1.0000 EOS"; | ||
quantity.symbol.code() //=> "EOS" | ||
quantity.symbol.precision //=> 4 | ||
``` | ||
@@ -50,29 +52,33 @@ | ||
- [constructor](#constructor) | ||
- [Asset](#asset) | ||
- [Parameters](#parameters) | ||
- [Examples](#examples) | ||
- [amount](#amount) | ||
- [is_amount_within_range](#is_amount_within_range) | ||
- [is_valid](#is_valid) | ||
- [max_amount](#max_amount) | ||
- [split](#split) | ||
- [amount](#amount) | ||
- [is_amount_within_range](#is_amount_within_range) | ||
- [is_valid](#is_valid) | ||
- [max_amount](#max_amount) | ||
- [check](#check) | ||
- [Parameters](#parameters-1) | ||
- [Examples](#examples-1) | ||
- [check](#check) | ||
- [SymbolCode](#symbolcode) | ||
- [Symbol](#symbol) | ||
- [Parameters](#parameters-2) | ||
- [Examples](#examples-2) | ||
- [SymbolCode](#symbolcode) | ||
- [constructor](#constructor-1) | ||
- [stake2vote](#stake2vote) | ||
- [Parameters](#parameters-3) | ||
- [vote2stake](#vote2stake) | ||
- [Parameters](#parameters-4) | ||
- [voteWeightToday](#voteweighttoday) | ||
- [split](#split) | ||
- [Parameters](#parameters-5) | ||
- [Examples](#examples-3) | ||
### constructor | ||
### Asset | ||
Asset Class | ||
Asset | ||
#### Parameters | ||
- `amount` | ||
- `sym` **[Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)** The name of the symbol | ||
- `a` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The amount of the asset | ||
- `amount` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The amount of the asset | ||
- `sym` **[Symbol](#symbol)** The name of the symbol | ||
@@ -88,9 +94,9 @@ #### Examples | ||
Returns **Asset** Asset | ||
Returns **[Asset](#asset)** Asset | ||
### amount | ||
#### amount | ||
{int64_t} The amount of the asset | ||
### is_amount_within_range | ||
#### is_amount_within_range | ||
@@ -103,3 +109,3 @@ Check if the amount doesn't exceed the max amount | ||
### is_valid | ||
#### is_valid | ||
@@ -112,25 +118,6 @@ Check if the asset is valid. %A valid asset has its amount <= max_amount and its symbol name valid | ||
### max_amount | ||
#### max_amount | ||
{constexpr int64_t} Maximum amount possible for this asset. It's capped to 2^62 - 1 | ||
### 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** | ||
### check | ||
@@ -151,9 +138,9 @@ | ||
Returns **void** | ||
Returns **void** | ||
### SymbolCode | ||
### constructor | ||
### Symbol | ||
Symbol Class | ||
Symbol | ||
@@ -173,2 +160,43 @@ #### Parameters | ||
Returns **[Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)** Symbol | ||
Returns **[Symbol](#symbol)** Symbol | ||
### stake2vote | ||
Convert EOS stake into decaying value | ||
#### Parameters | ||
- `staked` | ||
- `vote` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** vote | ||
### vote2stake | ||
Convert vote decay value into EOS stake | ||
#### Parameters | ||
- `vote` | ||
- `staked` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** staked | ||
### voteWeightToday | ||
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)** |
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
17289
21
365
195