@reyaxyz/sdk 0.2.0 → 0.3.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 (92) hide show
  1. package/dist/index.js +4 -1
  2. package/dist/index.js.map +1 -1
  3. package/dist/services/executeTransaction.js.map +1 -1
  4. package/dist/services/lp/index.js +19 -0
  5. package/dist/services/lp/index.js.map +1 -0
  6. package/dist/services/lp/lp.js +28 -9
  7. package/dist/services/lp/lp.js.map +1 -1
  8. package/dist/services/lp/types.js.map +1 -1
  9. package/dist/services/margin-accounts/account.js.map +1 -1
  10. package/dist/services/margin-accounts/deposit.js.map +1 -1
  11. package/dist/services/margin-accounts/index.js +21 -0
  12. package/dist/services/margin-accounts/index.js.map +1 -0
  13. package/dist/services/margin-accounts/types.js.map +1 -1
  14. package/dist/services/margin-accounts/withdraw.js.map +1 -1
  15. package/dist/services/orders/index.js +19 -0
  16. package/dist/services/orders/index.js.map +1 -0
  17. package/dist/services/orders/order.js +13 -4
  18. package/dist/services/orders/order.js.map +1 -1
  19. package/dist/services/orders/types.js.map +1 -1
  20. package/dist/services/token/approve.js +4 -3
  21. package/dist/services/token/approve.js.map +1 -1
  22. package/dist/services/token/common.js.map +1 -1
  23. package/dist/services/token/getAllowance.js +4 -3
  24. package/dist/services/token/getAllowance.js.map +1 -1
  25. package/dist/services/token/index.js +20 -0
  26. package/dist/services/token/index.js.map +1 -0
  27. package/dist/services/token/types.js.map +1 -1
  28. package/dist/types/index.d.ts +4 -1
  29. package/dist/types/index.d.ts.map +1 -1
  30. package/dist/types/services/executeTransaction.d.ts +3 -3
  31. package/dist/types/services/executeTransaction.d.ts.map +1 -1
  32. package/dist/types/services/lp/index.d.ts +3 -0
  33. package/dist/types/services/lp/index.d.ts.map +1 -0
  34. package/dist/types/services/lp/lp.d.ts +3 -3
  35. package/dist/types/services/lp/lp.d.ts.map +1 -1
  36. package/dist/types/services/lp/types.d.ts +12 -11
  37. package/dist/types/services/lp/types.d.ts.map +1 -1
  38. package/dist/types/services/margin-accounts/account.d.ts +2 -2
  39. package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
  40. package/dist/types/services/margin-accounts/deposit.d.ts +2 -2
  41. package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
  42. package/dist/types/services/margin-accounts/index.d.ts +5 -0
  43. package/dist/types/services/margin-accounts/index.d.ts.map +1 -0
  44. package/dist/types/services/margin-accounts/types.d.ts +7 -7
  45. package/dist/types/services/margin-accounts/types.d.ts.map +1 -1
  46. package/dist/types/services/margin-accounts/withdraw.d.ts +2 -2
  47. package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
  48. package/dist/types/services/orders/index.d.ts +3 -0
  49. package/dist/types/services/orders/index.d.ts.map +1 -0
  50. package/dist/types/services/orders/order.d.ts +2 -2
  51. package/dist/types/services/orders/order.d.ts.map +1 -1
  52. package/dist/types/services/orders/types.d.ts +14 -9
  53. package/dist/types/services/orders/types.d.ts.map +1 -1
  54. package/dist/types/services/token/approve.d.ts +2 -2
  55. package/dist/types/services/token/approve.d.ts.map +1 -1
  56. package/dist/types/services/token/common.d.ts +2 -2
  57. package/dist/types/services/token/common.d.ts.map +1 -1
  58. package/dist/types/services/token/getAllowance.d.ts +2 -2
  59. package/dist/types/services/token/getAllowance.d.ts.map +1 -1
  60. package/dist/types/services/token/index.d.ts +4 -0
  61. package/dist/types/services/token/index.d.ts.map +1 -0
  62. package/dist/types/services/token/types.d.ts +6 -8
  63. package/dist/types/services/token/types.d.ts.map +1 -1
  64. package/dist/types/utils/lp.d.ts +4 -0
  65. package/dist/types/utils/lp.d.ts.map +1 -0
  66. package/dist/types/utils/trade.d.ts +3 -0
  67. package/dist/types/utils/trade.d.ts.map +1 -0
  68. package/dist/utils/lp.js +28 -0
  69. package/dist/utils/lp.js.map +1 -0
  70. package/dist/utils/trade.js +33 -0
  71. package/dist/utils/trade.js.map +1 -0
  72. package/package.json +3 -2
  73. package/src/index.ts +4 -1
  74. package/src/services/executeTransaction.ts +3 -3
  75. package/src/services/lp/index.ts +2 -0
  76. package/src/services/lp/lp.ts +49 -16
  77. package/src/services/lp/types.ts +14 -11
  78. package/src/services/margin-accounts/account.ts +2 -2
  79. package/src/services/margin-accounts/deposit.ts +2 -2
  80. package/src/services/margin-accounts/index.ts +4 -0
  81. package/src/services/margin-accounts/types.ts +7 -7
  82. package/src/services/margin-accounts/withdraw.ts +2 -2
  83. package/src/services/orders/index.ts +2 -0
  84. package/src/services/orders/order.ts +33 -11
  85. package/src/services/orders/types.ts +16 -9
  86. package/src/services/token/approve.ts +7 -4
  87. package/src/services/token/common.ts +2 -2
  88. package/src/services/token/getAllowance.ts +7 -4
  89. package/src/services/token/index.ts +3 -0
  90. package/src/services/token/types.ts +9 -11
  91. package/src/utils/lp.ts +27 -0
  92. package/src/utils/trade.ts +39 -0
@@ -1,12 +1,15 @@
1
- import { ApprovalType, ApproveTokenArgs } from './types';
1
+ import { ApprovalType, ApproveTokenParams } from './types';
2
2
  import { getERC20Allowance, getERC20TokenContract } from './common';
3
3
  import { getAddress } from '../../utils/contractAddresses';
4
4
 
5
5
  export const approveTokenSpending = async (
6
- params: ApproveTokenArgs,
6
+ params: ApproveTokenParams,
7
7
  ): Promise<number> => {
8
8
  const underLyingTokenAddress = params.token;
9
9
 
10
+ // @todo update once we know this
11
+ const chainId = 80001;
12
+
10
13
  const tokenContract = getERC20TokenContract(
11
14
  underLyingTokenAddress,
12
15
  params.signer,
@@ -14,8 +17,8 @@ export const approveTokenSpending = async (
14
17
 
15
18
  const contractProxyAddress =
16
19
  params.type === ApprovalType.DEPOSIT
17
- ? getAddress(params.chainId, 'core_proxy')
18
- : getAddress(params.chainId, 'passive_pool_proxy');
20
+ ? getAddress(chainId, 'core_proxy')
21
+ : getAddress(chainId, 'passive_pool_proxy');
19
22
 
20
23
  // @todo validate if maxAllowance should be passed from UI
21
24
  const maxUint256Bn = BigInt(
@@ -1,7 +1,7 @@
1
1
  import { Contract, ethers, Signer } from 'ethers';
2
2
  import { tokenDetails } from './consts';
3
3
  import { exponentialBackoff } from '../../utils/retry';
4
- import { GetERC20AllowanceArgs } from './types';
4
+ import { GetERC20AllowanceParams } from './types';
5
5
 
6
6
  export const descale = (tokenDecimals: number) => {
7
7
  const f = (value: bigint) => {
@@ -70,7 +70,7 @@ export const getERC20Allowance = async ({
70
70
  tokenAddress,
71
71
  spenderAddress,
72
72
  subject,
73
- }: GetERC20AllowanceArgs): Promise<number> => {
73
+ }: GetERC20AllowanceParams): Promise<number> => {
74
74
  const tokenContract = getERC20TokenContract(tokenAddress, subject);
75
75
  const { tokenDecimals } = getTokenDetails(tokenAddress);
76
76
 
@@ -1,16 +1,19 @@
1
- import { ApprovalType, GetAllowanceArgs } from './types';
1
+ import { ApprovalType, GetAllowanceParams } from './types';
2
2
  import { getERC20Allowance } from './common';
3
3
  import { getAddress } from '../../utils/contractAddresses';
4
4
 
5
5
  export const getAllowance = async (
6
- params: GetAllowanceArgs,
6
+ params: GetAllowanceParams,
7
7
  ): Promise<number> => {
8
8
  const underLyingTokenAddress = params.token;
9
9
 
10
+ // @todo update
11
+ const chainId = 80001;
12
+
10
13
  const contractProxyAddress =
11
14
  params.type === ApprovalType.DEPOSIT
12
- ? getAddress(params.chainId, 'core_proxy')
13
- : getAddress(params.chainId, 'passive_pool_proxy');
15
+ ? getAddress(chainId, 'core_proxy')
16
+ : getAddress(chainId, 'passive_pool_proxy');
14
17
  const walletAddress: string = await params.signer.getAddress();
15
18
 
16
19
  const allowance = await getERC20Allowance({
@@ -0,0 +1,3 @@
1
+ export * from './types';
2
+ export * from './approve';
3
+ export * from './getAllowance';
@@ -1,27 +1,25 @@
1
- import { Signer } from 'ethers';
1
+ import { JsonRpcSigner, Signer } from 'ethers';
2
2
 
3
3
  export enum ApprovalType {
4
4
  LP,
5
5
  DEPOSIT,
6
6
  }
7
7
 
8
- export type ApproveTokenArgs = {
9
- signer: Signer;
10
- chainId: number;
11
- token: string;
8
+ export type ApproveTokenParams = {
9
+ signer: Signer | JsonRpcSigner;
10
+ token: string; // token address
12
11
  type: ApprovalType;
13
12
  };
14
13
 
15
- export type GetAllowanceArgs = {
16
- signer: Signer;
17
- chainId: number;
18
- token: string;
14
+ export type GetAllowanceParams = {
15
+ signer: Signer | JsonRpcSigner;
16
+ token: string; // token address
19
17
  type: ApprovalType;
20
18
  };
21
19
 
22
- export type GetERC20AllowanceArgs = {
20
+ export type GetERC20AllowanceParams = {
23
21
  walletAddress: string;
24
- tokenAddress: string;
22
+ tokenAddress: string; // LP POOL entity tokean address
25
23
  spenderAddress: string;
26
24
  subject: Signer;
27
25
  };
@@ -0,0 +1,27 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ export function calculateMinSharesForLp(lpAmount: number): number {
4
+ const amount = BigNumber(lpAmount);
5
+
6
+ // Calculate 1% // @todo update this number once we have it
7
+ const offset = amount.times(0.99);
8
+
9
+ return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed
10
+ }
11
+
12
+ export function calculateMinOutForLp(sharesAmount: number): number {
13
+ const amount = BigNumber(sharesAmount);
14
+
15
+ // Calculate 1% // @todo update once we have the exact number
16
+ const offset = amount.times(0.99);
17
+
18
+ return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed
19
+ }
20
+
21
+ export function expandLpAmount(amount: number): number {
22
+ const decimalFactor = BigNumber('1000000'); // 10^6
23
+
24
+ const amountDenormalised = BigNumber(amount).times(decimalFactor).toFixed(0);
25
+
26
+ return Number(amountDenormalised);
27
+ }
@@ -0,0 +1,39 @@
1
+ import BigNumber from 'bignumber.js';
2
+
3
+ export function calculatePriceLimitForTrade(
4
+ orderPrice: number,
5
+ size: number,
6
+ ): bigint {
7
+ // Ensure orderPrice is BigInt
8
+ const orderPriceBigInt = BigNumber(orderPrice);
9
+
10
+ // Calculate 50% of the order price
11
+ const offset = orderPriceBigInt.div(2);
12
+
13
+ // Determine if the size is positive or negative and adjust the price accordingly
14
+ if (size > 0) {
15
+ // Trade is long, so add
16
+ return BigInt(
17
+ BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),
18
+ );
19
+ } else {
20
+ return BigInt(
21
+ BigNumber(orderPriceBigInt).minus(offset).times(1e18).toFixed(0),
22
+ );
23
+ }
24
+ }
25
+
26
+ export function calculateOrderBaseFromOrderSize(
27
+ orderSize: number,
28
+ currentPrice: number,
29
+ ): bigint {
30
+ const decimalFactor = BigNumber('1000000'); // 10^6
31
+ // @todo check decimal places handling if this should stay hardcoded to 6
32
+
33
+ const orderBase = BigNumber(orderSize)
34
+ .times(decimalFactor)
35
+ .div(currentPrice)
36
+ .toFixed(0);
37
+
38
+ return BigInt(orderBase);
39
+ }