@reyaxyz/sdk 0.124.12 → 0.124.13

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/README.md CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-9.2%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-4.21%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-5.49%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-8.61%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-9.14%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-4.21%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-5.49%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-8.55%25-red.svg?style=flat) |
10
10
 
@@ -43,7 +43,7 @@ var encode_1 = require("./encode");
43
43
  var network_1 = require("../../utils/network");
44
44
  exports.DEFAULT_SLIPPAGE = 0.01;
45
45
  var swap = function (params) { return __awaiter(void 0, void 0, void 0, function () {
46
- var chainId, priceWithSlippage, minAmountOutBase, _a, data, eip712Payload, eip712Signature, result;
46
+ var chainId, tokenInfoFrom, tokenInfoTo, amountFromScaled, amountToScaled, priceWithSlippage, minAmountOutBase, _a, data, eip712Payload, eip712Signature, result;
47
47
  return __generator(this, function (_b) {
48
48
  switch (_b.label) {
49
49
  case 0:
@@ -51,12 +51,16 @@ var swap = function (params) { return __awaiter(void 0, void 0, void 0, function
51
51
  throw new Error('Swap amount cannot be 0');
52
52
  }
53
53
  chainId = (0, network_1.getReyaNetwork)();
54
- priceWithSlippage = Number(params.amountOutBase / params.amountInBase) *
54
+ tokenInfoFrom = (0, common_1.getTokenInfoByAddress)(params.path[0].toLowerCase());
55
+ tokenInfoTo = (0, common_1.getTokenInfoByAddress)(params.path[params.path.length - 1].toLowerCase());
56
+ amountFromScaled = (0, common_1.descale)(tokenInfoFrom.decimals)(BigInt(params.amountInBase));
57
+ amountToScaled = (0, common_1.descale)(tokenInfoTo.decimals)(BigInt(params.amountOutBase));
58
+ priceWithSlippage = Number(amountToScaled / amountFromScaled) *
55
59
  (1 -
56
60
  (params.slippage == undefined
57
61
  ? exports.DEFAULT_SLIPPAGE
58
62
  : params.slippage / 100));
59
- minAmountOutBase = Math.round(priceWithSlippage * Number(params.amountInBase));
63
+ minAmountOutBase = Math.round(priceWithSlippage * Number((0, common_1.scaleV2)(tokenInfoTo.decimals)(amountFromScaled)));
60
64
  return [4 /*yield*/, (0, encode_1.encodeSwap)(params.signer, chainId, params.owner.coreSigNonce, params.marginAccountId, params.amountInBase.toString(), minAmountOutBase.toString(), params.path, params.adapters, params.recipients, 0, // 0 for marketID
61
65
  0)];
62
66
  case 1:
@@ -1 +1 @@
1
- {"version":3,"file":"swap.js","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAA+C;AAC/C,8EAA6E;AAC7E,mCAAsC;AAEtC,+CAAqD;AAExC,QAAA,gBAAgB,GAAG,IAAI,CAAC;AAE9B,IAAM,IAAI,GAAG,UAAO,MAAkB;;;;;gBAC3C,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,EAAE,CAAC;oBAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;gBAC7C,CAAC;gBAEK,OAAO,GAAG,IAAA,wBAAc,GAAE,CAAC;gBAI3B,iBAAiB,GACrB,MAAM,CAAC,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,YAAY,CAAC;oBAClD,CAAC,CAAC;wBACA,CAAC,MAAM,CAAC,QAAQ,IAAI,SAAS;4BAC3B,CAAC,CAAC,wBAAgB;4BAClB,CAAC,CAAC,MAAM,CAAC,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC;gBAC1B,gBAAgB,GAAG,IAAI,CAAC,KAAK,CACjC,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAChD,CAAC;gBAME,qBAAM,IAAA,mBAAU,EAClB,MAAM,CAAC,MAAM,EACb,OAAO,EACP,MAAM,CAAC,KAAK,CAAC,YAAY,EACzB,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,EAC9B,gBAAgB,CAAC,QAAQ,EAAE,EAC3B,MAAM,CAAC,IAAI,EACX,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,UAAU,EACjB,CAAC,EAAE,iBAAiB;oBACpB,CAAC,CACF,EAAA;;gBAhBK,KAIF,SAYH,EAfW,IAAI,cAAA,EACL,aAAa,aAAA,EACX,eAAe,eAAA;gBAeb,qBAAM,IAAA,yDAA2B,EAC9C,IAAI,EACJ,OAAO,EACP,qBAAY,CAAC,eAAe,EAC5B;wBACE,MAAM,EAAE,MAAM;wBACd,SAAS,EAAE,MAAM,CAAC,eAAe;wBACjC,QAAQ,EAAE,CAAC;wBACX,WAAW,EAAE,MAAM,CAAC,WAAW;wBAC/B,UAAU,EAAE;4BACV,mBAAmB,EAAE,eAAe;4BACpC,aAAa,eAAA;yBACd;qBACF,CACF,EAAA;;gBAdK,MAAM,GAAG,SAcd;gBAED,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,KAAI,IAAI;wBACvC,YAAY,EAAE,MAAM,CAAC,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,YAAY,CAAC,IAAI,IAAI;qBACnD,EAAC;;;KACH,CAAC;AAzDW,QAAA,IAAI,QAyDf","sourcesContent":["import { ContractType } from '@reyaxyz/common';\nimport { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';\nimport { encodeSwap } from './encode';\nimport { SwapParams, SwapResult } from './types';\nimport { getReyaNetwork } from '../../utils/network';\n\nexport const DEFAULT_SLIPPAGE = 0.01;\n\nexport const swap = async (params: SwapParams): Promise<SwapResult> => {\n if (params.amountInBase === 0) {\n throw new Error('Swap amount cannot be 0');\n }\n\n const chainId = getReyaNetwork();\n\n // Add custom slippage set my the user\n // Default is 1% to account for price movements\n const priceWithSlippage =\n Number(params.amountOutBase / params.amountInBase) *\n (1 -\n (params.slippage == undefined\n ? DEFAULT_SLIPPAGE\n : params.slippage / 100));\n const minAmountOutBase = Math.round(\n priceWithSlippage * Number(params.amountInBase),\n );\n\n const {\n calldata: data,\n payload: eip712Payload,\n signature: eip712Signature,\n } = await encodeSwap(\n params.signer,\n chainId,\n params.owner.coreSigNonce,\n params.marginAccountId,\n params.amountInBase.toString(),\n minAmountOutBase.toString(),\n params.path,\n params.adapters,\n params.recipients,\n 0, // 0 for marketID\n 0, // 0 for exchangeID\n );\n\n const result = await signAndBroadcastTransaction(\n data,\n chainId,\n ContractType.PERIPHERY_PROXY,\n {\n action: 'swap',\n accountId: params.marginAccountId,\n marketId: 0,\n tradeSource: params.tradeSource,\n matchOrder: {\n userEIP712Signature: eip712Signature,\n eip712Payload,\n },\n },\n );\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n };\n};\n"]}
1
+ {"version":3,"file":"swap.js","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAKyB;AACzB,8EAA6E;AAC7E,mCAAsC;AAEtC,+CAAqD;AAExC,QAAA,gBAAgB,GAAG,IAAI,CAAC;AAE9B,IAAM,IAAI,GAAG,UAAO,MAAkB;;;;;gBAC3C,IAAI,MAAM,CAAC,YAAY,KAAK,CAAC,EAAE,CAAC;oBAC9B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;gBAC7C,CAAC;gBAEK,OAAO,GAAG,IAAA,wBAAc,GAAE,CAAC;gBAE3B,aAAa,GAAG,IAAA,8BAAqB,E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AAM,IAAA,yDAA2B,EAC9C,IAAI,EACJ,OAAO,EACP,qBAAY,CAAC,eAAe,EAC5B;wBACE,MAAM,EAAE,MAAM;wBACd,SAAS,EAAE,MAAM,CAAC,eAAe;wBACjC,QAAQ,EAAE,CAAC;wBACX,WAAW,EAAE,MAAM,CAAC,WAAW;wBAC/B,UAAU,EAAE;4BACV,mBAAmB,EAAE,eAAe;4BACpC,aAAa,eAAA;yBACd;qBACF,CACF,EAAA;;gBAdK,MAAM,GAAG,SAcd;gBAED,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,MAAM,KAAI,IAAI;wBACvC,YAAY,EAAE,MAAM,CAAC,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,YAAY,CAAC,IAAI,IAAI;qBACnD,EAAC;;;KACH,CAAC;AArEW,QAAA,IAAI,QAqEf","sourcesContent":["import {\n ContractType,\n descale,\n getTokenInfoByAddress,\n scaleV2,\n} from '@reyaxyz/common';\nimport { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';\nimport { encodeSwap } from './encode';\nimport { SwapParams, SwapResult } from './types';\nimport { getReyaNetwork } from '../../utils/network';\n\nexport const DEFAULT_SLIPPAGE = 0.01;\n\nexport const swap = async (params: SwapParams): Promise<SwapResult> => {\n if (params.amountInBase === 0) {\n throw new Error('Swap amount cannot be 0');\n }\n\n const chainId = getReyaNetwork();\n\n const tokenInfoFrom = getTokenInfoByAddress(params.path[0].toLowerCase());\n const tokenInfoTo = getTokenInfoByAddress(\n params.path[params.path.length - 1].toLowerCase(),\n );\n const amountFromScaled = descale(tokenInfoFrom.decimals)(\n BigInt(params.amountInBase),\n );\n const amountToScaled = descale(tokenInfoTo.decimals)(\n BigInt(params.amountOutBase),\n );\n\n // Add custom slippage set my the user\n // Default is 1% to account for price movements\n const priceWithSlippage =\n Number(amountToScaled / amountFromScaled) *\n (1 -\n (params.slippage == undefined\n ? DEFAULT_SLIPPAGE\n : params.slippage / 100));\n // scale from tokenIn decimals to tokenOut decimals\n const minAmountOutBase = Math.round(\n priceWithSlippage * Number(scaleV2(tokenInfoTo.decimals)(amountFromScaled)),\n );\n\n const {\n calldata: data,\n payload: eip712Payload,\n signature: eip712Signature,\n } = await encodeSwap(\n params.signer,\n chainId,\n params.owner.coreSigNonce,\n params.marginAccountId,\n params.amountInBase.toString(),\n minAmountOutBase.toString(),\n params.path,\n params.adapters,\n params.recipients,\n 0, // 0 for marketID\n 0, // 0 for exchangeID\n );\n\n const result = await signAndBroadcastTransaction(\n data,\n chainId,\n ContractType.PERIPHERY_PROXY,\n {\n action: 'swap',\n accountId: params.marginAccountId,\n marketId: 0,\n tradeSource: params.tradeSource,\n matchOrder: {\n userEIP712Signature: eip712Signature,\n eip712Payload,\n },\n },\n );\n\n return {\n transactionHash: result?.txHash || null,\n coreSigNonce: Number(result?.coreSigNonce) || null,\n };\n};\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"swap.d.ts","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGjD,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAErC,eAAO,MAAM,IAAI,WAAkB,UAAU,KAAG,QAAQ,UAAU,CAyDjE,CAAC"}
1
+ {"version":3,"file":"swap.d.ts","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGjD,eAAO,MAAM,gBAAgB,OAAO,CAAC;AAErC,eAAO,MAAM,IAAI,WAAkB,UAAU,KAAG,QAAQ,UAAU,CAqEjE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/sdk",
3
- "version": "0.124.12",
3
+ "version": "0.124.13",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -35,5 +35,5 @@
35
35
  "ethers": "6.9.0"
36
36
  },
37
37
  "packageManager": "pnpm@8.3.1",
38
- "gitHead": "0bd709a742fa4c2889eccf7cfae83e4aac3db28d"
38
+ "gitHead": "fcb996c4ff4606da6b94e79cffe8767129e12dd8"
39
39
  }
@@ -1,4 +1,9 @@
1
- import { ContractType } from '@reyaxyz/common';
1
+ import {
2
+ ContractType,
3
+ descale,
4
+ getTokenInfoByAddress,
5
+ scaleV2,
6
+ } from '@reyaxyz/common';
2
7
  import { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';
3
8
  import { encodeSwap } from './encode';
4
9
  import { SwapParams, SwapResult } from './types';
@@ -13,16 +18,28 @@ export const swap = async (params: SwapParams): Promise<SwapResult> => {
13
18
 
14
19
  const chainId = getReyaNetwork();
15
20
 
21
+ const tokenInfoFrom = getTokenInfoByAddress(params.path[0].toLowerCase());
22
+ const tokenInfoTo = getTokenInfoByAddress(
23
+ params.path[params.path.length - 1].toLowerCase(),
24
+ );
25
+ const amountFromScaled = descale(tokenInfoFrom.decimals)(
26
+ BigInt(params.amountInBase),
27
+ );
28
+ const amountToScaled = descale(tokenInfoTo.decimals)(
29
+ BigInt(params.amountOutBase),
30
+ );
31
+
16
32
  // Add custom slippage set my the user
17
33
  // Default is 1% to account for price movements
18
34
  const priceWithSlippage =
19
- Number(params.amountOutBase / params.amountInBase) *
35
+ Number(amountToScaled / amountFromScaled) *
20
36
  (1 -
21
37
  (params.slippage == undefined
22
38
  ? DEFAULT_SLIPPAGE
23
39
  : params.slippage / 100));
40
+ // scale from tokenIn decimals to tokenOut decimals
24
41
  const minAmountOutBase = Math.round(
25
- priceWithSlippage * Number(params.amountInBase),
42
+ priceWithSlippage * Number(scaleV2(tokenInfoTo.decimals)(amountFromScaled)),
26
43
  );
27
44
 
28
45
  const {