@reyaxyz/sdk 0.124.2 → 0.124.3
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 +1 -1
- package/dist/services/swaps/simulation.js +15 -2
- package/dist/services/swaps/simulation.js.map +1 -1
- package/dist/services/swaps/swap.js +3 -2
- package/dist/services/swaps/swap.js.map +1 -1
- package/dist/types/services/swaps/simulation.d.ts.map +1 -1
- package/dist/types/services/swaps/swap.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/services/swaps/simulation.ts +18 -3
- package/src/services/swaps/swap.ts +3 -1
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -120,10 +120,23 @@ var findBestPath = function (amountFrom, tokenFrom, tokenTo, maxSteps) { return
|
|
|
120
120
|
camelot = new ethers_1.Contract(camelotAddress, CamelotAbi, provider);
|
|
121
121
|
tokenInfoFrom = (0, common_1.getTokenInfoByAddress)(tokenFrom);
|
|
122
122
|
tokenInfoTo = (0, common_1.getTokenInfoByAddress)(tokenTo);
|
|
123
|
-
amountFromScaled = (0, common_1.
|
|
123
|
+
amountFromScaled = (0, common_1.scaleV2)(tokenInfoFrom.decimals)(amountFrom);
|
|
124
124
|
return [4 /*yield*/, camelot.findBestPath(amountFromScaled, tokenFrom, tokenTo, [], maxSteps || 4)];
|
|
125
125
|
case 1:
|
|
126
126
|
result = _a.sent();
|
|
127
|
+
// It can happen that the amount from results in a zero amount out
|
|
128
|
+
// dues to smaller precision of amount out, resulting in no path
|
|
129
|
+
if (result.amounts.length == 0) {
|
|
130
|
+
return [2 /*return*/, {
|
|
131
|
+
amountFrom: amountFrom,
|
|
132
|
+
amountTo: 0,
|
|
133
|
+
amountInBase: Number(amountFromScaled),
|
|
134
|
+
amountOutBase: 0,
|
|
135
|
+
adapters: [],
|
|
136
|
+
path: [tokenFrom, tokenTo],
|
|
137
|
+
recipients: [],
|
|
138
|
+
}];
|
|
139
|
+
}
|
|
127
140
|
return [2 /*return*/, {
|
|
128
141
|
amountFrom: (0, common_1.descale)(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
129
142
|
amountTo: (0, common_1.descale)(tokenInfoTo.decimals)(result.amounts[1]),
|
|
@@ -203,7 +216,7 @@ var simulateSwap = function (params) { return __awaiter(void 0, void 0, void 0,
|
|
|
203
216
|
adjustedAmountFrom = bestPathResult.amountFrom * (1 - tokenFromHaircut);
|
|
204
217
|
adjustedAmountTo = bestPathResult.amountTo * (1 - tokenToHaircut);
|
|
205
218
|
postSwapMargin = marginInfo.preSwapMarginBalance - adjustedAmountFrom + adjustedAmountTo;
|
|
206
|
-
marginRatio = marginInfo.lmr / postSwapMargin;
|
|
219
|
+
marginRatio = (marginInfo.lmr / postSwapMargin) * 100;
|
|
207
220
|
return [2 /*return*/, {
|
|
208
221
|
bestPath: bestPathResult,
|
|
209
222
|
estimatedSlippage: estimatedSlippage,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simulation.js","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAWyB;AACzB,iCAA0C;AAO1C,uCAA0D;AAC1D,+CAAqD;AAErD,IAAM,UAAU,GAAG;IACjB;QACE,MAAM,EAAE;YACN;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,SAAS;aAChB;YACD;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,UAAU;gBAChB,IAAI,EAAE,SAAS;aAChB;YACD;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,SAAS;aAChB;YACD;gBACE,YAAY,EAAE,WAAW;gBACzB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B,CAAC;gBAGpE,WAAW,GAAG,UAAU,CAAC,GAAG,GAAG,cAAc,CAAC;gBAEpD,sBAAO;wBACL,QAAQ,EAAE,cAAc;wBACxB,iBAAiB,EAAE,iBAAiB;wBACpC,WAAW,EAAE,WAAW;wBACxB,iBAAiB,EAAE,wBAAe,CAAC,oBAAoB,CAAC,WAAW,CAAC;qBACrE,EAAC;;;KACH,CAAC;AAlEW,QAAA,YAAY,gBAkEvB","sourcesContent":["import {\n ContractType,\n descale,\n getAddress,\n getTokenInfoByAddress,\n scale,\n reyaChainIdRPCMapper,\n ExposureCommand,\n CoreAbi,\n SpotMarketEntity,\n CollateralWithHaircut,\n} from '@reyaxyz/common';\nimport { Contract, ethers } from 'ethers';\nimport {\n FindBestPathResult,\n GetPreSwapMarginInfo,\n SimulateSwapParams,\n SimulateSwapResult,\n} from './types';\nimport { getGeneralRestModuleClient } from '../../config';\nimport { getReyaNetwork } from '../../utils/network';\n\nconst CamelotAbi = [\n {\n inputs: [\n {\n internalType: 'uint256',\n name: '_amountIn',\n type: 'uint256',\n },\n {\n internalType: 'address',\n name: '_tokenIn',\n type: 'address',\n },\n {\n internalType: 'address',\n name: '_tokenOut',\n type: 'address',\n },\n {\n internalType: 'address[]',\n name: '_trustedTokens',\n type: 'address[]',\n },\n {\n internalType: 'uint256',\n name: '_maxSteps',\n type: 'uint256',\n },\n ],\n name: 'findBestPath',\n outputs: [\n {\n components: [\n {\n internalType: 'uint256[]',\n name: 'amounts',\n type: 'uint256[]',\n },\n {\n internalType: 'address[]',\n name: 'adapters',\n type: 'address[]',\n },\n {\n internalType: 'address[]',\n name: 'path',\n type: 'address[]',\n },\n {\n internalType: 'address[]',\n name: 'recipients',\n type: 'address[]',\n },\n {\n internalType: 'uint256',\n name: 'gasEstimate',\n type: 'uint256',\n },\n ],\n internalType: 'struct FormattedOffer',\n name: '',\n type: 'tuple',\n },\n ],\n stateMutability: 'view',\n type: 'function',\n },\n];\n\nexport const findBestPath = async (\n amountFrom: number,\n tokenFrom: string,\n tokenTo: string,\n maxSteps?: number,\n): Promise<FindBestPathResult> => {\n const chainId = getReyaNetwork();\n const provider = ethers.getDefaultProvider(\n reyaChainIdRPCMapper[chainId] as string,\n );\n const camelotAddress = getAddress(chainId, ContractType.CAMELOT_YAK_ROUTER);\n const camelot = new Contract(camelotAddress, CamelotAbi, provider);\n\n const tokenInfoFrom = getTokenInfoByAddress(tokenFrom);\n const tokenInfoTo = getTokenInfoByAddress(tokenTo);\n const amountFromScaled = scale(tokenInfoFrom.decimals)(amountFrom);\n\n // amountFrom\n // tokenFrom\n // tokenTo\n // trustedTokens\n // maxSteps\n const result = await camelot.findBestPath(\n amountFromScaled,\n tokenFrom,\n tokenTo,\n [],\n maxSteps || 4,\n );\n return {\n amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),\n amountTo: descale(tokenInfoTo.decimals)(result.amounts[1]),\n amountInBase: result.amounts[0],\n amountOutBase: result.amounts[1],\n adapters: Array.from(result[1]),\n path: Array.from(result[2]),\n recipients: Array.from(result[3]),\n };\n};\n\n// Simulates sending the swap directly to Core using the publisher as msg.sender\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst getPreSwapMarginInfo = async (\n marginAccountId: number,\n): Promise<GetPreSwapMarginInfo> => {\n const chainId = getReyaNetwork();\n const provider = ethers.getDefaultProvider(\n reyaChainIdRPCMapper[chainId] as string,\n );\n const core = new Contract(\n getAddress(chainId, ContractType.CORE_PROXY),\n CoreAbi,\n provider,\n );\n\n const result = await core.getUsdNodeMarginInfo(marginAccountId);\n return {\n preSwapMarginBalance: Number(result[1]),\n lmr: Number(result[9]),\n };\n};\n\nexport const simulateSwap = async (\n params: SimulateSwapParams,\n): Promise<SimulateSwapResult> => {\n const bestPathResult = await findBestPath(\n params.amountFrom,\n params.tokenFrom,\n params.tokenTo,\n );\n\n // slippage calculation\n const simulatedPrice = bestPathResult.amountTo / bestPathResult.amountFrom;\n const restModule = getGeneralRestModuleClient();\n const markets: SpotMarketEntity[] = await restModule.getSpotMarkets();\n\n const market = markets.find((m) => {\n return (\n m.quoteToken.address == params.tokenFrom.toLowerCase() &&\n m.underlyingAsset.address == params.tokenTo.toLowerCase()\n );\n });\n let estimatedSlippage = 0;\n if (market != undefined) {\n estimatedSlippage = (market.price - simulatedPrice) / market.price;\n } else {\n const marketReversed = markets.find((m) => {\n return (\n m.underlyingAsset.address == params.tokenFrom.toLowerCase() &&\n m.quoteToken.address == params.tokenTo.toLowerCase()\n );\n });\n if (marketReversed != undefined) {\n estimatedSlippage =\n (1 / marketReversed.price - simulatedPrice) /\n (1 / marketReversed.price);\n }\n }\n\n const collaterals: CollateralWithHaircut[] =\n await restModule.getCollateralInfo();\n const tokenFromHaircut = collaterals.find(\n (c) => c.address == params.tokenFrom.toLowerCase(),\n )?.priceHaircut;\n const tokenToHaircut = collaterals.find(\n (c) => c.address == params.tokenFrom.toLowerCase(),\n )?.priceHaircut;\n\n if (tokenFromHaircut === undefined || tokenToHaircut === undefined) {\n throw new Error('Cannot find collateral tokens');\n }\n\n const marginInfo = await getPreSwapMarginInfo(params.marginAccountId);\n const adjustedAmountFrom = bestPathResult.amountFrom * (1 - tokenFromHaircut);\n const adjustedAmountTo = bestPathResult.amountTo * (1 - tokenToHaircut);\n\n const postSwapMargin =\n marginInfo.preSwapMarginBalance - adjustedAmountFrom + adjustedAmountTo;\n\n // Calculate the margin ratio\n const marginRatio = marginInfo.lmr / postSwapMargin;\n\n return {\n bestPath: bestPathResult,\n estimatedSlippage: estimatedSlippage,\n marginRatio: marginRatio,\n marginRatioHealth: ExposureCommand.evaluateHealthStatus(marginRatio),\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"simulation.js","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAWyB;AACzB,iCAA0C;AAO1C,uCAA0D;AAC1D,+CAAqD;AAErD,IAAM,UAAU,GAAG;IACjB;QACE,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CAA2C,CACnD,0CAAE,YAAY,CAAC;gBACV,cAAc,GAAG,MAAA,WAAW,CAAC,IAAI,CACrC,UAAC,CAAC,IAAK,OAAA,CAAC,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,CAAC,WAAW,EAAE,EAA3C,CAA2C,CACnD,0CAAE,YAAY,CAAC;gBAEhB,IAAI,gBAAgB,KAAK,SAAS,IAAI,cAAc,KAAK,SAAS,EAAE,CAAC;oBACnE,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;gBACnD,CAAC;gBAEkB,qBAAM,oBAAoB,CAAC,MAAM,CAAC,eAAe,CAAC,EAAA;;gBAA/D,UAAU,GAAG,SAAkD;gBAC/D,kBAAkB,GAAG,cAAc,CAAC,UAAU,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC;gBACxE,gBAAgB,GAAG,cAAc,CAAC,QAAQ,GAAG,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC;gBAElE,cAAc,GAClB,UAAU,CAAC,oBAAoB,GAAG,kBAAkB,GAAG,gBAAgB,CAAC;gBAGpE,WAAW,GAAG,CAAC,UAAU,CAAC,GAAG,GAAG,cAAc,CAAC,GAAG,GAAG,CAAC;gBAE5D,sBAAO;wBACL,QAAQ,EAAE,cAAc;wBACxB,iBAAiB,EAAE,iBAAiB;wBACpC,WAAW,EAAE,WAAW;wBACxB,iBAAiB,EAAE,wBAAe,CAAC,oBAAoB,CAAC,WAAW,CAAC;qBACrE,EAAC;;;KACH,CAAC;AAlEW,QAAA,YAAY,gBAkEvB","sourcesContent":["import {\n ContractType,\n descale,\n getAddress,\n getTokenInfoByAddress,\n scaleV2,\n reyaChainIdRPCMapper,\n ExposureCommand,\n CoreAbi,\n SpotMarketEntity,\n CollateralWithHaircut,\n} from '@reyaxyz/common';\nimport { Contract, ethers } from 'ethers';\nimport {\n FindBestPathResult,\n GetPreSwapMarginInfo,\n SimulateSwapParams,\n SimulateSwapResult,\n} from './types';\nimport { getGeneralRestModuleClient } from '../../config';\nimport { getReyaNetwork } from '../../utils/network';\n\nconst CamelotAbi = [\n {\n inputs: [\n {\n internalType: 'uint256',\n name: '_amountIn',\n type: 'uint256',\n },\n {\n internalType: 'address',\n name: '_tokenIn',\n type: 'address',\n },\n {\n internalType: 'address',\n name: '_tokenOut',\n type: 'address',\n },\n {\n internalType: 'address[]',\n name: '_trustedTokens',\n type: 'address[]',\n },\n {\n internalType: 'uint256',\n name: '_maxSteps',\n type: 'uint256',\n },\n ],\n name: 'findBestPath',\n outputs: [\n {\n components: [\n {\n internalType: 'uint256[]',\n name: 'amounts',\n type: 'uint256[]',\n },\n {\n internalType: 'address[]',\n name: 'adapters',\n type: 'address[]',\n },\n {\n internalType: 'address[]',\n name: 'path',\n type: 'address[]',\n },\n {\n internalType: 'address[]',\n name: 'recipients',\n type: 'address[]',\n },\n {\n internalType: 'uint256',\n name: 'gasEstimate',\n type: 'uint256',\n },\n ],\n internalType: 'struct FormattedOffer',\n name: '',\n type: 'tuple',\n },\n ],\n stateMutability: 'view',\n type: 'function',\n },\n];\n\nexport const findBestPath = async (\n amountFrom: number,\n tokenFrom: string,\n tokenTo: string,\n maxSteps?: number,\n): Promise<FindBestPathResult> => {\n const chainId = getReyaNetwork();\n const provider = ethers.getDefaultProvider(\n reyaChainIdRPCMapper[chainId] as string,\n );\n const camelotAddress = getAddress(chainId, ContractType.CAMELOT_YAK_ROUTER);\n const camelot = new Contract(camelotAddress, CamelotAbi, provider);\n\n const tokenInfoFrom = getTokenInfoByAddress(tokenFrom);\n const tokenInfoTo = getTokenInfoByAddress(tokenTo);\n const amountFromScaled = scaleV2(tokenInfoFrom.decimals)(amountFrom);\n\n // amountFrom\n // tokenFrom\n // tokenTo\n // trustedTokens\n // maxSteps\n const result = await camelot.findBestPath(\n amountFromScaled,\n tokenFrom,\n tokenTo,\n [],\n maxSteps || 4,\n );\n\n // It can happen that the amount from results in a zero amount out\n // dues to smaller precision of amount out, resulting in no path\n if (result.amounts.length == 0) {\n return {\n amountFrom: amountFrom,\n amountTo: 0,\n amountInBase: Number(amountFromScaled),\n amountOutBase: 0,\n adapters: [],\n path: [tokenFrom, tokenTo],\n recipients: [],\n };\n }\n\n return {\n amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),\n amountTo: descale(tokenInfoTo.decimals)(result.amounts[1]),\n amountInBase: result.amounts[0],\n amountOutBase: result.amounts[1],\n adapters: Array.from(result[1]),\n path: Array.from(result[2]),\n recipients: Array.from(result[3]),\n };\n};\n\n// Simulates sending the swap directly to Core using the publisher as msg.sender\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst getPreSwapMarginInfo = async (\n marginAccountId: number,\n): Promise<GetPreSwapMarginInfo> => {\n const chainId = getReyaNetwork();\n const provider = ethers.getDefaultProvider(\n reyaChainIdRPCMapper[chainId] as string,\n );\n const core = new Contract(\n getAddress(chainId, ContractType.CORE_PROXY),\n CoreAbi,\n provider,\n );\n\n const result = await core.getUsdNodeMarginInfo(marginAccountId);\n return {\n preSwapMarginBalance: Number(result[1]),\n lmr: Number(result[9]),\n };\n};\n\nexport const simulateSwap = async (\n params: SimulateSwapParams,\n): Promise<SimulateSwapResult> => {\n const bestPathResult = await findBestPath(\n params.amountFrom,\n params.tokenFrom,\n params.tokenTo,\n );\n\n // slippage calculation\n const simulatedPrice = bestPathResult.amountTo / bestPathResult.amountFrom;\n const restModule = getGeneralRestModuleClient();\n const markets: SpotMarketEntity[] = await restModule.getSpotMarkets();\n\n const market = markets.find((m) => {\n return (\n m.quoteToken.address == params.tokenFrom.toLowerCase() &&\n m.underlyingAsset.address == params.tokenTo.toLowerCase()\n );\n });\n let estimatedSlippage = 0;\n if (market != undefined) {\n estimatedSlippage = (market.price - simulatedPrice) / market.price;\n } else {\n const marketReversed = markets.find((m) => {\n return (\n m.underlyingAsset.address == params.tokenFrom.toLowerCase() &&\n m.quoteToken.address == params.tokenTo.toLowerCase()\n );\n });\n if (marketReversed != undefined) {\n estimatedSlippage =\n (1 / marketReversed.price - simulatedPrice) /\n (1 / marketReversed.price);\n }\n }\n\n const collaterals: CollateralWithHaircut[] =\n await restModule.getCollateralInfo();\n const tokenFromHaircut = collaterals.find(\n (c) => c.address == params.tokenFrom.toLowerCase(),\n )?.priceHaircut;\n const tokenToHaircut = collaterals.find(\n (c) => c.address == params.tokenFrom.toLowerCase(),\n )?.priceHaircut;\n\n if (tokenFromHaircut === undefined || tokenToHaircut === undefined) {\n throw new Error('Cannot find collateral tokens');\n }\n\n const marginInfo = await getPreSwapMarginInfo(params.marginAccountId);\n const adjustedAmountFrom = bestPathResult.amountFrom * (1 - tokenFromHaircut);\n const adjustedAmountTo = bestPathResult.amountTo * (1 - tokenToHaircut);\n\n const postSwapMargin =\n marginInfo.preSwapMarginBalance - adjustedAmountFrom + adjustedAmountTo;\n\n // Calculate the margin ratio\n const marginRatio = (marginInfo.lmr / postSwapMargin) * 100;\n\n return {\n bestPath: bestPathResult,\n estimatedSlippage: estimatedSlippage,\n marginRatio: marginRatio,\n marginRatioHealth: ExposureCommand.evaluateHealthStatus(marginRatio),\n };\n};\n"]}
|
|
@@ -42,7 +42,7 @@ var signAndBroadcastTransaction_1 = require("../signAndBroadcastTransaction");
|
|
|
42
42
|
var encode_1 = require("./encode");
|
|
43
43
|
var network_1 = require("../../utils/network");
|
|
44
44
|
var swap = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
45
|
-
var chainId, _a, data, eip712Payload, eip712Signature, result;
|
|
45
|
+
var chainId, slippageBufferMultiplier, _a, data, eip712Payload, eip712Signature, result;
|
|
46
46
|
return __generator(this, function (_b) {
|
|
47
47
|
switch (_b.label) {
|
|
48
48
|
case 0:
|
|
@@ -50,7 +50,8 @@ var swap = function (params) { return __awaiter(void 0, void 0, void 0, function
|
|
|
50
50
|
throw new Error('Swap amount cannot be 0');
|
|
51
51
|
}
|
|
52
52
|
chainId = (0, network_1.getReyaNetwork)();
|
|
53
|
-
|
|
53
|
+
slippageBufferMultiplier = 1.01;
|
|
54
|
+
return [4 /*yield*/, (0, encode_1.encodeSwap)(params.signer, chainId, params.owner.coreSigNonce, params.marginAccountId, params.amountInBase, params.amountOutBase * slippageBufferMultiplier, params.path, params.adapters, params.recipients, 0, // 0 for marketID
|
|
54
55
|
0)];
|
|
55
56
|
case 1:
|
|
56
57
|
_a = _b.sent(), data = _a.calldata, eip712Payload = _a.payload, eip712Signature = _a.signature;
|
|
@@ -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;AAE9C,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;
|
|
1
|
+
{"version":3,"file":"swap.js","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAA+C;AAC/C,8EAA6E;AAC7E,mCAAsC;AAEtC,+CAAqD;AAE9C,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,wBAAwB,GAAG,IAAI,CAAC;gBAMlC,qBAAM,IAAA,mBAAU,EAClB,MAAM,CAAC,MAAM,EACb,OAAO,EACP,MAAM,CAAC,KAAK,CAAC,YAAY,EACzB,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,YAAY,EACnB,MAAM,CAAC,aAAa,GAAG,wBAAwB,EAC/C,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;AA/CW,QAAA,IAAI,QA+Cf","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 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 slippageBufferMultiplier = 1.01; // 1% buffer\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,\n params.amountOutBase * slippageBufferMultiplier,\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":"simulation.d.ts","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,kBAAkB,EAElB,kBAAkB,EAClB,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAyEjB,eAAO,MAAM,YAAY,eACX,MAAM,aACP,MAAM,WACR,MAAM,aACJ,MAAM,KAChB,QAAQ,kBAAkB,
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,kBAAkB,EAElB,kBAAkB,EAClB,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAyEjB,eAAO,MAAM,YAAY,eACX,MAAM,aACP,MAAM,WACR,MAAM,aACJ,MAAM,KAChB,QAAQ,kBAAkB,CAgD5B,CAAC;AAwBF,eAAO,MAAM,YAAY,WACf,kBAAkB,KACzB,QAAQ,kBAAkB,CAgE5B,CAAC"}
|
|
@@ -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,IAAI,WAAkB,UAAU,KAAG,QAAQ,UAAU,
|
|
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,IAAI,WAAkB,UAAU,KAAG,QAAQ,UAAU,CA+CjE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.3",
|
|
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": "
|
|
38
|
+
"gitHead": "7d42118b5244b1d7fb487b5e1cc771c7218204e9"
|
|
39
39
|
}
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
descale,
|
|
4
4
|
getAddress,
|
|
5
5
|
getTokenInfoByAddress,
|
|
6
|
-
|
|
6
|
+
scaleV2,
|
|
7
7
|
reyaChainIdRPCMapper,
|
|
8
8
|
ExposureCommand,
|
|
9
9
|
CoreAbi,
|
|
@@ -104,7 +104,7 @@ export const findBestPath = async (
|
|
|
104
104
|
|
|
105
105
|
const tokenInfoFrom = getTokenInfoByAddress(tokenFrom);
|
|
106
106
|
const tokenInfoTo = getTokenInfoByAddress(tokenTo);
|
|
107
|
-
const amountFromScaled =
|
|
107
|
+
const amountFromScaled = scaleV2(tokenInfoFrom.decimals)(amountFrom);
|
|
108
108
|
|
|
109
109
|
// amountFrom
|
|
110
110
|
// tokenFrom
|
|
@@ -118,6 +118,21 @@ export const findBestPath = async (
|
|
|
118
118
|
[],
|
|
119
119
|
maxSteps || 4,
|
|
120
120
|
);
|
|
121
|
+
|
|
122
|
+
// It can happen that the amount from results in a zero amount out
|
|
123
|
+
// dues to smaller precision of amount out, resulting in no path
|
|
124
|
+
if (result.amounts.length == 0) {
|
|
125
|
+
return {
|
|
126
|
+
amountFrom: amountFrom,
|
|
127
|
+
amountTo: 0,
|
|
128
|
+
amountInBase: Number(amountFromScaled),
|
|
129
|
+
amountOutBase: 0,
|
|
130
|
+
adapters: [],
|
|
131
|
+
path: [tokenFrom, tokenTo],
|
|
132
|
+
recipients: [],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
121
136
|
return {
|
|
122
137
|
amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
123
138
|
amountTo: descale(tokenInfoTo.decimals)(result.amounts[1]),
|
|
@@ -209,7 +224,7 @@ export const simulateSwap = async (
|
|
|
209
224
|
marginInfo.preSwapMarginBalance - adjustedAmountFrom + adjustedAmountTo;
|
|
210
225
|
|
|
211
226
|
// Calculate the margin ratio
|
|
212
|
-
const marginRatio = marginInfo.lmr / postSwapMargin;
|
|
227
|
+
const marginRatio = (marginInfo.lmr / postSwapMargin) * 100;
|
|
213
228
|
|
|
214
229
|
return {
|
|
215
230
|
bestPath: bestPathResult,
|
|
@@ -11,6 +11,8 @@ export const swap = async (params: SwapParams): Promise<SwapResult> => {
|
|
|
11
11
|
|
|
12
12
|
const chainId = getReyaNetwork();
|
|
13
13
|
|
|
14
|
+
const slippageBufferMultiplier = 1.01; // 1% buffer
|
|
15
|
+
|
|
14
16
|
const {
|
|
15
17
|
calldata: data,
|
|
16
18
|
payload: eip712Payload,
|
|
@@ -21,7 +23,7 @@ export const swap = async (params: SwapParams): Promise<SwapResult> => {
|
|
|
21
23
|
params.owner.coreSigNonce,
|
|
22
24
|
params.marginAccountId,
|
|
23
25
|
params.amountInBase,
|
|
24
|
-
params.amountOutBase,
|
|
26
|
+
params.amountOutBase * slippageBufferMultiplier,
|
|
25
27
|
params.path,
|
|
26
28
|
params.adapters,
|
|
27
29
|
params.recipients,
|