@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.
Files changed (63) hide show
  1. package/dist/services/lp/lp.js +28 -9
  2. package/dist/services/lp/lp.js.map +1 -1
  3. package/dist/services/lp/types.js.map +1 -1
  4. package/dist/services/margin-accounts/account.js.map +1 -1
  5. package/dist/services/margin-accounts/deposit.js.map +1 -1
  6. package/dist/services/margin-accounts/types.js.map +1 -1
  7. package/dist/services/margin-accounts/withdraw.js.map +1 -1
  8. package/dist/services/orders/order.js +28 -2
  9. package/dist/services/orders/order.js.map +1 -1
  10. package/dist/services/orders/types.js.map +1 -1
  11. package/dist/services/token/approve.js +4 -3
  12. package/dist/services/token/approve.js.map +1 -1
  13. package/dist/services/token/common.js.map +1 -1
  14. package/dist/services/token/getAllowance.js +4 -3
  15. package/dist/services/token/getAllowance.js.map +1 -1
  16. package/dist/services/token/types.js.map +1 -1
  17. package/dist/types/services/lp/lp.d.ts +3 -3
  18. package/dist/types/services/lp/lp.d.ts.map +1 -1
  19. package/dist/types/services/lp/types.d.ts +9 -8
  20. package/dist/types/services/lp/types.d.ts.map +1 -1
  21. package/dist/types/services/margin-accounts/account.d.ts +2 -2
  22. package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
  23. package/dist/types/services/margin-accounts/deposit.d.ts +2 -2
  24. package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
  25. package/dist/types/services/margin-accounts/types.d.ts +3 -3
  26. package/dist/types/services/margin-accounts/types.d.ts.map +1 -1
  27. package/dist/types/services/margin-accounts/withdraw.d.ts +2 -2
  28. package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
  29. package/dist/types/services/orders/order.d.ts +3 -2
  30. package/dist/types/services/orders/order.d.ts.map +1 -1
  31. package/dist/types/services/orders/types.d.ts +13 -4
  32. package/dist/types/services/orders/types.d.ts.map +1 -1
  33. package/dist/types/services/token/approve.d.ts +2 -2
  34. package/dist/types/services/token/approve.d.ts.map +1 -1
  35. package/dist/types/services/token/common.d.ts +2 -2
  36. package/dist/types/services/token/common.d.ts.map +1 -1
  37. package/dist/types/services/token/getAllowance.d.ts +2 -2
  38. package/dist/types/services/token/getAllowance.d.ts.map +1 -1
  39. package/dist/types/services/token/types.d.ts +3 -5
  40. package/dist/types/services/token/types.d.ts.map +1 -1
  41. package/dist/types/utils/lp.d.ts +4 -0
  42. package/dist/types/utils/lp.d.ts.map +1 -0
  43. package/dist/types/utils/trade.d.ts +2 -2
  44. package/dist/types/utils/trade.d.ts.map +1 -1
  45. package/dist/utils/lp.js +28 -0
  46. package/dist/utils/lp.js.map +1 -0
  47. package/dist/utils/trade.js +18 -12
  48. package/dist/utils/trade.js.map +1 -1
  49. package/package.json +3 -2
  50. package/src/services/lp/lp.ts +49 -16
  51. package/src/services/lp/types.ts +11 -8
  52. package/src/services/margin-accounts/account.ts +2 -2
  53. package/src/services/margin-accounts/deposit.ts +2 -2
  54. package/src/services/margin-accounts/types.ts +3 -3
  55. package/src/services/margin-accounts/withdraw.ts +2 -2
  56. package/src/services/orders/order.ts +47 -3
  57. package/src/services/orders/types.ts +15 -4
  58. package/src/services/token/approve.ts +7 -4
  59. package/src/services/token/common.ts +2 -2
  60. package/src/services/token/getAllowance.ts +7 -4
  61. package/src/services/token/types.ts +6 -8
  62. package/src/utils/lp.ts +27 -0
  63. package/src/utils/trade.ts +21 -13
@@ -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"]}
@@ -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
- function calculatePriceLimitForTrade(orderPrice, size) {
7
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
8
+ function calculatePriceLimitForTrade(orderPrice, amount) {
5
9
  // Ensure orderPrice is BigInt
6
- var orderPriceBigInt = BigInt(orderPrice);
10
+ var orderPriceBigInt = (0, bignumber_js_1.default)(orderPrice);
7
11
  // Calculate 50% of the order price
8
- var offset = orderPriceBigInt / BigInt(2);
9
- // Determine if the size is positive or negative and adjust the price accordingly
10
- if (size > 0) {
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 + offset) * BigInt(1e18);
16
+ return BigInt((0, bignumber_js_1.default)(orderPriceBigInt).plus(offset).times(1e18).toFixed(0));
13
17
  }
14
18
  else {
15
- // Trade is short, so subtract
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 = BigInt('1000000'); // 10^6
22
- // @todo check decimal places handling
23
- var orderBase = (orderSize * decimalFactor) / BigInt(currentPrice);
24
- return orderBase;
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
@@ -1 +1 @@
1
- {"version":3,"file":"trade.js","sourceRoot":"/","sources":["utils/trade.ts"],"names":[],"mappings":";;;AAAA,SAAgB,2BAA2B,CACzC,UAAkB,EAClB,IAAY;IAEZ,8BAA8B;IAC9B,IAAM,gBAAgB,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;IAE5C,mCAAmC;IACnC,IAAM,MAAM,GAAG,gBAAgB,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAE5C,iFAAiF;IACjF,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;QACb,wBAAwB;QACxB,OAAO,CAAC,gBAAgB,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;SAAM,CAAC;QACN,8BAA8B;QAC9B,OAAO,CAAC,gBAAgB,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;AACH,CAAC;AAlBD,kEAkBC;AAED,SAAgB,+BAA+B,CAC7C,SAAiB,EACjB,YAAoB;IAEpB,IAAM,aAAa,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO;IAChD,sCAAsC;IAEtC,IAAM,SAAS,GAAG,CAAC,SAAS,GAAG,aAAa,CAAC,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;IAErE,OAAO,SAAS,CAAC;AACnB,CAAC;AAVD,0EAUC","sourcesContent":["export function calculatePriceLimitForTrade(\n orderPrice: number,\n size: bigint,\n): bigint {\n // Ensure orderPrice is BigInt\n const orderPriceBigInt = BigInt(orderPrice);\n\n // Calculate 50% of the order price\n const offset = orderPriceBigInt / BigInt(2);\n\n // Determine if the size is positive or negative and adjust the price accordingly\n if (size > 0) {\n // Trade is long, so add\n return (orderPriceBigInt + offset) * BigInt(1e18);\n } else {\n // Trade is short, so subtract\n return (orderPriceBigInt - offset) * BigInt(1e18);\n }\n}\n\nexport function calculateOrderBaseFromOrderSize(\n orderSize: bigint,\n currentPrice: number,\n): bigint {\n const decimalFactor = BigInt('1000000'); // 10^6\n // @todo check decimal places handling\n\n const orderBase = (orderSize * decimalFactor) / BigInt(currentPrice);\n\n return orderBase;\n}\n"]}
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.2.1",
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": "c93a765b3c4e44a13d3f96237342d0508fe55a71"
36
+ "gitHead": "87e449e615b82a9bcd2e38296f2c2c0daa09a61b"
36
37
  }
@@ -3,48 +3,81 @@ import {
3
3
  encodeProvideLiquidityCall,
4
4
  encodeRemoveLiquidityCall,
5
5
  } from './encode';
6
- import { ProvideLiquidityArgs, RemoveLiquidityArgs } from './types';
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: ProvideLiquidityArgs,
11
- ): Promise<unknown> => {
12
- // @todo update type once we agree on the structure
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
- params.owner,
16
- params.amount,
17
- params.minShares,
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
- params.chainId,
44
+ chainId,
25
45
  TransactionType.LP,
26
46
  );
27
47
 
28
- return result;
48
+ return {
49
+ transactionHash: result?.hash || null,
50
+ };
29
51
  };
30
52
 
31
53
  export const removeLiquidity = async (
32
- params: RemoveLiquidityArgs,
33
- ): Promise<unknown> => {
34
- // @todo update type once we agree on the structure
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
- params.sharesAmount,
38
- params.minOut,
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
- params.chainId,
76
+ chainId,
46
77
  TransactionType.LP,
47
78
  );
48
79
 
49
- return result;
80
+ return {
81
+ transactionHash: result?.hash || null,
82
+ };
50
83
  };
@@ -1,18 +1,21 @@
1
1
  import { JsonRpcSigner, Signer } from 'ethers';
2
2
 
3
- export type ProvideLiquidityArgs = {
3
+ export type ProvideLiquidityParams = {
4
4
  signer: Signer | JsonRpcSigner;
5
- chainId: number;
6
- poolId: number;
7
- owner: string;
8
5
  amount: number;
9
- minShares: number;
6
+ poolId: number;
10
7
  };
11
8
 
12
- export type RemoveLiquidityArgs = {
9
+ export type RemoveLiquidityParams = {
13
10
  signer: Signer | JsonRpcSigner;
14
- chainId: number;
15
11
  poolId: number;
16
12
  sharesAmount: number;
17
- minOut: number;
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 { CreateAccountArgs } from './types';
3
+ import { CreateAccountParams } from './types';
4
4
  import { TransactionType } from '../../utils/consts';
5
5
 
6
6
  export const createAccount = async (
7
- params: CreateAccountArgs,
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 { DepositArgs } from './types';
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: DepositArgs): Promise<unknown> => {
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 CreateAccountArgs = {
3
+ export type CreateAccountParams = {
4
4
  signer: Signer | JsonRpcSigner;
5
5
  chainId: number;
6
6
  ownerAddress: string;
7
7
  };
8
8
 
9
- export type DepositArgs = {
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 WithdrawArgs = {
19
+ export type WithdrawParams = {
20
20
  signer: Signer | JsonRpcSigner;
21
21
  chainId: number;
22
22
  accountId: number;
@@ -1,9 +1,9 @@
1
- import { WithdrawArgs } from './types';
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: WithdrawArgs): Promise<unknown> => {
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 { MatchOrderArgs, MatchOrderResult } from './types';
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: MatchOrderArgs,
17
+ params: MatchOrderParams,
12
18
  ): Promise<MatchOrderResult> => {
13
- if (params.orderSize === BigInt(0)) {
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 MarketArg = {
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 MatchOrderArgs = {
10
+ export type MatchOrderParams = {
11
11
  signer: Signer | JsonRpcSigner;
12
12
  marginAccountId: number;
13
- orderSize: bigint;
14
- market: MarketArg;
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, 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({
@@ -5,23 +5,21 @@ export enum ApprovalType {
5
5
  DEPOSIT,
6
6
  }
7
7
 
8
- export type ApproveTokenArgs = {
8
+ export type ApproveTokenParams = {
9
9
  signer: Signer | JsonRpcSigner;
10
- chainId: number;
11
- token: string;
10
+ token: string; // token address
12
11
  type: ApprovalType;
13
12
  };
14
13
 
15
- export type GetAllowanceArgs = {
14
+ export type GetAllowanceParams = {
16
15
  signer: Signer | JsonRpcSigner;
17
- chainId: number;
18
- token: string;
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
+ }
@@ -1,31 +1,39 @@
1
+ import BigNumber from 'bignumber.js';
2
+
1
3
  export function calculatePriceLimitForTrade(
2
4
  orderPrice: number,
3
- size: bigint,
5
+ amount: number,
4
6
  ): bigint {
5
7
  // Ensure orderPrice is BigInt
6
- const orderPriceBigInt = BigInt(orderPrice);
8
+ const orderPriceBigInt = BigNumber(orderPrice);
7
9
 
8
10
  // Calculate 50% of the order price
9
- const offset = orderPriceBigInt / BigInt(2);
11
+ const offset = orderPriceBigInt.div(2);
10
12
 
11
- // Determine if the size is positive or negative and adjust the price accordingly
12
- if (size > 0) {
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 (orderPriceBigInt + offset) * BigInt(1e18);
16
+ return BigInt(
17
+ BigNumber(orderPriceBigInt).plus(offset).times(1e18).toFixed(0),
18
+ );
15
19
  } else {
16
- // Trade is short, so subtract
17
- return (orderPriceBigInt - offset) * BigInt(1e18);
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: bigint,
27
+ orderSize: number,
23
28
  currentPrice: number,
24
29
  ): bigint {
25
- const decimalFactor = BigInt('1000000'); // 10^6
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 * decimalFactor) / BigInt(currentPrice);
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
  }