Comparing version 0.1.2 to 0.2.0
100
index.d.ts
/** | ||
* Bancor Formula | ||
* | ||
* - token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* - token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* | ||
* ```js | ||
* Formula: | ||
* 1.0000 / (77814.0638 + 1.0000) * 429519.5539120331 | ||
* //=> 5.519748143058556 | ||
* ``` | ||
* | ||
* @param {number} balance_from from token balance in the relay | ||
* @param {number} balance_to to token balance in the relay | ||
* @param {number} amount amount to convert | ||
* @returns {number} computed amount | ||
* @example | ||
* | ||
* // token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* // token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* // The Formula: | ||
* // 10.0000 / (77814.0638 + 10.0000) * 429519.5539120331 | ||
* // 55.19109809221157 | ||
* const balance_from = 77814.0638 // EOS | ||
* const balance_to = 429519.5539120331 // BNT | ||
* const amount = 1 | ||
* | ||
* const source_balance = 77814.0638 // EOS | ||
* const target_balance = 429519.5539120331 // BNT | ||
* const source_amount = 10 | ||
* bancorFormula(source_balance, target_balance, source_amount) | ||
* //=> 55.19109809221157 | ||
* bancorx.bancorFormula(balance_from, balance_to, amount) | ||
* //=> 5.519748143058556 | ||
*/ | ||
export declare function bancorFormula(source_balance: number, target_balance: number, source_amount?: number): number; | ||
export declare function bancorFormula(balance_from: number, balance_to: number, amount: number): number; | ||
/** | ||
* Bancor Inverse Formula | ||
* | ||
* - token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* - token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* | ||
* ```js | ||
* Inverse Formula: | ||
* 77814.0638 / (1.0 - 1 / 429519.5539120331) - 77814.0638 | ||
* //=> 0.18116577989712823 | ||
* ``` | ||
* | ||
* @param {number} balance_from from token balance in the relay | ||
* @param {number} balance_to to token balance in the relay | ||
* @param {number} amount_desired amount to desired | ||
* @returns {number} computed desired amount | ||
* @example | ||
* | ||
* const balance_from = 77814.0638 // EOS | ||
* const balance_to = 429519.5539120331 // BNT | ||
* const amount_desired = 1 | ||
* | ||
* bancorx.bancorInverseFormula(balance_from, balance_to, amount_desired) | ||
* //=> 0.18116577989712823 | ||
*/ | ||
export declare function bancorInverseFormula(balance_from: number, balance_to: number, amount_desired: number): number; | ||
/** | ||
* Parse Memo | ||
* | ||
* @param {Converter[]} converters relay converters | ||
* @param {number} min_return minimum return | ||
* @param {string} dest_account destination acccount | ||
* @param {number} [version] bancor protocol version | ||
* @returns {string} computed memo | ||
* @example | ||
* | ||
* const memo = parseMemo("bnt2eoscnvrt BNT bancorc11144 CUSD", "3.17", "b1") | ||
* //=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,b1" | ||
* const CUSD = bancorx.relays.CUSD; | ||
* const BNT = bancorx.relays.BNT; | ||
* | ||
* // Single converter (BNT => CUSD) | ||
* bancorx.parseMemo([CUSD], "3.17", "<account>") | ||
* //=> "1,bancorc11144 CUSD,3.17,<account>" | ||
* | ||
* // Multi converter (EOS => BNT => CUSD) | ||
* bancorx.parseMemo([BNT, CUSD], "3.17", "<account>") | ||
* //=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,<account>" | ||
*/ | ||
export declare function parseMemo(receiver: string, min_return: string, dest_account: string, version?: number): string; | ||
export declare function parseMemo(converters: Converter[], min_return: string, dest_account: string, version?: number): string; | ||
/** | ||
* Parse Balance | ||
* | ||
* @private | ||
* @param {string|number} balance token balance | ||
* @returns {Object} parsed balance | ||
* @example | ||
* | ||
* parseBalance("10.0000 EOS") //=> {quantity: 10.0, symbol: "EOS"} | ||
* parseBalance(10.0) //=> {quantity: 10.0} | ||
* bancorx.parseBalance("10.0000 EOS") //=> {quantity: 10.0, symbol: "EOS"} | ||
* bancorx.parseBalance(10.0) //=> {quantity: 10.0} | ||
*/ | ||
@@ -44,2 +92,15 @@ export declare function parseBalance(balance: string | number): { | ||
}; | ||
/** | ||
* Relays | ||
* | ||
* @example | ||
* | ||
* bancorx.relays.BNT | ||
* //=> { code: "bntbntbntbnt", account: "bnt2eoscnvrt", symbol: "BNT", precision: 10 } | ||
* | ||
* bancorx.relays.CUSD | ||
* //=> { code: "stablecarbon", account: "bancorc11144", symbol: "CUSD", precision: 2 } | ||
* | ||
*/ | ||
export declare const relays: Relays; | ||
export interface Relay { | ||
@@ -71,2 +132,5 @@ code: string; | ||
} | ||
export declare const relays: Relays; | ||
export interface Converter { | ||
account: string; | ||
symbol: string; | ||
} |
104
index.js
@@ -6,31 +6,84 @@ "use strict"; | ||
* | ||
* - token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* - token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* | ||
* ```js | ||
* Formula: | ||
* 1.0000 / (77814.0638 + 1.0000) * 429519.5539120331 | ||
* //=> 5.519748143058556 | ||
* ``` | ||
* | ||
* @param {number} balance_from from token balance in the relay | ||
* @param {number} balance_to to token balance in the relay | ||
* @param {number} amount amount to convert | ||
* @returns {number} computed amount | ||
* @example | ||
* | ||
* // token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* // token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* // The Formula: | ||
* // 10.0000 / (77814.0638 + 10.0000) * 429519.5539120331 | ||
* // 55.19109809221157 | ||
* const balance_from = 77814.0638 // EOS | ||
* const balance_to = 429519.5539120331 // BNT | ||
* const amount = 1 | ||
* | ||
* const source_balance = 77814.0638 // EOS | ||
* const target_balance = 429519.5539120331 // BNT | ||
* const source_amount = 10 | ||
* bancorFormula(source_balance, target_balance, source_amount) | ||
* //=> 55.19109809221157 | ||
* bancorx.bancorFormula(balance_from, balance_to, amount) | ||
* //=> 5.519748143058556 | ||
*/ | ||
function bancorFormula(source_balance, target_balance, source_amount) { | ||
if (source_amount === void 0) { source_amount = 1; } | ||
return source_amount / (source_balance + source_amount) * target_balance; | ||
function bancorFormula(balance_from, balance_to, amount) { | ||
return amount / (balance_from + amount) * balance_to; | ||
} | ||
exports.bancorFormula = bancorFormula; | ||
/** | ||
* Bancor Inverse Formula | ||
* | ||
* - token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
* - token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
* | ||
* ```js | ||
* Inverse Formula: | ||
* 77814.0638 / (1.0 - 1 / 429519.5539120331) - 77814.0638 | ||
* //=> 0.18116577989712823 | ||
* ``` | ||
* | ||
* @param {number} balance_from from token balance in the relay | ||
* @param {number} balance_to to token balance in the relay | ||
* @param {number} amount_desired amount to desired | ||
* @returns {number} computed desired amount | ||
* @example | ||
* | ||
* const balance_from = 77814.0638 // EOS | ||
* const balance_to = 429519.5539120331 // BNT | ||
* const amount_desired = 1 | ||
* | ||
* bancorx.bancorInverseFormula(balance_from, balance_to, amount_desired) | ||
* //=> 0.18116577989712823 | ||
*/ | ||
function bancorInverseFormula(balance_from, balance_to, amount_desired) { | ||
return balance_from / (1.0 - amount_desired / balance_to) - balance_from; | ||
} | ||
exports.bancorInverseFormula = bancorInverseFormula; | ||
/** | ||
* Parse Memo | ||
* | ||
* @param {Converter[]} converters relay converters | ||
* @param {number} min_return minimum return | ||
* @param {string} dest_account destination acccount | ||
* @param {number} [version] bancor protocol version | ||
* @returns {string} computed memo | ||
* @example | ||
* | ||
* const memo = parseMemo("bnt2eoscnvrt BNT bancorc11144 CUSD", "3.17", "b1") | ||
* //=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,b1" | ||
* const CUSD = bancorx.relays.CUSD; | ||
* const BNT = bancorx.relays.BNT; | ||
* | ||
* // Single converter (BNT => CUSD) | ||
* bancorx.parseMemo([CUSD], "3.17", "<account>") | ||
* //=> "1,bancorc11144 CUSD,3.17,<account>" | ||
* | ||
* // Multi converter (EOS => BNT => CUSD) | ||
* bancorx.parseMemo([BNT, CUSD], "3.17", "<account>") | ||
* //=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,<account>" | ||
*/ | ||
function parseMemo(receiver, min_return, dest_account, version) { | ||
function parseMemo(converters, min_return, dest_account, version) { | ||
if (version === void 0) { version = 1; } | ||
var receiver = converters.map(function (_a) { | ||
var account = _a.account, symbol = _a.symbol; | ||
return account + " " + symbol; | ||
}).join(" "); | ||
return version + "," + receiver + "," + min_return + "," + dest_account; | ||
@@ -42,7 +95,8 @@ } | ||
* | ||
* @private | ||
* @param {string|number} balance token balance | ||
* @returns {Object} parsed balance | ||
* @example | ||
* | ||
* parseBalance("10.0000 EOS") //=> {quantity: 10.0, symbol: "EOS"} | ||
* parseBalance(10.0) //=> {quantity: 10.0} | ||
* bancorx.parseBalance("10.0000 EOS") //=> {quantity: 10.0, symbol: "EOS"} | ||
* bancorx.parseBalance(10.0) //=> {quantity: 10.0} | ||
*/ | ||
@@ -57,2 +111,14 @@ function parseBalance(balance) { | ||
exports.parseBalance = parseBalance; | ||
/** | ||
* Relays | ||
* | ||
* @example | ||
* | ||
* bancorx.relays.BNT | ||
* //=> { code: "bntbntbntbnt", account: "bnt2eoscnvrt", symbol: "BNT", precision: 10 } | ||
* | ||
* bancorx.relays.CUSD | ||
* //=> { code: "stablecarbon", account: "bancorc11144", symbol: "CUSD", precision: 2 } | ||
* | ||
*/ | ||
exports.relays = { | ||
@@ -59,0 +125,0 @@ EOS: { |
{ | ||
"name": "bancorx", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "BancorX Utility", | ||
@@ -24,3 +24,4 @@ "main": "index.js", | ||
"test": "jest --coverage", | ||
"posttest": "tslint -p ." | ||
"posttest": "tslint -p .", | ||
"docs": "tsc && documentation readme index.js -s API" | ||
}, | ||
@@ -30,2 +31,3 @@ "dependencies": {}, | ||
"@types/jest": "*", | ||
"documentation": "*", | ||
"jest": "*", | ||
@@ -32,0 +34,0 @@ "ts-jest": "*", |
205
README.md
# BancorX utility | ||
> Collection of useful Javascript (Typescript) methods used for [BancorX](https://eos.bancor.network). | ||
> | ||
> Wallet (Lynx/Scatter, etc...) `transfer` actions are not included. | ||
@@ -33,66 +35,175 @@ ## Install | ||
## BancorX Formula | ||
## Relays | ||
- token balance of EOS (`eosio.token`) in the relay: `77814.0638 EOS` | ||
- token balance of BNT (`bntbntbntbnt`) in the relay: `429519.5539120331 BNT` | ||
| **symbol** | **code** | **account** | **precision** | | ||
| ---------- | ------------ | ------------ | ------------- | | ||
| EOS | eosio.token | bnt2eoscnvrt | 4 | | ||
| BNT | bntbntbntbnt | bnt2eoscnvrt | 10 | | ||
| ZOS | zosdiscounts | bancorc11151 | 4 | | ||
| IQ | everipediaiq | bancorc11123 | 3 | | ||
| PGL | prospectorsg | bancorc11113 | 4 | | ||
| CUSD | stablecarbon | bancorc11144 | 2 | | ||
| DICE | betdicetoken | bancorc11125 | 2 | | ||
| BLACK | eosblackteam | bancorc11111 | 4 | | ||
| CET | eosiochaince | bancorc11114 | 4 | | ||
| EPRA | epraofficial | bancorc11124 | 4 | | ||
| MEETONE | eosiomeetone | bancorc11122 | 4 | | ||
| ZKS | zkstokensr4u | bancorc11142 | 0 | | ||
| OCT | octtothemoon | bancorc11132 | 4 | | ||
| KARMA | therealkarma | bancorc11112 | 4 | | ||
| HVT | hirevibeshvt | bancorc11131 | 4 | | ||
| HORUS | horustokenio | bancorc11121 | 4 | | ||
| MEV | eosvegascoin | bancorc11134 | 4 | | ||
**The Formula:** | ||
## Get Relay Balances | ||
[`eosjs`](https://github.com/EOSIO/eosjs) is required to use `get_currency_balance` method. | ||
```js | ||
10.0000 / (77814.0638 + 10.0000) * 429519.5539120331 | ||
55.19109809221157 | ||
const {code, account, symbol} = bancorx.relays.CUSD; | ||
const balance = await rpc.get_currency_balance(code, account, symbol); | ||
``` | ||
## API | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
#### Table of Contents | ||
- [bancorFormula](#bancorformula) | ||
- [Parameters](#parameters) | ||
- [Examples](#examples) | ||
- [bancorInverseFormula](#bancorinverseformula) | ||
- [Parameters](#parameters-1) | ||
- [Examples](#examples-1) | ||
- [parseMemo](#parsememo) | ||
- [Parameters](#parameters-2) | ||
- [Examples](#examples-2) | ||
- [parseBalance](#parsebalance) | ||
- [Parameters](#parameters-3) | ||
- [Examples](#examples-3) | ||
- [relays](#relays) | ||
- [Examples](#examples-4) | ||
### bancorFormula | ||
Bancor Formula | ||
- token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
- token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
```js | ||
const source_balance = 77814.0638 // EOS | ||
const target_balance = 429519.5539120331 // BNT | ||
const source_amount = 10 | ||
bancorx.bancorFormula(source_balance, target_balance, source_amount) | ||
//=> 55.19109809221157 | ||
Formula: | ||
1.0000 / (77814.0638 + 1.0000) * 429519.5539120331 | ||
//=> 5.519748143058556 | ||
``` | ||
## Parse Memo | ||
#### Parameters | ||
```js | ||
const memo = bancorx.parseMemo("bnt2eoscnvrt BNT bancorc11144 CUSD", "3.17", "b1") | ||
//=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,b1" | ||
- `balance_from` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** from token balance in the relay | ||
- `balance_to` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** to token balance in the relay | ||
- `amount` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** amount to convert | ||
#### Examples | ||
```javascript | ||
const balance_from = 77814.0638 // EOS | ||
const balance_to = 429519.5539120331 // BNT | ||
const amount = 1 | ||
bancorx.bancorFormula(balance_from, balance_to, amount) | ||
//=> 5.519748143058556 | ||
``` | ||
## Relays | ||
Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** computed amount | ||
| **symbol** | **code** | **account** | **symbol** | | ||
|------------|--------------|--------------|------------| | ||
| EOS | eosio.token | bnt2eoscnvrt | 4 | | ||
| BNT | bntbntbntbnt | bnt2eoscnvrt | 10 | | ||
| ZOS | zosdiscounts | bancorc11151 | 4 | | ||
| IQ | everipediaiq | bancorc11123 | 3 | | ||
| PGL | prospectorsg | bancorc11113 | 4 | | ||
| CUSD | stablecarbon | bancorc11144 | 2 | | ||
| DICE | betdicetoken | bancorc11125 | 2 | | ||
| BLACK | eosblackteam | bancorc11111 | 4 | | ||
| CET | eosiochaince | bancorc11114 | 4 | | ||
| EPRA | epraofficial | bancorc11124 | 4 | | ||
| MEETONE | eosiomeetone | bancorc11122 | 4 | | ||
| ZKS | zkstokensr4u | bancorc11142 | 0 | | ||
| OCT | octtothemoon | bancorc11132 | 4 | | ||
| KARMA | therealkarma | bancorc11112 | 4 | | ||
| HVT | hirevibeshvt | bancorc11131 | 4 | | ||
| HORUS | horustokenio | bancorc11121 | 4 | | ||
| MEV | eosvegascoin | bancorc11134 | 4 | | ||
### bancorInverseFormula | ||
Bancor Inverse Formula | ||
- token balance of EOS (eosio.token) in the relay: 77814.0638 EOS | ||
- token balance of BNT (bntbntbntbnt) in the relay: 429519.5539120331 BNT | ||
```js | ||
bancorx.relays.CUSD | ||
// { | ||
// code: "stablecarbon", | ||
// account: "bancorc11144", | ||
// symbol: "CUSD", | ||
// precision: 2 | ||
// } | ||
Inverse Formula: | ||
77814.0638 / (1.0 - 1 / 429519.5539120331) - 77814.0638 | ||
//=> 0.18116577989712823 | ||
``` | ||
## Get Relay Balances | ||
#### Parameters | ||
```js | ||
const {code, account, symbol} = bancorx.relays.CUSD; | ||
const balance = await rpc.get_currency_balance(code, account, symbol); | ||
``` | ||
- `balance_from` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** from token balance in the relay | ||
- `balance_to` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** to token balance in the relay | ||
- `amount_desired` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** amount to desired | ||
#### Examples | ||
```javascript | ||
const balance_from = 77814.0638 // EOS | ||
const balance_to = 429519.5539120331 // BNT | ||
const amount_desired = 1 | ||
bancorx.bancorInverseFormula(balance_from, balance_to, amount_desired) | ||
//=> 0.18116577989712823 | ||
``` | ||
Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** computed desired amount | ||
### parseMemo | ||
Parse Memo | ||
#### Parameters | ||
- `converters` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<Converter>** relay converters | ||
- `min_return` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** minimum return | ||
- `dest_account` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** destination acccount | ||
- `version` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** bancor protocol version | ||
#### Examples | ||
```javascript | ||
const CUSD = bancorx.relays.CUSD; | ||
const BNT = bancorx.relays.BNT; | ||
// Single converter (BNT => CUSD) | ||
bancorx.parseMemo([CUSD], "3.17", "<account>") | ||
//=> "1,bancorc11144 CUSD,3.17,<account>" | ||
// Multi converter (EOS => BNT => CUSD) | ||
bancorx.parseMemo([BNT, CUSD], "3.17", "<account>") | ||
//=> "1,bnt2eoscnvrt BNT bancorc11144 CUSD,3.17,<account>" | ||
``` | ||
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** computed memo | ||
### parseBalance | ||
Parse Balance | ||
#### Parameters | ||
- `balance` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number))** token balance | ||
#### Examples | ||
```javascript | ||
bancorx.parseBalance("10.0000 EOS") //=> {quantity: 10.0, symbol: "EOS"} | ||
bancorx.parseBalance(10.0) //=> {quantity: 10.0} | ||
``` | ||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** parsed balance | ||
### relays | ||
Relays | ||
#### Examples | ||
```javascript | ||
bancorx.relays.BNT | ||
//=> { code: "bntbntbntbnt", account: "bnt2eoscnvrt", symbol: "BNT", precision: 10 } | ||
bancorx.relays.CUSD | ||
//=> { code: "stablecarbon", account: "bancorc11144", symbol: "CUSD", precision: 2 } | ||
``` |
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
20984
358
209
7