@proton/wrap-constants 0.0.56 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { Coin, CoinWallet } from './coins';
2
2
  export declare const WRAP_CONTRACT = "proton.wrap";
3
3
  export declare const WRAP_AUTH_ACTION = "generateauth";
4
+ export declare const FEE_FACTOR = 10000;
4
5
  export declare const initializeConstants: (chain: string) => {
5
6
  SWAP_CONTRACT: string;
6
7
  FEE_FACTOR: number;
@@ -0,0 +1,30 @@
1
+ export declare class INode<T> {
2
+ key: string;
3
+ data: T;
4
+ constructor(key: string, data?: T);
5
+ }
6
+ export declare class ILink<T> {
7
+ from: INode<T>;
8
+ to: INode<T>;
9
+ data: T;
10
+ constructor(from: INode<T>, to: INode<T>, data?: T);
11
+ }
12
+ export declare class Graph<T> {
13
+ nodes: INode<T>[];
14
+ links: ILink<T>[];
15
+ lastPathFound: INode<T>[];
16
+ isEquals: (a: INode<T>, b: INode<T>) => boolean;
17
+ addNode(k: string, v?: T): void;
18
+ addNodes(...nodes: (string | [string, T?])[]): void;
19
+ findNode(k: string): INode<T> | undefined;
20
+ findNodes(...keys: string[]): (INode<T> | undefined)[];
21
+ requireFindNodes(...keys: string[]): INode<T>[];
22
+ addLink(nodeA: string, nodeB: string, data?: T): void;
23
+ addLinks(...links: [nodeA: string, nodeB: string, data?: T][]): void;
24
+ /**
25
+ * Get all nodes linked
26
+ * to from `node`.
27
+ */
28
+ findLinkedNodes(node: INode<T>): INode<T>[];
29
+ findAllPaths(from: string, to: string): INode<T>[][];
30
+ }
package/dist/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export * from './coins';
2
2
  export * from './parsers';
3
3
  export * from './constants';
4
4
  export * from './utils';
5
+ export * from './graph';
6
+ export * from './math';
package/dist/math.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { BigNumber as BN } from 'bignumber.js';
2
+ /**
3
+ * XPR<>XUSDT (XPR -> XUSDT)
4
+ *
5
+ * pool1 * pool2
6
+ * term = pool2 - --------------
7
+ * pool1 + swap
8
+ *
9
+ * result = term - (term * fee)
10
+ */
11
+ export declare const compute_transfer: (pool1: BN.Instance, pool2: BN.Instance, swap: BN.Instance, exchange_fee: number, precision: number) => BN;
12
+ export declare const compute_transfer_curve: (reserve_in: number, reserve_out: number, amount_in: number, exchange_fee: number, precision: number, amplifier: number) => BN;
13
+ export declare const compute_liquidity: (amount_lt: BN.Instance, pool: BN.Instance, lt_supply: BN.Instance, precision: number) => number;
14
+ export declare const isInputInvalid: (data: string, value: string | number, pool: {
15
+ xtokenPrecision: number;
16
+ }) => boolean;
17
+ export declare const applyDiscountToFee: (fee: number, staked: number) => number;
@@ -1,5 +1,6 @@
1
1
  import { Coin } from "../coins";
2
2
  import { ExtendedAsset } from '@bloks/numbers';
3
+ import { INode } from "../graph";
3
4
  export interface RawPool {
4
5
  lt_symbol: string;
5
6
  creator: string;
@@ -25,8 +26,8 @@ export interface Pool extends Omit<RawPool, 'pool1' | 'pool2'> {
25
26
  lt_symbol: string;
26
27
  creator: string;
27
28
  memo: string;
28
- pool1: ExtendedToken;
29
- pool2: ExtendedToken;
29
+ pool1: ExtendedTokenWithRate;
30
+ pool2: ExtendedTokenWithRate;
30
31
  hash: string;
31
32
  fee: {
32
33
  exchange_fee: number;
@@ -42,14 +43,8 @@ export declare type ExtendedTokenWithRate = ExtendedToken & {
42
43
  rate: number;
43
44
  };
44
45
  export declare type PoolTable = {
45
- pool1: ExtendedToken & {
46
- rate: number;
47
- };
48
- pool2: ExtendedToken & {
49
- rate: number;
50
- };
51
- path: {
52
- data: Pool;
53
- }[];
46
+ pool1: ExtendedTokenWithRate;
47
+ pool2: ExtendedTokenWithRate;
48
+ path: INode<Pool>[];
54
49
  getPathString: () => string;
55
50
  } | undefined;
package/dist/utils.d.ts CHANGED
@@ -4,4 +4,5 @@ interface PartialCoin {
4
4
  xtokenContract: string;
5
5
  }
6
6
  export declare const isSameToken: (token1: PartialCoin, token2: PartialCoin) => boolean;
7
+ export declare const median: (numbers: number[]) => number;
7
8
  export {};
@@ -2,7 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
+
5
7
  var z = require('zod');
8
+ var cloneDeep = _interopDefault(require('lodash.clonedeep'));
9
+ var bignumber_js = require('bignumber.js');
6
10
 
7
11
  var _similarChains, _explorerLinkByChain;
8
12
 
@@ -269,7 +273,7 @@ var initializeCoins = function initializeCoins(protonChain) {
269
273
  xtokenPrecision: 4,
270
274
  xtokenContract: 'loan.token',
271
275
  wallets: [],
272
- image: 'https://www.protonchain.com/images/tokens/loan.tokenLOAN.png',
276
+ image: 'https://raw.githubusercontent.com/eoscafe/eos-airdrops/master/logos/loan.png',
273
277
  coingeckoId: 'proton-loan'
274
278
  }, {
275
279
  allowSwap: true,
@@ -942,8 +946,47 @@ function _extends() {
942
946
  return _extends.apply(this, arguments);
943
947
  }
944
948
 
949
+ function _unsupportedIterableToArray(o, minLen) {
950
+ if (!o) return;
951
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
952
+ var n = Object.prototype.toString.call(o).slice(8, -1);
953
+ if (n === "Object" && o.constructor) n = o.constructor.name;
954
+ if (n === "Map" || n === "Set") return Array.from(o);
955
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
956
+ }
957
+
958
+ function _arrayLikeToArray(arr, len) {
959
+ if (len == null || len > arr.length) len = arr.length;
960
+
961
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
962
+
963
+ return arr2;
964
+ }
965
+
966
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
967
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
968
+ if (it) return (it = it.call(o)).next.bind(it);
969
+
970
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
971
+ if (it) o = it;
972
+ var i = 0;
973
+ return function () {
974
+ if (i >= o.length) return {
975
+ done: true
976
+ };
977
+ return {
978
+ done: false,
979
+ value: o[i++]
980
+ };
981
+ };
982
+ }
983
+
984
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
985
+ }
986
+
945
987
  var WRAP_CONTRACT = 'proton.wrap';
946
988
  var WRAP_AUTH_ACTION = 'generateauth';
989
+ var FEE_FACTOR = 10000;
947
990
  var initializeConstants = function initializeConstants(chain) {
948
991
  var _initializeCoins = initializeCoins(chain),
949
992
  swapCoins = _initializeCoins.swapCoins,
@@ -956,7 +999,7 @@ var initializeConstants = function initializeConstants(chain) {
956
999
 
957
1000
  var constants = {
958
1001
  SWAP_CONTRACT: 'proton.swaps',
959
- FEE_FACTOR: 10000,
1002
+ FEE_FACTOR: FEE_FACTOR,
960
1003
  GOOGLE_MAPS_KEY: 'AIzaSyDb2_CrK032kSIqBXbkdwmaLKWELl0SxVs',
961
1004
  SWAP_URL: chain === 'proton' ? 'https://otc.protonswap.com' : 'https://otc-test.protonswap.com',
962
1005
  WRAP_SERVER_URL: chain === 'proton' ? 'https://www.api.bloks.io/proton-wrap-public2' : 'https://www.api.bloks.io/proton-wrap-testnet-public2',
@@ -1219,17 +1262,317 @@ var wrapParser = /*#__PURE__*/z.object({
1219
1262
  var isSameToken = function isSameToken(token1, token2) {
1220
1263
  return token1.xtokenSymbol === token2.xtokenSymbol && token1.xtokenPrecision === token2.xtokenPrecision && token1.xtokenContract === token2.xtokenContract;
1221
1264
  };
1265
+ var median = function median(numbers) {
1266
+ var sorted = numbers.slice().sort(function (a, b) {
1267
+ return a - b;
1268
+ });
1269
+ var middle = Math.floor(sorted.length / 2);
1270
+
1271
+ if (sorted.length % 2 === 0) {
1272
+ return (sorted[middle - 1] + sorted[middle]) / 2;
1273
+ }
1274
+
1275
+ return sorted[middle];
1276
+ };
1277
+
1278
+ var INode = function INode(key, data) {
1279
+ this.key = void 0;
1280
+ this.data = {};
1281
+ this.key = key;
1282
+
1283
+ if (data) {
1284
+ this.data = data;
1285
+ }
1286
+ };
1287
+ var ILink = function ILink(from, to, data) {
1288
+ this.from = void 0;
1289
+ this.to = void 0;
1290
+ this.data = {};
1291
+ this.from = from;
1292
+ this.to = to;
1293
+
1294
+ if (data) {
1295
+ this.data = data;
1296
+ }
1297
+ };
1298
+ var Graph = /*#__PURE__*/function () {
1299
+ function Graph() {
1300
+ this.nodes = [];
1301
+ this.links = [];
1302
+ this.lastPathFound = [];
1303
+
1304
+ this.isEquals = function (a, b) {
1305
+ return a.key === b.key;
1306
+ };
1307
+ }
1308
+
1309
+ var _proto = Graph.prototype;
1310
+
1311
+ _proto.addNode = function addNode(k, v) {
1312
+ var node = this.findNode(k);
1313
+
1314
+ if (!node) {
1315
+ this.nodes.push(new INode(k, v));
1316
+ }
1317
+ };
1318
+
1319
+ _proto.addNodes = function addNodes() {
1320
+ var _this = this;
1321
+
1322
+ for (var _len = arguments.length, nodes = new Array(_len), _key = 0; _key < _len; _key++) {
1323
+ nodes[_key] = arguments[_key];
1324
+ }
1325
+
1326
+ nodes.forEach(function (node) {
1327
+ return typeof node === "string" ? _this.addNode(node) : _this.addNode(node[0], node[1]);
1328
+ });
1329
+ };
1330
+
1331
+ _proto.findNode = function findNode(k) {
1332
+ var _this2 = this;
1333
+
1334
+ var inode = new INode(k);
1335
+ return this.nodes.find(function (node) {
1336
+ return _this2.isEquals(node, inode);
1337
+ });
1338
+ };
1339
+
1340
+ _proto.findNodes = function findNodes() {
1341
+ var _this3 = this;
1342
+
1343
+ for (var _len2 = arguments.length, keys = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1344
+ keys[_key2] = arguments[_key2];
1345
+ }
1346
+
1347
+ return keys.map(function (k) {
1348
+ return _this3.findNode(k);
1349
+ });
1350
+ };
1351
+
1352
+ _proto.requireFindNodes = function requireFindNodes() {
1353
+ var nodes = this.findNodes.apply(this, arguments);
1354
+
1355
+ if (nodes.some(function (n) {
1356
+ return n === undefined;
1357
+ })) {
1358
+ throw new Error('One of the nodes does not exist');
1359
+ }
1360
+
1361
+ return nodes;
1362
+ };
1363
+
1364
+ _proto.addLink = function addLink(nodeA, nodeB, data) {
1365
+ var _this$requireFindNode = this.requireFindNodes(nodeA, nodeB),
1366
+ inodeA = _this$requireFindNode[0],
1367
+ inodeB = _this$requireFindNode[1];
1368
+
1369
+ this.links.push(new ILink(inodeA, inodeB, data));
1370
+ };
1371
+
1372
+ _proto.addLinks = function addLinks() {
1373
+ var _this4 = this;
1374
+
1375
+ for (var _len3 = arguments.length, links = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
1376
+ links[_key3] = arguments[_key3];
1377
+ }
1378
+
1379
+ links.forEach(function (link) {
1380
+ return _this4.addLink.apply(_this4, link);
1381
+ });
1382
+ }
1383
+ /**
1384
+ * Get all nodes linked
1385
+ * to from `node`.
1386
+ */
1387
+ ;
1388
+
1389
+ _proto.findLinkedNodes = function findLinkedNodes(node) {
1390
+ var _this5 = this;
1391
+
1392
+ return this.links.reduce(function (acc, link) {
1393
+ if (_this5.isEquals(link.from, node)) {
1394
+ acc.push(link.to);
1395
+ }
1396
+
1397
+ return acc;
1398
+ }, []);
1399
+ };
1400
+
1401
+ _proto.findAllPaths = function findAllPaths(from, to) {
1402
+ var _this6 = this;
1403
+
1404
+ var allPaths = [];
1405
+
1406
+ var _this$requireFindNode2 = this.requireFindNodes(from, to),
1407
+ fromNode = _this$requireFindNode2[0],
1408
+ toNode = _this$requireFindNode2[1];
1409
+
1410
+ var path = [fromNode];
1411
+ var seen = new Set();
1412
+
1413
+ var stuck = function stuck(x) {
1414
+ if (_this6.isEquals(x, toNode)) {
1415
+ return false;
1416
+ }
1417
+
1418
+ for (var _iterator = _createForOfIteratorHelperLoose(_this6.findLinkedNodes(x)), _step; !(_step = _iterator()).done;) {
1419
+ var y = _step.value;
1420
+
1421
+ if (!seen.has(y.key)) {
1422
+ seen.add(y.key);
1423
+
1424
+ if (!stuck(y)) {
1425
+ return false;
1426
+ }
1427
+ }
1428
+ }
1429
+
1430
+ return true;
1431
+ };
1432
+
1433
+ var search = function search(x) {
1434
+ if (_this6.isEquals(x, toNode)) {
1435
+ allPaths.push(cloneDeep(path));
1436
+ return;
1437
+ }
1438
+
1439
+ seen = new Set(path.map(function (p) {
1440
+ return p.key;
1441
+ }));
1442
+
1443
+ if (stuck(x)) {
1444
+ return;
1445
+ }
1446
+
1447
+ for (var _iterator2 = _createForOfIteratorHelperLoose(_this6.findLinkedNodes(x)), _step2; !(_step2 = _iterator2()).done;) {
1448
+ var y = _step2.value;
1449
+
1450
+ if (!path.includes(y)) {
1451
+ path.push(y);
1452
+ search(y);
1453
+ path.pop();
1454
+ }
1455
+ }
1456
+ };
1457
+
1458
+ search(fromNode);
1459
+ return allPaths;
1460
+ };
1461
+
1462
+ return Graph;
1463
+ }();
1464
+
1465
+ /**
1466
+ * XPR<>XUSDT (XPR -> XUSDT)
1467
+ *
1468
+ * pool1 * pool2
1469
+ * term = pool2 - --------------
1470
+ * pool1 + swap
1471
+ *
1472
+ * result = term - (term * fee)
1473
+ */
1474
+
1475
+ var compute_transfer = function compute_transfer(pool1, pool2, swap, exchange_fee, precision) {
1476
+ var term1 = pool2;
1477
+ var term2over = pool1.multipliedBy(pool2);
1478
+ var term2under = pool1.plus(swap);
1479
+ var term2 = term2over.dividedBy(term2under).integerValue(bignumber_js.BigNumber.ROUND_DOWN);
1480
+ var term = term1.minus(term2);
1481
+ var fee = new bignumber_js.BigNumber(exchange_fee).dividedBy(FEE_FACTOR);
1482
+ var result = term.minus(term.multipliedBy(fee));
1483
+ return result.integerValue(bignumber_js.BigNumber.ROUND_DOWN).shiftedBy(-precision);
1484
+ };
1485
+ var compute_transfer_curve = function compute_transfer_curve(reserve_in, reserve_out, amount_in, exchange_fee, precision, amplifier) {
1486
+ if (amplifier === 0) {
1487
+ return compute_transfer(new bignumber_js.BigNumber(reserve_in), new bignumber_js.BigNumber(reserve_out), new bignumber_js.BigNumber(amount_in), exchange_fee, precision);
1488
+ }
1489
+
1490
+ var MAX_ITERATIONS = 10;
1491
+ var sum = reserve_in + reserve_out;
1492
+ var D = sum;
1493
+ var D_prev = 0;
1494
+
1495
+ var F = function F(v) {
1496
+ return (v >= 0 ? 1 : -1) * Math.floor(Math.abs(v));
1497
+ };
1498
+
1499
+ var i = MAX_ITERATIONS;
1500
+
1501
+ while (D != D_prev && i--) {
1502
+ var prod1 = F(F(D * D / (reserve_in * 2)) * D / (reserve_out * 2));
1503
+ D_prev = D;
1504
+ D = F(2 * D * (amplifier * sum + prod1) / ((2 * amplifier - 1) * D + 3 * prod1));
1505
+ }
1506
+
1507
+ var b = reserve_in + amount_in + F(D / (amplifier * 2)) - D;
1508
+ var c = F(F(D * D / ((reserve_in + amount_in) * 2)) * D / (amplifier * 4));
1509
+ var x = D,
1510
+ x_prev = 0;
1511
+ i = MAX_ITERATIONS;
1512
+
1513
+ while (x != x_prev && i--) {
1514
+ x_prev = F(x);
1515
+ x = +new bignumber_js.BigNumber(x * x + c).dividedBy(2 * x + b).toFixed(0, bignumber_js.BigNumber.ROUND_DOWN);
1516
+ }
1517
+
1518
+ if (reserve_out <= x) {
1519
+ throw new Error("curve: insufficient reserve out");
1520
+ } // Amount out + Fee + 1
1521
+
1522
+
1523
+ var amount_out_with_fee = new bignumber_js.BigNumber(reserve_out - x); // Fee + 1
1524
+ // +1 covers small precision issues
1525
+
1526
+ var fee = amount_out_with_fee.multipliedBy(exchange_fee).dividedBy(FEE_FACTOR).plus(1).toFixed(0, bignumber_js.BigNumber.ROUND_DOWN); // Amount out
1527
+
1528
+ var amount_out = amount_out_with_fee.minus(fee);
1529
+ return amount_out.integerValue(bignumber_js.BigNumber.ROUND_DOWN).shiftedBy(-precision);
1530
+ };
1531
+ var compute_liquidity = function compute_liquidity(amount_lt, pool, lt_supply, precision) {
1532
+ var over = amount_lt.multipliedBy(pool);
1533
+ var under = lt_supply;
1534
+ var result = over.dividedBy(under).integerValue(bignumber_js.BigNumber.ROUND_DOWN);
1535
+ return +result.shiftedBy(-precision).toFixed(precision, bignumber_js.BigNumber.ROUND_DOWN);
1536
+ };
1537
+ var isInputInvalid = function isInputInvalid(data, value, pool) {
1538
+ value = String(value);
1539
+ var isBackspace = data === null;
1540
+ var isNumber = !/[0-9]|\./.test(data);
1541
+ var isDoubleDot = data === '.' && value.indexOf('.') !== value.length - 1;
1542
+ var isPrecisionTooHigh = value && value.indexOf('.') !== -1 && value.split('.')[1].length > pool.xtokenPrecision;
1543
+ var isInvalid = !isBackspace && Boolean(isNumber || isDoubleDot || isPrecisionTooHigh);
1544
+ return isInvalid;
1545
+ };
1546
+ var applyDiscountToFee = function applyDiscountToFee(fee, staked) {
1547
+ if (staked >= 10000000) {
1548
+ return fee * 0;
1549
+ } else if (staked >= 1000000) {
1550
+ return fee * 0.33;
1551
+ } else if (staked >= 100000) {
1552
+ return fee * 0.66;
1553
+ } else {
1554
+ return fee;
1555
+ }
1556
+ };
1222
1557
 
1223
1558
  exports.ExtendedAssetParser = ExtendedAssetParser;
1559
+ exports.FEE_FACTOR = FEE_FACTOR;
1560
+ exports.Graph = Graph;
1561
+ exports.ILink = ILink;
1562
+ exports.INode = INode;
1224
1563
  exports.WRAP_AUTH_ACTION = WRAP_AUTH_ACTION;
1225
1564
  exports.WRAP_CONTRACT = WRAP_CONTRACT;
1226
1565
  exports.accountParser = accountParser;
1227
1566
  exports.addressParser = addressParser;
1567
+ exports.applyDiscountToFee = applyDiscountToFee;
1228
1568
  exports.authorizationParser = authorizationParser;
1229
1569
  exports.authorizationsParser = authorizationsParser;
1230
1570
  exports.baseActionParser = baseActionParser;
1231
1571
  exports.baseActionSerializedParser = baseActionSerializedParser;
1232
1572
  exports.bloksRatesParser = bloksRatesParser;
1573
+ exports.compute_liquidity = compute_liquidity;
1574
+ exports.compute_transfer = compute_transfer;
1575
+ exports.compute_transfer_curve = compute_transfer_curve;
1233
1576
  exports.createAbstractAuthParser = createAbstractAuthParser;
1234
1577
  exports.depositBodyParser = depositBodyParser;
1235
1578
  exports.exchangeRateParser = exchangeRateParser;
@@ -1240,7 +1583,9 @@ exports.generateAddressResponse = generateAddressResponse;
1240
1583
  exports.generateAuthParser = generateAuthParser;
1241
1584
  exports.initializeCoins = initializeCoins;
1242
1585
  exports.initializeConstants = initializeConstants;
1586
+ exports.isInputInvalid = isInputInvalid;
1243
1587
  exports.isSameToken = isSameToken;
1588
+ exports.median = median;
1244
1589
  exports.otcOrderResponseParser = otcOrderResponseParser;
1245
1590
  exports.otcQuoteBodyParser = otcQuoteBodyParser;
1246
1591
  exports.otcQuoteResponseParser = otcQuoteResponseParser;