@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.
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/services/executeTransaction.js.map +1 -1
- package/dist/services/lp/index.js +19 -0
- package/dist/services/lp/index.js.map +1 -0
- package/dist/services/lp/lp.js +28 -9
- package/dist/services/lp/lp.js.map +1 -1
- package/dist/services/lp/types.js.map +1 -1
- package/dist/services/margin-accounts/account.js.map +1 -1
- package/dist/services/margin-accounts/deposit.js.map +1 -1
- package/dist/services/margin-accounts/index.js +21 -0
- package/dist/services/margin-accounts/index.js.map +1 -0
- package/dist/services/margin-accounts/types.js.map +1 -1
- package/dist/services/margin-accounts/withdraw.js.map +1 -1
- package/dist/services/orders/index.js +19 -0
- package/dist/services/orders/index.js.map +1 -0
- package/dist/services/orders/order.js +13 -4
- package/dist/services/orders/order.js.map +1 -1
- package/dist/services/orders/types.js.map +1 -1
- package/dist/services/token/approve.js +4 -3
- package/dist/services/token/approve.js.map +1 -1
- package/dist/services/token/common.js.map +1 -1
- package/dist/services/token/getAllowance.js +4 -3
- package/dist/services/token/getAllowance.js.map +1 -1
- package/dist/services/token/index.js +20 -0
- package/dist/services/token/index.js.map +1 -0
- package/dist/services/token/types.js.map +1 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/executeTransaction.d.ts +3 -3
- package/dist/types/services/executeTransaction.d.ts.map +1 -1
- package/dist/types/services/lp/index.d.ts +3 -0
- package/dist/types/services/lp/index.d.ts.map +1 -0
- package/dist/types/services/lp/lp.d.ts +3 -3
- package/dist/types/services/lp/lp.d.ts.map +1 -1
- package/dist/types/services/lp/types.d.ts +12 -11
- package/dist/types/services/lp/types.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/account.d.ts +2 -2
- package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/deposit.d.ts +2 -2
- package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/index.d.ts +5 -0
- package/dist/types/services/margin-accounts/index.d.ts.map +1 -0
- package/dist/types/services/margin-accounts/types.d.ts +7 -7
- package/dist/types/services/margin-accounts/types.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/withdraw.d.ts +2 -2
- package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
- package/dist/types/services/orders/index.d.ts +3 -0
- package/dist/types/services/orders/index.d.ts.map +1 -0
- package/dist/types/services/orders/order.d.ts +2 -2
- package/dist/types/services/orders/order.d.ts.map +1 -1
- package/dist/types/services/orders/types.d.ts +14 -9
- package/dist/types/services/orders/types.d.ts.map +1 -1
- package/dist/types/services/token/approve.d.ts +2 -2
- package/dist/types/services/token/approve.d.ts.map +1 -1
- package/dist/types/services/token/common.d.ts +2 -2
- package/dist/types/services/token/common.d.ts.map +1 -1
- package/dist/types/services/token/getAllowance.d.ts +2 -2
- package/dist/types/services/token/getAllowance.d.ts.map +1 -1
- package/dist/types/services/token/index.d.ts +4 -0
- package/dist/types/services/token/index.d.ts.map +1 -0
- package/dist/types/services/token/types.d.ts +6 -8
- package/dist/types/services/token/types.d.ts.map +1 -1
- package/dist/types/utils/lp.d.ts +4 -0
- package/dist/types/utils/lp.d.ts.map +1 -0
- package/dist/types/utils/trade.d.ts +3 -0
- package/dist/types/utils/trade.d.ts.map +1 -0
- package/dist/utils/lp.js +28 -0
- package/dist/utils/lp.js.map +1 -0
- package/dist/utils/trade.js +33 -0
- package/dist/utils/trade.js.map +1 -0
- package/package.json +3 -2
- package/src/index.ts +4 -1
- package/src/services/executeTransaction.ts +3 -3
- package/src/services/lp/index.ts +2 -0
- package/src/services/lp/lp.ts +49 -16
- package/src/services/lp/types.ts +14 -11
- package/src/services/margin-accounts/account.ts +2 -2
- package/src/services/margin-accounts/deposit.ts +2 -2
- package/src/services/margin-accounts/index.ts +4 -0
- package/src/services/margin-accounts/types.ts +7 -7
- package/src/services/margin-accounts/withdraw.ts +2 -2
- package/src/services/orders/index.ts +2 -0
- package/src/services/orders/order.ts +33 -11
- package/src/services/orders/types.ts +16 -9
- package/src/services/token/approve.ts +7 -4
- package/src/services/token/common.ts +2 -2
- package/src/services/token/getAllowance.ts +7 -4
- package/src/services/token/index.ts +3 -0
- package/src/services/token/types.ts +9 -11
- package/src/utils/lp.ts +27 -0
- package/src/utils/trade.ts +39 -0
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { ApprovalType,
|
|
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:
|
|
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(
|
|
18
|
-
: getAddress(
|
|
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 {
|
|
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
|
-
}:
|
|
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,
|
|
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:
|
|
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(
|
|
13
|
-
: getAddress(
|
|
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({
|
|
@@ -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
|
|
9
|
-
signer: Signer;
|
|
10
|
-
|
|
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
|
|
16
|
-
signer: Signer;
|
|
17
|
-
|
|
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
|
|
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
|
};
|
package/src/utils/lp.ts
ADDED
|
@@ -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
|
+
}
|