@reyaxyz/sdk 0.124.13 → 0.124.14
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.
|
@@ -132,7 +132,7 @@ var findBestPath = function (amountFrom, tokenFrom, tokenTo, maxSteps) { return
|
|
|
132
132
|
}
|
|
133
133
|
return [2 /*return*/, {
|
|
134
134
|
amountFrom: (0, common_1.descale)(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
135
|
-
amountTo: (0, common_1.descale)(tokenInfoTo.decimals)(result.amounts[1]),
|
|
135
|
+
amountTo: (0, common_1.descale)(tokenInfoTo.decimals)(result.amounts[result.amounts.length - 1]),
|
|
136
136
|
amountInBase: result.amounts[0],
|
|
137
137
|
amountOutBase: result.amounts[1],
|
|
138
138
|
adapters: Array.from(result[1]),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simulation.js","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAWyB;AACzB,iCAA0C;AAO1C,uCAA0D;AAC1D,+CAAqD;AACrD,+BAA0C;AAE1C,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,IAAI,EAAE,gBAAgB;gBACtB,IAAI,EAAE,WAAW;aAClB;YACD;gBACE,YAAY,EAAE,SAAS;gBACvB,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,SAAS;aAChB;SACF;QACD,IAAI,EAAE,cAA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AAoB,CAAC,MAAM,CAAC,eAAe,CAAC,EAAA;;gBAA/D,UAAU,GAAG,SAAkD;gBAC/D,kBAAkB,GACtB,cAAc,CAAC,UAAU,GAAG,cAAc,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC;gBAChE,gBAAgB,GACpB,cAAc,CAAC,QAAQ,GAAG,YAAY,GAAG,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC;gBAE1D,cAAc,GAClB,UAAU,CAAC,oBAAoB;oBAC/B,CAAC,kBAAkB,GAAG,gBAAgB,CAAC,GAAG,SAAA,EAAE,EAAI,EAAE,CAAA,CAAC;gBAG/C,WAAW,GAAG,CAAC,UAAU,CAAC,GAAG,GAAG,cAAc,CAAC,GAAG,GAAG,CAAC;gBAE5D,sBAAO;wBACL,QAAQ,EAAE,cAAc;wBACxB,oBAAoB,EAAE,oBAAoB,GAAG,GAAG;wBAChD,eAAe,EAAE,uBAAgB,GAAG,GAAG;wBACvC,WAAW,EAAE,WAAW;wBACxB,iBAAiB,EAAE,wBAAe,CAAC,oBAAoB,CAAC,WAAW,CAAC;qBACrE,EAAC;;;KACH,CAAC;AA/EW,QAAA,YAAY,gBA+EvB","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';\nimport { DEFAULT_SLIPPAGE } from './swap';\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 // due to smaller precision of amount out, resulting in no path\n if (result.amounts.length == 0) {\n throw new Error('Amount to be received is zero. Not a viable swap.');\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 let tokenFromPrice = 1;\n let tokenToPrice = 1;\n let estimatedPriceImpact = 0;\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\n // check both directions of the market\n if (market != undefined) {\n estimatedPriceImpact = (market.price - simulatedPrice) / market.price;\n tokenFromPrice = market.quoteToken.price;\n tokenToPrice = market.underlyingAsset.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 estimatedPriceImpact =\n (1 / marketReversed.price - simulatedPrice) /\n (1 / marketReversed.price);\n tokenFromPrice = marketReversed.underlyingAsset.price;\n tokenToPrice = marketReversed.quoteToken.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 =\n bestPathResult.amountFrom * tokenFromPrice * (1 - tokenFromHaircut);\n const adjustedAmountTo =\n bestPathResult.amountTo * tokenToPrice * (1 - tokenToHaircut);\n\n const postSwapMargin =\n marginInfo.preSwapMarginBalance -\n (adjustedAmountFrom - adjustedAmountTo) * 10 ** 18;\n\n // Calculate the margin ratio\n const marginRatio = (marginInfo.lmr / postSwapMargin) * 100;\n\n return {\n bestPath: bestPathResult,\n estimatedPriceImpact: estimatedPriceImpact * 100,\n defaultSlippage: DEFAULT_SLIPPAGE * 100,\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;AACrD,+BAA0C;AAE1C,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,IAAI,EAAE,gBAAgB;gBACtB,IAAI,EAAE,WAAW;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AEhB,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,GACtB,cAAc,CAAC,UAAU,GAAG,cAAc,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC;gBAChE,gBAAgB,GACpB,cAAc,CAAC,QAAQ,GAAG,YAAY,GAAG,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC;gBAE1D,cAAc,GAClB,UAAU,CAAC,oBAAoB;oBAC/B,CAAC,kBAAkB,GAAG,gBAAgB,CAAC,GAAG,SAAA,EAAE,EAAI,EAAE,CAAA,CAAC;gBAG/C,WAAW,GAAG,CAAC,UAAU,CAAC,GAAG,GAAG,cAAc,CAAC,GAAG,GAAG,CAAC;gBAE5D,sBAAO;wBACL,QAAQ,EAAE,cAAc;wBACxB,oBAAoB,EAAE,oBAAoB,GAAG,GAAG;wBAChD,eAAe,EAAE,uBAAgB,GAAG,GAAG;wBACvC,WAAW,EAAE,WAAW;wBACxB,iBAAiB,EAAE,wBAAe,CAAC,oBAAoB,CAAC,WAAW,CAAC;qBACrE,EAAC;;;KACH,CAAC;AA/EW,QAAA,YAAY,gBA+EvB","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';\nimport { DEFAULT_SLIPPAGE } from './swap';\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 // due to smaller precision of amount out, resulting in no path\n if (result.amounts.length == 0) {\n throw new Error('Amount to be received is zero. Not a viable swap.');\n }\n\n return {\n amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),\n amountTo: descale(tokenInfoTo.decimals)(\n result.amounts[result.amounts.length - 1],\n ),\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 let tokenFromPrice = 1;\n let tokenToPrice = 1;\n let estimatedPriceImpact = 0;\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\n // check both directions of the market\n if (market != undefined) {\n estimatedPriceImpact = (market.price - simulatedPrice) / market.price;\n tokenFromPrice = market.quoteToken.price;\n tokenToPrice = market.underlyingAsset.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 estimatedPriceImpact =\n (1 / marketReversed.price - simulatedPrice) /\n (1 / marketReversed.price);\n tokenFromPrice = marketReversed.underlyingAsset.price;\n tokenToPrice = marketReversed.quoteToken.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 =\n bestPathResult.amountFrom * tokenFromPrice * (1 - tokenFromHaircut);\n const adjustedAmountTo =\n bestPathResult.amountTo * tokenToPrice * (1 - tokenToHaircut);\n\n const postSwapMargin =\n marginInfo.preSwapMarginBalance -\n (adjustedAmountFrom - adjustedAmountTo) * 10 ** 18;\n\n // Calculate the margin ratio\n const marginRatio = (marginInfo.lmr / postSwapMargin) * 100;\n\n return {\n bestPath: bestPathResult,\n estimatedPriceImpact: estimatedPriceImpact * 100,\n defaultSlippage: DEFAULT_SLIPPAGE * 100,\n marginRatio: marginRatio,\n marginRatioHealth: ExposureCommand.evaluateHealthStatus(marginRatio),\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;AA0EjB,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;AA0EjB,eAAO,MAAM,YAAY,eACX,MAAM,aACP,MAAM,WACR,MAAM,aACJ,MAAM,KAChB,QAAQ,kBAAkB,CA0C5B,CAAC;AAwBF,eAAO,MAAM,YAAY,WACf,kBAAkB,KACzB,QAAQ,kBAAkB,CA6E5B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.124.
|
|
3
|
+
"version": "0.124.14",
|
|
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": "7c44008b9fd423cf42ce497ccb6df8a8417fe6ed"
|
|
39
39
|
}
|
|
@@ -128,7 +128,9 @@ export const findBestPath = async (
|
|
|
128
128
|
|
|
129
129
|
return {
|
|
130
130
|
amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
131
|
-
amountTo: descale(tokenInfoTo.decimals)(
|
|
131
|
+
amountTo: descale(tokenInfoTo.decimals)(
|
|
132
|
+
result.amounts[result.amounts.length - 1],
|
|
133
|
+
),
|
|
132
134
|
amountInBase: result.amounts[0],
|
|
133
135
|
amountOutBase: result.amounts[1],
|
|
134
136
|
adapters: Array.from(result[1]),
|