@tcswap/helpers 4.5.15

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 (139) hide show
  1. package/dist/api/index.cjs +4 -0
  2. package/dist/api/index.cjs.map +16 -0
  3. package/dist/api/index.js +4 -0
  4. package/dist/api/index.js.map +16 -0
  5. package/dist/chunk-pfmeq01a.js +5 -0
  6. package/dist/chunk-pfmeq01a.js.map +9 -0
  7. package/dist/chunk-vb4wtm2w.js +4 -0
  8. package/dist/chunk-vb4wtm2w.js.map +9 -0
  9. package/dist/contracts.cjs +4 -0
  10. package/dist/contracts.cjs.map +10 -0
  11. package/dist/contracts.js +4 -0
  12. package/dist/contracts.js.map +10 -0
  13. package/dist/index.cjs +7 -0
  14. package/dist/index.cjs.map +30 -0
  15. package/dist/index.js +7 -0
  16. package/dist/index.js.map +30 -0
  17. package/dist/tokens.cjs +4 -0
  18. package/dist/tokens.cjs.map +10 -0
  19. package/dist/tokens.js +4 -0
  20. package/dist/tokens.js.map +10 -0
  21. package/dist/types/api/index.d.ts +502 -0
  22. package/dist/types/api/index.d.ts.map +1 -0
  23. package/dist/types/api/memoless/endpoints.d.ts +56 -0
  24. package/dist/types/api/memoless/endpoints.d.ts.map +1 -0
  25. package/dist/types/api/memoless/types.d.ts +85 -0
  26. package/dist/types/api/memoless/types.d.ts.map +1 -0
  27. package/dist/types/api/midgard/endpoints.d.ts +80 -0
  28. package/dist/types/api/midgard/endpoints.d.ts.map +1 -0
  29. package/dist/types/api/midgard/types.d.ts +543 -0
  30. package/dist/types/api/midgard/types.d.ts.map +1 -0
  31. package/dist/types/api/thornode/endpoints.d.ts +34 -0
  32. package/dist/types/api/thornode/endpoints.d.ts.map +1 -0
  33. package/dist/types/api/thornode/types.d.ts +264 -0
  34. package/dist/types/api/thornode/types.d.ts.map +1 -0
  35. package/dist/types/api/uswap/endpoints.d.ts +372 -0
  36. package/dist/types/api/uswap/endpoints.d.ts.map +1 -0
  37. package/dist/types/api/uswap/types.d.ts +1487 -0
  38. package/dist/types/api/uswap/types.d.ts.map +1 -0
  39. package/dist/types/contracts.d.ts +2 -0
  40. package/dist/types/contracts.d.ts.map +1 -0
  41. package/dist/types/index.d.ts +32 -0
  42. package/dist/types/index.d.ts.map +1 -0
  43. package/dist/types/modules/assetValue.d.ts +82 -0
  44. package/dist/types/modules/assetValue.d.ts.map +1 -0
  45. package/dist/types/modules/bigIntArithmetics.d.ts +60 -0
  46. package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -0
  47. package/dist/types/modules/feeMultiplier.d.ts +48 -0
  48. package/dist/types/modules/feeMultiplier.d.ts.map +1 -0
  49. package/dist/types/modules/requestClient.d.ts +33 -0
  50. package/dist/types/modules/requestClient.d.ts.map +1 -0
  51. package/dist/types/modules/uSwapConfig.d.ts +249 -0
  52. package/dist/types/modules/uSwapConfig.d.ts.map +1 -0
  53. package/dist/types/modules/uSwapError.d.ts +879 -0
  54. package/dist/types/modules/uSwapError.d.ts.map +1 -0
  55. package/dist/types/modules/uSwapNumber.d.ts +10 -0
  56. package/dist/types/modules/uSwapNumber.d.ts.map +1 -0
  57. package/dist/types/tokens.d.ts +2 -0
  58. package/dist/types/tokens.d.ts.map +1 -0
  59. package/dist/types/types/commonTypes.d.ts +16 -0
  60. package/dist/types/types/commonTypes.d.ts.map +1 -0
  61. package/dist/types/types/derivationPath.d.ts +4 -0
  62. package/dist/types/types/derivationPath.d.ts.map +1 -0
  63. package/dist/types/types/errors/apiV1.d.ts +2 -0
  64. package/dist/types/types/errors/apiV1.d.ts.map +1 -0
  65. package/dist/types/types/index.d.ts +6 -0
  66. package/dist/types/types/index.d.ts.map +1 -0
  67. package/dist/types/types/quotes.d.ts +180 -0
  68. package/dist/types/types/quotes.d.ts.map +1 -0
  69. package/dist/types/types/sdk.d.ts +35 -0
  70. package/dist/types/types/sdk.d.ts.map +1 -0
  71. package/dist/types/types/wallet.d.ts +130 -0
  72. package/dist/types/types/wallet.d.ts.map +1 -0
  73. package/dist/types/utils/asset.d.ts +37 -0
  74. package/dist/types/utils/asset.d.ts.map +1 -0
  75. package/dist/types/utils/chains.d.ts +13 -0
  76. package/dist/types/utils/chains.d.ts.map +1 -0
  77. package/dist/types/utils/derivationPath.d.ts +21 -0
  78. package/dist/types/utils/derivationPath.d.ts.map +1 -0
  79. package/dist/types/utils/explorerUrls.d.ts +10 -0
  80. package/dist/types/utils/explorerUrls.d.ts.map +1 -0
  81. package/dist/types/utils/liquidity.d.ts +62 -0
  82. package/dist/types/utils/liquidity.d.ts.map +1 -0
  83. package/dist/types/utils/memo.d.ts +65 -0
  84. package/dist/types/utils/memo.d.ts.map +1 -0
  85. package/dist/types/utils/others.d.ts +15 -0
  86. package/dist/types/utils/others.d.ts.map +1 -0
  87. package/dist/types/utils/validators.d.ts +6 -0
  88. package/dist/types/utils/validators.d.ts.map +1 -0
  89. package/dist/types/utils/wallets.d.ts +36 -0
  90. package/dist/types/utils/wallets.d.ts.map +1 -0
  91. package/package.json +67 -0
  92. package/src/api/index.ts +15 -0
  93. package/src/api/memoless/endpoints.ts +62 -0
  94. package/src/api/memoless/types.ts +83 -0
  95. package/src/api/midgard/endpoints.ts +352 -0
  96. package/src/api/midgard/types.ts +515 -0
  97. package/src/api/thornode/endpoints.ts +109 -0
  98. package/src/api/thornode/types.ts +247 -0
  99. package/src/api/uswap/endpoints.ts +252 -0
  100. package/src/api/uswap/types.ts +626 -0
  101. package/src/contracts.ts +1 -0
  102. package/src/index.ts +32 -0
  103. package/src/modules/__tests__/assetValue.test.ts +2452 -0
  104. package/src/modules/__tests__/bigIntArithmetics.test.ts +410 -0
  105. package/src/modules/__tests__/feeMultiplier.test.ts +131 -0
  106. package/src/modules/__tests__/uSwapConfig.test.ts +429 -0
  107. package/src/modules/__tests__/uSwapNumber.test.ts +439 -0
  108. package/src/modules/assetValue.ts +536 -0
  109. package/src/modules/bigIntArithmetics.ts +366 -0
  110. package/src/modules/feeMultiplier.ts +84 -0
  111. package/src/modules/requestClient.ts +116 -0
  112. package/src/modules/uSwapConfig.ts +189 -0
  113. package/src/modules/uSwapError.ts +474 -0
  114. package/src/modules/uSwapNumber.ts +17 -0
  115. package/src/tokens.ts +1 -0
  116. package/src/types/commonTypes.ts +10 -0
  117. package/src/types/derivationPath.ts +11 -0
  118. package/src/types/errors/apiV1.ts +0 -0
  119. package/src/types/index.ts +5 -0
  120. package/src/types/quotes.ts +182 -0
  121. package/src/types/sdk.ts +38 -0
  122. package/src/types/wallet.ts +124 -0
  123. package/src/utils/__tests__/asset.test.ts +186 -0
  124. package/src/utils/__tests__/derivationPath.test.ts +142 -0
  125. package/src/utils/__tests__/explorerUrls.test.ts +59 -0
  126. package/src/utils/__tests__/liquidity.test.ts +302 -0
  127. package/src/utils/__tests__/memo.test.ts +99 -0
  128. package/src/utils/__tests__/others.test.ts +169 -0
  129. package/src/utils/__tests__/validators.test.ts +84 -0
  130. package/src/utils/__tests__/wallets.test.ts +625 -0
  131. package/src/utils/asset.ts +399 -0
  132. package/src/utils/chains.ts +104 -0
  133. package/src/utils/derivationPath.ts +101 -0
  134. package/src/utils/explorerUrls.ts +32 -0
  135. package/src/utils/liquidity.ts +154 -0
  136. package/src/utils/memo.ts +102 -0
  137. package/src/utils/others.ts +64 -0
  138. package/src/utils/validators.ts +36 -0
  139. package/src/utils/wallets.ts +238 -0
@@ -0,0 +1,247 @@
1
+ export type THORNodeType = "thorchain" | "mayachain";
2
+
3
+ export type THORNodeTNSDetails = {
4
+ name: string;
5
+ expire_block_height: number;
6
+ owner: string;
7
+ preferred_asset: string;
8
+ affiliate_collector_rune: string;
9
+ aliases: { chain: string; address: string }[];
10
+ };
11
+
12
+ export type InboundAddressesItem = {
13
+ address: string;
14
+ chain: string;
15
+ gas_rate?: string;
16
+ halted: boolean;
17
+ pub_key: string;
18
+ router?: string;
19
+ global_trading_paused: boolean;
20
+ chain_trading_paused: boolean;
21
+ chain_lp_actions_paused: boolean;
22
+ observed_fee_rate?: string;
23
+ gas_rate_units: string;
24
+ outbound_tx_size: string;
25
+ outbound_fee: string;
26
+ dust_threshold: string;
27
+ };
28
+
29
+ export type LastBlockItem<T extends THORNodeType = "thorchain"> = {
30
+ chain: string;
31
+ last_observed_in: number;
32
+ last_signed_out: number;
33
+ } & {
34
+ [K in T]: number;
35
+ };
36
+
37
+ export type NodeItem = {
38
+ active_block_height: number;
39
+ bond_address: string;
40
+ current_award: string;
41
+ forced_to_leave: boolean;
42
+ ip_address: string;
43
+ leave_height: number;
44
+ node_address: string;
45
+ requested_to_leave: boolean;
46
+ signer_membership: string[];
47
+ slash_points: number;
48
+ status: string;
49
+ status_since: number;
50
+ total_bond: string;
51
+ validator_cons_pub_key: string;
52
+ version: string;
53
+ jail: any;
54
+ preflight_status: any;
55
+ pub_key_set: any;
56
+ observe_chains: { chain: string; height: number }[];
57
+ };
58
+
59
+ export type MimirData = {
60
+ ADR012: number;
61
+ ASGARDSIZE: number;
62
+ BADVALIDATORREDLINE: number;
63
+ BURNSYNTHS: number;
64
+ CHURNINTERVAL: number;
65
+ CLOUTLIMIT: number;
66
+ CONFMULTIPLIERBASISPOINTS: number;
67
+ DEPRECATEILP: number;
68
+ DERIVEDDEPTHBASISPTS: number;
69
+ DERIVEDMINDEPTH: number;
70
+ DESIREDVALIDATORSET: number;
71
+ DYNAMICMAXANCHORTARGET: number;
72
+ EMISSIONCURVE: number;
73
+ ENABLEAVAXCHAIN: number;
74
+ ENABLEBSC: number;
75
+ ENABLESAVINGSVAULTS: number;
76
+ FULLIMPLOSSPROTECTIONBLOCKS: number;
77
+ FUNDMIGRATIONINTERVAL: number;
78
+ HALTAVAXCHAIN: number;
79
+ HALTAVAXTRADING: number;
80
+ HALTBCHCHAIN: number;
81
+ HALTBCHTRADING: number;
82
+ HALTBSCCHAIN: number;
83
+ HALTBSCTRADING: number;
84
+ HALTBTCCHAIN: number;
85
+ HALTBTCTRADING: number;
86
+ HALTCHAINGLOBAL: number;
87
+ HALTCHURNING: number;
88
+ HALTDOGECHAIN: number;
89
+ HALTDOGETRADING: number;
90
+ HALTETHCHAIN: number;
91
+ HALTETHTRADING: number;
92
+ HALTGAIACHAIN: number;
93
+ HALTGAIATRADING: number;
94
+ HALTLTCCHAIN: number;
95
+ HALTLTCTRADING: number;
96
+ HALTSIGNING: number;
97
+ HALTSIGNINGAVAX: number;
98
+ HALTSIGNINGBCH: number;
99
+ HALTSIGNINGBSC: number;
100
+ HALTSIGNINGBTC: number;
101
+ HALTSIGNINGDOGE: number;
102
+ HALTSIGNINGETH: number;
103
+ HALTSIGNINGGAIA: number;
104
+ HALTSIGNINGLTC: number;
105
+ HALTSIGNINGTERRA: number;
106
+ HALTTERRACHAIN: number;
107
+ HALTTERRATRADING: number;
108
+ HALTTHORCHAIN: number;
109
+ HALTTRADING: number;
110
+ ILPCUTOFF: number;
111
+ KEYGENRETRYINTERVAL: number;
112
+ KILLSWITCHSTART: number;
113
+ "LENDING-THOR-BTC": number;
114
+ "LENDING-THOR-ETH": number;
115
+ LENDINGLEVER: number;
116
+ LOANREPAYMENTMATURITY: number;
117
+ LOANSTREAMINGSWAPSINTERVAL: number;
118
+ MAXANCHORBLOCKS: number;
119
+ MAXANCHORSLIP: number;
120
+ MAXBONDPROVIDERS: number;
121
+ MAXCONFIRMATIONS: number;
122
+ MAXCR: number;
123
+ MAXIMUMLIQUIDITYRUNE: number;
124
+ MAXNODETOCHURNOUTFORLOWVERSION: number;
125
+ MAXOUTBOUNDATTEMPTS: number;
126
+ MAXOUTBOUNDFEEMULTIPLIERBASISPOINTS: number;
127
+ MAXRUNESUPPLY: number;
128
+ MAXSYNTHPERPOOLDEPTH: number;
129
+ MAXSYNTHSFORSAVERSYIELD: number;
130
+ MAXUTXOSTOSPEND: number;
131
+ MINCR: number;
132
+ MINIMUMBONDINRUNE: number;
133
+ MINIMUML1OUTBOUNDFEEUSD: number;
134
+ MINOUTBOUNDFEEMULTIPLIERBASISPOINTS: number;
135
+ MINRUNEPOOLDEPTH: number;
136
+ MINTSYNTHS: number;
137
+ NODEOPERATORFEE: number;
138
+ NODEPAUSECHAINGLOBAL: number;
139
+ NUMBEROFNEWNODESPERCHURN: number;
140
+ OBSERVATIONDELAYFLEXIBILITY: number;
141
+ "PAUSEASYMWITHDRAWAL-TERRA": number;
142
+ PAUSELOANS: number;
143
+ PAUSELP: number;
144
+ PAUSELPAVAX: number;
145
+ PAUSELPBCH: number;
146
+ PAUSELPBSC: number;
147
+ PAUSELPBTC: number;
148
+ PAUSELPDOGE: number;
149
+ PAUSELPETH: number;
150
+ PAUSELPGAIA: number;
151
+ PAUSELPLTC: number;
152
+ PAUSELPTERRA: number;
153
+ PAUSEUNBOND: number;
154
+ PENDINGLIQUIDITYAGELIMIT: number;
155
+ "POL-AVAX-AVAX": number;
156
+ "POL-AVAX-USDC-0XB97EF9EF8734C71904D8002F8B6BC66DD9C48A6E": number;
157
+ "POL-BCH-BCH": number;
158
+ "POL-BSC-USDC-0X8AC76A51CC950D9822D68B83FE1AD97B32CD580D": number;
159
+ "POL-BTC-BTC": number;
160
+ "POL-DOGE-DOGE": number;
161
+ "POL-ETH-DAI-0X6B175474E89094C44DA98B954EEDEAC495271D0F": number;
162
+ "POL-ETH-ETH": number;
163
+ "POL-ETH-USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48": number;
164
+ "POL-ETH-USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7": number;
165
+ "POL-GAIA-ATOM": number;
166
+ "POL-LTC-LTC": number;
167
+ POLBUFFER: number;
168
+ POLMAXNETWORKDEPOSIT: number;
169
+ POLMAXPOOLMOVEMENT: number;
170
+ POLTARGETSYNTHPERPOOLDEPTH: number;
171
+ POOLCYCLE: number;
172
+ POOLDEPTHFORYGGFUNDINGMIN: number;
173
+ PREFERREDASSETOUTBOUNDFEEMULTIPLIER: number;
174
+ RESCHEDULECOALESCEBLOCKS: number;
175
+ SAVERSSTREAMINGSWAPSINTERVAL: number;
176
+ SIGNERCONCURRENCY: number;
177
+ SLASHPENALTY: number;
178
+ SOLVENCYHALTAVAXCHAIN: number;
179
+ SOLVENCYHALTBCHCHAIN: number;
180
+ SOLVENCYHALTBSCCHAIN: number;
181
+ SOLVENCYHALTDOGECHAIN: number;
182
+ SOLVENCYHALTETHCHAIN: number;
183
+ SOLVENCYHALTGAIACHAIN: number;
184
+ SOLVENCYHALTTERRACHAIN: number;
185
+ STOPFUNDYGGDRASIL: number;
186
+ STOPSOLVENCYCHECK: number;
187
+ STOPSOLVENCYCHECKAVAX: number;
188
+ STOPSOLVENCYCHECKBSC: number;
189
+ STOPSOLVENCYCHECKDOGE: number;
190
+ STOPSOLVENCYCHECKETH: number;
191
+ STOPSOLVENCYCHECKGAIA: number;
192
+ STOPSOLVENCYCHECKTERRA: number;
193
+ STREAMINGSWAPMINBPFEE: number;
194
+ SYNTHYIELDBASISPOINTS: number;
195
+ THORNAMES: number;
196
+ "TORANCHOR-AVAX-USDC-0XB97EF9EF8734C71904D8002F8B6BC66DD9C48A6E": number;
197
+ "TORANCHOR-AVAX-USDT-0X9702230A8EA53601F5CD2DC00FDBC13D4DF4A8C7": number;
198
+ "TORANCHOR-BSC-USDC-0X8AC76A51CC950D9822D68B83FE1AD97B32CD580D": number;
199
+ "TORANCHOR-ETH-DAI-0X6B175474E89094C44DA98B954EEDEAC495271D0F": number;
200
+ "TORANCHOR-ETH-USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48": number;
201
+ "TORANCHOR-ETH-USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7": number;
202
+ TXOUTDELAYRATE: number;
203
+ VIRTUALMULTSYNTHS: number;
204
+ VOTEDOFM: number;
205
+ VOTELENDING: number;
206
+ VOTEMAXSYNTHSFORSAVERSYIELD: number;
207
+ YGGFUNDLIMIT: number;
208
+ YGGFUNDRETRY: number;
209
+ };
210
+
211
+ export type RunePoolInfo = {
212
+ pol: { rune_deposited: string; rune_withdrawn: string; value: string; pnl: string; current_deposit: string };
213
+ providers: {
214
+ units: string;
215
+ pending_units: string;
216
+ pending_rune: string;
217
+ value: string;
218
+ pnl: string;
219
+ current_deposit: string;
220
+ };
221
+ reserve: { units: string; value: string; pnl: string; current_deposit: string };
222
+ };
223
+
224
+ export type RunePoolProviderInfo = {
225
+ rune_address: string;
226
+ units: string;
227
+ value: string;
228
+ pnl: string;
229
+ deposit_amount: string;
230
+ withdraw_amount: string;
231
+ last_deposit_height: number;
232
+ last_withdraw_height: number;
233
+ };
234
+
235
+ export type TCYStakerSummary = { amount: string };
236
+
237
+ export type TCYStaker = { address?: string; amount: string };
238
+
239
+ export type TCYStakerResponse = TCYStaker;
240
+ export type TCYStakersResponse = TCYStakerSummary[];
241
+
242
+ export type TCYClaimerSummary = { amount: string; asset: string };
243
+
244
+ export type TCYClaimer = { l1_address?: string; amount: string; asset: string };
245
+
246
+ export type TCYClaimerResponse = TCYClaimer;
247
+ export type TCYClaimersResponse = TCYClaimerSummary[];
@@ -0,0 +1,252 @@
1
+ /**
2
+ * Modifications © 2025 Horizontal Systems.
3
+ */
4
+
5
+ import {
6
+ type Chain,
7
+ type EVMChain,
8
+ EVMChains,
9
+ isGasAsset,
10
+ type ProviderName,
11
+ RequestClient,
12
+ type RequestOptions,
13
+ USwapConfig,
14
+ USwapError,
15
+ } from "@tcswap/helpers";
16
+ import { match, P } from "ts-pattern";
17
+ import {
18
+ type BalanceResponse,
19
+ type BrokerDepositChannelParams,
20
+ type DepositChannelResponse,
21
+ DepositChannelResponseSchema,
22
+ type GasResponse,
23
+ GasResponseSchema,
24
+ type NearDepositChannelParams,
25
+ type NearSwapResponse,
26
+ NearSwapResponseSchema,
27
+ type PriceRequest,
28
+ type PriceResponse,
29
+ PriceResponseSchema,
30
+ type QuoteRequest,
31
+ type QuoteResponse,
32
+ type QuoteResponseRoute,
33
+ QuoteResponseRouteItem,
34
+ QuoteResponseSchema,
35
+ type TokenListProvidersResponse,
36
+ type TokensResponseV2,
37
+ type TrackerResponse,
38
+ TrackerResponseSchema,
39
+ type TrackingRequest,
40
+ } from "./types";
41
+
42
+ const USwapRequestClient = RequestClient.extend({
43
+ dynamicHeader: () => {
44
+ const { uSwap } = USwapConfig.get("apiKeys");
45
+ return uSwap ? { "x-api-key": uSwap } : {};
46
+ },
47
+ });
48
+
49
+ export async function getTrackerDetails(json: TrackingRequest, options?: RequestOptions) {
50
+ const response = await USwapRequestClient.post<TrackerResponse>(getApiUrl("/track"), { json, ...options });
51
+
52
+ try {
53
+ const parsedResponse = TrackerResponseSchema.safeParse(response);
54
+
55
+ if (!parsedResponse.success) {
56
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
57
+ }
58
+
59
+ return parsedResponse.data;
60
+ } catch (_error) {
61
+ // throw new USwapError("api_v2_invalid_response", error);
62
+ return response;
63
+ }
64
+ }
65
+
66
+ export async function getSwapQuote(json: QuoteRequest, options?: RequestOptions) {
67
+ const { getQuote } = USwapConfig.get("endpoints");
68
+
69
+ if (getQuote) return getQuote(json);
70
+
71
+ const response = await USwapRequestClient.post<QuoteResponse>(getApiUrl("/quote"), { json, ...options });
72
+
73
+ if (response.error) {
74
+ throw new USwapError("api_v2_server_error", { message: response.error });
75
+ }
76
+
77
+ try {
78
+ const parsedResponse = QuoteResponseSchema.safeParse(response);
79
+
80
+ if (!parsedResponse.success) {
81
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
82
+ }
83
+
84
+ return parsedResponse.data;
85
+ } catch {
86
+ // throw new USwapError("api_v2_invalid_response", error);
87
+ return response;
88
+ }
89
+ }
90
+
91
+ export async function getRouteWithTx(json: { routeId: string; sourceAddress: string; destinationAddress: string }) {
92
+ const { getRouteWithTx } = USwapConfig.get("endpoints");
93
+
94
+ if (getRouteWithTx) return getRouteWithTx(json);
95
+
96
+ const response = await USwapRequestClient.post<QuoteResponseRoute>(getApiUrl("/swap"), { json });
97
+
98
+ try {
99
+ const parsedResponse = QuoteResponseRouteItem.safeParse(response);
100
+
101
+ if (!parsedResponse.success) {
102
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
103
+ }
104
+
105
+ return parsedResponse.data;
106
+ } catch (error) {
107
+ console.error(new USwapError("api_v2_invalid_response", error));
108
+ return response;
109
+ }
110
+ }
111
+
112
+ export async function getChainBalance<T extends Chain>({
113
+ chain,
114
+ address,
115
+ scamFilter = true,
116
+ }: {
117
+ chain: T;
118
+ address: string;
119
+ scamFilter?: boolean;
120
+ }) {
121
+ const { getBalance } = USwapConfig.get("endpoints");
122
+ if (getBalance) return getBalance({ address, chain });
123
+
124
+ const url = getApiUrl(`/balance?chain=${chain}&address=${address}`);
125
+ const balanceResponse = await USwapRequestClient.get<BalanceResponse>(url);
126
+ const balances = Array.isArray(balanceResponse) ? balanceResponse : [];
127
+ return scamFilter ? filterAssets(balances) : balances;
128
+ }
129
+
130
+ export function getTokenListProviders() {
131
+ const url = getApiUrl("/providers");
132
+ return USwapRequestClient.get<TokenListProvidersResponse>(url);
133
+ }
134
+
135
+ export function getTokenList(provider: ProviderName) {
136
+ const url = getApiUrl(`/tokens?provider=${provider}`);
137
+ return USwapRequestClient.get<TokensResponseV2>(url);
138
+ }
139
+
140
+ export async function getPrice(body: PriceRequest) {
141
+ const url = getApiUrl("/price");
142
+ const response = await USwapRequestClient.post<PriceResponse>(url, { json: body });
143
+
144
+ try {
145
+ const parsedResponse = PriceResponseSchema.safeParse(response);
146
+
147
+ if (!parsedResponse.success) {
148
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
149
+ }
150
+
151
+ return parsedResponse.data;
152
+ } catch (error) {
153
+ throw new USwapError("api_v2_invalid_response", error);
154
+ }
155
+ }
156
+
157
+ export async function getGasRate() {
158
+ const url = getApiUrl("/gas");
159
+ const response = await USwapRequestClient.get<GasResponse>(url);
160
+
161
+ try {
162
+ const parsedResponse = GasResponseSchema.safeParse(response);
163
+
164
+ if (!parsedResponse.success) {
165
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
166
+ }
167
+
168
+ const gasRates = Array.isArray(parsedResponse) ? parsedResponse : [parsedResponse];
169
+ return gasRates;
170
+ } catch (error) {
171
+ throw new USwapError("api_v2_invalid_response", error);
172
+ }
173
+ }
174
+
175
+ export async function getChainflipDepositChannel(body: BrokerDepositChannelParams) {
176
+ const { destinationAddress } = body;
177
+
178
+ if (!destinationAddress) {
179
+ throw new USwapError("chainflip_broker_invalid_params");
180
+ }
181
+ const url = USwapConfig.get("integrations").chainflip?.brokerUrl || getApiUrl("/chainflip/broker/channel");
182
+
183
+ const response = await USwapRequestClient.post<DepositChannelResponse>(url, { json: body });
184
+
185
+ try {
186
+ const parsedResponse = DepositChannelResponseSchema.safeParse(response);
187
+
188
+ if (!parsedResponse.success) {
189
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
190
+ }
191
+
192
+ return parsedResponse.data;
193
+ } catch (error) {
194
+ throw new USwapError("api_v2_invalid_response", error);
195
+ }
196
+ }
197
+
198
+ export async function getNearDepositChannel(body: NearDepositChannelParams) {
199
+ const { destinationAddress } = body;
200
+
201
+ if (!destinationAddress) {
202
+ throw new USwapError("chainflip_broker_invalid_params");
203
+ }
204
+ const url = getApiUrl("/near/channel");
205
+
206
+ const response = await USwapRequestClient.post<NearSwapResponse>(url, { json: body });
207
+
208
+ try {
209
+ const parsedResponse = NearSwapResponseSchema.safeParse(response);
210
+
211
+ if (!parsedResponse.success) {
212
+ throw new USwapError("api_v2_invalid_response", parsedResponse.error);
213
+ }
214
+
215
+ return parsedResponse.data;
216
+ } catch (error) {
217
+ throw new USwapError("api_v2_invalid_response", error);
218
+ }
219
+ }
220
+
221
+ function getApiUrl(path?: `/${string}`) {
222
+ const { isDev, apiUrl, devApiUrl, experimental_apiUrlQuote, experimental_apiUrlSwap } = USwapConfig.get("envs");
223
+
224
+ const defaultUrl = `${isDev ? devApiUrl : apiUrl}${path}`;
225
+
226
+ return match({ experimental_apiUrlQuote, experimental_apiUrlSwap, path })
227
+ .with(
228
+ { experimental_apiUrlQuote: P.string.startsWith("http"), path: "/quote" },
229
+ ({ experimental_apiUrlQuote, path }) => `${experimental_apiUrlQuote}${path}`,
230
+ )
231
+ .with(
232
+ { experimental_apiUrlSwap: P.string.startsWith("http"), path: "/swap" },
233
+ ({ experimental_apiUrlSwap, path }) => `${experimental_apiUrlSwap}${path}`,
234
+ )
235
+ .otherwise(() => defaultUrl);
236
+ }
237
+
238
+ function evmAssetHasAddress(assetString: string) {
239
+ const [chain, symbol] = assetString.split(".") as [EVMChain, string];
240
+ if (!EVMChains.includes(chain as EVMChain)) return true;
241
+ const splitSymbol = symbol.split("-");
242
+ const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
243
+
244
+ return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
245
+ }
246
+
247
+ const potentialScamRegex = new RegExp(/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/, "gmi");
248
+ function filterAssets(tokens: BalanceResponse) {
249
+ return tokens.filter((token) => {
250
+ return !potentialScamRegex.test(token.identifier) && evmAssetHasAddress(token.identifier);
251
+ });
252
+ }