@shapeshiftoss/utils 1.0.3-alpha.1 → 1.0.3
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 +6 -3
- package/dist/cjs/.tsbuildinfo +0 -1
- package/dist/cjs/assertUnreachable.d.ts +0 -1
- package/dist/cjs/assertUnreachable.js +0 -7
- package/dist/cjs/assetData/assetData.test.d.ts +0 -1
- package/dist/cjs/assetData/assetData.test.js +0 -136
- package/dist/cjs/assetData/baseAssets.d.ts +0 -21
- package/dist/cjs/assetData/baseAssets.js +0 -319
- package/dist/cjs/assetData/constants.d.ts +0 -1
- package/dist/cjs/assetData/constants.js +0 -13
- package/dist/cjs/assetData/decodeAssetData.d.ts +0 -7
- package/dist/cjs/assetData/decodeAssetData.js +0 -106
- package/dist/cjs/assetData/decodeRelatedAssetIndex.d.ts +0 -3
- package/dist/cjs/assetData/decodeRelatedAssetIndex.js +0 -14
- package/dist/cjs/assetData/encodeAssetData.d.ts +0 -4
- package/dist/cjs/assetData/encodeAssetData.js +0 -72
- package/dist/cjs/assetData/encodeRelatedAssetIndex.d.ts +0 -3
- package/dist/cjs/assetData/encodeRelatedAssetIndex.js +0 -20
- package/dist/cjs/assetData/getBaseAsset.d.ts +0 -3
- package/dist/cjs/assetData/getBaseAsset.js +0 -48
- package/dist/cjs/assetData/index.d.ts +0 -7
- package/dist/cjs/assetData/index.js +0 -23
- package/dist/cjs/assetData/relatedAssetIndex.test.d.ts +0 -1
- package/dist/cjs/assetData/relatedAssetIndex.test.js +0 -32
- package/dist/cjs/assetData/types.d.ts +0 -28
- package/dist/cjs/assetData/types.js +0 -2
- package/dist/cjs/baseUnits/baseUnits.d.ts +0 -5
- package/dist/cjs/baseUnits/baseUnits.js +0 -32
- package/dist/cjs/baseUnits/baseUnits.test.d.ts +0 -1
- package/dist/cjs/baseUnits/baseUnits.test.js +0 -32
- package/dist/cjs/basisPoints.d.ts +0 -23
- package/dist/cjs/basisPoints.js +0 -48
- package/dist/cjs/bignumber/bignumber.d.ts +0 -11
- package/dist/cjs/bignumber/bignumber.js +0 -27
- package/dist/cjs/bignumber/bignumber.test.d.ts +0 -1
- package/dist/cjs/bignumber/bignumber.test.js +0 -52
- package/dist/cjs/chainIdToFeeAsset.d.ts +0 -3
- package/dist/cjs/chainIdToFeeAsset.js +0 -9
- package/dist/cjs/chainIdToFeeAssetId.d.ts +0 -2
- package/dist/cjs/chainIdToFeeAssetId.js +0 -48
- package/dist/cjs/createThrottle.d.ts +0 -9
- package/dist/cjs/createThrottle.js +0 -36
- package/dist/cjs/getAssetNamespaceFromChainId.d.ts +0 -3
- package/dist/cjs/getAssetNamespaceFromChainId.js +0 -33
- package/dist/cjs/getChainShortName.d.ts +0 -2
- package/dist/cjs/getChainShortName.js +0 -47
- package/dist/cjs/getNativeFeeAssetReference.d.ts +0 -2
- package/dist/cjs/getNativeFeeAssetReference.js +0 -67
- package/dist/cjs/historyTimeframe.d.ts +0 -6
- package/dist/cjs/historyTimeframe.js +0 -36
- package/dist/cjs/index.d.ts +0 -24
- package/dist/cjs/index.js +0 -58
- package/dist/cjs/makeAsset/makeAsset.d.ts +0 -10
- package/dist/cjs/makeAsset/makeAsset.js +0 -45
- package/dist/cjs/makeAsset/makeAsset.test.d.ts +0 -1
- package/dist/cjs/makeAsset/makeAsset.test.js +0 -47
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/promises.d.ts +0 -4
- package/dist/cjs/promises.js +0 -9
- package/dist/cjs/sha256.d.ts +0 -1
- package/dist/cjs/sha256.js +0 -9
- package/dist/cjs/timeout.d.ts +0 -7
- package/dist/cjs/timeout.js +0 -34
- package/dist/cjs/treasury.d.ts +0 -16
- package/dist/cjs/treasury.js +0 -34
- package/dist/cjs/unfreeze.d.ts +0 -4
- package/dist/cjs/unfreeze.js +0 -10
- package/dist/cjs/utxo.d.ts +0 -2
- package/dist/cjs/utxo.js +0 -6
- package/dist/esm/.tsbuildinfo +0 -1
- package/dist/esm/assertUnreachable.d.ts +0 -1
- package/dist/esm/assertUnreachable.js +0 -3
- package/dist/esm/assetData/assetData.test.d.ts +0 -1
- package/dist/esm/assetData/assetData.test.js +0 -134
- package/dist/esm/assetData/baseAssets.d.ts +0 -21
- package/dist/esm/assetData/baseAssets.js +0 -293
- package/dist/esm/assetData/constants.d.ts +0 -1
- package/dist/esm/assetData/constants.js +0 -10
- package/dist/esm/assetData/decodeAssetData.d.ts +0 -7
- package/dist/esm/assetData/decodeAssetData.js +0 -102
- package/dist/esm/assetData/decodeRelatedAssetIndex.d.ts +0 -3
- package/dist/esm/assetData/decodeRelatedAssetIndex.js +0 -10
- package/dist/esm/assetData/encodeAssetData.d.ts +0 -4
- package/dist/esm/assetData/encodeAssetData.js +0 -68
- package/dist/esm/assetData/encodeRelatedAssetIndex.d.ts +0 -3
- package/dist/esm/assetData/encodeRelatedAssetIndex.js +0 -16
- package/dist/esm/assetData/getBaseAsset.d.ts +0 -3
- package/dist/esm/assetData/getBaseAsset.js +0 -44
- package/dist/esm/assetData/index.d.ts +0 -7
- package/dist/esm/assetData/index.js +0 -7
- package/dist/esm/assetData/relatedAssetIndex.test.d.ts +0 -1
- package/dist/esm/assetData/relatedAssetIndex.test.js +0 -30
- package/dist/esm/assetData/types.d.ts +0 -28
- package/dist/esm/assetData/types.js +0 -1
- package/dist/esm/baseUnits/baseUnits.d.ts +0 -5
- package/dist/esm/baseUnits/baseUnits.js +0 -23
- package/dist/esm/baseUnits/baseUnits.test.d.ts +0 -1
- package/dist/esm/baseUnits/baseUnits.test.js +0 -30
- package/dist/esm/basisPoints.d.ts +0 -23
- package/dist/esm/basisPoints.js +0 -39
- package/dist/esm/bignumber/bignumber.d.ts +0 -11
- package/dist/esm/bignumber/bignumber.js +0 -16
- package/dist/esm/bignumber/bignumber.test.d.ts +0 -1
- package/dist/esm/bignumber/bignumber.test.js +0 -50
- package/dist/esm/chainIdToFeeAsset.d.ts +0 -3
- package/dist/esm/chainIdToFeeAsset.js +0 -5
- package/dist/esm/chainIdToFeeAssetId.d.ts +0 -2
- package/dist/esm/chainIdToFeeAssetId.js +0 -44
- package/dist/esm/createThrottle.d.ts +0 -9
- package/dist/esm/createThrottle.js +0 -32
- package/dist/esm/getAssetNamespaceFromChainId.d.ts +0 -3
- package/dist/esm/getAssetNamespaceFromChainId.js +0 -29
- package/dist/esm/getChainShortName.d.ts +0 -2
- package/dist/esm/getChainShortName.js +0 -43
- package/dist/esm/getNativeFeeAssetReference.d.ts +0 -2
- package/dist/esm/getNativeFeeAssetReference.js +0 -63
- package/dist/esm/historyTimeframe.d.ts +0 -6
- package/dist/esm/historyTimeframe.js +0 -29
- package/dist/esm/index.d.ts +0 -24
- package/dist/esm/index.js +0 -37
- package/dist/esm/makeAsset/makeAsset.d.ts +0 -10
- package/dist/esm/makeAsset/makeAsset.js +0 -41
- package/dist/esm/makeAsset/makeAsset.test.d.ts +0 -1
- package/dist/esm/makeAsset/makeAsset.test.js +0 -45
- package/dist/esm/promises.d.ts +0 -4
- package/dist/esm/promises.js +0 -3
- package/dist/esm/sha256.d.ts +0 -1
- package/dist/esm/sha256.js +0 -2
- package/dist/esm/timeout.d.ts +0 -7
- package/dist/esm/timeout.js +0 -28
- package/dist/esm/treasury.d.ts +0 -16
- package/dist/esm/treasury.js +0 -30
- package/dist/esm/unfreeze.d.ts +0 -4
- package/dist/esm/unfreeze.js +0 -6
- package/dist/esm/utxo.d.ts +0 -2
- package/dist/esm/utxo.js +0 -2
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
import type { BN } from '../bignumber/bignumber.js';
|
|
3
|
-
export declare const fromBaseUnit: (value: BigNumber.Value, precision: number, displayDecimals?: number) => string;
|
|
4
|
-
export declare const toBaseUnit: (amount: BigNumber.Value | undefined, precision: number, roundingMode?: BigNumber.RoundingMode) => string;
|
|
5
|
-
export declare const firstNonZeroDecimal: (number: BN) => string | undefined;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
import { bn, bnOrZero } from '../bignumber/bignumber.js';
|
|
3
|
-
// Converts from base unit to a precision/ish number
|
|
4
|
-
// - If no displayDecimals are provided, it will use the full precision of the number being converted
|
|
5
|
-
// - If displayDecimals are provided, it will use that precision, to return e.g a human, precision, or number stripped to any arbitrary decimal places
|
|
6
|
-
// and use the ROUND_DOWN rounding mode
|
|
7
|
-
export const fromBaseUnit = (value, precision, displayDecimals) => {
|
|
8
|
-
const precisionNumber = bnOrZero(value).div(bn(10).pow(precision));
|
|
9
|
-
if (typeof displayDecimals === 'number') {
|
|
10
|
-
return precisionNumber
|
|
11
|
-
.decimalPlaces(displayDecimals, BigNumber.ROUND_DOWN)
|
|
12
|
-
.toFixed(displayDecimals);
|
|
13
|
-
}
|
|
14
|
-
return precisionNumber.toFixed();
|
|
15
|
-
};
|
|
16
|
-
export const toBaseUnit = (amount, precision, roundingMode) => {
|
|
17
|
-
return bnOrZero(amount)
|
|
18
|
-
.times(bn(10).exponentiatedBy(bnOrZero(precision)))
|
|
19
|
-
.toFixed(0, roundingMode);
|
|
20
|
-
};
|
|
21
|
-
export const firstNonZeroDecimal = (number) => {
|
|
22
|
-
return number.toFixed(10).match(/^-?\d*\.?0*\d{0,2}/)?.[0];
|
|
23
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { fromBaseUnit } from './baseUnits.js';
|
|
3
|
-
describe('@/lib/math', () => {
|
|
4
|
-
describe('fromBaseUnit', () => {
|
|
5
|
-
it('should correctly convert and round down with given displayDecimals', () => {
|
|
6
|
-
const result = fromBaseUnit(123456789, 4, 2);
|
|
7
|
-
expect(result).toBe('12345.67');
|
|
8
|
-
});
|
|
9
|
-
it('should correctly convert without displayDecimals and return full precision', () => {
|
|
10
|
-
const result = fromBaseUnit(123456789, 4);
|
|
11
|
-
expect(result).toBe('12345.6789');
|
|
12
|
-
});
|
|
13
|
-
it('should correctly convert and round down with given displayDecimals for another input', () => {
|
|
14
|
-
const result = fromBaseUnit(987654321, 6, 3);
|
|
15
|
-
expect(result).toBe('987.654');
|
|
16
|
-
});
|
|
17
|
-
it('should correctly convert without displayDecimals and return full precision for another input', () => {
|
|
18
|
-
const result = fromBaseUnit(987654321, 6);
|
|
19
|
-
expect(result).toBe('987.654321');
|
|
20
|
-
});
|
|
21
|
-
it('should correctly convert and round down with given displayDecimals for high precision input', () => {
|
|
22
|
-
const result = fromBaseUnit('182912819182912192', 18, 8);
|
|
23
|
-
expect(result).toBe('0.18291281');
|
|
24
|
-
});
|
|
25
|
-
it('should correctly convert without displayDecimals and return full precision for high precision input', () => {
|
|
26
|
-
const result = fromBaseUnit('182912819182912192', 18);
|
|
27
|
-
expect(result).toBe('0.182912819182912192');
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { BigNumber } from './bignumber/bignumber.js';
|
|
2
|
-
export declare const convertBasisPointsToDecimalPercentage: (basisPoints: BigNumber.Value) => BigNumber;
|
|
3
|
-
export declare const convertDecimalPercentageToBasisPoints: (decimalPercentage: BigNumber.Value) => BigNumber;
|
|
4
|
-
export declare const convertBasisPointsToPercentage: (basisPoints: BigNumber.Value) => BigNumber;
|
|
5
|
-
export declare const convertPercentageToBasisPoints: (percentage: BigNumber.Value) => BigNumber;
|
|
6
|
-
/**
|
|
7
|
-
* Subtracts basis point amount from a given value.
|
|
8
|
-
*
|
|
9
|
-
* @param value The value to subtract basis points from.
|
|
10
|
-
* @param basisPoints The number of basis points to subtract.
|
|
11
|
-
* @param roundingMode
|
|
12
|
-
* @returns The new number that is the input value minus the basis points of the value.
|
|
13
|
-
*/
|
|
14
|
-
export declare const subtractBasisPointAmount: (value: string, basisPoints: BigNumber.Value, roundingMode?: BigNumber.RoundingMode) => string;
|
|
15
|
-
/**
|
|
16
|
-
* Adds basis point amount from a given value.
|
|
17
|
-
*
|
|
18
|
-
* @param value The value to subtract basis points from.
|
|
19
|
-
* @param basisPoints The number of basis points to subtract.
|
|
20
|
-
* @param roundingMode
|
|
21
|
-
* @returns The new number that is the input value minus the basis points of the value.
|
|
22
|
-
*/
|
|
23
|
-
export declare const addBasisPointAmount: (value: string, basisPoints: BigNumber.Value, roundingMode?: BigNumber.RoundingMode) => string;
|
package/dist/esm/basisPoints.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { bn, bnOrZero } from './bignumber/bignumber.js';
|
|
2
|
-
export const convertBasisPointsToDecimalPercentage = (basisPoints) => bnOrZero(basisPoints).div(10000);
|
|
3
|
-
export const convertDecimalPercentageToBasisPoints = (decimalPercentage) => bnOrZero(decimalPercentage).times(10000);
|
|
4
|
-
export const convertBasisPointsToPercentage = (basisPoints) => bnOrZero(basisPoints).div(100);
|
|
5
|
-
export const convertPercentageToBasisPoints = (percentage) => bnOrZero(percentage).times(100);
|
|
6
|
-
/**
|
|
7
|
-
* Subtracts basis point amount from a given value.
|
|
8
|
-
*
|
|
9
|
-
* @param value The value to subtract basis points from.
|
|
10
|
-
* @param basisPoints The number of basis points to subtract.
|
|
11
|
-
* @param roundingMode
|
|
12
|
-
* @returns The new number that is the input value minus the basis points of the value.
|
|
13
|
-
*/
|
|
14
|
-
export const subtractBasisPointAmount = (value, basisPoints, roundingMode) => {
|
|
15
|
-
const bigNumValue = bn(value);
|
|
16
|
-
// Basis point is 1/100th of a percent
|
|
17
|
-
const percentValue = convertBasisPointsToDecimalPercentage(basisPoints);
|
|
18
|
-
const subtractValue = bigNumValue.times(percentValue);
|
|
19
|
-
// Subtract basis points from the original value
|
|
20
|
-
const resultValue = bigNumValue.minus(subtractValue);
|
|
21
|
-
return roundingMode !== undefined ? resultValue.toFixed(0, roundingMode) : resultValue.toFixed();
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Adds basis point amount from a given value.
|
|
25
|
-
*
|
|
26
|
-
* @param value The value to subtract basis points from.
|
|
27
|
-
* @param basisPoints The number of basis points to subtract.
|
|
28
|
-
* @param roundingMode
|
|
29
|
-
* @returns The new number that is the input value minus the basis points of the value.
|
|
30
|
-
*/
|
|
31
|
-
export const addBasisPointAmount = (value, basisPoints, roundingMode) => {
|
|
32
|
-
const bigNumValue = bn(value);
|
|
33
|
-
// Basis point is 1/100th of a percent
|
|
34
|
-
const percentValue = convertBasisPointsToDecimalPercentage(basisPoints);
|
|
35
|
-
const addValue = bigNumValue.times(percentValue);
|
|
36
|
-
// Subtract basis points from the original value
|
|
37
|
-
const resultValue = bigNumValue.plus(addValue);
|
|
38
|
-
return roundingMode !== undefined ? resultValue.toFixed(0, roundingMode) : resultValue.toFixed();
|
|
39
|
-
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
export { default as BigNumber } from 'bignumber.js';
|
|
3
|
-
export type BN = BigNumber;
|
|
4
|
-
export declare const bn: (n: BigNumber.Value, base?: number) => BN;
|
|
5
|
-
export declare const bnOrZero: (n: BigNumber.Value | null | undefined) => BN;
|
|
6
|
-
export declare const positiveOrZero: (n: BigNumber.Value | null | undefined) => BN;
|
|
7
|
-
export declare const convertPrecision: ({ value, inputExponent, outputExponent, }: {
|
|
8
|
-
value: BigNumber.Value;
|
|
9
|
-
inputExponent?: number | undefined;
|
|
10
|
-
outputExponent?: number | undefined;
|
|
11
|
-
}) => BigNumber;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
export { default as BigNumber } from 'bignumber.js';
|
|
3
|
-
export const bn = (n, base = 10) => new BigNumber(n, base);
|
|
4
|
-
export const bnOrZero = (n) => {
|
|
5
|
-
const value = bn(n || 0);
|
|
6
|
-
return value.isFinite() ? value : bn(0);
|
|
7
|
-
};
|
|
8
|
-
export const positiveOrZero = (n) => {
|
|
9
|
-
const value = bn(n || 0);
|
|
10
|
-
return value.isPositive() ? value : bn(0);
|
|
11
|
-
};
|
|
12
|
-
export const convertPrecision = ({ value, inputExponent = 0, outputExponent = 0, }) => {
|
|
13
|
-
return bnOrZero(value)
|
|
14
|
-
.dividedBy(bn(10).exponentiatedBy(inputExponent))
|
|
15
|
-
.multipliedBy(bn(10).exponentiatedBy(outputExponent));
|
|
16
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { BigNumber, bn, bnOrZero, convertPrecision } from './bignumber.js';
|
|
3
|
-
describe('bignumber', () => {
|
|
4
|
-
describe('bnOrZero', () => {
|
|
5
|
-
it('returns an instance of bignumber', () => {
|
|
6
|
-
const zero = bnOrZero(0);
|
|
7
|
-
expect(zero).toBeInstanceOf(BigNumber);
|
|
8
|
-
const one = bnOrZero(1);
|
|
9
|
-
expect(one).toBeInstanceOf(BigNumber);
|
|
10
|
-
const foo = bnOrZero('foo');
|
|
11
|
-
expect(foo).toBeInstanceOf(BigNumber);
|
|
12
|
-
expect(foo.toString()).toBe('0');
|
|
13
|
-
const empty = bnOrZero('');
|
|
14
|
-
expect(empty).toBeInstanceOf(BigNumber);
|
|
15
|
-
expect(empty.toString()).toBe('0');
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
describe('convertPrecision', () => {
|
|
19
|
-
it('can convert values for increased precision', () => {
|
|
20
|
-
const value = '1234';
|
|
21
|
-
const expectation = bn('1234000');
|
|
22
|
-
const result = convertPrecision({ value, inputExponent: 3, outputExponent: 6 });
|
|
23
|
-
expect(result).toEqual(expectation);
|
|
24
|
-
});
|
|
25
|
-
it('can convert values for decreased precision', () => {
|
|
26
|
-
const value = '1234000';
|
|
27
|
-
const expectation = bn('1234');
|
|
28
|
-
const result = convertPrecision({ value, inputExponent: 6, outputExponent: 3 });
|
|
29
|
-
expect(result).toEqual(expectation);
|
|
30
|
-
});
|
|
31
|
-
it('treats 0 as 0 when increasing precision', () => {
|
|
32
|
-
const value = '0';
|
|
33
|
-
const expectation = bn('0');
|
|
34
|
-
const result = convertPrecision({ value, inputExponent: 12, outputExponent: 18 });
|
|
35
|
-
expect(result).toEqual(expectation);
|
|
36
|
-
});
|
|
37
|
-
it('treats 0 as 0 when decreasing precision', () => {
|
|
38
|
-
const value = '0';
|
|
39
|
-
const expectation = bn('0');
|
|
40
|
-
const result = convertPrecision({ value, inputExponent: 18, outputExponent: 12 });
|
|
41
|
-
expect(result).toEqual(expectation);
|
|
42
|
-
});
|
|
43
|
-
it('does not remove decimals with rounding', () => {
|
|
44
|
-
const value = '1234567';
|
|
45
|
-
const expectation = bn('1234.567');
|
|
46
|
-
const result = convertPrecision({ value, inputExponent: 6, outputExponent: 3 });
|
|
47
|
-
expect(result).toEqual(expectation);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { arbitrumAssetId, arbitrumNovaAssetId, avalancheAssetId, baseAssetId, bchAssetId, bscAssetId, btcAssetId, cosmosAssetId, dogeAssetId, ethAssetId, gnosisAssetId, ltcAssetId, mayachainAssetId, optimismAssetId, polygonAssetId, solAssetId, thorchainAssetId, } from '@shapeshiftoss/caip';
|
|
2
|
-
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
3
|
-
import { assertUnreachable } from './assertUnreachable.js';
|
|
4
|
-
export const chainIdToFeeAssetId = (_chainId) => {
|
|
5
|
-
const chainId = _chainId;
|
|
6
|
-
switch (chainId) {
|
|
7
|
-
case KnownChainIds.ArbitrumMainnet:
|
|
8
|
-
return arbitrumAssetId;
|
|
9
|
-
case KnownChainIds.AvalancheMainnet:
|
|
10
|
-
return avalancheAssetId;
|
|
11
|
-
case KnownChainIds.ArbitrumNovaMainnet:
|
|
12
|
-
return arbitrumNovaAssetId;
|
|
13
|
-
case KnownChainIds.BaseMainnet:
|
|
14
|
-
return baseAssetId;
|
|
15
|
-
case KnownChainIds.BitcoinCashMainnet:
|
|
16
|
-
return bchAssetId;
|
|
17
|
-
case KnownChainIds.BitcoinMainnet:
|
|
18
|
-
return btcAssetId;
|
|
19
|
-
case KnownChainIds.BnbSmartChainMainnet:
|
|
20
|
-
return bscAssetId;
|
|
21
|
-
case KnownChainIds.CosmosMainnet:
|
|
22
|
-
return cosmosAssetId;
|
|
23
|
-
case KnownChainIds.DogecoinMainnet:
|
|
24
|
-
return dogeAssetId;
|
|
25
|
-
case KnownChainIds.EthereumMainnet:
|
|
26
|
-
return ethAssetId;
|
|
27
|
-
case KnownChainIds.GnosisMainnet:
|
|
28
|
-
return gnosisAssetId;
|
|
29
|
-
case KnownChainIds.LitecoinMainnet:
|
|
30
|
-
return ltcAssetId;
|
|
31
|
-
case KnownChainIds.OptimismMainnet:
|
|
32
|
-
return optimismAssetId;
|
|
33
|
-
case KnownChainIds.PolygonMainnet:
|
|
34
|
-
return polygonAssetId;
|
|
35
|
-
case KnownChainIds.ThorchainMainnet:
|
|
36
|
-
return thorchainAssetId;
|
|
37
|
-
case KnownChainIds.MayachainMainnet:
|
|
38
|
-
return mayachainAssetId;
|
|
39
|
-
case KnownChainIds.SolanaMainnet:
|
|
40
|
-
return solAssetId;
|
|
41
|
-
default:
|
|
42
|
-
return assertUnreachable(chainId);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export const createThrottle = ({ capacity, costPerReq, drainPerInterval, intervalMs, }) => {
|
|
2
|
-
let currentLevel = 0;
|
|
3
|
-
let pendingResolves = [];
|
|
4
|
-
const drain = () => {
|
|
5
|
-
const drainAmount = Math.min(currentLevel, drainPerInterval);
|
|
6
|
-
currentLevel -= drainAmount;
|
|
7
|
-
// Resolve pending promises if there's enough capacity
|
|
8
|
-
while (pendingResolves.length > 0 && currentLevel + costPerReq <= capacity) {
|
|
9
|
-
const resolve = pendingResolves.shift();
|
|
10
|
-
if (resolve) {
|
|
11
|
-
currentLevel += costPerReq;
|
|
12
|
-
resolve();
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
// Start the interval to drain the capacity
|
|
17
|
-
const intervalId = setInterval(drain, intervalMs);
|
|
18
|
-
const throttle = async () => {
|
|
19
|
-
if (currentLevel + costPerReq <= capacity) {
|
|
20
|
-
// If adding another request doesn't exceed capacity, proceed immediately
|
|
21
|
-
currentLevel += costPerReq;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
// Otherwise, wait until there's enough capacity
|
|
25
|
-
await new Promise(resolve => {
|
|
26
|
-
pendingResolves.push(resolve);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
const clear = () => clearInterval(intervalId);
|
|
31
|
-
return { throttle, clear };
|
|
32
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { ASSET_NAMESPACE } from '@shapeshiftoss/caip';
|
|
2
|
-
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
3
|
-
import { assertUnreachable } from './assertUnreachable.js';
|
|
4
|
-
export const getAssetNamespaceFromChainId = (chainId) => {
|
|
5
|
-
switch (chainId) {
|
|
6
|
-
case KnownChainIds.SolanaMainnet:
|
|
7
|
-
return ASSET_NAMESPACE.splToken;
|
|
8
|
-
case KnownChainIds.EthereumMainnet:
|
|
9
|
-
case KnownChainIds.AvalancheMainnet:
|
|
10
|
-
case KnownChainIds.OptimismMainnet:
|
|
11
|
-
case KnownChainIds.PolygonMainnet:
|
|
12
|
-
case KnownChainIds.GnosisMainnet:
|
|
13
|
-
case KnownChainIds.ArbitrumMainnet:
|
|
14
|
-
case KnownChainIds.ArbitrumNovaMainnet:
|
|
15
|
-
case KnownChainIds.BaseMainnet:
|
|
16
|
-
case KnownChainIds.BnbSmartChainMainnet:
|
|
17
|
-
return ASSET_NAMESPACE.erc20;
|
|
18
|
-
case KnownChainIds.CosmosMainnet:
|
|
19
|
-
case KnownChainIds.BitcoinMainnet:
|
|
20
|
-
case KnownChainIds.BitcoinCashMainnet:
|
|
21
|
-
case KnownChainIds.DogecoinMainnet:
|
|
22
|
-
case KnownChainIds.LitecoinMainnet:
|
|
23
|
-
case KnownChainIds.ThorchainMainnet:
|
|
24
|
-
case KnownChainIds.MayachainMainnet:
|
|
25
|
-
throw Error(`Unhandled case '${chainId}'`);
|
|
26
|
-
default:
|
|
27
|
-
return assertUnreachable(chainId);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
-
export declare const getChainShortName: (chainId: KnownChainIds) => "AVA" | "OP" | "ETH" | "POLY" | "GNO" | "BNB" | "ARB" | "ARB-Nova" | "BAS" | "BTC" | "BCH" | "COSM" | "THOR" | "MAYA" | "DOGE" | "LTC" | "SOL" | undefined;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
2
|
-
import { assertUnreachable } from './assertUnreachable.js';
|
|
3
|
-
export const getChainShortName = (chainId) => {
|
|
4
|
-
switch (chainId) {
|
|
5
|
-
case KnownChainIds.AvalancheMainnet:
|
|
6
|
-
return 'AVA';
|
|
7
|
-
case KnownChainIds.OptimismMainnet:
|
|
8
|
-
return 'OP';
|
|
9
|
-
case KnownChainIds.EthereumMainnet:
|
|
10
|
-
return 'ETH';
|
|
11
|
-
case KnownChainIds.PolygonMainnet:
|
|
12
|
-
return 'POLY';
|
|
13
|
-
case KnownChainIds.GnosisMainnet:
|
|
14
|
-
return 'GNO';
|
|
15
|
-
case KnownChainIds.BnbSmartChainMainnet:
|
|
16
|
-
return 'BNB';
|
|
17
|
-
case KnownChainIds.ArbitrumMainnet:
|
|
18
|
-
return 'ARB';
|
|
19
|
-
case KnownChainIds.ArbitrumNovaMainnet:
|
|
20
|
-
return 'ARB-Nova';
|
|
21
|
-
case KnownChainIds.BaseMainnet:
|
|
22
|
-
return 'BAS';
|
|
23
|
-
case KnownChainIds.BitcoinMainnet:
|
|
24
|
-
return 'BTC';
|
|
25
|
-
case KnownChainIds.BitcoinCashMainnet:
|
|
26
|
-
return 'BCH';
|
|
27
|
-
case KnownChainIds.CosmosMainnet:
|
|
28
|
-
return 'COSM';
|
|
29
|
-
case KnownChainIds.ThorchainMainnet:
|
|
30
|
-
return 'THOR';
|
|
31
|
-
case KnownChainIds.MayachainMainnet:
|
|
32
|
-
return 'MAYA';
|
|
33
|
-
case KnownChainIds.DogecoinMainnet:
|
|
34
|
-
return 'DOGE';
|
|
35
|
-
case KnownChainIds.LitecoinMainnet:
|
|
36
|
-
return 'LTC';
|
|
37
|
-
case KnownChainIds.SolanaMainnet:
|
|
38
|
-
return 'SOL';
|
|
39
|
-
default: {
|
|
40
|
-
assertUnreachable(chainId);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { ASSET_REFERENCE, CHAIN_NAMESPACE, CHAIN_REFERENCE } from '@shapeshiftoss/caip';
|
|
2
|
-
export const getNativeFeeAssetReference = (chainNamespace, chainReference) => {
|
|
3
|
-
return (() => {
|
|
4
|
-
switch (chainNamespace) {
|
|
5
|
-
case CHAIN_NAMESPACE.Utxo:
|
|
6
|
-
switch (chainReference) {
|
|
7
|
-
case CHAIN_REFERENCE.BitcoinMainnet:
|
|
8
|
-
return ASSET_REFERENCE.Bitcoin;
|
|
9
|
-
case CHAIN_REFERENCE.BitcoinCashMainnet:
|
|
10
|
-
return ASSET_REFERENCE.BitcoinCash;
|
|
11
|
-
case CHAIN_REFERENCE.DogecoinMainnet:
|
|
12
|
-
return ASSET_REFERENCE.Dogecoin;
|
|
13
|
-
case CHAIN_REFERENCE.LitecoinMainnet:
|
|
14
|
-
return ASSET_REFERENCE.Litecoin;
|
|
15
|
-
default:
|
|
16
|
-
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
17
|
-
}
|
|
18
|
-
case CHAIN_NAMESPACE.Evm:
|
|
19
|
-
switch (chainReference) {
|
|
20
|
-
case CHAIN_REFERENCE.AvalancheCChain:
|
|
21
|
-
return ASSET_REFERENCE.AvalancheC;
|
|
22
|
-
case CHAIN_REFERENCE.EthereumMainnet:
|
|
23
|
-
return ASSET_REFERENCE.Ethereum;
|
|
24
|
-
case CHAIN_REFERENCE.OptimismMainnet:
|
|
25
|
-
return ASSET_REFERENCE.Optimism;
|
|
26
|
-
case CHAIN_REFERENCE.BnbSmartChainMainnet:
|
|
27
|
-
return ASSET_REFERENCE.BnbSmartChain;
|
|
28
|
-
case CHAIN_REFERENCE.PolygonMainnet:
|
|
29
|
-
return ASSET_REFERENCE.Polygon;
|
|
30
|
-
case CHAIN_REFERENCE.GnosisMainnet:
|
|
31
|
-
return ASSET_REFERENCE.Gnosis;
|
|
32
|
-
case CHAIN_REFERENCE.ArbitrumMainnet:
|
|
33
|
-
return ASSET_REFERENCE.Arbitrum;
|
|
34
|
-
case CHAIN_REFERENCE.ArbitrumNovaMainnet:
|
|
35
|
-
return ASSET_REFERENCE.ArbitrumNova;
|
|
36
|
-
case CHAIN_REFERENCE.BaseMainnet:
|
|
37
|
-
return ASSET_REFERENCE.Base;
|
|
38
|
-
default:
|
|
39
|
-
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
40
|
-
}
|
|
41
|
-
case CHAIN_NAMESPACE.CosmosSdk:
|
|
42
|
-
switch (chainReference) {
|
|
43
|
-
case CHAIN_REFERENCE.CosmosHubMainnet:
|
|
44
|
-
return ASSET_REFERENCE.Cosmos;
|
|
45
|
-
case CHAIN_REFERENCE.ThorchainMainnet:
|
|
46
|
-
return ASSET_REFERENCE.Thorchain;
|
|
47
|
-
case CHAIN_REFERENCE.MayachainMainnet:
|
|
48
|
-
return ASSET_REFERENCE.Mayachain;
|
|
49
|
-
default:
|
|
50
|
-
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
51
|
-
}
|
|
52
|
-
case CHAIN_NAMESPACE.Solana:
|
|
53
|
-
switch (chainReference) {
|
|
54
|
-
case CHAIN_REFERENCE.SolanaMainnet:
|
|
55
|
-
return ASSET_REFERENCE.Solana;
|
|
56
|
-
default:
|
|
57
|
-
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
58
|
-
}
|
|
59
|
-
default:
|
|
60
|
-
throw new Error(`Chain namespace ${chainNamespace} on ${chainReference} not supported.`);
|
|
61
|
-
}
|
|
62
|
-
})();
|
|
63
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { HistoryTimeframe } from '@shapeshiftoss/types';
|
|
2
|
-
import dayjs from 'dayjs';
|
|
3
|
-
import { assertUnreachable } from './assertUnreachable.js';
|
|
4
|
-
export const getHistoryTimeframeBounds = (timeframe, end = dayjs().endOf('day')) => {
|
|
5
|
-
let start = end; // Shut up typescript.
|
|
6
|
-
switch (timeframe) {
|
|
7
|
-
case HistoryTimeframe.HOUR:
|
|
8
|
-
start = end.subtract(1, 'day');
|
|
9
|
-
break;
|
|
10
|
-
case HistoryTimeframe.DAY:
|
|
11
|
-
start = end.subtract(1, 'day');
|
|
12
|
-
break;
|
|
13
|
-
case HistoryTimeframe.WEEK:
|
|
14
|
-
start = end.subtract(1, 'week');
|
|
15
|
-
break;
|
|
16
|
-
case HistoryTimeframe.MONTH:
|
|
17
|
-
start = end.subtract(1, 'month');
|
|
18
|
-
break;
|
|
19
|
-
case HistoryTimeframe.YEAR:
|
|
20
|
-
start = end.subtract(1, 'year');
|
|
21
|
-
break;
|
|
22
|
-
case HistoryTimeframe.ALL:
|
|
23
|
-
start = end.subtract(5, 'years');
|
|
24
|
-
break;
|
|
25
|
-
default:
|
|
26
|
-
assertUnreachable(timeframe);
|
|
27
|
-
}
|
|
28
|
-
return { start, end };
|
|
29
|
-
};
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
export * from './makeAsset/makeAsset.js';
|
|
3
|
-
export * from './assertUnreachable.js';
|
|
4
|
-
export * from './sha256.js';
|
|
5
|
-
export * from './chainIdToFeeAsset.js';
|
|
6
|
-
export * from './chainIdToFeeAssetId.js';
|
|
7
|
-
export * from './utxo.js';
|
|
8
|
-
export * from './bignumber/bignumber.js';
|
|
9
|
-
export * from './basisPoints.js';
|
|
10
|
-
export * from './baseUnits/baseUnits.js';
|
|
11
|
-
export * from './promises.js';
|
|
12
|
-
export * from './treasury.js';
|
|
13
|
-
export * from './timeout.js';
|
|
14
|
-
export * from './createThrottle.js';
|
|
15
|
-
export * from './historyTimeframe.js';
|
|
16
|
-
export * from './getNativeFeeAssetReference.js';
|
|
17
|
-
export * from './assetData/index.js';
|
|
18
|
-
export * from './unfreeze.js';
|
|
19
|
-
export * from './getAssetNamespaceFromChainId.js';
|
|
20
|
-
export * from './getChainShortName.js';
|
|
21
|
-
export declare const isSome: <T>(option: T | null | undefined) => option is T;
|
|
22
|
-
export declare const isToken: (assetId: AssetId) => boolean;
|
|
23
|
-
export declare const isSplToken: (assetId: AssetId) => boolean;
|
|
24
|
-
export declare const contractAddressOrUndefined: (assetId: AssetId) => import("@shapeshiftoss/caip").AssetReference | undefined;
|
package/dist/esm/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ASSET_NAMESPACE, fromAssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { isNull, isUndefined } from 'lodash';
|
|
3
|
-
export * from './makeAsset/makeAsset.js';
|
|
4
|
-
export * from './assertUnreachable.js';
|
|
5
|
-
export * from './sha256.js';
|
|
6
|
-
export * from './chainIdToFeeAsset.js';
|
|
7
|
-
export * from './chainIdToFeeAssetId.js';
|
|
8
|
-
export * from './utxo.js';
|
|
9
|
-
export * from './bignumber/bignumber.js';
|
|
10
|
-
export * from './basisPoints.js';
|
|
11
|
-
export * from './baseUnits/baseUnits.js';
|
|
12
|
-
export * from './promises.js';
|
|
13
|
-
export * from './treasury.js';
|
|
14
|
-
export * from './timeout.js';
|
|
15
|
-
export * from './createThrottle.js';
|
|
16
|
-
export * from './historyTimeframe.js';
|
|
17
|
-
export * from './getNativeFeeAssetReference.js';
|
|
18
|
-
export * from './assetData/index.js';
|
|
19
|
-
export * from './unfreeze.js';
|
|
20
|
-
export * from './getAssetNamespaceFromChainId.js';
|
|
21
|
-
export * from './getChainShortName.js';
|
|
22
|
-
export const isSome = (option) => !isUndefined(option) && !isNull(option);
|
|
23
|
-
export const isToken = (assetId) => {
|
|
24
|
-
switch (fromAssetId(assetId).assetNamespace) {
|
|
25
|
-
case ASSET_NAMESPACE.erc20:
|
|
26
|
-
case ASSET_NAMESPACE.erc721:
|
|
27
|
-
case ASSET_NAMESPACE.erc1155:
|
|
28
|
-
case ASSET_NAMESPACE.splToken:
|
|
29
|
-
return true;
|
|
30
|
-
default:
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
export const isSplToken = (assetId) => {
|
|
35
|
-
return fromAssetId(assetId).assetNamespace === ASSET_NAMESPACE.splToken;
|
|
36
|
-
};
|
|
37
|
-
export const contractAddressOrUndefined = (assetId) => isToken(assetId) || isSplToken(assetId) ? fromAssetId(assetId).assetReference : undefined;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { AssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import type { Asset } from '@shapeshiftoss/types';
|
|
3
|
-
export type MinimalAsset = Partial<Asset> & Pick<Asset, 'assetId' | 'symbol' | 'name' | 'precision'>;
|
|
4
|
-
/**
|
|
5
|
-
* utility to create an asset from minimal asset data from external sources at runtime
|
|
6
|
-
* e.g. zapper/zerion/etherscan
|
|
7
|
-
* required fields are assetId, symbol, name, precision
|
|
8
|
-
* the rest can be inferred from existing data
|
|
9
|
-
*/
|
|
10
|
-
export declare const makeAsset: (assetsById: Partial<Record<AssetId, Asset>>, minimalAsset: MinimalAsset) => Asset;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { fromAssetId } from '@shapeshiftoss/caip';
|
|
2
|
-
import { chainIdToFeeAsset } from '../chainIdToFeeAsset.js';
|
|
3
|
-
import { sha256 } from '../sha256.js';
|
|
4
|
-
/**
|
|
5
|
-
* utility to create an asset from minimal asset data from external sources at runtime
|
|
6
|
-
* e.g. zapper/zerion/etherscan
|
|
7
|
-
* required fields are assetId, symbol, name, precision
|
|
8
|
-
* the rest can be inferred from existing data
|
|
9
|
-
*/
|
|
10
|
-
export const makeAsset = (assetsById, minimalAsset) => {
|
|
11
|
-
const { assetId } = minimalAsset;
|
|
12
|
-
const color = (() => {
|
|
13
|
-
if (minimalAsset.color)
|
|
14
|
-
return minimalAsset.color;
|
|
15
|
-
const shaAssetId = sha256(assetId);
|
|
16
|
-
return `#${shaAssetId.slice(0, 6)}`;
|
|
17
|
-
})();
|
|
18
|
-
const chainId = (() => {
|
|
19
|
-
if (minimalAsset.chainId)
|
|
20
|
-
return minimalAsset.chainId;
|
|
21
|
-
return fromAssetId(assetId).chainId;
|
|
22
|
-
})();
|
|
23
|
-
// currently, dynamic assets are LP pairs, and they have two icon urls and are rendered differently
|
|
24
|
-
const icon = minimalAsset?.icon ?? '';
|
|
25
|
-
const explorerLinks = (() => {
|
|
26
|
-
const feeAsset = chainIdToFeeAsset(assetsById, chainId);
|
|
27
|
-
return {
|
|
28
|
-
explorer: feeAsset?.explorer ?? '',
|
|
29
|
-
explorerTxLink: feeAsset?.explorerTxLink ?? '',
|
|
30
|
-
explorerAddressLink: feeAsset?.explorerAddressLink ?? '',
|
|
31
|
-
};
|
|
32
|
-
})();
|
|
33
|
-
return Object.assign({}, minimalAsset, explorerLinks, {
|
|
34
|
-
chainId,
|
|
35
|
-
color,
|
|
36
|
-
icon,
|
|
37
|
-
relatedAssetKey: null,
|
|
38
|
-
isPrimary: true,
|
|
39
|
-
isChainSpecific: true,
|
|
40
|
-
});
|
|
41
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|