@reyaxyz/sdk 0.1.0 → 0.2.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 (66) hide show
  1. package/dist/services/executeTransaction.js +9 -7
  2. package/dist/services/executeTransaction.js.map +1 -1
  3. package/dist/services/lp/lp.js +3 -2
  4. package/dist/services/lp/lp.js.map +1 -1
  5. package/dist/services/margin-accounts/account.js +2 -1
  6. package/dist/services/margin-accounts/account.js.map +1 -1
  7. package/dist/services/margin-accounts/deposit.js +2 -1
  8. package/dist/services/margin-accounts/deposit.js.map +1 -1
  9. package/dist/services/margin-accounts/withdraw.js +2 -1
  10. package/dist/services/margin-accounts/withdraw.js.map +1 -1
  11. package/dist/services/orders/order.js +2 -1
  12. package/dist/services/orders/order.js.map +1 -1
  13. package/dist/services/token/approve.js +95 -0
  14. package/dist/services/token/approve.js.map +1 -0
  15. package/dist/services/token/common.js +107 -0
  16. package/dist/services/token/common.js.map +1 -0
  17. package/dist/services/token/consts.js +29 -0
  18. package/dist/services/token/consts.js.map +1 -0
  19. package/dist/services/token/getAllowance.js +68 -0
  20. package/dist/services/token/getAllowance.js.map +1 -0
  21. package/dist/services/token/types.js +9 -0
  22. package/dist/services/token/types.js.map +1 -0
  23. package/dist/types/services/executeTransaction.d.ts +3 -2
  24. package/dist/types/services/executeTransaction.d.ts.map +1 -1
  25. package/dist/types/services/lp/lp.d.ts.map +1 -1
  26. package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
  27. package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
  28. package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
  29. package/dist/types/services/orders/order.d.ts.map +1 -1
  30. package/dist/types/services/token/approve.d.ts +3 -0
  31. package/dist/types/services/token/approve.d.ts.map +1 -0
  32. package/dist/types/services/token/common.d.ts +14 -0
  33. package/dist/types/services/token/common.d.ts.map +1 -0
  34. package/dist/types/services/token/consts.d.ts +8 -0
  35. package/dist/types/services/token/consts.d.ts.map +1 -0
  36. package/dist/types/services/token/getAllowance.d.ts +3 -0
  37. package/dist/types/services/token/getAllowance.d.ts.map +1 -0
  38. package/dist/types/services/token/types.d.ts +24 -0
  39. package/dist/types/services/token/types.d.ts.map +1 -0
  40. package/dist/types/utils/consts.d.ts +8 -0
  41. package/dist/types/utils/consts.d.ts.map +1 -0
  42. package/dist/types/utils/contractAddresses.d.ts +5 -0
  43. package/dist/types/utils/contractAddresses.d.ts.map +1 -0
  44. package/dist/types/utils/retry.d.ts +2 -0
  45. package/dist/types/utils/retry.d.ts.map +1 -0
  46. package/dist/utils/consts.js +12 -0
  47. package/dist/utils/consts.js.map +1 -0
  48. package/dist/utils/contractAddresses.js +27 -0
  49. package/dist/utils/contractAddresses.js.map +1 -0
  50. package/dist/utils/retry.js +94 -0
  51. package/dist/utils/retry.js.map +1 -0
  52. package/package.json +2 -2
  53. package/src/services/executeTransaction.ts +15 -3
  54. package/src/services/lp/lp.ts +3 -0
  55. package/src/services/margin-accounts/account.ts +2 -0
  56. package/src/services/margin-accounts/deposit.ts +2 -0
  57. package/src/services/margin-accounts/withdraw.ts +2 -0
  58. package/src/services/orders/order.ts +2 -0
  59. package/src/services/token/approve.ts +52 -0
  60. package/src/services/token/common.ts +84 -0
  61. package/src/services/token/consts.ts +32 -0
  62. package/src/services/token/getAllowance.ts +24 -0
  63. package/src/services/token/types.ts +27 -0
  64. package/src/utils/consts.ts +7 -0
  65. package/src/utils/contractAddresses.ts +37 -0
  66. package/src/utils/retry.ts +40 -0
@@ -0,0 +1,37 @@
1
+ export type Address = Lowercase<string>;
2
+
3
+ // @todo update with proper chainId for mainnet
4
+ export type SupportedChainId = 80001 | 999;
5
+
6
+ export type ContractType = 'core_proxy' | 'passive_pool_proxy';
7
+
8
+ const addresses: Record<SupportedChainId, Record<ContractType, Address>> = {
9
+ 80001: {
10
+ core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
11
+ passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
12
+ },
13
+ 999: {
14
+ core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
15
+ passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
16
+ },
17
+ };
18
+
19
+ // @todo update values for mainnet
20
+
21
+ export const getAddress = (
22
+ chainId: number,
23
+ contractName: ContractType,
24
+ ): string => {
25
+ const keyChainId = chainId.toString();
26
+
27
+ if (!Object.keys(addresses).includes(keyChainId)) {
28
+ return `Unspecified addresses for chain id ${keyChainId}`;
29
+ }
30
+
31
+ const networkAddresses = addresses[chainId as SupportedChainId];
32
+ if (!Object.keys(networkAddresses).includes(contractName)) {
33
+ return `Unspecified address for ${contractName} contract`;
34
+ }
35
+
36
+ return networkAddresses[contractName];
37
+ };
@@ -0,0 +1,40 @@
1
+ // It introduces a delay of ms milliseconds
2
+ function delay(ms: number): Promise<unknown> {
3
+ return new Promise((resolve) => setTimeout(resolve, ms));
4
+ }
5
+
6
+ // It returns a random number between [0, max-1]
7
+ function getRandomNumber(max: number): number {
8
+ return Math.floor(Math.random() * max);
9
+ }
10
+
11
+ // It retries a call for a few times while waiting an exponential time before each new attempt.
12
+ // You pass the query and the number of attempts you want to use and it performs as follows:
13
+ // 1. Initiates the waiting time to 1s
14
+ // 2. Tries to fetch the data
15
+ // 3.1. If it succeeds, it returns the data
16
+ // 3.2. If it fails, it waits, it doubles the waiting time and go to point 2. if the number of attemps is not reached
17
+ export const exponentialBackoff = async <T = never>(
18
+ query: () => Promise<T>,
19
+ attempts = 5,
20
+ factor = 2,
21
+ ): Promise<T> => {
22
+ let waitingTime = 1000;
23
+
24
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
25
+ try {
26
+ const data = await query();
27
+
28
+ return data;
29
+ } catch (error) {
30
+ if (attempt + 1 === attempts) {
31
+ throw error;
32
+ }
33
+ }
34
+
35
+ await delay(waitingTime + getRandomNumber(100));
36
+ waitingTime *= factor;
37
+ }
38
+
39
+ throw new Error('Retry loop failed unexpectedly');
40
+ };