@reyaxyz/sdk 0.2.1 → 0.4.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/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/types.js.map +1 -1
- package/dist/services/margin-accounts/withdraw.js.map +1 -1
- package/dist/services/orders/order.js +28 -2
- 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/types.js.map +1 -1
- 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 +9 -8
- 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/types.d.ts +3 -3
- 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/order.d.ts +3 -2
- package/dist/types/services/orders/order.d.ts.map +1 -1
- package/dist/types/services/orders/types.d.ts +13 -4
- 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/types.d.ts +3 -5
- 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 +2 -2
- package/dist/types/utils/trade.d.ts.map +1 -1
- package/dist/utils/lp.js +28 -0
- package/dist/utils/lp.js.map +1 -0
- package/dist/utils/trade.js +18 -12
- package/dist/utils/trade.js.map +1 -1
- package/package.json +3 -2
- package/src/services/lp/lp.ts +49 -16
- package/src/services/lp/types.ts +11 -8
- package/src/services/margin-accounts/account.ts +2 -2
- package/src/services/margin-accounts/deposit.ts +2 -2
- package/src/services/margin-accounts/types.ts +3 -3
- package/src/services/margin-accounts/withdraw.ts +2 -2
- package/src/services/orders/order.ts +47 -3
- package/src/services/orders/types.ts +15 -4
- 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/types.ts +6 -8
- package/src/utils/lp.ts +27 -0
- package/src/utils/trade.ts +21 -13
package/dist/utils/lp.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.expandLpAmount = exports.calculateMinOutForLp = exports.calculateMinSharesForLp = void 0;
|
|
7
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
function calculateMinSharesForLp(lpAmount) {
|
|
9
|
+
var amount = (0, bignumber_js_1.default)(lpAmount);
|
|
10
|
+
// Calculate 1% // @todo update this number once we have it
|
|
11
|
+
var offset = amount.times(0.99);
|
|
12
|
+
return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed
|
|
13
|
+
}
|
|
14
|
+
exports.calculateMinSharesForLp = calculateMinSharesForLp;
|
|
15
|
+
function calculateMinOutForLp(sharesAmount) {
|
|
16
|
+
var amount = (0, bignumber_js_1.default)(sharesAmount);
|
|
17
|
+
// Calculate 1% // @todo update once we have the exact number
|
|
18
|
+
var offset = amount.times(0.99);
|
|
19
|
+
return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed
|
|
20
|
+
}
|
|
21
|
+
exports.calculateMinOutForLp = calculateMinOutForLp;
|
|
22
|
+
function expandLpAmount(amount) {
|
|
23
|
+
var decimalFactor = (0, bignumber_js_1.default)('1000000'); // 10^6
|
|
24
|
+
var amountDenormalised = (0, bignumber_js_1.default)(amount).times(decimalFactor).toFixed(0);
|
|
25
|
+
return Number(amountDenormalised);
|
|
26
|
+
}
|
|
27
|
+
exports.expandLpAmount = expandLpAmount;
|
|
28
|
+
//# sourceMappingURL=lp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lp.js","sourceRoot":"/","sources":["utils/lp.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,SAAgB,uBAAuB,CAAC,QAAgB;IACtD,IAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,QAAQ,CAAC,CAAC;IAEnC,2DAA2D;IAC3D,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAElC,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,6CAA6C;AAClG,CAAC;AAPD,0DAOC;AAED,SAAgB,oBAAoB,CAAC,YAAoB;IACvD,IAAM,MAAM,GAAG,IAAA,sBAAS,EAAC,YAAY,CAAC,CAAC;IAEvC,6DAA6D;IAC7D,IAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAElC,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,6CAA6C;AAClG,CAAC;AAPD,oDAOC;AAED,SAAgB,cAAc,CAAC,MAAc;IAC3C,IAAM,aAAa,GAAG,IAAA,sBAAS,EAAC,SAAS,CAAC,CAAC,CAAC,OAAO;IAEnD,IAAM,kBAAkB,GAAG,IAAA,sBAAS,EAAC,MAAM,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAE7E,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAC;AACpC,CAAC;AAND,wCAMC","sourcesContent":["import BigNumber from 'bignumber.js';\n\nexport function calculateMinSharesForLp(lpAmount: number): number {\n const amount = BigNumber(lpAmount);\n\n // Calculate 1% // @todo update this number once we have it\n const offset = amount.times(0.99);\n\n return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed\n}\n\nexport function calculateMinOutForLp(sharesAmount: number): number {\n const amount = BigNumber(sharesAmount);\n\n // Calculate 1% // @todo update once we have the exact number\n const offset = amount.times(0.99);\n\n return amount.minus(offset).times(1e6).toNumber(); // rUSD/USDC has 6 decimals, change if needed\n}\n\nexport function expandLpAmount(amount: number): number {\n const decimalFactor = BigNumber('1000000'); // 10^6\n\n const amountDenormalised = BigNumber(amount).times(decimalFactor).toFixed(0);\n\n return Number(amountDenormalised);\n}\n"]}
|
package/dist/utils/trade.js
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.calculateOrderBaseFromOrderSize = exports.calculatePriceLimitForTrade = void 0;
|
|
4
|
-
|
|
7
|
+
var bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
8
|
+
function calculatePriceLimitForTrade(orderPrice, amount) {
|
|
5
9
|
// Ensure orderPrice is BigInt
|
|
6
|
-
var orderPriceBigInt =
|
|
10
|
+
var orderPriceBigInt = (0, bignumber_js_1.default)(orderPrice);
|
|
7
11
|
// Calculate 50% of the order price
|
|
8
|
-
var offset = orderPriceBigInt
|
|
9
|
-
// Determine if the
|
|
10
|
-
if (
|
|
12
|
+
var offset = orderPriceBigInt.div(2);
|
|
13
|
+
// Determine if the amount is positive or negative and adjust the price accordingly
|
|
14
|
+
if (amount > 0) {
|
|
11
15
|
// Trade is long, so add
|
|
12
|
-
return (orderPriceBigInt
|
|
16
|
+
return BigInt((0, bignumber_js_1.default)(orderPriceBigInt).plus(offset).times(1e18).toFixed(0));
|
|
13
17
|
}
|
|
14
18
|
else {
|
|
15
|
-
|
|
16
|
-
return (orderPriceBigInt - offset) * BigInt(1e18);
|
|
19
|
+
return BigInt((0, bignumber_js_1.default)(orderPriceBigInt).minus(offset).times(1e18).toFixed(0));
|
|
17
20
|
}
|
|
18
21
|
}
|
|
19
22
|
exports.calculatePriceLimitForTrade = calculatePriceLimitForTrade;
|
|
20
23
|
function calculateOrderBaseFromOrderSize(orderSize, currentPrice) {
|
|
21
|
-
var decimalFactor =
|
|
22
|
-
// @todo check decimal places handling
|
|
23
|
-
var orderBase = (
|
|
24
|
-
|
|
24
|
+
var decimalFactor = (0, bignumber_js_1.default)('1000000'); // 10^6
|
|
25
|
+
// @todo check decimal places handling if this should stay hardcoded to 6
|
|
26
|
+
var orderBase = (0, bignumber_js_1.default)(orderSize)
|
|
27
|
+
.times(decimalFactor)
|
|
28
|
+
.div(currentPrice)
|
|
29
|
+
.toFixed(0);
|
|
30
|
+
return BigInt(orderBase);
|
|
25
31
|
}
|
|
26
32
|
exports.calculateOrderBaseFromOrderSize = calculateOrderBaseFromOrderSize;
|
|
27
33
|
//# sourceMappingURL=trade.js.map
|
package/dist/utils/trade.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trade.js","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"trade.js","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAqC;AAErC,SAAgB,2BAA2B,CACzC,UAAkB,EAClB,MAAc;IAEd,8BAA8B;IAC9B,IAAM,gBAAgB,GAAG,IAAA,sBAAS,EAAC,UAAU,CAAC,CAAC;IAE/C,mCAAmC;IACnC,IAAM,MAAM,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAEvC,mFAAmF;IACnF,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QACf,wBAAwB;QACxB,OAAO,MAAM,CACX,IAAA,sBAAS,EAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAChE,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CACX,IAAA,sBAAS,EAAC,gBAAgB,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CACjE,CAAC;IACJ,CAAC;AACH,CAAC;AArBD,kEAqBC;AAED,SAAgB,+BAA+B,CAC7C,SAAiB,EACjB,YAAoB;IAEpB,IAAM,aAAa,GAAG,IAAA,sBAAS,EAAC,SAAS,CAAC,CAAC,CAAC,OAAO;IACnD,yEAAyE;IAEzE,IAAM,SAAS,GAAG,IAAA,sBAAS,EAAC,SAAS,CAAC;SACnC,KAAK,CAAC,aAAa,CAAC;SACpB,GAAG,CAAC,YAAY,CAAC;SACjB,OAAO,CAAC,CAAC,CAAC,CAAC;IAEd,OAAO,MAAM,CAAC,SAAS,CAAC,CAAC;AAC3B,CAAC;AAbD,0EAaC","sourcesContent":["import BigNumber from 'bignumber.js';\n\nexport function calculatePriceLimitForTrade(\n orderPrice: number,\n amount: number,\n): bigint {\n // Ensure orderPrice is BigInt\n const orderPriceBigInt = BigNumber(orderPrice);\n\n // Calculate 50% of the order price\n const offset = orderPriceBigInt.div(2);\n\n // Determine if the amount is positive or negative and adjust the price accordingly\n if (amount > 0) {\n // Trade is long, so add\n return BigInt(\n BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),\n );\n } else {\n return BigInt(\n BigNumber(orderPriceBigInt).minus(offset).times(1e18).toFixed(0),\n );\n }\n}\n\nexport function calculateOrderBaseFromOrderSize(\n orderSize: number,\n currentPrice: number,\n): bigint {\n const decimalFactor = BigNumber('1000000'); // 10^6\n // @todo check decimal places handling if this should stay hardcoded to 6\n\n const orderBase = BigNumber(orderSize)\n .times(decimalFactor)\n .div(currentPrice)\n .toFixed(0);\n\n return BigInt(orderBase);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -29,8 +29,9 @@
|
|
|
29
29
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
+
"bignumber.js": "^9.1.2",
|
|
32
33
|
"ethers": "6.9.0"
|
|
33
34
|
},
|
|
34
35
|
"packageManager": "pnpm@8.10.4",
|
|
35
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "87e449e615b82a9bcd2e38296f2c2c0daa09a61b"
|
|
36
37
|
}
|
package/src/services/lp/lp.ts
CHANGED
|
@@ -3,48 +3,81 @@ import {
|
|
|
3
3
|
encodeProvideLiquidityCall,
|
|
4
4
|
encodeRemoveLiquidityCall,
|
|
5
5
|
} from './encode';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
ProvideLiquidityParams,
|
|
8
|
+
ProvideLiquidityResult,
|
|
9
|
+
RemoveLiquidityParams,
|
|
10
|
+
RemoveLiquidityResult,
|
|
11
|
+
} from './types';
|
|
7
12
|
import { TransactionType } from '../../utils/consts';
|
|
13
|
+
import {
|
|
14
|
+
calculateMinOutForLp,
|
|
15
|
+
calculateMinSharesForLp,
|
|
16
|
+
expandLpAmount,
|
|
17
|
+
} from '../../utils/lp';
|
|
8
18
|
|
|
9
19
|
export const provideLiquidity = async (
|
|
10
|
-
params:
|
|
11
|
-
): Promise<
|
|
12
|
-
|
|
20
|
+
params: ProvideLiquidityParams,
|
|
21
|
+
): Promise<ProvideLiquidityResult> => {
|
|
22
|
+
const ownerAddrees = await params.signer.getAddress();
|
|
23
|
+
// @todo we might not have chainId for reya network, update once we have information
|
|
24
|
+
const chainId = 80001;
|
|
25
|
+
const minShares = calculateMinSharesForLp(params.amount);
|
|
26
|
+
|
|
27
|
+
if (params.amount === 0) {
|
|
28
|
+
throw new Error('LP amount can not be 0');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const amount = expandLpAmount(params.amount);
|
|
32
|
+
|
|
13
33
|
const { calldata: data, value } = encodeProvideLiquidityCall(
|
|
14
34
|
params.poolId,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
35
|
+
ownerAddrees,
|
|
36
|
+
amount,
|
|
37
|
+
minShares,
|
|
18
38
|
);
|
|
19
39
|
|
|
20
40
|
const result = await executeTransaction(
|
|
21
41
|
params.signer,
|
|
22
42
|
data,
|
|
23
43
|
value,
|
|
24
|
-
|
|
44
|
+
chainId,
|
|
25
45
|
TransactionType.LP,
|
|
26
46
|
);
|
|
27
47
|
|
|
28
|
-
return
|
|
48
|
+
return {
|
|
49
|
+
transactionHash: result?.hash || null,
|
|
50
|
+
};
|
|
29
51
|
};
|
|
30
52
|
|
|
31
53
|
export const removeLiquidity = async (
|
|
32
|
-
params:
|
|
33
|
-
): Promise<
|
|
34
|
-
// @todo update
|
|
54
|
+
params: RemoveLiquidityParams,
|
|
55
|
+
): Promise<RemoveLiquidityResult> => {
|
|
56
|
+
// @todo we might not have chainId for reya nework, update once we have information
|
|
57
|
+
const chainId = 80001;
|
|
58
|
+
|
|
59
|
+
if (params.sharesAmount === 0) {
|
|
60
|
+
throw new Error('Shares amount can not be 0');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const minOut = calculateMinOutForLp(params.sharesAmount);
|
|
64
|
+
|
|
65
|
+
const amount = expandLpAmount(params.sharesAmount);
|
|
35
66
|
const { calldata: data, value } = encodeRemoveLiquidityCall(
|
|
36
67
|
params.poolId,
|
|
37
|
-
|
|
38
|
-
|
|
68
|
+
amount,
|
|
69
|
+
minOut,
|
|
39
70
|
);
|
|
40
71
|
|
|
41
72
|
const result = await executeTransaction(
|
|
42
73
|
params.signer,
|
|
43
74
|
data,
|
|
44
75
|
value,
|
|
45
|
-
|
|
76
|
+
chainId,
|
|
46
77
|
TransactionType.LP,
|
|
47
78
|
);
|
|
48
79
|
|
|
49
|
-
return
|
|
80
|
+
return {
|
|
81
|
+
transactionHash: result?.hash || null,
|
|
82
|
+
};
|
|
50
83
|
};
|
package/src/services/lp/types.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type ProvideLiquidityParams = {
|
|
4
4
|
signer: Signer | JsonRpcSigner;
|
|
5
|
-
chainId: number;
|
|
6
|
-
poolId: number;
|
|
7
|
-
owner: string;
|
|
8
5
|
amount: number;
|
|
9
|
-
|
|
6
|
+
poolId: number;
|
|
10
7
|
};
|
|
11
8
|
|
|
12
|
-
export type
|
|
9
|
+
export type RemoveLiquidityParams = {
|
|
13
10
|
signer: Signer | JsonRpcSigner;
|
|
14
|
-
chainId: number;
|
|
15
11
|
poolId: number;
|
|
16
12
|
sharesAmount: number;
|
|
17
|
-
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ProvideLiquidityResult = {
|
|
16
|
+
transactionHash: string | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type RemoveLiquidityResult = {
|
|
20
|
+
transactionHash: string | null;
|
|
18
21
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { executeTransaction } from '../executeTransaction';
|
|
2
2
|
import { encodeCreateAccountCall } from './encode';
|
|
3
|
-
import {
|
|
3
|
+
import { CreateAccountParams } from './types';
|
|
4
4
|
import { TransactionType } from '../../utils/consts';
|
|
5
5
|
|
|
6
6
|
export const createAccount = async (
|
|
7
|
-
params:
|
|
7
|
+
params: CreateAccountParams,
|
|
8
8
|
): Promise<unknown> => {
|
|
9
9
|
// @todo update type once we agree on the structure
|
|
10
10
|
const { calldata: data, value } = encodeCreateAccountCall(
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DepositParams } from './types';
|
|
2
2
|
import { encodeDeposit } from './encode';
|
|
3
3
|
import { executeTransaction } from '../executeTransaction';
|
|
4
4
|
import { TransactionType } from '../../utils/consts';
|
|
5
5
|
|
|
6
|
-
export const deposit = async (params:
|
|
6
|
+
export const deposit = async (params: DepositParams): Promise<unknown> => {
|
|
7
7
|
// @todo update type once we agree on the structure
|
|
8
8
|
const { calldata: data, value } = encodeDeposit(
|
|
9
9
|
params.accountId,
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type CreateAccountParams = {
|
|
4
4
|
signer: Signer | JsonRpcSigner;
|
|
5
5
|
chainId: number;
|
|
6
6
|
ownerAddress: string;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type DepositParams = {
|
|
10
10
|
signer: Signer | JsonRpcSigner;
|
|
11
11
|
chainId: number;
|
|
12
12
|
accountId: number;
|
|
@@ -16,7 +16,7 @@ export type DepositArgs = {
|
|
|
16
16
|
exchangeId: number;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export type
|
|
19
|
+
export type WithdrawParams = {
|
|
20
20
|
signer: Signer | JsonRpcSigner;
|
|
21
21
|
chainId: number;
|
|
22
22
|
accountId: number;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { WithdrawParams } from './types';
|
|
2
2
|
import { encodeWithdraw } from './encode';
|
|
3
3
|
import { executeTransaction } from '../executeTransaction';
|
|
4
4
|
import { TransactionType } from '../../utils/consts';
|
|
5
5
|
|
|
6
|
-
export const withdraw = async (params:
|
|
6
|
+
export const withdraw = async (params: WithdrawParams): Promise<unknown> => {
|
|
7
7
|
// @todo update type once we agree on the structure
|
|
8
8
|
const { calldata: data, value } = encodeWithdraw(
|
|
9
9
|
params.accountId,
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
CloseOrderParams,
|
|
3
|
+
CloseOrderResult,
|
|
4
|
+
MatchOrderParams,
|
|
5
|
+
MatchOrderResult,
|
|
6
|
+
} from './types';
|
|
2
7
|
import { encodeMatchOrder } from './encode';
|
|
3
8
|
import { executeTransaction } from '../executeTransaction';
|
|
4
9
|
import { TransactionType } from '../../utils/consts';
|
|
@@ -6,11 +11,12 @@ import {
|
|
|
6
11
|
calculateOrderBaseFromOrderSize,
|
|
7
12
|
calculatePriceLimitForTrade,
|
|
8
13
|
} from '../../utils/trade';
|
|
14
|
+
import BigNumber from 'bignumber.js';
|
|
9
15
|
|
|
10
16
|
export const matchOrder = async (
|
|
11
|
-
params:
|
|
17
|
+
params: MatchOrderParams,
|
|
12
18
|
): Promise<MatchOrderResult> => {
|
|
13
|
-
if (params.orderSize ===
|
|
19
|
+
if (params.orderSize === 0) {
|
|
14
20
|
throw new Error('Position size can not be 0');
|
|
15
21
|
}
|
|
16
22
|
const orderPriceLimit = calculatePriceLimitForTrade(
|
|
@@ -46,3 +52,41 @@ export const matchOrder = async (
|
|
|
46
52
|
transactionHash: result?.hash || null,
|
|
47
53
|
};
|
|
48
54
|
};
|
|
55
|
+
|
|
56
|
+
export const closeOrder = async (
|
|
57
|
+
params: CloseOrderParams,
|
|
58
|
+
): Promise<CloseOrderResult> => {
|
|
59
|
+
if (params.orderBase === 0) {
|
|
60
|
+
throw new Error('Position base can not be 0');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const base = BigNumber(params.orderBase).negated().toNumber(); // when closing position we are doing trade in the opposite direction
|
|
64
|
+
|
|
65
|
+
const orderPriceLimit = calculatePriceLimitForTrade(
|
|
66
|
+
params.market.currentPrice,
|
|
67
|
+
base,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const { calldata: data, value } = encodeMatchOrder(
|
|
71
|
+
params.marginAccountId,
|
|
72
|
+
BigInt(base),
|
|
73
|
+
orderPriceLimit,
|
|
74
|
+
params.market.counterpartyAccountIds,
|
|
75
|
+
params.market.id,
|
|
76
|
+
params.market.exchangeId,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// @todo update it
|
|
80
|
+
const chainId = 80001;
|
|
81
|
+
const result = await executeTransaction(
|
|
82
|
+
params.signer,
|
|
83
|
+
data,
|
|
84
|
+
value,
|
|
85
|
+
chainId,
|
|
86
|
+
TransactionType.TRADE,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
transactionHash: result?.hash || null,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
@@ -1,19 +1,30 @@
|
|
|
1
1
|
import { Signer, JsonRpcSigner } from 'ethers';
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type MarketParams = {
|
|
4
4
|
id: number;
|
|
5
5
|
exchangeId: number;
|
|
6
6
|
counterpartyAccountIds: number[];
|
|
7
7
|
currentPrice: number;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export type
|
|
10
|
+
export type MatchOrderParams = {
|
|
11
11
|
signer: Signer | JsonRpcSigner;
|
|
12
12
|
marginAccountId: number;
|
|
13
|
-
orderSize:
|
|
14
|
-
market:
|
|
13
|
+
orderSize: number;
|
|
14
|
+
market: MarketParams;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type CloseOrderParams = {
|
|
18
|
+
signer: Signer | JsonRpcSigner;
|
|
19
|
+
marginAccountId: number;
|
|
20
|
+
orderBase: number; // PositionEntity[base]
|
|
21
|
+
market: MarketParams;
|
|
15
22
|
};
|
|
16
23
|
|
|
17
24
|
export type MatchOrderResult = {
|
|
18
25
|
transactionHash: string | null;
|
|
19
26
|
};
|
|
27
|
+
|
|
28
|
+
export type CloseOrderResult = {
|
|
29
|
+
transactionHash: string | null;
|
|
30
|
+
};
|
|
@@ -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({
|
|
@@ -5,23 +5,21 @@ export enum ApprovalType {
|
|
|
5
5
|
DEPOSIT,
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export type
|
|
8
|
+
export type ApproveTokenParams = {
|
|
9
9
|
signer: Signer | JsonRpcSigner;
|
|
10
|
-
|
|
11
|
-
token: string;
|
|
10
|
+
token: string; // token address
|
|
12
11
|
type: ApprovalType;
|
|
13
12
|
};
|
|
14
13
|
|
|
15
|
-
export type
|
|
14
|
+
export type GetAllowanceParams = {
|
|
16
15
|
signer: Signer | JsonRpcSigner;
|
|
17
|
-
|
|
18
|
-
token: string;
|
|
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
|
+
}
|
package/src/utils/trade.ts
CHANGED
|
@@ -1,31 +1,39 @@
|
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
|
|
1
3
|
export function calculatePriceLimitForTrade(
|
|
2
4
|
orderPrice: number,
|
|
3
|
-
|
|
5
|
+
amount: number,
|
|
4
6
|
): bigint {
|
|
5
7
|
// Ensure orderPrice is BigInt
|
|
6
|
-
const orderPriceBigInt =
|
|
8
|
+
const orderPriceBigInt = BigNumber(orderPrice);
|
|
7
9
|
|
|
8
10
|
// Calculate 50% of the order price
|
|
9
|
-
const offset = orderPriceBigInt
|
|
11
|
+
const offset = orderPriceBigInt.div(2);
|
|
10
12
|
|
|
11
|
-
// Determine if the
|
|
12
|
-
if (
|
|
13
|
+
// Determine if the amount is positive or negative and adjust the price accordingly
|
|
14
|
+
if (amount > 0) {
|
|
13
15
|
// Trade is long, so add
|
|
14
|
-
return
|
|
16
|
+
return BigInt(
|
|
17
|
+
BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),
|
|
18
|
+
);
|
|
15
19
|
} else {
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
return BigInt(
|
|
21
|
+
BigNumber(orderPriceBigInt).minus(offset).times(1e18).toFixed(0),
|
|
22
|
+
);
|
|
18
23
|
}
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
export function calculateOrderBaseFromOrderSize(
|
|
22
|
-
orderSize:
|
|
27
|
+
orderSize: number,
|
|
23
28
|
currentPrice: number,
|
|
24
29
|
): bigint {
|
|
25
|
-
const decimalFactor =
|
|
26
|
-
// @todo check decimal places handling
|
|
30
|
+
const decimalFactor = BigNumber('1000000'); // 10^6
|
|
31
|
+
// @todo check decimal places handling if this should stay hardcoded to 6
|
|
27
32
|
|
|
28
|
-
const orderBase = (orderSize
|
|
33
|
+
const orderBase = BigNumber(orderSize)
|
|
34
|
+
.times(decimalFactor)
|
|
35
|
+
.div(currentPrice)
|
|
36
|
+
.toFixed(0);
|
|
29
37
|
|
|
30
|
-
return orderBase;
|
|
38
|
+
return BigInt(orderBase);
|
|
31
39
|
}
|