@defisaver/tokens 0.0.95-pt-april-dev

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.
package/cjs/index.js ADDED
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assetAmountInWei = exports.assetAmountInEth = exports.getAaveV2MarketInfo = exports.tokenFromReflexerJoin = exports.tokenFromMakerJoin = exports.tokenFromJoin = exports.aaveAsset = exports.compoundAsset = exports.ilkToAsset = exports.getAssetInfoByAddress = exports.getIlkInfo = exports.getAssetInfo = exports.MAXUINT = exports.set = exports.compare = exports.bytesToString = exports.stringToBytes = exports.aaveV2Markets = exports.reflexerCollTypes = exports.ilks = exports.assets = void 0;
4
+ const decimal_js_1 = require("decimal.js");
5
+ const assets_js_1 = require("./assets.js");
6
+ Object.defineProperty(exports, "assets", { enumerable: true, get: function () { return assets_js_1.assets; } });
7
+ const ilks_js_1 = require("./ilks.js");
8
+ Object.defineProperty(exports, "ilks", { enumerable: true, get: function () { return ilks_js_1.ilks; } });
9
+ const reflexerCollTypes_js_1 = require("./reflexerCollTypes.js");
10
+ Object.defineProperty(exports, "reflexerCollTypes", { enumerable: true, get: function () { return reflexerCollTypes_js_1.reflexerCollTypes; } });
11
+ const aaveV2Markets_js_1 = require("./aaveV2Markets.js");
12
+ Object.defineProperty(exports, "aaveV2Markets", { enumerable: true, get: function () { return aaveV2Markets_js_1.aaveV2Markets; } });
13
+ const utils_js_1 = require("./utils.js");
14
+ Object.defineProperty(exports, "stringToBytes", { enumerable: true, get: function () { return utils_js_1.stringToBytes; } });
15
+ Object.defineProperty(exports, "bytesToString", { enumerable: true, get: function () { return utils_js_1.bytesToString; } });
16
+ Object.defineProperty(exports, "compare", { enumerable: true, get: function () { return utils_js_1.compare; } });
17
+ const config = {
18
+ iconFunc: undefined,
19
+ network: 1,
20
+ };
21
+ const set = (key, value) => {
22
+ config[key] = value;
23
+ };
24
+ exports.set = set;
25
+ exports.MAXUINT = '115792089237316195423570985008687907853269984665640564039457584007913129639935';
26
+ decimal_js_1.Decimal.set({
27
+ rounding: decimal_js_1.Decimal.ROUND_DOWN,
28
+ toExpPos: 9e15,
29
+ toExpNeg: -9e15,
30
+ precision: 50,
31
+ });
32
+ /**
33
+ *
34
+ * @param symbol {string}
35
+ * @return symbol {string}
36
+ */
37
+ const handleWBTCLegacy = (symbol = '') => (symbol === 'WBTC Legacy' ? 'WBTC' : symbol);
38
+ const _addChainSpecificData = (assetDataBase, chainId) => {
39
+ const assetData = Object.assign(Object.assign({}, assetDataBase), { address: assetDataBase.addresses[chainId || config.network] || '' });
40
+ if (config.iconFunc)
41
+ assetData.icon = config.iconFunc(Object.assign({}, assetData));
42
+ return assetData;
43
+ };
44
+ /**
45
+ * Returns asset info.
46
+ * Warning: will not throw if asset not found. Instead, will return a placeholder object.
47
+ *
48
+ * @param symbol {string}
49
+ * @param chainId {number}
50
+ * @return {AssetData}
51
+ */
52
+ const getAssetInfo = (symbol = '', chainId) => {
53
+ let assetData = assets_js_1.assets.find(t => (0, utils_js_1.compare)(t.symbol, handleWBTCLegacy(symbol)));
54
+ if (!assetData)
55
+ assetData = Object.assign({}, assets_js_1.assetProto);
56
+ return _addChainSpecificData(assetData, chainId);
57
+ };
58
+ exports.getAssetInfo = getAssetInfo;
59
+ /**
60
+ * Returns Maker or Reflexer ilk info, and asset info as `assetData` attribute.
61
+ * Warning: will not throw if ilk not found. Instead, will return a placeholder object.
62
+ *
63
+ * @param ilk {string} Ilk encoded as string or as hex
64
+ * @returns {ExtendedIlkData}
65
+ */
66
+ const getIlkInfo = (ilk = '') => {
67
+ const _ilk = (ilk.substr(0, 2) === '0x' ? (0, utils_js_1.bytesToString)(ilk) : ilk).toUpperCase();
68
+ const ilkData = ilks_js_1.ilks.find(i => i.ilkLabel === _ilk) || reflexerCollTypes_js_1.reflexerCollTypes.find(i => i.ilkLabel === _ilk);
69
+ if (!ilkData) {
70
+ console.error(`Ilk "${ilk}" not found`);
71
+ return {
72
+ ilkLabel: _ilk,
73
+ ilkBytes: (0, utils_js_1.stringToBytes)(_ilk),
74
+ asset: (0, exports.ilkToAsset)(ilk),
75
+ pip: '',
76
+ join: '',
77
+ isLP: false,
78
+ isCrop: false,
79
+ };
80
+ }
81
+ const assetData = (0, exports.getAssetInfo)(ilkData.asset);
82
+ return Object.assign(Object.assign({}, ilkData), { assetData });
83
+ };
84
+ exports.getIlkInfo = getIlkInfo;
85
+ const getAssetInfoByAddress = (address = '', chainId) => {
86
+ const assetDataBase = assets_js_1.assets.find(t => { var _a; return ((_a = t.addresses[chainId || config.network]) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === address.toLowerCase(); });
87
+ return _addChainSpecificData(assetDataBase || Object.assign({}, assets_js_1.assetProto), chainId);
88
+ };
89
+ exports.getAssetInfoByAddress = getAssetInfoByAddress;
90
+ const ilkToAsset = (ilk = '') => {
91
+ var _a;
92
+ let ilkLabel = ilk.substr(0, 2) === '0x' ? (0, utils_js_1.bytesToString)(ilk) : ilk;
93
+ let asset = ilkLabel.replace(/-.*/, '');
94
+ if (ilkLabel.startsWith('PSM'))
95
+ asset = ((_a = ilkLabel.match(/^[A-Z]*-(.*)-[A-Z]*/)) === null || _a === void 0 ? void 0 : _a[1]) || asset;
96
+ if (asset === 'KNC')
97
+ return 'KNCL';
98
+ if (asset === 'PAXUSD' || asset === 'PAX')
99
+ return 'USDP';
100
+ if (asset === 'WSTETH')
101
+ return 'wstETH';
102
+ if (asset === 'CRVV1ETHSTETH')
103
+ return 'steCRV';
104
+ if (asset === 'RETH')
105
+ return 'rETH';
106
+ return asset;
107
+ };
108
+ exports.ilkToAsset = ilkToAsset;
109
+ /** @private **/
110
+ const compoundAsset = (underlyingAsset) => `c${underlyingAsset.toUpperCase()}`;
111
+ exports.compoundAsset = compoundAsset;
112
+ /** @private **/
113
+ const aaveAsset = (underlyingAsset) => `a${underlyingAsset.toUpperCase()}`;
114
+ exports.aaveAsset = aaveAsset;
115
+ /**
116
+ * @param join {string} Maker or Reflexer ilk join
117
+ * @param fromIlks {IlkData[]}
118
+ * @returns {string} Token symbol
119
+ */
120
+ const tokenFromJoin = (join, fromIlks = ilks_js_1.ilks) => {
121
+ for (const ilkInfo of fromIlks) {
122
+ if ((0, utils_js_1.compare)(ilkInfo.join, join))
123
+ return ilkInfo.asset;
124
+ }
125
+ if ((0, utils_js_1.compare)('0x448a5065aebb8e423f0896e6c5d525c040f59af3', join))
126
+ return 'ETH'; // SCD ETH
127
+ return '';
128
+ };
129
+ exports.tokenFromJoin = tokenFromJoin;
130
+ /**
131
+ * @param join {string} Maker ilk join
132
+ * @returns {string} Token symbol
133
+ */
134
+ const tokenFromMakerJoin = (join) => (0, exports.tokenFromJoin)(join, ilks_js_1.ilks);
135
+ exports.tokenFromMakerJoin = tokenFromMakerJoin;
136
+ /**
137
+ * @param join {string} Reflexer ilk join
138
+ * @returns {string} Token symbol
139
+ */
140
+ const tokenFromReflexerJoin = (join) => (0, exports.tokenFromJoin)(join, reflexerCollTypes_js_1.reflexerCollTypes);
141
+ exports.tokenFromReflexerJoin = tokenFromReflexerJoin;
142
+ const getAaveV2MarketInfo = (name = '') => aaveV2Markets_js_1.aaveV2Markets.find(i => i.name === name);
143
+ exports.getAaveV2MarketInfo = getAaveV2MarketInfo;
144
+ /**
145
+ * @param amount {Number|String|Object} Amount in wei
146
+ * @param asset {String} Asset symbol (or `MCD-${symbol}` for maker asset - always 18dec)
147
+ * @return {String}
148
+ */
149
+ const assetAmountInEth = (amount, asset = 'ETH') => {
150
+ if ((amount === null || amount === void 0 ? void 0 : amount.toString()) === 'Infinity')
151
+ return exports.MAXUINT;
152
+ let decimals;
153
+ // USDC/WBTC have 6/8 decimals but Vat is storing it in 18 decimal precision
154
+ if (asset.substr(0, 4) === 'MCD-')
155
+ decimals = 18;
156
+ // Compound returns borrowing power in USD with 18 decimals
157
+ else if (asset === 'USD')
158
+ decimals = 18;
159
+ else
160
+ decimals = (0, exports.getAssetInfo)(asset).decimals;
161
+ return new decimal_js_1.Decimal(amount && amount.toString() || 0).div(Math.pow(10, decimals)).toDP(decimals).toString();
162
+ };
163
+ exports.assetAmountInEth = assetAmountInEth;
164
+ /**
165
+ * @param amount {Number|String|Object} Amount in eth
166
+ * @param asset {String} Asset symbol
167
+ * @return {String}
168
+ */
169
+ const assetAmountInWei = (amount, asset) => {
170
+ if ((amount === null || amount === void 0 ? void 0 : amount.toString()) === 'Infinity')
171
+ return exports.MAXUINT;
172
+ const { decimals } = (0, exports.getAssetInfo)(asset);
173
+ return new decimal_js_1.Decimal(amount && amount.toString() || 0).mul(Math.pow(10, decimals)).floor().toString();
174
+ };
175
+ exports.assetAmountInWei = assetAmountInWei;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,2 @@
1
+ import { IlkData } from './types.js';
2
+ export declare const reflexerCollTypes: IlkData[];
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reflexerCollTypes = void 0;
4
+ exports.reflexerCollTypes = [
5
+ {
6
+ "asset": "ETH",
7
+ "ilkLabel": "ETH-A",
8
+ "ilkBytes": "0x4554482d41000000000000000000000000000000000000000000000000000000",
9
+ "join": '0x2D3cD7b81c93f188F3CB8aD87c8Acc73d6226e3A',
10
+ "pip": '0xE6F5377DE93A361cd5531bdFbDf0f4b522E16B2B',
11
+ "isLP": false,
12
+ "isCrop": false
13
+ },
14
+ ];
package/cjs/types.d.ts ADDED
@@ -0,0 +1,79 @@
1
+ type Config = {
2
+ network: number;
3
+ iconFunc: ((props: object) => () => string) | undefined;
4
+ };
5
+ type AddressMapping = {
6
+ [key: number]: string;
7
+ };
8
+ type BoolMapping = {
9
+ [key: number]: boolean;
10
+ };
11
+ /**
12
+ * Chain-agnostic asset info type
13
+ */
14
+ type AssetDataBase = {
15
+ symbol: string;
16
+ name: string;
17
+ addresses: AddressMapping;
18
+ feedAvailability: BoolMapping;
19
+ decimals: number;
20
+ icon: Function;
21
+ underlyingAsset: string;
22
+ exchange: boolean;
23
+ compoundCollateral: boolean;
24
+ aaveCollateral: boolean;
25
+ yearnCollateral: boolean;
26
+ isStable: boolean;
27
+ isPendle: boolean;
28
+ nativeChainId: number;
29
+ expiryTimestamp: number;
30
+ is4626: boolean;
31
+ };
32
+ /**
33
+ * Chain-specific asset info type
34
+ */
35
+ type AssetData = {
36
+ symbol: string;
37
+ name: string;
38
+ address: string;
39
+ addresses: AddressMapping;
40
+ feedAvailability: BoolMapping;
41
+ decimals: number;
42
+ icon: Function;
43
+ underlyingAsset: string;
44
+ exchange: boolean;
45
+ compoundCollateral: boolean;
46
+ aaveCollateral: boolean;
47
+ yearnCollateral: boolean;
48
+ isStable: boolean;
49
+ isPendle: boolean;
50
+ nativeChainId: number;
51
+ expiryTimestamp: number;
52
+ is4626: boolean;
53
+ };
54
+ /**
55
+ * Maker ilk info type
56
+ */
57
+ type IlkData = {
58
+ ilkLabel: (string);
59
+ pip: (string);
60
+ join: (string);
61
+ asset: (string);
62
+ flip?: (string);
63
+ clip?: (string);
64
+ clipCalc?: (string);
65
+ ilkBytes: (string);
66
+ isLP: (boolean);
67
+ isCrop: (boolean);
68
+ assetAddress?: (string);
69
+ };
70
+ type AaveMarketData = {
71
+ name: (string);
72
+ lendingPool: (string);
73
+ lendingPoolAddressProvider: (string);
74
+ dataProvider: (string);
75
+ };
76
+ type ExtendedIlkData = IlkData | {
77
+ assetData: AssetData;
78
+ };
79
+ export { AddressMapping, AssetDataBase, AssetData, IlkData, ExtendedIlkData, AaveMarketData, Config, };
package/cjs/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/utils.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @param hex {string}
3
+ * @returns {string}
4
+ */
5
+ export declare function bytesToString(hex: string): string;
6
+ /**
7
+ * @param str {string}
8
+ * @return {string} input encoded to hex, padded to 32 bytes
9
+ */
10
+ export declare function stringToBytes(str: string): string;
11
+ export declare function compare(a?: string, b?: string): boolean;
package/cjs/utils.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bytesToString = bytesToString;
4
+ exports.stringToBytes = stringToBytes;
5
+ exports.compare = compare;
6
+ /**
7
+ * @param hex {string}
8
+ * @returns {string}
9
+ */
10
+ function bytesToString(hex) {
11
+ return Buffer.from(hex.replace(/^0x/, ''), 'hex')
12
+ .toString()
13
+ .replace(/\x00/g, '');
14
+ }
15
+ /**
16
+ * @param str {string}
17
+ * @return {string} input encoded to hex, padded to 32 bytes
18
+ */
19
+ function stringToBytes(str) {
20
+ let n = Buffer.from(str).toString('hex');
21
+ while (n.length < 64)
22
+ n = `${n}0`;
23
+ return `0x${n}`;
24
+ }
25
+ function compare(a = '', b = '') {
26
+ return a.toLowerCase() === b.toLowerCase();
27
+ }
@@ -0,0 +1,2 @@
1
+ import { AaveMarketData } from './types.js';
2
+ export declare const aaveV2Markets: AaveMarketData[];
@@ -0,0 +1,8 @@
1
+ export const aaveV2Markets = [
2
+ {
3
+ name: 'v2default',
4
+ lendingPool: '0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9',
5
+ lendingPoolAddressProvider: '0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5',
6
+ dataProvider: '0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d',
7
+ },
8
+ ];
@@ -0,0 +1,3 @@
1
+ import { AssetDataBase } from './types.js';
2
+ export declare const assetProto: AssetDataBase;
3
+ export declare const assets: AssetDataBase[];