@swapkit/helpers 1.0.0-rc.54 → 1.0.0-rc.56

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.
@@ -89,17 +89,21 @@ export class AssetValue extends BigIntArithmetics {
89
89
  }
90
90
 
91
91
  static fromStringSync(assetString: string, value: NumberPrimitives = 0) {
92
- const { isSynthetic } = getAssetInfo(assetString);
92
+ const { chain, isSynthetic } = getAssetInfo(assetString);
93
93
  const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
94
94
 
95
- if (!tokenInfo) {
96
- console.error(
97
- `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
98
- );
99
- return undefined;
100
- }
101
-
102
- const { tax, decimal, identifier } = tokenInfo;
95
+ if (isSynthetic) return createSyntheticAssetValue(assetString, value);
96
+ // TODO: write logger that will only run in dev mode with some flag
97
+ // if (!tokenInfo) {
98
+ // console.error(
99
+ // `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
100
+ // );
101
+ // }
102
+
103
+ const { tax, decimal, identifier } = tokenInfo || {
104
+ decimal: BaseDecimal[chain],
105
+ identifier: assetString,
106
+ };
103
107
  return new AssetValue({
104
108
  tax,
105
109
  value: safeValue(value, decimal),
@@ -109,16 +113,21 @@ export class AssetValue extends BigIntArithmetics {
109
113
  }
110
114
 
111
115
  static fromIdentifierSync(assetString: TokenNames, value: NumberPrimitives = 0) {
116
+ const { chain, isSynthetic } = getAssetInfo(assetString);
112
117
  const tokenInfo = staticTokensMap.get(assetString);
113
118
 
114
- if (!tokenInfo) {
115
- console.error(
116
- `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
117
- );
118
- return undefined;
119
- }
120
-
121
- const { tax, decimal, identifier } = tokenInfo;
119
+ if (isSynthetic) return createSyntheticAssetValue(assetString, value);
120
+ // TODO: write logger that will only run in dev mode with some flag
121
+ // if (!tokenInfo) {
122
+ // console.error(
123
+ // `Asset ${assetString} is not loaded. - Loading with base Chain. Use AssetValue.loadStaticAssets() to load it`,
124
+ // );
125
+ // }
126
+
127
+ const { tax, decimal, identifier } = tokenInfo || {
128
+ decimal: BaseDecimal[chain],
129
+ identifier: assetString,
130
+ };
122
131
  return new AssetValue({ tax, decimal, identifier, value: safeValue(value, decimal) });
123
132
  }
124
133
 
@@ -194,6 +203,16 @@ async function createAssetValue(identifier: string, value: NumberPrimitives = 0)
194
203
  return new AssetValue({ decimal, value: safeValue(value, decimal), identifier });
195
204
  }
196
205
 
206
+ function createSyntheticAssetValue(identifier: string, value: NumberPrimitives = 0) {
207
+ const [symbol, ticker] = identifier.split('/');
208
+
209
+ return new AssetValue({
210
+ decimal: 8,
211
+ value: safeValue(value, 8),
212
+ identifier: `${Chain.THORChain}.${symbol}/${ticker}`,
213
+ });
214
+ }
215
+
197
216
  function safeValue(value: NumberPrimitives, decimal: number) {
198
217
  return typeof value === 'bigint'
199
218
  ? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })