@swapkit/helpers 1.0.0-rc.23 → 1.0.0-rc.25

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/package.json CHANGED
@@ -42,7 +42,7 @@
42
42
  "repository": "https://github.com/thorswap/SwapKit.git",
43
43
  "type": "module",
44
44
  "types": "./dist/index.d.ts",
45
- "version": "1.0.0-rc.23",
45
+ "version": "1.0.0-rc.25",
46
46
  "scripts": {
47
47
  "build": "vite build",
48
48
  "clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
@@ -1,7 +1,7 @@
1
1
  import type { EVMChain } from '@swapkit/types';
2
2
  import { BaseDecimal, Chain, ChainToRPC, EVMChainList, FeeOption } from '@swapkit/types';
3
3
 
4
- import { type AssetValue, RequestClient } from '../index.ts';
4
+ import { RequestClient } from '../index.ts';
5
5
 
6
6
  const getDecimalMethodHex = '0x313ce567';
7
7
 
@@ -187,16 +187,28 @@ const potentialScamRegex = new RegExp(
187
187
  'gmi',
188
188
  );
189
189
 
190
- const evmAssetHasAddress = (asset: AssetValue) => {
191
- if (!EVMChainList.includes(asset.chain as EVMChain)) return true;
190
+ const evmAssetHasAddress = (assetString: string) => {
191
+ const [chain, symbol] = assetString.split('.') as [EVMChain, string];
192
+ if (!EVMChainList.includes(chain as EVMChain)) return true;
193
+ const [, address] = symbol.split('-') as [string, string?];
192
194
 
193
- return !asset.isGasAsset && asset.address && !Number.isNaN(asset.decimal);
195
+ return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
194
196
  };
195
197
 
196
- export const filterAssets = (assets: AssetValue[]) =>
197
- assets.filter(
198
- (asset) =>
199
- !asset.toString().includes('undefined') &&
200
- !potentialScamRegex.test(asset.toString()) &&
201
- evmAssetHasAddress(asset),
202
- );
198
+ export const filterAssets = (
199
+ tokens: {
200
+ value: string;
201
+ decimal: number;
202
+ chain: Chain;
203
+ symbol: string;
204
+ }[],
205
+ ) =>
206
+ tokens.filter((token) => {
207
+ const assetString = `${token.chain}.${token.symbol}`;
208
+
209
+ return (
210
+ !potentialScamRegex.test(assetString) &&
211
+ evmAssetHasAddress(assetString) &&
212
+ token.value !== '0'
213
+ );
214
+ });
@@ -12,14 +12,13 @@ describe('AssetValue', () => {
12
12
  chain: Chain.Avalanche,
13
13
  symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
14
14
  });
15
- expect(fakeAvaxUSDCAsset.assetValue).toBe('1234567890 USDC');
15
+ expect(fakeAvaxUSDCAsset.toString(true)).toBe('USDC');
16
16
  expect(fakeAvaxUSDCAsset.toString()).toBe(
17
17
  'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
18
18
  );
19
- expect(fakeAvaxUSDCAsset.toString(true)).toBe('AVAX.USDC');
20
19
 
21
20
  const thor = AssetValue.fromChainOrSignature('ETH.THOR');
22
- expect(thor.assetValue).toBe('0 THOR');
21
+ expect(thor.toString(true)).toBe('THOR');
23
22
 
24
23
  const ethSynth = new AssetValue({
25
24
  chain: Chain.THORChain,
@@ -28,7 +27,6 @@ describe('AssetValue', () => {
28
27
  value: 1234567890,
29
28
  });
30
29
 
31
- expect(ethSynth.assetValue).toBe('1234567890 ETH');
32
30
  expect(ethSynth.toString()).toBe('THOR.ETH/ETH');
33
31
  expect(ethSynth.toString(true)).toBe('ETH/ETH');
34
32
  expect(ethSynth.mul(21.37).getValue('string')).toBe('26382715809.3');
@@ -39,9 +37,8 @@ describe('AssetValue', () => {
39
37
  value: 123456789,
40
38
  });
41
39
 
42
- expect(atomDerived.assetValue).toBe('123456789 ATOM');
40
+ expect(atomDerived.toString(true)).toBe('ATOM');
43
41
  expect(atomDerived.toString()).toBe('THOR.ATOM');
44
- expect(atomDerived.toString(true)).toBe('THOR.ATOM');
45
42
  });
46
43
  });
47
44
 
@@ -66,26 +66,23 @@ const createAssetValue = async (assetString: string, value: NumberPrimitives = 0
66
66
  return new AssetValue({ decimal, value: parsedValue, identifier: assetString });
67
67
  };
68
68
 
69
- export class AssetValue extends BigIntArithmetics {
70
- address?: string;
71
- chain: Chain;
72
- isSynthetic = false;
73
- isGasAsset = false;
74
- symbol: string;
75
- ticker: string;
76
- type: ReturnType<typeof getAssetType>;
69
+ const cacheAssetValue = new Map<string, AssetValue>();
77
70
 
71
+ export class AssetValue extends BigIntArithmetics {
78
72
  constructor(params: AssetValueParams) {
73
+ const identifier =
74
+ 'identifier' in params ? params.identifier : `${params.chain}.${params.symbol}`;
75
+
76
+ const cachedAsset = cacheAssetValue.get(identifier);
77
+ if (cachedAsset) return cachedAsset.set(params.value);
78
+
79
79
  super(
80
80
  params.value instanceof BigIntArithmetics
81
81
  ? params.value
82
82
  : { decimal: params.decimal, value: params.value },
83
83
  );
84
84
 
85
- const identifier =
86
- 'identifier' in params ? params.identifier : `${params.chain}.${params.symbol}`;
87
85
  const assetInfo = getAssetInfo(identifier);
88
-
89
86
  this.type = getAssetType(assetInfo);
90
87
  this.chain = assetInfo.chain;
91
88
  this.ticker = assetInfo.ticker;
@@ -93,17 +90,25 @@ export class AssetValue extends BigIntArithmetics {
93
90
  this.address = assetInfo.address;
94
91
  this.isSynthetic = assetInfo.isSynthetic;
95
92
  this.isGasAsset = assetInfo.isGasAsset;
96
- }
97
93
 
98
- get assetValue() {
99
- return `${this.getValue('string')} ${this.ticker}`;
94
+ cacheAssetValue.set(identifier, this);
100
95
  }
101
96
 
97
+ address?: string;
98
+ isSynthetic = false;
99
+ isGasAsset = false;
100
+ // @ts-expect-error cache is false positive on that case
101
+ chain: Chain;
102
+ // @ts-expect-error cache is false positive on that case
103
+ symbol: string;
104
+ // @ts-expect-error cache is false positive on that case
105
+ ticker: string;
106
+ // @ts-expect-error cache is false positive on that case
107
+ type: ReturnType<typeof getAssetType>;
108
+
102
109
  toString(short = false) {
103
110
  // THOR.RUNE | ETH/ETH
104
- const shortFormat = this.isSynthetic
105
- ? this.symbol.split('-')[0]
106
- : `${this.chain}.${this.ticker}`;
111
+ const shortFormat = this.isSynthetic ? this.symbol.split('-')[0] : this.ticker;
107
112
 
108
113
  return short
109
114
  ? shortFormat
@@ -127,11 +132,13 @@ export class AssetValue extends BigIntArithmetics {
127
132
 
128
133
  const parsedValue = safeValue(value, decimal);
129
134
 
130
- return tokenIdentifier
135
+ const asset = tokenIdentifier
131
136
  ? new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue })
132
137
  : isSynthetic
133
138
  ? new AssetValue({ decimal: 8, identifier: assetString, value: parsedValue })
134
139
  : undefined;
140
+
141
+ return asset;
135
142
  }
136
143
 
137
144
  static async fromIdentifier(
@@ -93,8 +93,8 @@ export class BigIntArithmetics {
93
93
  }
94
94
 
95
95
  set(value: SKBigIntParams): this {
96
- // @ts-expect-error False positive
97
- return new this.constructor({ decimal: this.decimal, value, identifier: this.toString() });
96
+ this.#setValue(getStringValue(value));
97
+ return this;
98
98
  }
99
99
  add(...args: InitialisationValueType[]) {
100
100
  return this.#arithmetics('add', ...args);