@reyaxyz/common 0.135.2 → 0.137.0

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 (59) hide show
  1. package/README.md +1 -1
  2. package/dist/services/getSocketAddresses.js +12 -22
  3. package/dist/services/getSocketAddresses.js.map +1 -1
  4. package/dist/transactions/executeTransaction.js +10 -5
  5. package/dist/transactions/executeTransaction.js.map +1 -1
  6. package/dist/types/services/getSocketAddresses.d.ts +32 -2
  7. package/dist/types/services/getSocketAddresses.d.ts.map +1 -1
  8. package/dist/types/transactions/executeTransaction.d.ts +2 -2
  9. package/dist/types/transactions/executeTransaction.d.ts.map +1 -1
  10. package/dist/types/types.d.ts +1 -9
  11. package/dist/types/types.d.ts.map +1 -1
  12. package/dist/types/utils/network.d.ts +1 -1
  13. package/dist/types/utils/network.d.ts.map +1 -1
  14. package/dist/types/utils/token/batch-token-getters.d.ts +22 -0
  15. package/dist/types/utils/token/batch-token-getters.d.ts.map +1 -0
  16. package/dist/types/utils/token/index.d.ts +6 -0
  17. package/dist/types/utils/token/index.d.ts.map +1 -0
  18. package/dist/types/utils/token/token-getters.d.ts +23 -0
  19. package/dist/types/utils/token/token-getters.d.ts.map +1 -0
  20. package/dist/types/utils/token/token-info-getters.d.ts +6 -0
  21. package/dist/types/utils/token/token-info-getters.d.ts.map +1 -0
  22. package/dist/types/utils/token/token-info.d.ts +3 -0
  23. package/dist/types/utils/token/token-info.d.ts.map +1 -0
  24. package/dist/types/utils/token/utils.d.ts +7 -0
  25. package/dist/types/utils/token/utils.d.ts.map +1 -0
  26. package/dist/types.js.map +1 -1
  27. package/dist/utils/network.js +2 -0
  28. package/dist/utils/network.js.map +1 -1
  29. package/dist/utils/token/batch-token-getters.js +175 -0
  30. package/dist/utils/token/batch-token-getters.js.map +1 -0
  31. package/dist/utils/token/index.js +22 -0
  32. package/dist/utils/token/index.js.map +1 -0
  33. package/dist/utils/token/token-getters.js +104 -0
  34. package/dist/utils/token/token-getters.js.map +1 -0
  35. package/dist/utils/token/token-info-getters.js +49 -0
  36. package/dist/utils/token/token-info-getters.js.map +1 -0
  37. package/dist/utils/{token-info.js → token/token-info.js} +1 -1
  38. package/dist/utils/token/token-info.js.map +1 -0
  39. package/dist/utils/token/utils.js +46 -0
  40. package/dist/utils/token/utils.js.map +1 -0
  41. package/package.json +2 -2
  42. package/src/services/getSocketAddresses.ts +3 -23
  43. package/src/transactions/executeTransaction.ts +9 -5
  44. package/src/types.ts +1 -11
  45. package/src/utils/network.ts +5 -2
  46. package/src/utils/token/batch-token-getters.ts +111 -0
  47. package/src/utils/token/index.ts +5 -0
  48. package/src/utils/token/token-getters.ts +71 -0
  49. package/src/utils/token/token-info-getters.ts +84 -0
  50. package/src/utils/{token-info.ts → token/token-info.ts} +1 -1
  51. package/src/utils/token/utils.ts +49 -0
  52. package/dist/types/utils/token-info.d.ts +0 -3
  53. package/dist/types/utils/token-info.d.ts.map +0 -1
  54. package/dist/types/utils/token.d.ts +0 -49
  55. package/dist/types/utils/token.d.ts.map +0 -1
  56. package/dist/utils/token-info.js.map +0 -1
  57. package/dist/utils/token.js +0 -340
  58. package/dist/utils/token.js.map +0 -1
  59. package/src/utils/token.ts +0 -317
@@ -1,317 +0,0 @@
1
- import { ethers, Provider } from 'ethers';
2
-
3
- import { Contract, Signer } from 'ethers';
4
- import { TOKEN_INFO } from './token-info';
5
- import { exponentialBackoff } from './retry';
6
- import {
7
- Address,
8
- GetERC20AllowanceParams,
9
- MoneyInOutChainId,
10
- ReyaChainId,
11
- TokenInfo,
12
- TokenName,
13
- } from '../types';
14
- import { ChainRpcUrls } from './network';
15
- export const descale = (tokenDecimals: number) => {
16
- const f = (value: bigint) => {
17
- return Number(ethers.formatUnits(value.toString(), tokenDecimals));
18
- };
19
-
20
- return f;
21
- };
22
-
23
- export const scale = (tokenDecimals: number): ((value: number) => bigint) => {
24
- return (value: number): bigint => {
25
- // Convert the number to a string
26
- const valueStr = value.toString();
27
- // Split the string into whole and fractional parts
28
- // eslint-disable-next-line prefer-const
29
- let [whole, fractional = ''] = valueStr.split('.');
30
- // Ensure the fractional part is the correct length, padding with zeros if necessary
31
- fractional = fractional
32
- .padEnd(tokenDecimals, '0')
33
- .substring(0, tokenDecimals);
34
- // Concatenate the whole part with the correctly length-limited fractional part
35
- const scaledValueStr = whole + fractional;
36
- // Convert the concatenated string to a BigInt, assuming it represents a value in wei (or equivalent smallest unit)
37
- // This is safe as we're not losing precision - just scaling the number to its smallest unit representation
38
- return BigInt(scaledValueStr);
39
- };
40
- };
41
-
42
- export const getERC20TokenContract = (
43
- tokenAddress: string,
44
- subject: Signer | Provider,
45
- ): Contract => {
46
- const abi: string[] = [
47
- `function approve(address, uint256) external returns (bool)`,
48
- `function balanceOf(address) external view returns (uint256)`,
49
- `function allowance(address,address) external view returns (uint256)`,
50
- `function approve(address,uint256) external returns (bool)`,
51
- ];
52
-
53
- const contract = new Contract(tokenAddress, abi, subject);
54
-
55
- return contract;
56
- };
57
-
58
- export const convertToAddress = (str: string): Address => {
59
- return str.toLowerCase() as Address;
60
- };
61
-
62
- export const getTokenInfoByAddress = (
63
- tokenAddressParam: Address | string,
64
- chainId?: ReyaChainId | MoneyInOutChainId,
65
- ): TokenInfo => {
66
- const tokenAddress =
67
- typeof tokenAddressParam === 'string'
68
- ? convertToAddress(tokenAddressParam)
69
- : tokenAddressParam;
70
-
71
- if (chainId) {
72
- const tokenInfo = TOKEN_INFO[chainId].find(
73
- (tokenInfo) => tokenInfo.address === tokenAddress,
74
- );
75
- if (!tokenInfo) {
76
- throw new Error(
77
- `getTokenInfoByAddress: no token info found [chainId ${chainId}, token address ${tokenAddress}]`,
78
- );
79
- }
80
- return tokenInfo;
81
- } else {
82
- for (const chainId in TOKEN_INFO) {
83
- const tokenInfos =
84
- TOKEN_INFO[chainId as unknown as keyof typeof TOKEN_INFO];
85
- const tokenInfo = tokenInfos.find(
86
- (tokenInfo) => tokenInfo.address === tokenAddress,
87
- );
88
- if (tokenInfo) {
89
- return tokenInfo;
90
- }
91
- }
92
- throw new Error(
93
- `getTokenInfoByAddress: no token info found [token address ${tokenAddress}]`,
94
- );
95
- }
96
- };
97
-
98
- export const getTokenInfoByName = (
99
- tokenName: TokenName,
100
- chainId: ReyaChainId | MoneyInOutChainId,
101
- ): TokenInfo => {
102
- const tokenInfo = TOKEN_INFO[chainId].find(
103
- (tokenInfo) => tokenInfo.name === tokenName,
104
- );
105
-
106
- if (!tokenInfo) {
107
- throw new Error(
108
- `getTokenInfoByName: no token info found [chain id ${chainId}, token name ${tokenName}]`,
109
- );
110
- }
111
-
112
- return tokenInfo;
113
- };
114
-
115
- export const getTokenInfosByChain = (
116
- chainId: ReyaChainId | MoneyInOutChainId,
117
- ): TokenInfo[] => {
118
- return TOKEN_INFO[chainId];
119
- };
120
-
121
- export const getRUSDUnderlyingTokenInfo = (
122
- chainId: ReyaChainId | MoneyInOutChainId,
123
- ): TokenInfo => {
124
- const tokenInfo = TOKEN_INFO[chainId].find(
125
- (tokenInfo) => tokenInfo.isRUSDUnderlying === true,
126
- );
127
-
128
- if (!tokenInfo) {
129
- throw new Error(
130
- `getRUSDUnderlyingTokenInfo: no token info found [chain id ${chainId}]`,
131
- );
132
- }
133
-
134
- return tokenInfo;
135
- };
136
-
137
- export const getERC20Allowance = async ({
138
- walletAddress,
139
- tokenAddress,
140
- spenderAddress,
141
- subject,
142
- }: GetERC20AllowanceParams): Promise<number> => {
143
- const tokenContract = getERC20TokenContract(tokenAddress, subject);
144
- const tokenInfo = getTokenInfoByAddress(tokenAddress);
145
-
146
- const allowance = await exponentialBackoff(() =>
147
- tokenContract.allowance(walletAddress, spenderAddress),
148
- );
149
-
150
- return BigInt(allowance) > scale(tokenInfo.decimals)(Number.MAX_SAFE_INTEGER)
151
- ? Number.MAX_SAFE_INTEGER
152
- : descale(tokenInfo.decimals)(allowance);
153
- };
154
-
155
- export type GetERC20BalanceArgs = {
156
- tokenAddress: string;
157
- walletAddress: string;
158
- subject: Signer | Provider;
159
- };
160
-
161
- export type GetETHBalanceArgs = {
162
- walletAddress: string;
163
- subject: Signer | Provider;
164
- };
165
-
166
- export type GetETHBalanceBatchArgs = {
167
- walletAddress: string;
168
- chain: MoneyInOutChainId;
169
- };
170
-
171
- export type GetERC20BalanceBatchArgs = {
172
- tokenAddress: string;
173
- walletAddress: string;
174
- chain: MoneyInOutChainId;
175
- };
176
-
177
- export type GetReyaETHBalanceArgs = {
178
- walletAddress: string;
179
- chain: ReyaChainId;
180
- };
181
-
182
- export type GetReyaERC20BalanceArgs = {
183
- tokenAddress: string;
184
- walletAddress: string;
185
- chain: ReyaChainId;
186
- };
187
-
188
- export type GetBalancesForBridgeArgs = {
189
- walletAddress: string;
190
- };
191
-
192
- export const getERC20Balance = async ({
193
- tokenAddress,
194
- walletAddress,
195
- subject,
196
- }: GetERC20BalanceArgs): Promise<number> => {
197
- const token = getERC20TokenContract(tokenAddress, subject);
198
-
199
- const tokenInfo = getTokenInfoByAddress(convertToAddress(tokenAddress));
200
-
201
- const currentBalance = await exponentialBackoff(() =>
202
- token.balanceOf(walletAddress),
203
- );
204
-
205
- return descale(tokenInfo.decimals)(currentBalance);
206
- };
207
-
208
- export const getETHBalance = async ({
209
- walletAddress,
210
- subject,
211
- }: GetETHBalanceArgs): Promise<number> => {
212
- const provider = subject.provider;
213
- if (!provider) throw new Error("User's provider not found");
214
-
215
- const currentBalance = await exponentialBackoff(() =>
216
- provider.getBalance(ethers.getAddress(walletAddress)),
217
- );
218
-
219
- return descale(18)(currentBalance);
220
- };
221
-
222
- export const getETHBalanceBatch = async ({
223
- walletAddress,
224
- chain,
225
- }: GetETHBalanceBatchArgs): Promise<number> => {
226
- const rpcUrls = ChainRpcUrls[chain];
227
- if (!rpcUrls || rpcUrls.length === 0) {
228
- throw new Error('No RPC URLs configured for the chain: ' + chain);
229
- }
230
-
231
- let lastError = null;
232
-
233
- for (const url of rpcUrls) {
234
- try {
235
- const provider = new ethers.JsonRpcProvider(url);
236
- const currentBalance = await exponentialBackoff(() =>
237
- provider.getBalance(ethers.getAddress(walletAddress)),
238
- );
239
- return descale(18)(currentBalance);
240
- } catch (error) {
241
- lastError = error;
242
- }
243
- }
244
-
245
- throw new Error('All RPC URLs failed; last error: ' + lastError);
246
- };
247
-
248
- export const getERC20BalanceBatch = async ({
249
- tokenAddress,
250
- walletAddress,
251
- chain,
252
- }: GetERC20BalanceBatchArgs): Promise<number> => {
253
- const rpcUrls = ChainRpcUrls[chain];
254
- if (!rpcUrls || rpcUrls.length === 0) {
255
- throw new Error('No RPC URLs configured for the chain: ' + chain);
256
- }
257
-
258
- let lastError = null;
259
-
260
- for (const url of rpcUrls) {
261
- try {
262
- const provider = new ethers.JsonRpcProvider(url);
263
- const token = getERC20TokenContract(tokenAddress, provider);
264
- const currentBalance = await token.balanceOf(walletAddress);
265
-
266
- const tokenInfo = getTokenInfoByAddress(convertToAddress(tokenAddress));
267
- return descale(tokenInfo.decimals)(currentBalance);
268
- } catch (error) {
269
- lastError = error;
270
- }
271
- }
272
-
273
- throw new Error('All RPC URLs failed; last error: ' + lastError);
274
- };
275
-
276
- export const getReyaETHBalance = async ({
277
- walletAddress,
278
- chain,
279
- }: GetReyaETHBalanceArgs): Promise<number> => {
280
- try {
281
- const provider = ethers.getDefaultProvider(
282
- chain === ReyaChainId.reyaCronos
283
- ? 'https://rpc.reya-cronos.gelato.digital'
284
- : 'https://rpc.reya.network',
285
- );
286
- const currentBalance = await exponentialBackoff(() =>
287
- provider.getBalance(ethers.getAddress(walletAddress)),
288
- );
289
-
290
- return descale(18)(currentBalance);
291
- } catch (error) {
292
- throw new Error('All RPC URLs failed; last error: ' + error);
293
- }
294
- };
295
-
296
- export const getReyaERC20Balance = async ({
297
- tokenAddress,
298
- walletAddress,
299
- chain,
300
- }: GetReyaERC20BalanceArgs): Promise<number> => {
301
- try {
302
- const provider = ethers.getDefaultProvider(
303
- chain === ReyaChainId.reyaCronos
304
- ? 'https://rpc.reya-cronos.gelato.digital'
305
- : 'https://rpc.reya.network',
306
- );
307
- const token = getERC20TokenContract(tokenAddress, provider);
308
- const currentBalance = await exponentialBackoff(() =>
309
- token.balanceOf(walletAddress),
310
- );
311
-
312
- const tokenInfo = getTokenInfoByAddress(convertToAddress(tokenAddress));
313
- return descale(tokenInfo.decimals)(currentBalance);
314
- } catch (error) {
315
- throw new Error('All RPC URLs failed; last error: ' + error);
316
- }
317
- };