@swapkit/helpers 1.0.0-rc.71 → 1.0.0-rc.72

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.
@@ -1,8 +1,8 @@
1
- import { RequestClient } from '@swapkit/api';
2
- import type { EVMChain } from '@swapkit/types';
3
- import { BaseDecimal, Chain, ChainToRPC, EVMChainList, FeeOption } from '@swapkit/types';
1
+ import { RequestClient } from "@swapkit/api";
2
+ import type { EVMChain } from "@swapkit/types";
3
+ import { BaseDecimal, Chain, ChainToRPC, EVMChains, FeeOption } from "@swapkit/types";
4
4
 
5
- const getDecimalMethodHex = '0x313ce567';
5
+ const getDecimalMethodHex = "0x313ce567";
6
6
 
7
7
  export type CommonAssetString =
8
8
  | `${Chain.Maya}.MAYA`
@@ -15,15 +15,15 @@ const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string
15
15
  try {
16
16
  const { result } = await RequestClient.post<{ result: string }>(ChainToRPC[chain], {
17
17
  headers: {
18
- accept: '*/*',
19
- 'content-type': 'application/json',
20
- 'cache-control': 'no-cache',
18
+ accept: "*/*",
19
+ "content-type": "application/json",
20
+ "cache-control": "no-cache",
21
21
  },
22
22
  body: JSON.stringify({
23
23
  id: 44,
24
- jsonrpc: '2.0',
25
- method: 'eth_call',
26
- params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
24
+ jsonrpc: "2.0",
25
+ method: "eth_call",
26
+ params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, "latest"],
27
27
  }),
28
28
  });
29
29
 
@@ -34,30 +34,30 @@ const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string
34
34
  }
35
35
  };
36
36
 
37
- const getETHAssetDecimal = async (symbol: string) => {
37
+ const getETHAssetDecimal = (symbol: string) => {
38
38
  if (symbol === Chain.Ethereum) return BaseDecimal.ETH;
39
- const [, address] = symbol.split('-');
39
+ const [, address] = symbol.split("-");
40
40
 
41
- return address?.startsWith('0x')
41
+ return address?.startsWith("0x")
42
42
  ? getContractDecimals({ chain: Chain.Ethereum, to: address })
43
43
  : BaseDecimal.ETH;
44
44
  };
45
45
 
46
- const getAVAXAssetDecimal = async (symbol: string) => {
47
- const [, address] = symbol.split('-');
46
+ const getAVAXAssetDecimal = (symbol: string) => {
47
+ const [, address] = symbol.split("-");
48
48
 
49
- return address?.startsWith('0x')
49
+ return address?.startsWith("0x")
50
50
  ? getContractDecimals({ chain: Chain.Avalanche, to: address.toLowerCase() })
51
51
  : BaseDecimal.AVAX;
52
52
  };
53
53
 
54
- const getBSCAssetDecimal = async (symbol: string) => {
54
+ const getBSCAssetDecimal = (symbol: string) => {
55
55
  if (symbol === Chain.BinanceSmartChain) return BaseDecimal.BSC;
56
56
 
57
57
  return BaseDecimal.BSC;
58
58
  };
59
59
 
60
- export const getDecimal = async ({ chain, symbol }: { chain: Chain; symbol: string }) => {
60
+ export const getDecimal = ({ chain, symbol }: { chain: Chain; symbol: string }) => {
61
61
  switch (chain) {
62
62
  case Chain.Ethereum:
63
63
  return getETHAssetDecimal(symbol);
@@ -80,19 +80,19 @@ export const isGasAsset = ({ chain, symbol }: { chain: Chain; symbol: string })
80
80
  switch (chain) {
81
81
  case Chain.Arbitrum:
82
82
  case Chain.Optimism:
83
- return symbol === 'ETH';
83
+ return symbol === "ETH";
84
84
  case Chain.Maya:
85
- return symbol === 'CACAO';
85
+ return symbol === "CACAO";
86
86
  case Chain.Kujira:
87
- return symbol === 'KUJI';
87
+ return symbol === "KUJI";
88
88
  case Chain.Cosmos:
89
- return symbol === 'ATOM';
89
+ return symbol === "ATOM";
90
90
  case Chain.Polygon:
91
- return symbol === 'MATIC';
91
+ return symbol === "MATIC";
92
92
  case Chain.BinanceSmartChain:
93
- return symbol === 'BNB';
93
+ return symbol === "BNB";
94
94
  case Chain.THORChain:
95
- return symbol === 'RUNE';
95
+ return symbol === "RUNE";
96
96
 
97
97
  default:
98
98
  return symbol === chain;
@@ -104,20 +104,20 @@ export const getCommonAssetInfo = (
104
104
  ): { identifier: string; decimal: number } => {
105
105
  switch (assetString) {
106
106
  case `${Chain.Ethereum}.THOR`:
107
- return { identifier: 'ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044', decimal: 18 };
107
+ return { identifier: "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044", decimal: 18 };
108
108
  case `${Chain.Ethereum}.vTHOR`:
109
- return { identifier: 'ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d', decimal: 18 };
109
+ return { identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", decimal: 18 };
110
110
 
111
111
  case Chain.Cosmos:
112
- return { identifier: 'GAIA.ATOM', decimal: BaseDecimal[assetString] };
112
+ return { identifier: "GAIA.ATOM", decimal: BaseDecimal[assetString] };
113
113
  case Chain.THORChain:
114
- return { identifier: 'THOR.RUNE', decimal: BaseDecimal[assetString] };
114
+ return { identifier: "THOR.RUNE", decimal: BaseDecimal[assetString] };
115
115
  case Chain.BinanceSmartChain:
116
- return { identifier: 'BSC.BNB', decimal: BaseDecimal[assetString] };
116
+ return { identifier: "BSC.BNB", decimal: BaseDecimal[assetString] };
117
117
  case Chain.Maya:
118
- return { identifier: 'MAYA.CACAO', decimal: BaseDecimal.MAYA };
118
+ return { identifier: "MAYA.CACAO", decimal: BaseDecimal.MAYA };
119
119
  case `${Chain.Maya}.MAYA`:
120
- return { identifier: 'MAYA.MAYA', decimal: 4 };
120
+ return { identifier: "MAYA.MAYA", decimal: 4 };
121
121
 
122
122
  case `${Chain.Kujira}.USK`:
123
123
  return { identifier: `${Chain.Kujira}.USK`, decimal: 6 };
@@ -127,53 +127,54 @@ export const getCommonAssetInfo = (
127
127
  }
128
128
  };
129
129
 
130
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: Refactor
130
131
  export const getAssetType = ({ chain, symbol }: { chain: Chain; symbol: string }) => {
131
- if (symbol.includes('/')) return 'Synth';
132
+ if (symbol.includes("/")) return "Synth";
132
133
 
133
134
  switch (chain) {
134
135
  case Chain.Cosmos:
135
- return symbol === 'ATOM' ? 'Native' : Chain.Cosmos;
136
+ return symbol === "ATOM" ? "Native" : Chain.Cosmos;
136
137
  case Chain.Kujira:
137
- return symbol === Chain.Kujira ? 'Native' : Chain.Kujira;
138
+ return symbol === Chain.Kujira ? "Native" : Chain.Kujira;
138
139
  case Chain.Binance:
139
- return symbol === Chain.Binance ? 'Native' : 'BEP2';
140
+ return symbol === Chain.Binance ? "Native" : "BEP2";
140
141
  case Chain.BinanceSmartChain:
141
- return symbol === Chain.Binance ? 'Native' : 'BEP20';
142
+ return symbol === Chain.Binance ? "Native" : "BEP20";
142
143
  case Chain.Ethereum:
143
- return symbol === Chain.Ethereum ? 'Native' : 'ERC20';
144
+ return symbol === Chain.Ethereum ? "Native" : "ERC20";
144
145
  case Chain.Avalanche:
145
- return symbol === Chain.Avalanche ? 'Native' : Chain.Avalanche;
146
+ return symbol === Chain.Avalanche ? "Native" : Chain.Avalanche;
146
147
  case Chain.Polygon:
147
- return symbol === Chain.Polygon ? 'Native' : 'POLYGON';
148
+ return symbol === Chain.Polygon ? "Native" : "POLYGON";
148
149
 
149
150
  case Chain.Arbitrum:
150
- return [Chain.Ethereum, Chain.Arbitrum].includes(symbol as Chain) ? 'Native' : 'ARBITRUM';
151
+ return [Chain.Ethereum, Chain.Arbitrum].includes(symbol as Chain) ? "Native" : "ARBITRUM";
151
152
  case Chain.Optimism:
152
- return [Chain.Ethereum, Chain.Optimism].includes(symbol as Chain) ? 'Native' : 'OPTIMISM';
153
+ return [Chain.Ethereum, Chain.Optimism].includes(symbol as Chain) ? "Native" : "OPTIMISM";
153
154
 
154
155
  default:
155
- return 'Native';
156
+ return "Native";
156
157
  }
157
158
  };
158
159
 
159
160
  export const assetFromString = (assetString: string) => {
160
- const [chain, ...symbolArray] = assetString.split('.') as [Chain, ...(string | undefined)[]];
161
- const synth = assetString.includes('/');
162
- const symbol = symbolArray.join('.');
163
- const ticker = symbol?.split('-')?.[0];
161
+ const [chain, ...symbolArray] = assetString.split(".") as [Chain, ...(string | undefined)[]];
162
+ const synth = assetString.includes("/");
163
+ const symbol = symbolArray.join(".");
164
+ const ticker = symbol?.split("-")?.[0];
164
165
 
165
166
  return { chain, symbol, ticker, synth };
166
167
  };
167
168
 
168
169
  const potentialScamRegex = new RegExp(
169
170
  /(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/,
170
- 'gmi',
171
+ "gmi",
171
172
  );
172
173
 
173
174
  const evmAssetHasAddress = (assetString: string) => {
174
- const [chain, symbol] = assetString.split('.') as [EVMChain, string];
175
- if (!EVMChainList.includes(chain as EVMChain)) return true;
176
- const [, address] = symbol.split('-') as [string, string?];
175
+ const [chain, symbol] = assetString.split(".") as [EVMChain, string];
176
+ if (!EVMChains.includes(chain as EVMChain)) return true;
177
+ const [, address] = symbol.split("-") as [string, string?];
177
178
 
178
179
  return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
179
180
  };
@@ -190,6 +191,6 @@ export const filterAssets = (
190
191
  const assetString = `${chain}.${symbol}`;
191
192
 
192
193
  return (
193
- !potentialScamRegex.test(assetString) && evmAssetHasAddress(assetString) && value !== '0'
194
+ !potentialScamRegex.test(assetString) && evmAssetHasAddress(assetString) && value !== "0"
194
195
  );
195
196
  });
@@ -1,13 +1,13 @@
1
- import { BaseDecimal } from '@swapkit/types';
1
+ import { BaseDecimal } from "@swapkit/types";
2
2
 
3
- import { SwapKitNumber } from '../index.ts';
3
+ import { SwapKitNumber } from "../index.ts";
4
4
 
5
- type ShareParams<T = {}> = T & {
5
+ type ShareParams<T extends {}> = T & {
6
6
  liquidityUnits: string;
7
7
  poolUnits: string;
8
8
  };
9
9
 
10
- type PoolParams<T = {}> = T & {
10
+ type PoolParams = {
11
11
  runeAmount: string;
12
12
  assetAmount: string;
13
13
  runeDepth: string;
@@ -148,14 +148,14 @@ export function getEstimatedPoolShare({
148
148
  const liquidityUnitsAfterAdd = numerator.div(denominator);
149
149
  const estimatedLiquidityUnits = toTCSwapKitNumber(liquidityUnits).add(liquidityUnitsAfterAdd);
150
150
 
151
- if (liquidityUnitsAfterAdd.getBaseValue('number') === 0) {
152
- return estimatedLiquidityUnits.div(P).getBaseValue('number');
151
+ if (liquidityUnitsAfterAdd.getBaseValue("number") === 0) {
152
+ return estimatedLiquidityUnits.div(P).getBaseValue("number");
153
153
  }
154
154
 
155
155
  // get pool units after add
156
156
  const newPoolUnits = P.add(estimatedLiquidityUnits);
157
157
 
158
- return estimatedLiquidityUnits.div(newPoolUnits).getBaseValue('number');
158
+ return estimatedLiquidityUnits.div(newPoolUnits).getBaseValue("number");
159
159
  }
160
160
 
161
161
  export function getLiquiditySlippage({
@@ -164,7 +164,7 @@ export function getLiquiditySlippage({
164
164
  runeDepth,
165
165
  assetDepth,
166
166
  }: PoolParams) {
167
- if (runeAmount === '0' || assetAmount === '0' || runeDepth === '0' || assetDepth === '0')
167
+ if (runeAmount === "0" || assetAmount === "0" || runeDepth === "0" || assetDepth === "0")
168
168
  return 0;
169
169
  // formula: (t * R - T * r)/ (T*r + R*T)
170
170
  const R = toTCSwapKitNumber(runeDepth);
@@ -176,5 +176,5 @@ export function getLiquiditySlippage({
176
176
  const denominator = T.mul(runeAddAmount).add(R.mul(T));
177
177
 
178
178
  // set absolute value of percent, no negative allowed
179
- return Math.abs(numerator.div(denominator).getBaseValue('number'));
179
+ return Math.abs(numerator.div(denominator).getBaseValue("number"));
180
180
  }
@@ -1,4 +1,4 @@
1
- import { Chain, MemoType } from '@swapkit/types';
1
+ import { Chain, MemoType } from "@swapkit/types";
2
2
 
3
3
  export type ThornameRegisterParam = {
4
4
  name: string;
@@ -9,15 +9,14 @@ export type ThornameRegisterParam = {
9
9
  expiryBlock?: string;
10
10
  };
11
11
 
12
- type WithAddress<T = {}> = T & { address: string };
13
- type WithChain<T = {}> = T & { chain: Chain };
12
+ type WithChain<T extends {}> = T & { chain: Chain };
14
13
 
15
14
  export type MemoOptions<T extends MemoType> = {
16
- [MemoType.BOND]: WithAddress;
17
- [MemoType.LEAVE]: WithAddress;
18
- [MemoType.CLOSE_LOAN]: WithAddress<{ asset: string; minAmount?: string }>;
19
- [MemoType.OPEN_LOAN]: WithAddress<{ asset: string; minAmount?: string }>;
20
- [MemoType.UNBOND]: WithAddress<{ unbondAmount: number }>;
15
+ [MemoType.BOND]: { address: string };
16
+ [MemoType.LEAVE]: { address: string };
17
+ [MemoType.CLOSE_LOAN]: { address: string; asset: string; minAmount?: string };
18
+ [MemoType.OPEN_LOAN]: { address: string; asset: string; minAmount?: string };
19
+ [MemoType.UNBOND]: { address: string; unbondAmount: number };
21
20
  [MemoType.DEPOSIT]: WithChain<{ symbol: string; address?: string; singleSide?: boolean }>;
22
21
  [MemoType.WITHDRAW]: WithChain<{
23
22
  ticker: string;
@@ -26,7 +25,7 @@ export type MemoOptions<T extends MemoType> = {
26
25
  targetAssetString?: string;
27
26
  singleSide?: boolean;
28
27
  }>;
29
- [MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, 'preferredAsset' | 'expiryBlock'>;
28
+ [MemoType.THORNAME_REGISTER]: Omit<ThornameRegisterParam, "preferredAsset" | "expiryBlock">;
30
29
  }[T];
31
30
 
32
31
  export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions<T>) => {
@@ -44,7 +43,7 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
44
43
 
45
44
  case MemoType.THORNAME_REGISTER: {
46
45
  const { name, chain, address, owner } = options as MemoOptions<MemoType.THORNAME_REGISTER>;
47
- return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ''}`;
46
+ return `${memoType}:${name}:${chain}:${address}${owner ? `:${owner}` : ""}`;
48
47
  }
49
48
 
50
49
  case MemoType.DEPOSIT: {
@@ -53,11 +52,11 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
53
52
  const getPoolIdentifier = (chain: Chain, symbol: string): string => {
54
53
  switch (chain) {
55
54
  case Chain.Litecoin:
56
- return 'l';
55
+ return "l";
57
56
  case Chain.Dogecoin:
58
- return 'd';
57
+ return "d";
59
58
  case Chain.BitcoinCash:
60
- return 'c';
59
+ return "c";
61
60
  default:
62
61
  return `${chain}.${symbol}`;
63
62
  }
@@ -65,7 +64,7 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
65
64
 
66
65
  return singleSide
67
66
  ? `${memoType}:${chain}/${symbol}`
68
- : `${memoType}:${getPoolIdentifier(chain, symbol)}:${address || ''}`;
67
+ : `${memoType}:${getPoolIdentifier(chain, symbol)}:${address || ""}`;
69
68
  }
70
69
 
71
70
  case MemoType.WITHDRAW: {
@@ -73,9 +72,9 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
73
72
  options as MemoOptions<MemoType.WITHDRAW>;
74
73
 
75
74
  const shortenedSymbol =
76
- chain === 'ETH' && ticker !== 'ETH' ? `${ticker}-${symbol.slice(-3)}` : symbol;
77
- const target = !singleSide && targetAssetString ? `:${targetAssetString}` : '';
78
- const assetDivider = singleSide ? '/' : '.';
75
+ chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
76
+ const target = !singleSide && targetAssetString ? `:${targetAssetString}` : "";
77
+ const assetDivider = singleSide ? "/" : ".";
79
78
 
80
79
  return `${memoType}:${chain}${assetDivider}${shortenedSymbol}:${basisPoints}${target}`;
81
80
  }
@@ -88,6 +87,6 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
88
87
  }
89
88
 
90
89
  default:
91
- return '';
90
+ return "";
92
91
  }
93
92
  };
@@ -1,7 +1,7 @@
1
1
  // 10 rune for register, 1 rune per year
2
2
  // MINIMUM_REGISTRATION_FEE = 11
3
3
  export function getTHORNameCost(year: number) {
4
- if (year < 0) throw new Error('Invalid number of year');
4
+ if (year < 0) throw new Error("Invalid number of year");
5
5
  return 10 + year;
6
6
  }
7
7
 
@@ -14,7 +14,7 @@ export function validateTHORName(name: string) {
14
14
  }
15
15
 
16
16
  export function derivationPathToString([network, chainId, account, change, index]: number[]) {
17
- const shortPath = typeof index !== 'number';
17
+ const shortPath = typeof index !== "number";
18
18
 
19
- return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
19
+ return `${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
20
20
  }
@@ -1,15 +1,15 @@
1
- import { Chain } from '@swapkit/types';
1
+ import { Chain } from "@swapkit/types";
2
2
 
3
3
  // Backward compatibility
4
- const supportedChains = [...Object.values(Chain), 'TERRA'];
4
+ const supportedChains = [...Object.values(Chain), "TERRA"];
5
5
 
6
- export function validateIdentifier(identifier = '') {
6
+ export function validateIdentifier(identifier = "") {
7
7
  const uppercasedIdentifier = identifier.toUpperCase();
8
8
 
9
- const [chain] = uppercasedIdentifier.split('.') as [Chain, string];
9
+ const [chain] = uppercasedIdentifier.split(".") as [Chain, string];
10
10
  if (supportedChains.includes(chain)) return true;
11
11
 
12
- const [synthChain] = uppercasedIdentifier.split('/') as [Chain, string];
12
+ const [synthChain] = uppercasedIdentifier.split("/") as [Chain, string];
13
13
  if (supportedChains.includes(synthChain)) return true;
14
14
 
15
15
  throw new Error(
package/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- export * from '@swapkit/api';
1
+ export * from "@swapkit/api";
2
2
 
3
3
  /**
4
4
  * Helpers
5
5
  */
6
- export * from './helpers/asset.ts';
7
- export * from './helpers/liquidity.ts';
8
- export * from './helpers/memo.ts';
9
- export * from './helpers/others.ts';
6
+ export * from "./helpers/asset.ts";
7
+ export * from "./helpers/liquidity.ts";
8
+ export * from "./helpers/memo.ts";
9
+ export * from "./helpers/others.ts";
10
10
 
11
11
  /**
12
12
  * Modules
13
13
  */
14
- export * from './modules/assetValue.ts';
15
- export * from './modules/bigIntArithmetics.ts';
16
- export * from './modules/swapKitError.ts';
17
- export * from './modules/swapKitNumber.ts';
14
+ export * from "./modules/assetValue.ts";
15
+ export * from "./modules/bigIntArithmetics.ts";
16
+ export * from "./modules/swapKitError.ts";
17
+ export * from "./modules/swapKitNumber.ts";