@swapkit/helpers 4.0.0-beta.32 → 4.0.0-beta.34

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.
Files changed (251) hide show
  1. package/dist/api/index.js +2 -2
  2. package/dist/api/index.js.map +1 -1
  3. package/dist/{chunk-ganzk321.js → chunk-hm14bfyg.js} +3 -3
  4. package/dist/{chunk-fazw0jvt.js.map → chunk-hm14bfyg.js.map} +1 -1
  5. package/dist/chunk-vb4wtm2w.js +4 -0
  6. package/dist/{chunk-ganzk321.js.map → chunk-vb4wtm2w.js.map} +1 -1
  7. package/dist/contracts.cjs +4 -0
  8. package/dist/contracts.cjs.map +10 -0
  9. package/dist/contracts.js +4 -0
  10. package/dist/contracts.js.map +10 -0
  11. package/dist/index.cjs +3 -3
  12. package/dist/index.cjs.map +11 -11
  13. package/dist/index.js +3 -3
  14. package/dist/index.js.map +11 -11
  15. package/dist/tokens.cjs +4 -0
  16. package/dist/tokens.cjs.map +10 -0
  17. package/dist/tokens.js +4 -0
  18. package/dist/tokens.js.map +10 -0
  19. package/dist/types/api/index.d.ts +238 -0
  20. package/dist/types/api/index.d.ts.map +1 -0
  21. package/dist/types/api/microgard/endpoints.d.ts +25 -0
  22. package/dist/types/api/microgard/endpoints.d.ts.map +1 -0
  23. package/dist/types/api/microgard/types.d.ts +56 -0
  24. package/dist/types/api/microgard/types.d.ts.map +1 -0
  25. package/dist/types/api/midgard/endpoints.d.ts +35 -0
  26. package/dist/types/api/midgard/endpoints.d.ts.map +1 -0
  27. package/dist/types/api/midgard/types.d.ts +102 -0
  28. package/dist/types/api/midgard/types.d.ts.map +1 -0
  29. package/dist/types/api/swapkitApi/endpoints.d.ts +194 -0
  30. package/dist/types/api/swapkitApi/endpoints.d.ts.map +1 -0
  31. package/dist/types/api/swapkitApi/types.d.ts +7238 -0
  32. package/dist/types/api/swapkitApi/types.d.ts.map +1 -0
  33. package/dist/types/api/thornode/endpoints.d.ts +31 -0
  34. package/dist/types/api/thornode/endpoints.d.ts.map +1 -0
  35. package/dist/types/api/thornode/types.d.ts +264 -0
  36. package/dist/types/api/thornode/types.d.ts.map +1 -0
  37. package/dist/types/api/thorswapStatic/endpoints.d.ts +7 -0
  38. package/dist/types/api/thorswapStatic/endpoints.d.ts.map +1 -0
  39. package/dist/types/api/thorswapStatic/types.d.ts +22 -0
  40. package/dist/types/api/thorswapStatic/types.d.ts.map +1 -0
  41. package/dist/types/contracts.d.ts +2 -0
  42. package/dist/types/contracts.d.ts.map +1 -0
  43. package/{src/index.ts → dist/types/index.d.ts} +3 -4
  44. package/dist/types/index.d.ts.map +1 -0
  45. package/dist/types/modules/assetValue.d.ts +71 -0
  46. package/dist/types/modules/assetValue.d.ts.map +1 -0
  47. package/dist/types/modules/bigIntArithmetics.d.ts +56 -0
  48. package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -0
  49. package/dist/types/modules/feeMultiplier.d.ts +45 -0
  50. package/dist/types/modules/feeMultiplier.d.ts.map +1 -0
  51. package/dist/types/modules/requestClient.d.ts +22 -0
  52. package/dist/types/modules/requestClient.d.ts.map +1 -0
  53. package/dist/types/modules/swapKitConfig.d.ts +98 -0
  54. package/dist/types/modules/swapKitConfig.d.ts.map +1 -0
  55. package/dist/types/modules/swapKitError.d.ts +829 -0
  56. package/dist/types/modules/swapKitError.d.ts.map +1 -0
  57. package/dist/types/modules/swapKitNumber.d.ts +7 -0
  58. package/dist/types/modules/swapKitNumber.d.ts.map +1 -0
  59. package/dist/types/tokens.d.ts +2 -0
  60. package/dist/types/tokens.d.ts.map +1 -0
  61. package/dist/types/types/chains.d.ts +187 -0
  62. package/dist/types/types/chains.d.ts.map +1 -0
  63. package/dist/types/types/commonTypes.d.ts +37 -0
  64. package/dist/types/types/commonTypes.d.ts.map +1 -0
  65. package/dist/types/types/derivationPath.d.ts +17 -0
  66. package/dist/types/types/derivationPath.d.ts.map +1 -0
  67. package/dist/types/types/errors/apiV1.d.ts +2 -0
  68. package/dist/types/types/errors/apiV1.d.ts.map +1 -0
  69. package/{src/types/index.ts → dist/types/types/index.d.ts} +1 -1
  70. package/dist/types/types/index.d.ts.map +1 -0
  71. package/dist/types/types/quotes.d.ts +170 -0
  72. package/dist/types/types/quotes.d.ts.map +1 -0
  73. package/dist/types/types/radix.d.ts +15 -0
  74. package/dist/types/types/radix.d.ts.map +1 -0
  75. package/dist/types/types/sdk.d.ts +35 -0
  76. package/dist/types/types/sdk.d.ts.map +1 -0
  77. package/dist/types/types/wallet.d.ts +121 -0
  78. package/dist/types/types/wallet.d.ts.map +1 -0
  79. package/dist/types/utils/asset.d.ts +40 -0
  80. package/dist/types/utils/asset.d.ts.map +1 -0
  81. package/dist/types/utils/derivationPath.d.ts +19 -0
  82. package/dist/types/utils/derivationPath.d.ts.map +1 -0
  83. package/dist/types/utils/explorerUrls.d.ts +10 -0
  84. package/dist/types/utils/explorerUrls.d.ts.map +1 -0
  85. package/dist/types/utils/liquidity.d.ts +59 -0
  86. package/dist/types/utils/liquidity.d.ts.map +1 -0
  87. package/dist/types/utils/memo.d.ts +65 -0
  88. package/dist/types/utils/memo.d.ts.map +1 -0
  89. package/dist/types/utils/others.d.ts +9 -0
  90. package/dist/types/utils/others.d.ts.map +1 -0
  91. package/dist/types/utils/validators.d.ts +3 -0
  92. package/dist/types/utils/validators.d.ts.map +1 -0
  93. package/dist/types/utils/wallets.d.ts +32 -0
  94. package/dist/types/utils/wallets.d.ts.map +1 -0
  95. package/package.json +14 -15
  96. package/dist/chunk-06q78xy3.js +0 -4
  97. package/dist/chunk-06q78xy3.js.map +0 -10
  98. package/dist/chunk-0q5vvg5c.js +0 -5
  99. package/dist/chunk-0q5vvg5c.js.map +0 -10
  100. package/dist/chunk-18bxh1n7.js +0 -5
  101. package/dist/chunk-18bxh1n7.js.map +0 -10
  102. package/dist/chunk-3qya5ng9.js +0 -4
  103. package/dist/chunk-3qya5ng9.js.map +0 -10
  104. package/dist/chunk-4yqymkbp.js +0 -5
  105. package/dist/chunk-4yqymkbp.js.map +0 -10
  106. package/dist/chunk-50gpwcq9.js +0 -4
  107. package/dist/chunk-50gpwcq9.js.map +0 -10
  108. package/dist/chunk-6atq9cs7.js +0 -4
  109. package/dist/chunk-6atq9cs7.js.map +0 -10
  110. package/dist/chunk-6m0d5pv4.js +0 -5
  111. package/dist/chunk-6m0d5pv4.js.map +0 -10
  112. package/dist/chunk-8x46g3wq.js +0 -5
  113. package/dist/chunk-8x46g3wq.js.map +0 -10
  114. package/dist/chunk-92sk0g47.js +0 -5
  115. package/dist/chunk-92sk0g47.js.map +0 -10
  116. package/dist/chunk-9f2b3enk.js +0 -5
  117. package/dist/chunk-9f2b3enk.js.map +0 -10
  118. package/dist/chunk-adrafsdk.js +0 -5
  119. package/dist/chunk-adrafsdk.js.map +0 -10
  120. package/dist/chunk-b1hang9z.js +0 -5
  121. package/dist/chunk-b1hang9z.js.map +0 -10
  122. package/dist/chunk-becdmywt.js +0 -4
  123. package/dist/chunk-becdmywt.js.map +0 -10
  124. package/dist/chunk-ehz0t9d1.js +0 -4
  125. package/dist/chunk-ehz0t9d1.js.map +0 -10
  126. package/dist/chunk-fazw0jvt.js +0 -4
  127. package/dist/chunk-fm9ycn0h.js +0 -5
  128. package/dist/chunk-fm9ycn0h.js.map +0 -10
  129. package/dist/chunk-g67e2r57.js +0 -4
  130. package/dist/chunk-g67e2r57.js.map +0 -10
  131. package/dist/chunk-he3vy8gt.js +0 -4
  132. package/dist/chunk-he3vy8gt.js.map +0 -10
  133. package/dist/chunk-jjayk2y5.js +0 -4
  134. package/dist/chunk-jjayk2y5.js.map +0 -10
  135. package/dist/chunk-mmyv2vsv.js +0 -5
  136. package/dist/chunk-mmyv2vsv.js.map +0 -10
  137. package/dist/chunk-phev7aw7.js +0 -4
  138. package/dist/chunk-phev7aw7.js.map +0 -10
  139. package/dist/chunk-rhqk87p2.js +0 -5
  140. package/dist/chunk-rhqk87p2.js.map +0 -10
  141. package/dist/chunk-rnv7fdjk.js +0 -4
  142. package/dist/chunk-rnv7fdjk.js.map +0 -10
  143. package/dist/chunk-rwh98p1c.js +0 -4
  144. package/dist/chunk-rwh98p1c.js.map +0 -10
  145. package/dist/chunk-v9zj2gsj.js +0 -5
  146. package/dist/chunk-v9zj2gsj.js.map +0 -10
  147. package/dist/chunk-vv5bgsn8.js +0 -4
  148. package/dist/chunk-vv5bgsn8.js.map +0 -10
  149. package/dist/chunk-vwh9jfxn.js +0 -5
  150. package/dist/chunk-vwh9jfxn.js.map +0 -10
  151. package/dist/chunk-x0rqy75t.js +0 -4
  152. package/dist/chunk-x0rqy75t.js.map +0 -10
  153. package/dist/contracts/index.cjs +0 -4
  154. package/dist/contracts/index.cjs.map +0 -38
  155. package/dist/contracts/index.js +0 -4
  156. package/dist/contracts/index.js.map +0 -38
  157. package/dist/tokens/index.cjs +0 -4
  158. package/dist/tokens/index.cjs.map +0 -10
  159. package/dist/tokens/index.js +0 -4
  160. package/dist/tokens/index.js.map +0 -10
  161. package/src/api/index.ts +0 -19
  162. package/src/api/microgard/endpoints.ts +0 -83
  163. package/src/api/microgard/types.ts +0 -57
  164. package/src/api/midgard/endpoints.ts +0 -90
  165. package/src/api/midgard/types.ts +0 -106
  166. package/src/api/swapkitApi/endpoints.ts +0 -193
  167. package/src/api/swapkitApi/types.ts +0 -635
  168. package/src/api/thornode/endpoints.ts +0 -116
  169. package/src/api/thornode/types.ts +0 -276
  170. package/src/api/thorswapStatic/endpoints.ts +0 -46
  171. package/src/api/thorswapStatic/types.ts +0 -18
  172. package/src/contracts/abis/avaxGeneric.ts +0 -92
  173. package/src/contracts/abis/avaxWoofi.ts +0 -145
  174. package/src/contracts/abis/bscGeneric.ts +0 -106
  175. package/src/contracts/abis/chainflipGateway.ts +0 -330
  176. package/src/contracts/abis/erc20.ts +0 -99
  177. package/src/contracts/abis/ethGeneric.ts +0 -92
  178. package/src/contracts/abis/mayaEvmVaults.ts +0 -331
  179. package/src/contracts/abis/pancakeV2.ts +0 -145
  180. package/src/contracts/abis/pangolin.ts +0 -120
  181. package/src/contracts/abis/sushiswap.ts +0 -120
  182. package/src/contracts/abis/tcEthVault.ts +0 -650
  183. package/src/contracts/abis/traderJoe.ts +0 -120
  184. package/src/contracts/abis/uniswapV2.ts +0 -120
  185. package/src/contracts/abis/uniswapV2Leg.ts +0 -128
  186. package/src/contracts/abis/uniswapV3_100.ts +0 -128
  187. package/src/contracts/abis/uniswapV3_10000.ts +0 -128
  188. package/src/contracts/abis/uniswapV3_3000.ts +0 -128
  189. package/src/contracts/abis/uniswapV3_500.ts +0 -128
  190. package/src/contracts/index.ts +0 -95
  191. package/src/contracts/routers/index.ts +0 -58
  192. package/src/contracts/routers/kyber.ts +0 -402
  193. package/src/contracts/routers/oneinch.ts +0 -2188
  194. package/src/contracts/routers/pancakeswap.ts +0 -340
  195. package/src/contracts/routers/pangolin.ts +0 -340
  196. package/src/contracts/routers/sushiswap.ts +0 -340
  197. package/src/contracts/routers/traderJoe.ts +0 -340
  198. package/src/contracts/routers/uniswapv2.ts +0 -340
  199. package/src/contracts/routers/uniswapv3.ts +0 -254
  200. package/src/contracts/routers/woofi.ts +0 -171
  201. package/src/modules/__tests__/assetValue.test.ts +0 -877
  202. package/src/modules/__tests__/bigIntArithmetics.test.ts +0 -30
  203. package/src/modules/__tests__/swapKitConfig.test.ts +0 -37
  204. package/src/modules/__tests__/swapKitNumber.test.ts +0 -560
  205. package/src/modules/assetValue.ts +0 -387
  206. package/src/modules/bigIntArithmetics.ts +0 -436
  207. package/src/modules/feeMultiplier.ts +0 -87
  208. package/src/modules/requestClient.ts +0 -83
  209. package/src/modules/swapKitConfig.ts +0 -143
  210. package/src/modules/swapKitError.ts +0 -442
  211. package/src/modules/swapKitNumber.ts +0 -16
  212. package/src/tokens/index.ts +0 -15
  213. package/src/tokens/lists/camelot_v3.ts +0 -16060
  214. package/src/tokens/lists/caviar_v1.ts +0 -1704
  215. package/src/tokens/lists/chainflip.ts +0 -104
  216. package/src/tokens/lists/index.ts +0 -14
  217. package/src/tokens/lists/jupiter.ts +0 -33563
  218. package/src/tokens/lists/mayachain.ts +0 -509
  219. package/src/tokens/lists/near.ts +0 -458
  220. package/src/tokens/lists/oneinch.ts +0 -15006
  221. package/src/tokens/lists/openocean_v2.ts +0 -12434
  222. package/src/tokens/lists/pancakeswap.ts +0 -6136
  223. package/src/tokens/lists/pangolin_v1.ts +0 -75
  224. package/src/tokens/lists/sushiswap_v2.ts +0 -595
  225. package/src/tokens/lists/thorchain.ts +0 -748
  226. package/src/tokens/lists/traderjoe_v2.ts +0 -594
  227. package/src/tokens/lists/uniswap_v2.ts +0 -11484
  228. package/src/tokens/lists/uniswap_v3.ts +0 -19424
  229. package/src/types/chains.ts +0 -555
  230. package/src/types/commonTypes.ts +0 -58
  231. package/src/types/derivationPath.ts +0 -58
  232. package/src/types/errors/apiV1.ts +0 -0
  233. package/src/types/quotes.ts +0 -172
  234. package/src/types/radix.ts +0 -14
  235. package/src/types/sdk.ts +0 -42
  236. package/src/types/tokens.ts +0 -17
  237. package/src/types/wallet.ts +0 -166
  238. package/src/utils/__tests__/asset.test.ts +0 -193
  239. package/src/utils/__tests__/derivationPath.test.ts +0 -16
  240. package/src/utils/__tests__/memo.test.ts +0 -103
  241. package/src/utils/__tests__/others.test.ts +0 -88
  242. package/src/utils/__tests__/validators.test.ts +0 -24
  243. package/src/utils/asset.ts +0 -231
  244. package/src/utils/derivationPath.ts +0 -98
  245. package/src/utils/liquidity.ts +0 -179
  246. package/src/utils/memo.ts +0 -143
  247. package/src/utils/others.ts +0 -86
  248. package/src/utils/plugin.ts +0 -13
  249. package/src/utils/tokens.ts +0 -73
  250. package/src/utils/validators.ts +0 -32
  251. package/src/utils/wallets.ts +0 -276
@@ -1,436 +0,0 @@
1
- import { BaseDecimal } from "../types/chains";
2
- import type { SwapKitNumber } from "./swapKitNumber";
3
-
4
- type NumberPrimitivesType = {
5
- bigint: bigint;
6
- number: number;
7
- string: string;
8
- };
9
- export type NumberPrimitives = bigint | number | string;
10
- type InitialisationValueType = NumberPrimitives | BigIntArithmetics | SwapKitNumber;
11
-
12
- type SKBigIntParams = InitialisationValueType | { decimal?: number; value: number | string };
13
- type AllowedNumberTypes = "bigint" | "number" | "string";
14
-
15
- const DEFAULT_DECIMAL = 8;
16
- const toMultiplier = (decimal: number) => 10n ** BigInt(decimal);
17
- const decimalFromMultiplier = (multiplier: bigint) =>
18
- Math.log10(Number.parseFloat(multiplier.toString()));
19
-
20
- export function formatBigIntToSafeValue({
21
- value,
22
- bigIntDecimal = DEFAULT_DECIMAL,
23
- decimal = DEFAULT_DECIMAL,
24
- }: {
25
- value: bigint;
26
- bigIntDecimal?: number;
27
- decimal?: number;
28
- }) {
29
- if (decimal === 0) return value.toString();
30
- const isNegative = value < 0n;
31
- let valueString = value.toString().substring(isNegative ? 1 : 0);
32
-
33
- const padLength = decimal - (valueString.length - 1);
34
-
35
- if (padLength > 0) {
36
- valueString = "0".repeat(padLength) + valueString;
37
- }
38
-
39
- const decimalIndex = valueString.length - decimal;
40
- let decimalString = valueString.slice(-decimal);
41
-
42
- // Check if we need to round up
43
- if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
44
- // Increment the last decimal place and slice off the rest
45
- decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
46
- } else {
47
- // Just slice off the extra digits
48
- decimalString = decimalString.substring(0, bigIntDecimal);
49
- }
50
-
51
- return `${isNegative ? "-" : ""}${valueString.slice(0, decimalIndex)}.${decimalString}`.replace(
52
- /\.?0*$/,
53
- "",
54
- );
55
- }
56
-
57
- function divideBigIntWithRounding(n: bigint, d: bigint) {
58
- if (d === 0n) {
59
- throw new Error("Cannot divide by zero");
60
- }
61
- const half = d / 2n;
62
- if ((n >= 0n && d >= 0n) || (n < 0n && d < 0n)) {
63
- return (n + half) / d;
64
- }
65
- return (n - half) / d;
66
- }
67
-
68
- export class BigIntArithmetics {
69
- decimalMultiplier: bigint = 10n ** 8n;
70
- bigIntValue = 0n;
71
- decimal?: number;
72
-
73
- static fromBigInt(value: bigint, decimal?: number) {
74
- return new BigIntArithmetics({
75
- decimal,
76
- value: formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal }),
77
- });
78
- }
79
-
80
- static shiftDecimals({
81
- value,
82
- from,
83
- to,
84
- }: {
85
- value: InstanceType<typeof SwapKitNumber>;
86
- from: number;
87
- to: number;
88
- }) {
89
- return BigIntArithmetics.fromBigInt(
90
- (value.getBaseValue("bigint") * toMultiplier(to)) / toMultiplier(from),
91
- to,
92
- );
93
- }
94
-
95
- constructor(params: SKBigIntParams) {
96
- const value = getStringValue(params);
97
- const isComplex = typeof params === "object";
98
- this.decimal = isComplex ? params.decimal : undefined;
99
-
100
- // use the multiplier to keep track of decimal point - defaults to 8 if lower than 8
101
- this.decimalMultiplier =
102
- isComplex && "decimalMultiplier" in params
103
- ? params.decimalMultiplier
104
- : toMultiplier(Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0));
105
- this.#setValue(value);
106
- }
107
-
108
- set(value: SKBigIntParams): this {
109
- // @ts-expect-error False positive
110
- return new this.constructor({
111
- decimal: this.decimal,
112
- value,
113
- // @ts-expect-error False positive
114
- identifier: this.toString({ includeSynthProtocol: true }),
115
- });
116
- }
117
- add(...args: InitialisationValueType[]) {
118
- return this.#arithmetics("add", ...args);
119
- }
120
- sub(...args: InitialisationValueType[]) {
121
- return this.#arithmetics("sub", ...args);
122
- }
123
- mul(...args: InitialisationValueType[]) {
124
- return this.#arithmetics("mul", ...args);
125
- }
126
- div(...args: InitialisationValueType[]) {
127
- return this.#arithmetics("div", ...args);
128
- }
129
- gt(value: InitialisationValueType) {
130
- return this.#comparison("gt", value);
131
- }
132
- gte(value: InitialisationValueType) {
133
- return this.#comparison("gte", value);
134
- }
135
- lt(value: InitialisationValueType) {
136
- return this.#comparison("lt", value);
137
- }
138
- lte(value: InitialisationValueType) {
139
- return this.#comparison("lte", value);
140
- }
141
- eqValue(value: InitialisationValueType) {
142
- return this.#comparison("eqValue", value);
143
- }
144
-
145
- getValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
146
- const value = this.formatBigIntToSafeValue(
147
- this.bigIntValue,
148
- decimal || this.decimal || decimalFromMultiplier(this.decimalMultiplier),
149
- );
150
-
151
- switch (type) {
152
- case "number":
153
- return Number(value) as NumberPrimitivesType[T];
154
- case "string":
155
- return value as NumberPrimitivesType[T];
156
- case "bigint":
157
- return ((this.bigIntValue * 10n ** BigInt(this.decimal || 8n)) /
158
- this.decimalMultiplier) as NumberPrimitivesType[T];
159
- default:
160
- return value as NumberPrimitivesType[T];
161
- }
162
- }
163
-
164
- getBaseValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
165
- const divisor =
166
- this.decimalMultiplier / toMultiplier(decimal || this.decimal || BaseDecimal.THOR);
167
- const baseValue = divideBigIntWithRounding(this.bigIntValue, divisor);
168
-
169
- switch (type) {
170
- case "number":
171
- return Number(baseValue) as NumberPrimitivesType[T];
172
- case "string":
173
- return baseValue.toString() as NumberPrimitivesType[T];
174
- case "bigint":
175
- return baseValue as NumberPrimitivesType[T];
176
- default:
177
- return baseValue as NumberPrimitivesType[T];
178
- }
179
- }
180
-
181
- getBigIntValue(value: InitialisationValueType, decimal?: number) {
182
- if (!decimal && typeof value === "object") return value.bigIntValue;
183
-
184
- const stringValue = getStringValue(value);
185
- const safeValue = toSafeValue(stringValue);
186
-
187
- if (safeValue === "0" || safeValue === "undefined") return 0n;
188
- return this.#toBigInt(safeValue, decimal);
189
- }
190
-
191
- toSignificant(significantDigits = 6) {
192
- const [int, dec] = this.getValue("string").split(".");
193
- const integer = int || "";
194
- const decimal = dec || "";
195
- const valueLength = Number.parseInt(integer) ? integer.length + decimal.length : decimal.length;
196
-
197
- if (valueLength <= significantDigits) {
198
- return this.getValue("string");
199
- }
200
-
201
- if (integer.length >= significantDigits) {
202
- return integer.slice(0, significantDigits).padEnd(integer.length, "0");
203
- }
204
-
205
- if (Number.parseInt(integer)) {
206
- return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(
207
- significantDigits - integer.length,
208
- "0",
209
- );
210
- }
211
-
212
- const trimmedDecimal = Number.parseInt(decimal);
213
- const slicedDecimal = `${trimmedDecimal}`.slice(0, significantDigits);
214
-
215
- return `0.${slicedDecimal.padStart(
216
- decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
217
- "0",
218
- )}`;
219
- }
220
-
221
- toFixed(fixedDigits = 6) {
222
- const [int, dec] = this.getValue("string").split(".");
223
- const integer = int || "";
224
- const decimal = dec || "";
225
-
226
- if (Number.parseInt(integer)) {
227
- return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits, "0");
228
- }
229
-
230
- const trimmedDecimal = Number.parseInt(decimal);
231
- const slicedDecimal = `${trimmedDecimal}`.slice(0, fixedDigits);
232
-
233
- return `0.${slicedDecimal.padStart(
234
- decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length,
235
- "0",
236
- )}`;
237
- }
238
-
239
- toAbbreviation(digits = 2) {
240
- const value = this.getValue("number");
241
- const abbreviations = ["", "K", "M", "B", "T", "Q", "Qi", "S"];
242
- const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
243
- const suffix = abbreviations[tier];
244
-
245
- if (!suffix) return this.getValue("string");
246
-
247
- const scale = 10 ** (tier * 3);
248
- const scaled = value / scale;
249
-
250
- return `${scaled.toFixed(digits)}${suffix}`;
251
- }
252
-
253
- toCurrency(
254
- currency = "$",
255
- {
256
- currencyPosition = "start",
257
- decimal = 2,
258
- decimalSeparator = ".",
259
- thousandSeparator = ",",
260
- } = {},
261
- ) {
262
- const value = this.getValue("number");
263
- const [int = "", dec = ""] = value.toFixed(6).split(".");
264
- const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
265
-
266
- const parsedValue =
267
- int || dec
268
- ? int === "0"
269
- ? `${Number.parseFloat(`0.${dec}`)}`.replace(".", decimalSeparator)
270
- : `${integer}${Number.parseInt(dec) ? `${decimalSeparator}${dec.slice(0, decimal)}` : ""}`
271
- : "0.00";
272
-
273
- return `${currencyPosition === "start" ? currency : ""}${parsedValue}${
274
- currencyPosition === "end" ? currency : ""
275
- }`;
276
- }
277
-
278
- formatBigIntToSafeValue(value: bigint, decimal?: number) {
279
- const bigIntDecimal = decimal || this.decimal || DEFAULT_DECIMAL;
280
- const decimalToUseForConversion = Math.max(
281
- bigIntDecimal,
282
- decimalFromMultiplier(this.decimalMultiplier),
283
- );
284
- const isNegative = value < 0n;
285
-
286
- const valueString = value.toString().substring(isNegative ? 1 : 0);
287
- const padLength = decimalToUseForConversion - (valueString.length - 1);
288
-
289
- const parsedValueString = padLength > 0 ? "0".repeat(padLength) + valueString : valueString;
290
-
291
- const decimalIndex = parsedValueString.length - decimalToUseForConversion;
292
- let decimalString = parsedValueString.slice(-decimalToUseForConversion);
293
-
294
- // Check if we need to round up
295
- if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
296
- // Increment the last decimal place and slice off the rest
297
- decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1).toString()}`;
298
- } else {
299
- // Just slice off the extra digits
300
- decimalString = decimalString.substring(0, bigIntDecimal);
301
- }
302
-
303
- return `${isNegative ? "-" : ""}${parsedValueString.slice(
304
- 0,
305
- decimalIndex,
306
- )}.${decimalString}`.replace(/\.?0*$/, "");
307
- }
308
-
309
- #arithmetics(method: "add" | "sub" | "mul" | "div", ...args: InitialisationValueType[]): this {
310
- const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args);
311
- const decimal = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
312
- const precisionDecimalMultiplier = toMultiplier(decimal);
313
-
314
- const result = args.reduce(
315
- (acc: bigint, arg) => {
316
- const value = this.getBigIntValue(arg, decimal);
317
-
318
- switch (method) {
319
- case "add":
320
- return acc + value;
321
- case "sub":
322
- return acc - value;
323
- /**
324
- * Multiplication & division would end up with wrong result if we don't adjust the value
325
- * 200000000n * 200000000n => 40000000000000000n
326
- * 200000000n / 200000000n => 1n
327
- * So we do the following:
328
- * 200000000n * 200000000n = 40000000000000000n / 100000000n (decimals) => 400000000n
329
- * (200000000n * 100000000n (decimals)) / 200000000n => 100000000n
330
- */
331
- case "mul":
332
- return (acc * value) / precisionDecimalMultiplier;
333
- case "div": {
334
- if (value === 0n) throw new RangeError("Division by zero");
335
- return (acc * precisionDecimalMultiplier) / value;
336
- }
337
- default:
338
- return acc;
339
- }
340
- },
341
- //normalize is to precision multiplier base
342
- (this.bigIntValue * precisionDecimalMultiplier) / this.decimalMultiplier,
343
- );
344
-
345
- const value = formatBigIntToSafeValue({
346
- bigIntDecimal: decimal,
347
- decimal,
348
- value: result,
349
- });
350
-
351
- // @ts-expect-error False positive
352
- return new this.constructor({
353
- decimalMultiplier: toMultiplier(decimal),
354
- decimal: this.decimal,
355
- value,
356
- identifier: this.toString(),
357
- });
358
- }
359
-
360
- #comparison(method: "gt" | "gte" | "lt" | "lte" | "eqValue", ...args: InitialisationValueType[]) {
361
- const decimal = this.#retrievePrecisionDecimal(this, ...args);
362
- const value = this.getBigIntValue(args[0] || "0", decimal);
363
- const compareToValue = this.getBigIntValue(this, decimal);
364
-
365
- switch (method) {
366
- case "gt":
367
- return compareToValue > value;
368
- case "gte":
369
- return compareToValue >= value;
370
- case "lt":
371
- return compareToValue < value;
372
- case "lte":
373
- return compareToValue <= value;
374
- case "eqValue":
375
- return compareToValue === value;
376
- default:
377
- return false;
378
- }
379
- }
380
-
381
- #setValue(value: InitialisationValueType) {
382
- const safeValue = toSafeValue(value) || "0";
383
- this.bigIntValue = this.#toBigInt(safeValue);
384
- }
385
-
386
- #retrievePrecisionDecimal(...args: InitialisationValueType[]) {
387
- const decimals = args
388
- .map((arg) => {
389
- const isObject = typeof arg === "object";
390
- const value = isObject
391
- ? arg.decimal || decimalFromMultiplier(arg.decimalMultiplier)
392
- : getFloatDecimals(toSafeValue(arg));
393
-
394
- return value;
395
- })
396
- .filter(Boolean) as number[];
397
-
398
- return Math.max(...decimals, DEFAULT_DECIMAL);
399
- }
400
-
401
- #toBigInt(value: string, decimal?: number) {
402
- const multiplier = decimal ? toMultiplier(decimal) : this.decimalMultiplier;
403
- const padDecimal = decimalFromMultiplier(multiplier);
404
- const [integerPart = "", decimalPart = ""] = value.split(".");
405
-
406
- return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, "0")}`);
407
- }
408
- }
409
-
410
- const numberFormatter = Intl.NumberFormat("fullwide", {
411
- useGrouping: false,
412
- maximumFractionDigits: 20,
413
- });
414
-
415
- function toSafeValue(value: InitialisationValueType) {
416
- const parsedValue =
417
- typeof value === "number" ? numberFormatter.format(value) : getStringValue(value);
418
- const splitValue = `${parsedValue}`.replaceAll(",", ".").split(".");
419
-
420
- return splitValue.length > 1
421
- ? `${splitValue.slice(0, -1).join("")}.${splitValue.at(-1)}`
422
- : splitValue[0] || "0";
423
- }
424
-
425
- function getFloatDecimals(value: string) {
426
- const decimals = value.split(".")[1]?.length || 0;
427
- return Math.max(decimals, DEFAULT_DECIMAL);
428
- }
429
-
430
- function getStringValue(param: SKBigIntParams) {
431
- return typeof param === "object"
432
- ? "getValue" in param
433
- ? param.getValue("string")
434
- : param.value
435
- : param;
436
- }
@@ -1,87 +0,0 @@
1
- import { FeeOption } from "../types";
2
- import { SKConfig } from "./swapKitConfig";
3
-
4
- export interface FeeMultiplierConfig {
5
- [FeeOption.Average]: number;
6
- [FeeOption.Fast]: number;
7
- [FeeOption.Fastest]: number;
8
- }
9
-
10
- const DEFAULT_FEE_MULTIPLIERS: FeeMultiplierConfig = {
11
- [FeeOption.Average]: 1.0,
12
- [FeeOption.Fast]: 1.5,
13
- [FeeOption.Fastest]: 2.0,
14
- };
15
-
16
- /**
17
- * Get fee multiplier for the given fee option.
18
- * Checks SKConfig for custom multipliers first, then falls back to defaults.
19
- *
20
- * @param feeOption - The fee option (Average, Fast, Fastest)
21
- * @returns The fee multiplier as a number
22
- */
23
- export function getFeeMultiplier(feeOption: FeeOption = FeeOption.Average): number {
24
- const customMultipliers = SKConfig.get("feeMultipliers");
25
-
26
- if (customMultipliers && customMultipliers[feeOption] !== undefined) {
27
- return customMultipliers[feeOption];
28
- }
29
-
30
- return DEFAULT_FEE_MULTIPLIERS[feeOption];
31
- }
32
-
33
- /**
34
- * Get fee multiplier as BigInt for EVM calculations.
35
- * Returns numerator and denominator for precise BigInt arithmetic.
36
- *
37
- * @param feeOption - The fee option (Average, Fast, Fastest)
38
- * @returns Object with numerator and denominator for BigInt calculations
39
- */
40
- export function getFeeMultiplierAsBigInt(feeOption: FeeOption = FeeOption.Average): {
41
- numerator: bigint;
42
- denominator: bigint;
43
- } {
44
- const multiplier = getFeeMultiplier(feeOption);
45
-
46
- // Convert decimal multiplier to fraction for precise BigInt arithmetic
47
- // e.g., 1.5 -> 15/10, 2.0 -> 20/10
48
- const denominator = 10n;
49
- const numerator = BigInt(Math.round(multiplier * 10));
50
-
51
- return { numerator, denominator };
52
- }
53
-
54
- /**
55
- * Apply fee multiplier to a BigInt value (for EVM chains).
56
- *
57
- * @param value - The base fee value as BigInt
58
- * @param feeOption - The fee option (Average, Fast, Fastest)
59
- * @returns The multiplied fee value as BigInt
60
- */
61
- export function applyFeeMultiplierToBigInt(
62
- value: bigint,
63
- feeOption: FeeOption = FeeOption.Average,
64
- ): bigint {
65
- const { numerator, denominator } = getFeeMultiplierAsBigInt(feeOption);
66
- return (value * numerator) / denominator;
67
- }
68
-
69
- /**
70
- * Apply fee multiplier to a number value (for non-EVM chains).
71
- *
72
- * @param value - The base fee value as number
73
- * @param feeOption - The fee option (Average, Fast, Fastest)
74
- * @param floor - Whether to floor the result (default: false)
75
- * @returns The multiplied fee value as number
76
- */
77
- export function applyFeeMultiplier(
78
- value: number,
79
- feeOption: FeeOption = FeeOption.Average,
80
- floor = false,
81
- ): number {
82
- const multiplier = getFeeMultiplier(feeOption);
83
- const result = value * multiplier;
84
- return floor ? Math.floor(result) : result;
85
- }
86
-
87
- export { DEFAULT_FEE_MULTIPLIERS };
@@ -1,83 +0,0 @@
1
- import { SwapKitError } from "./swapKitError";
2
-
3
- type Options = RequestInit & {
4
- /**
5
- * @deprecated Use onSuccess instead - will be removed in next major @MarkedV4
6
- */
7
- responseHandler?: (response: any) => any;
8
- json?: unknown;
9
- onError?: (error: any) => any;
10
- onSuccess?: (response: any) => any;
11
- searchParams?: Record<string, string>;
12
- dynamicHeader?: () => Record<string, string> | {};
13
- };
14
-
15
- export const RequestClient = {
16
- get: fetchWithConfig("GET"),
17
- post: fetchWithConfig("POST"),
18
- extend: (extendOptions: Options) => ({
19
- get: fetchWithConfig("GET", extendOptions),
20
- post: fetchWithConfig("POST", extendOptions),
21
- extend: (newOptions: Options) => RequestClient.extend({ ...extendOptions, ...newOptions }),
22
- }),
23
- };
24
-
25
- function fetchWithConfig(method: "GET" | "POST", extendOptions: Options = {}) {
26
- return async <T>(url: string, options: Options = {}): Promise<T> => {
27
- const {
28
- searchParams,
29
- json,
30
- body,
31
- headers: headersOptions,
32
- dynamicHeader,
33
- } = { ...extendOptions, ...options };
34
-
35
- const isJson = !!json || url.endsWith(".json");
36
- const bodyToSend = isJson ? JSON.stringify(json) : body;
37
-
38
- try {
39
- const requestUrl = buildUrl(url, searchParams);
40
- const headers = buildHeaders(isJson, {
41
- ...headersOptions,
42
- ...dynamicHeader?.(),
43
- });
44
-
45
- const response = await fetch(requestUrl, { ...options, method, body: bodyToSend, headers });
46
-
47
- if (!response.ok) {
48
- const message = await response.text();
49
- throw new SwapKitError("helpers_invalid_response", {
50
- status: response.status,
51
- statusText: response.statusText,
52
- message,
53
- });
54
- }
55
-
56
- const body = await response.json();
57
-
58
- return options.onSuccess?.(body) || options.responseHandler?.(body) || body;
59
- } catch (error) {
60
- if (options.onError) {
61
- return options.onError(error);
62
- }
63
- throw error;
64
- }
65
- };
66
- }
67
-
68
- function buildHeaders(isJson: boolean, headersOptions?: HeadersInit) {
69
- return {
70
- ...headersOptions,
71
- ...(isJson ? { "Content-Type": "application/json" } : {}),
72
- };
73
- }
74
-
75
- function buildUrl(url: string, searchParams?: Record<string, string>) {
76
- const urlInstance = new URL(url);
77
-
78
- if (searchParams) {
79
- urlInstance.search = new URLSearchParams(searchParams).toString();
80
- }
81
-
82
- return urlInstance.toString();
83
- }