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