@reyaxyz/sdk 0.118.2 → 0.118.4
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/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/services/encode.js +6 -1
- package/dist/services/encode.js.map +1 -1
- package/dist/services/swaps/encode.js +70 -0
- package/dist/services/swaps/encode.js.map +1 -0
- package/dist/services/swaps/index.js +19 -0
- package/dist/services/swaps/index.js.map +1 -0
- package/dist/services/swaps/simulation.js +140 -0
- package/dist/services/swaps/simulation.js.map +1 -0
- package/dist/services/swaps/swap.js +78 -0
- package/dist/services/swaps/swap.js.map +1 -0
- package/dist/services/swaps/types.js +3 -0
- package/dist/services/swaps/types.js.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/encode.d.ts +1 -0
- package/dist/types/services/encode.d.ts.map +1 -1
- package/dist/types/services/swaps/encode.d.ts +4 -0
- package/dist/types/services/swaps/encode.d.ts.map +1 -0
- package/dist/types/services/swaps/index.d.ts +3 -0
- package/dist/types/services/swaps/index.d.ts.map +1 -0
- package/dist/types/services/swaps/simulation.d.ts +3 -0
- package/dist/types/services/swaps/simulation.d.ts.map +1 -0
- package/dist/types/services/swaps/swap.d.ts +3 -0
- package/dist/types/services/swaps/swap.d.ts.map +1 -0
- package/dist/types/services/swaps/types.d.ts +27 -0
- package/dist/types/services/swaps/types.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/services/encode.ts +21 -0
- package/src/services/swaps/encode.ts +67 -0
- package/src/services/swaps/index.ts +2 -0
- package/src/services/swaps/simulation.ts +120 -0
- package/src/services/swaps/swap.ts +53 -0
- package/src/services/swaps/types.ts +29 -0
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ __exportStar(require("./services/orders"), exports);
|
|
|
22
22
|
__exportStar(require("./services/conditional-orders"), exports);
|
|
23
23
|
__exportStar(require("./services/isolated-order"), exports);
|
|
24
24
|
__exportStar(require("./services/token"), exports);
|
|
25
|
+
__exportStar(require("./services/swaps"), exports);
|
|
25
26
|
var config_1 = require("./config");
|
|
26
27
|
Object.defineProperty(exports, "configureSDK", { enumerable: true, get: function () { return config_1.configureSDK; } });
|
|
27
28
|
Object.defineProperty(exports, "getTransactionModuleClient", { enumerable: true, get: function () { return config_1.getTransactionModuleClient; } });
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,6DAA2C;AAC3C,yDAAuC;AACvC,oDAAkC;AAClC,gEAA8C;AAC9C,4DAA0C;AAC1C,mDAAiC;AACjC,mCAAoE;AAA3D,sGAAA,YAAY,OAAA;AAAE,oHAAA,0BAA0B,OAAA","sourcesContent":["export * from './services/lp';\nexport * from './services/margin-accounts';\nexport * from './services/money-flows';\nexport * from './services/orders';\nexport * from './services/conditional-orders';\nexport * from './services/isolated-order';\nexport * from './services/token';\nexport { configureSDK, getTransactionModuleClient } from './config';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,6DAA2C;AAC3C,yDAAuC;AACvC,oDAAkC;AAClC,gEAA8C;AAC9C,4DAA0C;AAC1C,mDAAiC;AACjC,mDAAiC;AACjC,mCAAoE;AAA3D,sGAAA,YAAY,OAAA;AAAE,oHAAA,0BAA0B,OAAA","sourcesContent":["export * from './services/lp';\nexport * from './services/margin-accounts';\nexport * from './services/money-flows';\nexport * from './services/orders';\nexport * from './services/conditional-orders';\nexport * from './services/isolated-order';\nexport * from './services/token';\nexport * from './services/swaps';\nexport { configureSDK, getTransactionModuleClient } from './config';\n"]}
|
package/dist/services/encode.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
//////////////////// ENCODE SINGLE ////////////////////
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.encodeRouterCall = exports.encodeSingleTrade = exports.encodeSingleTransferMargin = exports.encodeSingleWithdraw = exports.encodeSingleDeposit = void 0;
|
|
4
|
+
exports.encodeRouterCall = exports.encodeSingleSwap = exports.encodeSingleTrade = exports.encodeSingleTransferMargin = exports.encodeSingleWithdraw = exports.encodeSingleDeposit = void 0;
|
|
5
5
|
var common_1 = require("@reyaxyz/common");
|
|
6
6
|
var common_2 = require("@reyaxyz/common");
|
|
7
7
|
var ethers_1 = require("ethers");
|
|
@@ -26,6 +26,11 @@ var encodeSingleTrade = function (counterpartyAccountIds, orderBase, orderPriceL
|
|
|
26
26
|
multiAction.newAction((0, common_2.getCommand)(common_2.CommandType.MATCH_ORDER, [counterpartyAccountIds, encodedBasePrice], marketId, exchangeId));
|
|
27
27
|
};
|
|
28
28
|
exports.encodeSingleTrade = encodeSingleTrade;
|
|
29
|
+
var encodeSingleSwap = function (amountIn, amountOut, path, adapters, recipients, marketId, exchangeId, multiAction) {
|
|
30
|
+
// Trade memory trade = abi.decode(command.inputs, (Trade));
|
|
31
|
+
multiAction.newAction((0, common_2.getCommand)(common_2.CommandType.HOOK, [[amountIn, amountOut, path, adapters, recipients]], marketId, exchangeId));
|
|
32
|
+
};
|
|
33
|
+
exports.encodeSingleSwap = encodeSingleSwap;
|
|
29
34
|
var encodeRouterCall = function (multiAction, accountId, nativeCurrencyValue) {
|
|
30
35
|
var functionSignature = 'execute';
|
|
31
36
|
var parameters = [accountId, multiAction.commands];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.js","sourceRoot":"/","sources":["services/encode.ts"],"names":[],"mappings":";AAAA,yDAAyD;;;AAEzD,0CAAyE;AACzE,0CAA0D;AAC1D,iCAA6C;AAC7C,gBAAgB;AAChB,wGAAwG;AACxG,KAAK;AACE,IAAM,mBAAmB,GAAG,UACjC,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CAAC,IAAA,mBAAU,EAAC,oBAAW,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAChF,CAAC,CAAC;AANW,QAAA,mBAAmB,uBAM9B;AAEK,IAAM,oBAAoB,GAAG,UAClC,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EAAC,oBAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CACxD,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,oBAAoB,wBAQ/B;AAEK,IAAM,0BAA0B,GAAG,UACxC,iBAAyB,EACzB,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EACR,oBAAW,CAAC,uBAAuB,EACnC,CAAC,CAAC,iBAAiB,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,EACpC,CAAC,EACD,CAAC,CACF,CACF,CAAC;AACJ,CAAC,CAAC;AAdW,QAAA,0BAA0B,8BAcrC;AAEK,IAAM,iBAAiB,GAAG,UAC/B,sBAAgC,EAChC,SAAiB,EACjB,eAAuB,EACvB,QAAgB,EAChB,UAAkB,EAClB,WAAwB;IAExB,wGAAwG;IACxG,IAAM,gBAAgB,GAAG,iBAAQ,CAAC,eAAe,EAAE,CAAC,MAAM,CACxD,CAAC,QAAQ,EAAE,SAAS,CAAC,EACrB,CAAC,SAAS,EAAE,eAAe,CAAC,CAC7B,CAAC;IAEF,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EACR,oBAAW,CAAC,WAAW,EACvB,CAAC,sBAAsB,EAAE,gBAAgB,CAAC,EAC1C,QAAQ,EACR,UAAU,CACX,CACF,CAAC;AACJ,CAAC,CAAC;AAtBW,QAAA,iBAAiB,qBAsB5B;AAEK,IAAM,gBAAgB,GAAG,UAC9B,WAAwB,EACxB,SAAiB,EACjB,mBAA2B;IAE3B,IAAM,iBAAiB,GAAG,SAAS,CAAC;IACpC,IAAM,UAAU,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAErD,IAAM,SAAS,GAAG,IAAI,kBAAS,CAAC,gBAAO,CAAC,CAAC;IACzC,IAAM,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;IAC7E,OAAO;QACL,QAAQ,EAAE,QAAQ;QAClB,KAAK,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;KAChD,CAAC;AACJ,CAAC,CAAC;AAdW,QAAA,gBAAgB,oBAc3B","sourcesContent":["//////////////////// ENCODE SINGLE ////////////////////\n\nimport { MethodParameters, MultiAction, CoreAbi } from '@reyaxyz/common';\nimport { CommandType, getCommand } from '@reyaxyz/common';\nimport { AbiCoder, Interface } from 'ethers';\n// const abi = [\n// 'function coreExecute(uint128 poolId, uint128 accountId, Command[] commands, EIP712Signature sig)',\n// ];\nexport const encodeSingleDeposit = (\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(getCommand(CommandType.DEPOSIT, [token, amount], 0, 0));\n};\n\nexport const encodeSingleWithdraw = (\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(\n getCommand(CommandType.WITHDRAW, [token, amount], 0, 0),\n );\n};\n\nexport const encodeSingleTransferMargin = (\n toMarginAccountId: number,\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(\n getCommand(\n CommandType.TRANSFER_MARGIN_ACCOUNT,\n [[toMarginAccountId, token, amount]],\n 0,\n 0,\n ),\n );\n};\n\nexport const encodeSingleTrade = (\n counterpartyAccountIds: number[],\n orderBase: bigint,\n orderPriceLimit: bigint,\n marketId: number,\n exchangeId: number,\n multiAction: MultiAction,\n): void => {\n // (int256 orderBase, UD60x18 orderPriceLimit) = abi.decode(matchOrderInputs.inputs, (int256, UD60x18));\n const encodedBasePrice = AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [orderBase, orderPriceLimit],\n );\n\n multiAction.newAction(\n getCommand(\n CommandType.MATCH_ORDER,\n [counterpartyAccountIds, encodedBasePrice],\n marketId,\n exchangeId,\n ),\n );\n};\n\nexport const encodeRouterCall = (\n multiAction: MultiAction,\n accountId: number,\n nativeCurrencyValue: bigint,\n): MethodParameters => {\n const functionSignature = 'execute';\n const parameters = [accountId, multiAction.commands];\n\n const INTERFACE = new Interface(CoreAbi);\n const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);\n return {\n calldata: calldata,\n value: BigInt(nativeCurrencyValue).toString(10),\n };\n};\n"]}
|
|
1
|
+
{"version":3,"file":"encode.js","sourceRoot":"/","sources":["services/encode.ts"],"names":[],"mappings":";AAAA,yDAAyD;;;AAEzD,0CAAyE;AACzE,0CAA0D;AAC1D,iCAA6C;AAC7C,gBAAgB;AAChB,wGAAwG;AACxG,KAAK;AACE,IAAM,mBAAmB,GAAG,UACjC,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CAAC,IAAA,mBAAU,EAAC,oBAAW,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAChF,CAAC,CAAC;AANW,QAAA,mBAAmB,uBAM9B;AAEK,IAAM,oBAAoB,GAAG,UAClC,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EAAC,oBAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CACxD,CAAC;AACJ,CAAC,CAAC;AARW,QAAA,oBAAoB,wBAQ/B;AAEK,IAAM,0BAA0B,GAAG,UACxC,iBAAyB,EACzB,KAAa,EACb,MAAc,EACd,WAAwB;IAExB,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EACR,oBAAW,CAAC,uBAAuB,EACnC,CAAC,CAAC,iBAAiB,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,EACpC,CAAC,EACD,CAAC,CACF,CACF,CAAC;AACJ,CAAC,CAAC;AAdW,QAAA,0BAA0B,8BAcrC;AAEK,IAAM,iBAAiB,GAAG,UAC/B,sBAAgC,EAChC,SAAiB,EACjB,eAAuB,EACvB,QAAgB,EAChB,UAAkB,EAClB,WAAwB;IAExB,wGAAwG;IACxG,IAAM,gBAAgB,GAAG,iBAAQ,CAAC,eAAe,EAAE,CAAC,MAAM,CACxD,CAAC,QAAQ,EAAE,SAAS,CAAC,EACrB,CAAC,SAAS,EAAE,eAAe,CAAC,CAC7B,CAAC;IAEF,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EACR,oBAAW,CAAC,WAAW,EACvB,CAAC,sBAAsB,EAAE,gBAAgB,CAAC,EAC1C,QAAQ,EACR,UAAU,CACX,CACF,CAAC;AACJ,CAAC,CAAC;AAtBW,QAAA,iBAAiB,qBAsB5B;AAEK,IAAM,gBAAgB,GAAG,UAC9B,QAAgB,EAChB,SAAiB,EACjB,IAAc,EACd,QAAkB,EAClB,UAAoB,EACpB,QAAgB,EAChB,UAAkB,EAClB,WAAwB;IAExB,4DAA4D;IAC5D,WAAW,CAAC,SAAS,CACnB,IAAA,mBAAU,EACR,oBAAW,CAAC,IAAI,EAChB,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,CAAC,CAAC,EACnD,QAAQ,EACR,UAAU,CACX,CACF,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,gBAAgB,oBAmB3B;AAEK,IAAM,gBAAgB,GAAG,UAC9B,WAAwB,EACxB,SAAiB,EACjB,mBAA2B;IAE3B,IAAM,iBAAiB,GAAG,SAAS,CAAC;IACpC,IAAM,UAAU,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAErD,IAAM,SAAS,GAAG,IAAI,kBAAS,CAAC,gBAAO,CAAC,CAAC;IACzC,IAAM,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;IAC7E,OAAO;QACL,QAAQ,EAAE,QAAQ;QAClB,KAAK,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;KAChD,CAAC;AACJ,CAAC,CAAC;AAdW,QAAA,gBAAgB,oBAc3B","sourcesContent":["//////////////////// ENCODE SINGLE ////////////////////\n\nimport { MethodParameters, MultiAction, CoreAbi } from '@reyaxyz/common';\nimport { CommandType, getCommand } from '@reyaxyz/common';\nimport { AbiCoder, Interface } from 'ethers';\n// const abi = [\n// 'function coreExecute(uint128 poolId, uint128 accountId, Command[] commands, EIP712Signature sig)',\n// ];\nexport const encodeSingleDeposit = (\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(getCommand(CommandType.DEPOSIT, [token, amount], 0, 0));\n};\n\nexport const encodeSingleWithdraw = (\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(\n getCommand(CommandType.WITHDRAW, [token, amount], 0, 0),\n );\n};\n\nexport const encodeSingleTransferMargin = (\n toMarginAccountId: number,\n token: string,\n amount: bigint,\n multiAction: MultiAction,\n): void => {\n multiAction.newAction(\n getCommand(\n CommandType.TRANSFER_MARGIN_ACCOUNT,\n [[toMarginAccountId, token, amount]],\n 0,\n 0,\n ),\n );\n};\n\nexport const encodeSingleTrade = (\n counterpartyAccountIds: number[],\n orderBase: bigint,\n orderPriceLimit: bigint,\n marketId: number,\n exchangeId: number,\n multiAction: MultiAction,\n): void => {\n // (int256 orderBase, UD60x18 orderPriceLimit) = abi.decode(matchOrderInputs.inputs, (int256, UD60x18));\n const encodedBasePrice = AbiCoder.defaultAbiCoder().encode(\n ['int256', 'uint256'],\n [orderBase, orderPriceLimit],\n );\n\n multiAction.newAction(\n getCommand(\n CommandType.MATCH_ORDER,\n [counterpartyAccountIds, encodedBasePrice],\n marketId,\n exchangeId,\n ),\n );\n};\n\nexport const encodeSingleSwap = (\n amountIn: number,\n amountOut: number,\n path: string[],\n adapters: string[],\n recipients: string[],\n marketId: number,\n exchangeId: number,\n multiAction: MultiAction,\n): void => {\n // Trade memory trade = abi.decode(command.inputs, (Trade));\n multiAction.newAction(\n getCommand(\n CommandType.HOOK,\n [[amountIn, amountOut, path, adapters, recipients]],\n marketId,\n exchangeId,\n ),\n );\n};\n\nexport const encodeRouterCall = (\n multiAction: MultiAction,\n accountId: number,\n nativeCurrencyValue: bigint,\n): MethodParameters => {\n const functionSignature = 'execute';\n const parameters = [accountId, multiAction.commands];\n\n const INTERFACE = new Interface(CoreAbi);\n const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);\n return {\n calldata: calldata,\n value: BigInt(nativeCurrencyValue).toString(10),\n };\n};\n"]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.encodeSwap = void 0;
|
|
40
|
+
var ethers_1 = require("ethers");
|
|
41
|
+
var common_1 = require("@reyaxyz/common");
|
|
42
|
+
var encode_1 = require("../encode");
|
|
43
|
+
var encodeSwap = function (signer, chainId, coreSigNonce, accountId, amountIn, amountOut, path, adapters, recipients, marketId, exchangeId) { return __awaiter(void 0, void 0, void 0, function () {
|
|
44
|
+
var multiAction, _a, eip712Signature, eip712Payload, value, functionSignature, parameters, INTERFACE, calldata;
|
|
45
|
+
return __generator(this, function (_b) {
|
|
46
|
+
switch (_b.label) {
|
|
47
|
+
case 0:
|
|
48
|
+
multiAction = new common_1.MultiAction();
|
|
49
|
+
(0, encode_1.encodeSingleSwap)(amountIn, amountOut, path, adapters, recipients, marketId, exchangeId, multiAction);
|
|
50
|
+
return [4 /*yield*/, (0, common_1.signCoreCommands)(signer, chainId, (0, common_1.getAddress)(chainId, common_1.ContractType.PERIPHERY_PROXY), accountId, multiAction.commands, coreSigNonce + 1, (0, common_1.getCurrentTimestampInSeconds)() + common_1.CORE_DEADLINE_IN_SECONDS, ethers_1.ethers.AbiCoder.defaultAbiCoder().encode([], []))];
|
|
51
|
+
case 1:
|
|
52
|
+
_a = _b.sent(), eip712Signature = _a.signature, eip712Payload = _a.payload;
|
|
53
|
+
value = BigInt(0);
|
|
54
|
+
functionSignature = 'execute';
|
|
55
|
+
parameters = [
|
|
56
|
+
{ accountId: accountId, commands: multiAction.commands, sig: eip712Signature },
|
|
57
|
+
];
|
|
58
|
+
INTERFACE = new ethers_1.Interface(common_1.PeripheryAbi);
|
|
59
|
+
calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
60
|
+
return [2 /*return*/, {
|
|
61
|
+
calldata: calldata,
|
|
62
|
+
value: BigInt(value).toString(10),
|
|
63
|
+
signature: eip712Signature,
|
|
64
|
+
payload: eip712Payload,
|
|
65
|
+
}];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}); };
|
|
69
|
+
exports.encodeSwap = encodeSwap;
|
|
70
|
+
//# sourceMappingURL=encode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.js","sourceRoot":"/","sources":["services/swaps/encode.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAmD;AAEnD,0CAQyB;AACzB,oCAA6C;AAEtC,IAAM,UAAU,GAAG,UACxB,MAAc,EACd,OAAe,EACf,YAAoB,EACpB,SAAiB,EACjB,QAAgB,EAChB,SAAiB,EACjB,IAAc,EACd,QAAkB,EAClB,UAAoB,EACpB,QAAgB,EAChB,UAAkB;;;;;gBAEZ,WAAW,GAAG,IAAI,oBAAW,EAAE,CAAC;gBAEtC,IAAA,yBAAgB,EACd,QAAQ,EACR,SAAS,EACT,IAAI,EACJ,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,UAAU,EACV,WAAW,CACZ,CAAC;gBAGA,qBAAM,IAAA,yBAAgB,EACpB,MAAM,EACN,OAAO,EACP,IAAA,mBAAU,EAAC,OAAO,EAAE,qBAAY,CAAC,eAAe,CAAC,EACjD,SAAS,EACT,WAAW,CAAC,QAAQ,EACpB,YAAY,GAAG,CAAC,EAChB,IAAA,qCAA4B,GAAE,GAAG,iCAAwB,EACzD,eAAM,CAAC,QAAQ,CAAC,eAAe,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,EAAE,CAAC,CACjD,EAAA;;gBAVG,KACJ,SASC,EAVgB,eAAe,eAAA,EAAW,aAAa,aAAA;gBAYpD,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;gBAElB,iBAAiB,GAAG,SAAS,CAAC;gBAC9B,UAAU,GAAG;oBACjB,EAAE,SAAS,WAAA,EAAE,QAAQ,EAAE,WAAW,CAAC,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE;iBACpE,CAAC;gBAEI,SAAS,GAAG,IAAI,kBAAS,CAAC,qBAAG,CAAC,CAAC;gBAC/B,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;gBAC7E,sBAAO;wBACL,QAAQ,EAAE,QAAQ;wBAClB,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;wBACjC,SAAS,EAAE,eAAe;wBAC1B,OAAO,EAAE,aAAa;qBACvB,EAAC;;;KACH,CAAC;AArDW,QAAA,UAAU,cAqDrB","sourcesContent":["import { ethers, Interface, Signer } from 'ethers';\nimport { MethodParametersAndEIP712Payload } from '../orders';\nimport {\n ContractType,\n CORE_DEADLINE_IN_SECONDS,\n getAddress,\n getCurrentTimestampInSeconds,\n MultiAction,\n PeripheryAbi as abi,\n signCoreCommands,\n} from '@reyaxyz/common';\nimport { encodeSingleSwap } from '../encode';\n\nexport const encodeSwap = async (\n signer: Signer,\n chainId: number,\n coreSigNonce: number,\n accountId: number,\n amountIn: number,\n amountOut: number,\n path: string[],\n adapters: string[],\n recipients: string[],\n marketId: number,\n exchangeId: number,\n): Promise<MethodParametersAndEIP712Payload> => {\n const multiAction = new MultiAction();\n\n encodeSingleSwap(\n amountIn,\n amountOut,\n path,\n adapters,\n recipients,\n marketId,\n exchangeId,\n multiAction,\n );\n\n const { signature: eip712Signature, payload: eip712Payload } =\n await signCoreCommands(\n signer,\n chainId,\n getAddress(chainId, ContractType.PERIPHERY_PROXY),\n accountId,\n multiAction.commands,\n coreSigNonce + 1,\n getCurrentTimestampInSeconds() + CORE_DEADLINE_IN_SECONDS,\n ethers.AbiCoder.defaultAbiCoder().encode([], []), // @todo check this\n );\n\n const value = BigInt(0);\n\n const functionSignature = 'execute';\n const parameters = [\n { accountId, commands: multiAction.commands, sig: eip712Signature },\n ];\n\n const INTERFACE = new Interface(abi);\n const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);\n return {\n calldata: calldata,\n value: BigInt(value).toString(10),\n signature: eip712Signature,\n payload: eip712Payload,\n };\n};\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./simulation"), exports);
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["services/swaps/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,0CAAwB","sourcesContent":["export * from './simulation';\nexport * from './types';\n"]}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.findBestPath = void 0;
|
|
40
|
+
var common_1 = require("@reyaxyz/common");
|
|
41
|
+
var ethers_1 = require("ethers");
|
|
42
|
+
var config_1 = require("../../config");
|
|
43
|
+
var abi = [
|
|
44
|
+
{
|
|
45
|
+
inputs: [
|
|
46
|
+
{
|
|
47
|
+
internalType: 'uint256',
|
|
48
|
+
name: '_amountIn',
|
|
49
|
+
type: 'uint256',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
internalType: 'address',
|
|
53
|
+
name: '_tokenIn',
|
|
54
|
+
type: 'address',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
internalType: 'address',
|
|
58
|
+
name: '_tokenOut',
|
|
59
|
+
type: 'address',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
internalType: 'address[]',
|
|
63
|
+
name: '_trustedTokens',
|
|
64
|
+
type: 'address[]',
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
internalType: 'uint256',
|
|
68
|
+
name: '_maxSteps',
|
|
69
|
+
type: 'uint256',
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
name: 'findBestPath',
|
|
73
|
+
outputs: [
|
|
74
|
+
{
|
|
75
|
+
components: [
|
|
76
|
+
{
|
|
77
|
+
internalType: 'uint256[]',
|
|
78
|
+
name: 'amounts',
|
|
79
|
+
type: 'uint256[]',
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
internalType: 'address[]',
|
|
83
|
+
name: 'adapters',
|
|
84
|
+
type: 'address[]',
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
internalType: 'address[]',
|
|
88
|
+
name: 'path',
|
|
89
|
+
type: 'address[]',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
internalType: 'address[]',
|
|
93
|
+
name: 'recipients',
|
|
94
|
+
type: 'address[]',
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
internalType: 'uint256',
|
|
98
|
+
name: 'gasEstimate',
|
|
99
|
+
type: 'uint256',
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
internalType: 'struct FormattedOffer',
|
|
103
|
+
name: '',
|
|
104
|
+
type: 'tuple',
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
stateMutability: 'view',
|
|
108
|
+
type: 'function',
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
var env = (0, config_1.getSdkEnvironment)();
|
|
112
|
+
var chainId = (0, common_1.getReyaNetwork)(env === 'production' ? 'production' : 'test');
|
|
113
|
+
var provider = ethers_1.ethers.getDefaultProvider(common_1.reyaChainIdRPCMapper[chainId]);
|
|
114
|
+
var findBestPath = function (amountFrom, tokenFrom, tokenTo, maxSteps) { return __awaiter(void 0, void 0, void 0, function () {
|
|
115
|
+
var coreAddress, core, tokenInfoFrom, tokenInfoTo, amountFromScaled, result;
|
|
116
|
+
return __generator(this, function (_a) {
|
|
117
|
+
switch (_a.label) {
|
|
118
|
+
case 0:
|
|
119
|
+
coreAddress = (0, common_1.getAddress)(chainId, common_1.ContractType.CAMELOT_YAK_ROUTER);
|
|
120
|
+
core = new ethers_1.Contract(coreAddress, abi, provider);
|
|
121
|
+
tokenInfoFrom = (0, common_1.getTokenInfoByAddress)(tokenFrom);
|
|
122
|
+
tokenInfoTo = (0, common_1.getTokenInfoByAddress)(tokenTo);
|
|
123
|
+
amountFromScaled = (0, common_1.scale)(tokenInfoFrom.decimals)(amountFrom);
|
|
124
|
+
return [4 /*yield*/, core.findBestPath(amountFromScaled, tokenFrom, tokenTo, [], maxSteps || 4)];
|
|
125
|
+
case 1:
|
|
126
|
+
result = _a.sent();
|
|
127
|
+
return [2 /*return*/, {
|
|
128
|
+
amountFrom: (0, common_1.descale)(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
129
|
+
amountTo: (0, common_1.descale)(tokenInfoTo.decimals)(result.amounts[1]),
|
|
130
|
+
amountInBase: result.amounts[0],
|
|
131
|
+
amountOutBase: result.amounts[1],
|
|
132
|
+
adapters: Array.from(result[1]),
|
|
133
|
+
path: Array.from(result[2]),
|
|
134
|
+
recipients: Array.from(result[3]),
|
|
135
|
+
}];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}); };
|
|
139
|
+
exports.findBestPath = findBestPath;
|
|
140
|
+
//# sourceMappingURL=simulation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.js","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAQyB;AACzB,iCAA0C;AAE1C,uCAAiD;AACjD,IAAM,GAAG,GAAG;IACV;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,cAAc;QACpB,OAAO,EAAE;YACP;gBACE,UAAU,EAAE;oBACV;wBACE,YAAY,EAAE,WAAW;wBACzB,IAAI,EAAE,SAAS;wBACf,IAAI,EAAE,WAAW;qBAClB;oBACD;wBACE,YAAY,EAAE,WAAW;wBACzB,IAAI,EAAE,UAAU;wBAChB,IAAI,EAAE,WAAW;qBAClB;oBACD;wBACE,YAAY,EAAE,WAAW;wBACzB,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,WAAW;qBAClB;oBACD;wBACE,YAAY,EAAE,WAAW;wBACzB,IAAI,EAAE,YAAY;wBAClB,IAAI,EAAE,WAAW;qBAClB;oBACD;wBACE,YAAY,EAAE,SAAS;wBACvB,IAAI,EAAE,aAAa;wBACnB,IAAI,EAAE,SAAS;qBAChB;iBACF;gBACD,YAAY,EAAE,uBAAuB;gBACrC,IAAI,EAAE,EAAE;gBACR,IAAI,EAAE,OAAO;aACd;SACF;QACD,eAAe,EAAE,MAAM;QACvB,IAAI,EAAE,UAAU;KACjB;CACF,CAAC;AACF,IAAM,GAAG,GAAG,IAAA,0BAAiB,GAAE,CAAC;AAChC,IAAM,OAAO,GAAG,IAAA,uBAAc,EAAC,GAAG,KAAK,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAC7E,IAAM,QAAQ,GAAG,eAAM,CAAC,kBAAkB,CACxC,6BAAoB,CAAC,OAAO,CAAW,CACxC,CAAC;AACK,IAAM,YAAY,GAAG,UAC1B,UAAkB,EAClB,SAAiB,EACjB,OAAe,EACf,QAAiB;;;;;gBAEX,WAAW,GAAG,IAAA,mBAAU,EAAC,OAAO,EAAE,qBAAY,CAAC,kBAAkB,CAAC,CAAC;gBACnE,IAAI,GAAG,IAAI,iBAAQ,CAAC,WAAW,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;gBAEhD,aAAa,GAAG,IAAA,8BAAqB,EAAC,SAAS,CAAC,CAAC;gBACjD,WAAW,GAAG,IAAA,8BAAqB,EAAC,OAAO,CAAC,CAAC;gBAC7C,gBAAgB,GAAG,IAAA,cAAK,EAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC;gBAOpD,qBAAM,IAAI,CAAC,YAAY,CACpC,gBAAgB,EAChB,SAAS,EACT,OAAO,EACP,EAAE,EACF,QAAQ,IAAI,CAAC,CACd,EAAA;;gBANK,MAAM,GAAG,SAMd;gBACD,sBAAO;wBACL,UAAU,EAAE,IAAA,gBAAO,EAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;wBAC9D,QAAQ,EAAE,IAAA,gBAAO,EAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;wBAC1D,YAAY,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;wBAC/B,aAAa,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;wBAChC,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;wBAC/B,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;wBAC3B,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;qBAClC,EAAC;;;KACH,CAAC;AAlCW,QAAA,YAAY,gBAkCvB","sourcesContent":["import {\n ContractType,\n descale,\n getAddress,\n getReyaNetwork,\n getTokenInfoByAddress,\n scale,\n reyaChainIdRPCMapper,\n} from '@reyaxyz/common';\nimport { Contract, ethers } from 'ethers';\nimport { FindBestPathResult } from './types';\nimport { getSdkEnvironment } from '../../config';\nconst abi = [\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];\nconst env = getSdkEnvironment();\nconst chainId = getReyaNetwork(env === 'production' ? 'production' : 'test');\nconst provider = ethers.getDefaultProvider(\n reyaChainIdRPCMapper[chainId] as string,\n);\nexport const findBestPath = async (\n amountFrom: number,\n tokenFrom: string,\n tokenTo: string,\n maxSteps?: number,\n): Promise<FindBestPathResult> => {\n const coreAddress = getAddress(chainId, ContractType.CAMELOT_YAK_ROUTER);\n const core = new Contract(coreAddress, abi, 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 core.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"]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.swap = void 0;
|
|
40
|
+
var common_1 = require("@reyaxyz/common");
|
|
41
|
+
var config_1 = require("../../config");
|
|
42
|
+
var signAndBroadcastTransaction_1 = require("../signAndBroadcastTransaction");
|
|
43
|
+
var encode_1 = require("./encode");
|
|
44
|
+
var swap = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
45
|
+
var env, chainId, _a, data, eip712Payload, eip712Signature, result;
|
|
46
|
+
return __generator(this, function (_b) {
|
|
47
|
+
switch (_b.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
if (params.amountInBase === 0) {
|
|
50
|
+
throw new Error('Swap amount cannot be 0');
|
|
51
|
+
}
|
|
52
|
+
env = (0, config_1.getSdkEnvironment)();
|
|
53
|
+
chainId = (0, common_1.getReyaNetwork)(env === 'production' ? 'production' : 'test');
|
|
54
|
+
return [4 /*yield*/, (0, encode_1.encodeSwap)(params.signer, chainId, params.owner.coreSigNonce, params.marginAccountId, params.amountInBase, params.amountOutBase, params.path, params.adapters, params.recipients, 0, // 0 for marketID
|
|
55
|
+
0)];
|
|
56
|
+
case 1:
|
|
57
|
+
_a = _b.sent(), data = _a.calldata, eip712Payload = _a.payload, eip712Signature = _a.signature;
|
|
58
|
+
return [4 /*yield*/, (0, signAndBroadcastTransaction_1.signAndBroadcastTransaction)(data, chainId, common_1.ContractType.PERIPHERY_PROXY, {
|
|
59
|
+
action: 'swap',
|
|
60
|
+
accountId: params.marginAccountId,
|
|
61
|
+
marketId: 0,
|
|
62
|
+
tradeSource: params.tradeSource,
|
|
63
|
+
matchOrder: {
|
|
64
|
+
userEIP712Signature: eip712Signature,
|
|
65
|
+
eip712Payload: eip712Payload,
|
|
66
|
+
},
|
|
67
|
+
})];
|
|
68
|
+
case 2:
|
|
69
|
+
result = _b.sent();
|
|
70
|
+
return [2 /*return*/, {
|
|
71
|
+
transactionHash: (result === null || result === void 0 ? void 0 : result.txHash) || null,
|
|
72
|
+
coreSigNonce: Number(result === null || result === void 0 ? void 0 : result.coreSigNonce) || null,
|
|
73
|
+
}];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}); };
|
|
77
|
+
exports.swap = swap;
|
|
78
|
+
//# sourceMappingURL=swap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swap.js","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAA+D;AAC/D,uCAAiD;AACjD,8EAA6E;AAC7E,mCAAsC;AAG/B,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,GAAG,GAAG,IAAA,0BAAiB,GAAE,CAAC;gBAC1B,OAAO,GAAG,IAAA,uBAAc,EAAC,GAAG,KAAK,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;gBAMzE,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,EACpB,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;AA9CW,QAAA,IAAI,QA8Cf","sourcesContent":["import { ContractType, getReyaNetwork } from '@reyaxyz/common';\nimport { getSdkEnvironment } from '../../config';\nimport { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';\nimport { encodeSwap } from './encode';\nimport { SwapParams, SwapResult } from './types';\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 env = getSdkEnvironment();\n const chainId = getReyaNetwork(env === 'production' ? 'production' : 'test');\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,\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"/","sources":["services/swaps/types.ts"],"names":[],"mappings":"","sourcesContent":["import { JsonRpcSigner, Signer } from 'ethers';\nimport { MarginAccountEntity, OwnerMetadataEntity } from '@reyaxyz/common';\n\nexport type SwapParams = {\n signer: Signer | JsonRpcSigner;\n owner: Pick<OwnerMetadataEntity, 'coreSigNonce'>;\n marginAccountId: MarginAccountEntity['id'];\n amountInBase: FindBestPathResult['amountInBase'];\n amountOutBase: FindBestPathResult['amountOutBase'];\n path: FindBestPathResult['path'];\n adapters: FindBestPathResult['adapters'];\n recipients: FindBestPathResult['recipients'];\n tradeSource?: 'reya' | 'rage' | 'other';\n};\n\nexport type SwapResult = {\n transactionHash: string | null;\n coreSigNonce: number | null;\n};\n\nexport type FindBestPathResult = {\n amountFrom: number;\n amountTo: number;\n amountInBase: number;\n amountOutBase: number;\n path: string[];\n adapters: string[];\n recipients: string[];\n};\n"]}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export * from './services/orders';
|
|
|
5
5
|
export * from './services/conditional-orders';
|
|
6
6
|
export * from './services/isolated-order';
|
|
7
7
|
export * from './services/token';
|
|
8
|
+
export * from './services/swaps';
|
|
8
9
|
export { configureSDK, getTransactionModuleClient } from './config';
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -3,5 +3,6 @@ export declare const encodeSingleDeposit: (token: string, amount: bigint, multiA
|
|
|
3
3
|
export declare const encodeSingleWithdraw: (token: string, amount: bigint, multiAction: MultiAction) => void;
|
|
4
4
|
export declare const encodeSingleTransferMargin: (toMarginAccountId: number, token: string, amount: bigint, multiAction: MultiAction) => void;
|
|
5
5
|
export declare const encodeSingleTrade: (counterpartyAccountIds: number[], orderBase: bigint, orderPriceLimit: bigint, marketId: number, exchangeId: number, multiAction: MultiAction) => void;
|
|
6
|
+
export declare const encodeSingleSwap: (amountIn: number, amountOut: number, path: string[], adapters: string[], recipients: string[], marketId: number, exchangeId: number, multiAction: MultiAction) => void;
|
|
6
7
|
export declare const encodeRouterCall: (multiAction: MultiAction, accountId: number, nativeCurrencyValue: bigint) => MethodParameters;
|
|
7
8
|
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encode.d.ts","sourceRoot":"/","sources":["services/encode.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAW,MAAM,iBAAiB,CAAC;AAMzE,eAAO,MAAM,mBAAmB,UACvB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IAEF,CAAC;AAEF,eAAO,MAAM,oBAAoB,UACxB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IAIF,CAAC;AAEF,eAAO,MAAM,0BAA0B,sBAClB,MAAM,SAClB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IASF,CAAC;AAEF,eAAO,MAAM,iBAAiB,2BACJ,MAAM,EAAE,aACrB,MAAM,mBACA,MAAM,YACb,MAAM,cACJ,MAAM,eACL,WAAW,KACvB,IAeF,CAAC;AAEF,eAAO,MAAM,gBAAgB,gBACd,WAAW,aACb,MAAM,uBACI,MAAM,KAC1B,gBAUF,CAAC"}
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"/","sources":["services/encode.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAW,MAAM,iBAAiB,CAAC;AAMzE,eAAO,MAAM,mBAAmB,UACvB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IAEF,CAAC;AAEF,eAAO,MAAM,oBAAoB,UACxB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IAIF,CAAC;AAEF,eAAO,MAAM,0BAA0B,sBAClB,MAAM,SAClB,MAAM,UACL,MAAM,eACD,WAAW,KACvB,IASF,CAAC;AAEF,eAAO,MAAM,iBAAiB,2BACJ,MAAM,EAAE,aACrB,MAAM,mBACA,MAAM,YACb,MAAM,cACJ,MAAM,eACL,WAAW,KACvB,IAeF,CAAC;AAEF,eAAO,MAAM,gBAAgB,aACjB,MAAM,aACL,MAAM,QACX,MAAM,EAAE,YACJ,MAAM,EAAE,cACN,MAAM,EAAE,YACV,MAAM,cACJ,MAAM,eACL,WAAW,KACvB,IAUF,CAAC;AAEF,eAAO,MAAM,gBAAgB,gBACd,WAAW,aACb,MAAM,uBACI,MAAM,KAC1B,gBAUF,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
import { MethodParametersAndEIP712Payload } from '../orders';
|
|
3
|
+
export declare const encodeSwap: (signer: Signer, chainId: number, coreSigNonce: number, accountId: number, amountIn: number, amountOut: number, path: string[], adapters: string[], recipients: string[], marketId: number, exchangeId: number) => Promise<MethodParametersAndEIP712Payload>;
|
|
4
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"/","sources":["services/swaps/encode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,EAAE,MAAM,QAAQ,CAAC;AACnD,OAAO,EAAE,gCAAgC,EAAE,MAAM,WAAW,CAAC;AAY7D,eAAO,MAAM,UAAU,WACb,MAAM,WACL,MAAM,gBACD,MAAM,aACT,MAAM,YACP,MAAM,aACL,MAAM,QACX,MAAM,EAAE,YACJ,MAAM,EAAE,cACN,MAAM,EAAE,YACV,MAAM,cACJ,MAAM,KACjB,QAAQ,gCAAgC,CAyC1C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["services/swaps/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"/","sources":["services/swaps/simulation.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AA2E7C,eAAO,MAAM,YAAY,eACX,MAAM,aACP,MAAM,WACR,MAAM,aACJ,MAAM,KAChB,QAAQ,kBAAkB,CA6B5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swap.d.ts","sourceRoot":"/","sources":["services/swaps/swap.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEjD,eAAO,MAAM,IAAI,WAAkB,UAAU,KAAG,QAAQ,UAAU,CA8CjE,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
|
+
import { MarginAccountEntity, OwnerMetadataEntity } from '@reyaxyz/common';
|
|
3
|
+
export type SwapParams = {
|
|
4
|
+
signer: Signer | JsonRpcSigner;
|
|
5
|
+
owner: Pick<OwnerMetadataEntity, 'coreSigNonce'>;
|
|
6
|
+
marginAccountId: MarginAccountEntity['id'];
|
|
7
|
+
amountInBase: FindBestPathResult['amountInBase'];
|
|
8
|
+
amountOutBase: FindBestPathResult['amountOutBase'];
|
|
9
|
+
path: FindBestPathResult['path'];
|
|
10
|
+
adapters: FindBestPathResult['adapters'];
|
|
11
|
+
recipients: FindBestPathResult['recipients'];
|
|
12
|
+
tradeSource?: 'reya' | 'rage' | 'other';
|
|
13
|
+
};
|
|
14
|
+
export type SwapResult = {
|
|
15
|
+
transactionHash: string | null;
|
|
16
|
+
coreSigNonce: number | null;
|
|
17
|
+
};
|
|
18
|
+
export type FindBestPathResult = {
|
|
19
|
+
amountFrom: number;
|
|
20
|
+
amountTo: number;
|
|
21
|
+
amountInBase: number;
|
|
22
|
+
amountOutBase: number;
|
|
23
|
+
path: string[];
|
|
24
|
+
adapters: string[];
|
|
25
|
+
recipients: string[];
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"/","sources":["services/swaps/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAE3E,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,GAAG,aAAa,CAAC;IAC/B,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,cAAc,CAAC,CAAC;IACjD,eAAe,EAAE,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAC3C,YAAY,EAAE,kBAAkB,CAAC,cAAc,CAAC,CAAC;IACjD,aAAa,EAAE,kBAAkB,CAAC,eAAe,CAAC,CAAC;IACnD,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACjC,QAAQ,EAAE,kBAAkB,CAAC,UAAU,CAAC,CAAC;IACzC,UAAU,EAAE,kBAAkB,CAAC,YAAY,CAAC,CAAC;IAC7C,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CACzC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.118.
|
|
3
|
+
"version": "0.118.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@reyaxyz/common": "0.224.
|
|
32
|
+
"@reyaxyz/common": "0.224.2",
|
|
33
33
|
"axios": "^1.6.2",
|
|
34
34
|
"bignumber.js": "^9.1.2",
|
|
35
35
|
"ethers": "6.9.0"
|
|
36
36
|
},
|
|
37
37
|
"packageManager": "pnpm@8.3.1",
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "67ff5af5ba00f220e99aa876b1c48b3f628a180a"
|
|
39
39
|
}
|
package/src/index.ts
CHANGED
package/src/services/encode.ts
CHANGED
|
@@ -64,6 +64,27 @@ export const encodeSingleTrade = (
|
|
|
64
64
|
);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
export const encodeSingleSwap = (
|
|
68
|
+
amountIn: number,
|
|
69
|
+
amountOut: number,
|
|
70
|
+
path: string[],
|
|
71
|
+
adapters: string[],
|
|
72
|
+
recipients: string[],
|
|
73
|
+
marketId: number,
|
|
74
|
+
exchangeId: number,
|
|
75
|
+
multiAction: MultiAction,
|
|
76
|
+
): void => {
|
|
77
|
+
// Trade memory trade = abi.decode(command.inputs, (Trade));
|
|
78
|
+
multiAction.newAction(
|
|
79
|
+
getCommand(
|
|
80
|
+
CommandType.HOOK,
|
|
81
|
+
[[amountIn, amountOut, path, adapters, recipients]],
|
|
82
|
+
marketId,
|
|
83
|
+
exchangeId,
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
|
|
67
88
|
export const encodeRouterCall = (
|
|
68
89
|
multiAction: MultiAction,
|
|
69
90
|
accountId: number,
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ethers, Interface, Signer } from 'ethers';
|
|
2
|
+
import { MethodParametersAndEIP712Payload } from '../orders';
|
|
3
|
+
import {
|
|
4
|
+
ContractType,
|
|
5
|
+
CORE_DEADLINE_IN_SECONDS,
|
|
6
|
+
getAddress,
|
|
7
|
+
getCurrentTimestampInSeconds,
|
|
8
|
+
MultiAction,
|
|
9
|
+
PeripheryAbi as abi,
|
|
10
|
+
signCoreCommands,
|
|
11
|
+
} from '@reyaxyz/common';
|
|
12
|
+
import { encodeSingleSwap } from '../encode';
|
|
13
|
+
|
|
14
|
+
export const encodeSwap = async (
|
|
15
|
+
signer: Signer,
|
|
16
|
+
chainId: number,
|
|
17
|
+
coreSigNonce: number,
|
|
18
|
+
accountId: number,
|
|
19
|
+
amountIn: number,
|
|
20
|
+
amountOut: number,
|
|
21
|
+
path: string[],
|
|
22
|
+
adapters: string[],
|
|
23
|
+
recipients: string[],
|
|
24
|
+
marketId: number,
|
|
25
|
+
exchangeId: number,
|
|
26
|
+
): Promise<MethodParametersAndEIP712Payload> => {
|
|
27
|
+
const multiAction = new MultiAction();
|
|
28
|
+
|
|
29
|
+
encodeSingleSwap(
|
|
30
|
+
amountIn,
|
|
31
|
+
amountOut,
|
|
32
|
+
path,
|
|
33
|
+
adapters,
|
|
34
|
+
recipients,
|
|
35
|
+
marketId,
|
|
36
|
+
exchangeId,
|
|
37
|
+
multiAction,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const { signature: eip712Signature, payload: eip712Payload } =
|
|
41
|
+
await signCoreCommands(
|
|
42
|
+
signer,
|
|
43
|
+
chainId,
|
|
44
|
+
getAddress(chainId, ContractType.PERIPHERY_PROXY),
|
|
45
|
+
accountId,
|
|
46
|
+
multiAction.commands,
|
|
47
|
+
coreSigNonce + 1,
|
|
48
|
+
getCurrentTimestampInSeconds() + CORE_DEADLINE_IN_SECONDS,
|
|
49
|
+
ethers.AbiCoder.defaultAbiCoder().encode([], []), // @todo check this
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const value = BigInt(0);
|
|
53
|
+
|
|
54
|
+
const functionSignature = 'execute';
|
|
55
|
+
const parameters = [
|
|
56
|
+
{ accountId, commands: multiAction.commands, sig: eip712Signature },
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
const INTERFACE = new Interface(abi);
|
|
60
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
61
|
+
return {
|
|
62
|
+
calldata: calldata,
|
|
63
|
+
value: BigInt(value).toString(10),
|
|
64
|
+
signature: eip712Signature,
|
|
65
|
+
payload: eip712Payload,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ContractType,
|
|
3
|
+
descale,
|
|
4
|
+
getAddress,
|
|
5
|
+
getReyaNetwork,
|
|
6
|
+
getTokenInfoByAddress,
|
|
7
|
+
scale,
|
|
8
|
+
reyaChainIdRPCMapper,
|
|
9
|
+
} from '@reyaxyz/common';
|
|
10
|
+
import { Contract, ethers } from 'ethers';
|
|
11
|
+
import { FindBestPathResult } from './types';
|
|
12
|
+
import { getSdkEnvironment } from '../../config';
|
|
13
|
+
const abi = [
|
|
14
|
+
{
|
|
15
|
+
inputs: [
|
|
16
|
+
{
|
|
17
|
+
internalType: 'uint256',
|
|
18
|
+
name: '_amountIn',
|
|
19
|
+
type: 'uint256',
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
internalType: 'address',
|
|
23
|
+
name: '_tokenIn',
|
|
24
|
+
type: 'address',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
internalType: 'address',
|
|
28
|
+
name: '_tokenOut',
|
|
29
|
+
type: 'address',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
internalType: 'address[]',
|
|
33
|
+
name: '_trustedTokens',
|
|
34
|
+
type: 'address[]',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
internalType: 'uint256',
|
|
38
|
+
name: '_maxSteps',
|
|
39
|
+
type: 'uint256',
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
name: 'findBestPath',
|
|
43
|
+
outputs: [
|
|
44
|
+
{
|
|
45
|
+
components: [
|
|
46
|
+
{
|
|
47
|
+
internalType: 'uint256[]',
|
|
48
|
+
name: 'amounts',
|
|
49
|
+
type: 'uint256[]',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
internalType: 'address[]',
|
|
53
|
+
name: 'adapters',
|
|
54
|
+
type: 'address[]',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
internalType: 'address[]',
|
|
58
|
+
name: 'path',
|
|
59
|
+
type: 'address[]',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
internalType: 'address[]',
|
|
63
|
+
name: 'recipients',
|
|
64
|
+
type: 'address[]',
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
internalType: 'uint256',
|
|
68
|
+
name: 'gasEstimate',
|
|
69
|
+
type: 'uint256',
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
internalType: 'struct FormattedOffer',
|
|
73
|
+
name: '',
|
|
74
|
+
type: 'tuple',
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
stateMutability: 'view',
|
|
78
|
+
type: 'function',
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
const env = getSdkEnvironment();
|
|
82
|
+
const chainId = getReyaNetwork(env === 'production' ? 'production' : 'test');
|
|
83
|
+
const provider = ethers.getDefaultProvider(
|
|
84
|
+
reyaChainIdRPCMapper[chainId] as string,
|
|
85
|
+
);
|
|
86
|
+
export const findBestPath = async (
|
|
87
|
+
amountFrom: number,
|
|
88
|
+
tokenFrom: string,
|
|
89
|
+
tokenTo: string,
|
|
90
|
+
maxSteps?: number,
|
|
91
|
+
): Promise<FindBestPathResult> => {
|
|
92
|
+
const coreAddress = getAddress(chainId, ContractType.CAMELOT_YAK_ROUTER);
|
|
93
|
+
const core = new Contract(coreAddress, abi, provider);
|
|
94
|
+
|
|
95
|
+
const tokenInfoFrom = getTokenInfoByAddress(tokenFrom);
|
|
96
|
+
const tokenInfoTo = getTokenInfoByAddress(tokenTo);
|
|
97
|
+
const amountFromScaled = scale(tokenInfoFrom.decimals)(amountFrom);
|
|
98
|
+
|
|
99
|
+
// amountFrom
|
|
100
|
+
// tokenFrom
|
|
101
|
+
// tokenTo
|
|
102
|
+
// trustedTokens
|
|
103
|
+
// maxSteps
|
|
104
|
+
const result = await core.findBestPath(
|
|
105
|
+
amountFromScaled,
|
|
106
|
+
tokenFrom,
|
|
107
|
+
tokenTo,
|
|
108
|
+
[],
|
|
109
|
+
maxSteps || 4,
|
|
110
|
+
);
|
|
111
|
+
return {
|
|
112
|
+
amountFrom: descale(tokenInfoFrom.decimals)(result.amounts[0]),
|
|
113
|
+
amountTo: descale(tokenInfoTo.decimals)(result.amounts[1]),
|
|
114
|
+
amountInBase: result.amounts[0],
|
|
115
|
+
amountOutBase: result.amounts[1],
|
|
116
|
+
adapters: Array.from(result[1]),
|
|
117
|
+
path: Array.from(result[2]),
|
|
118
|
+
recipients: Array.from(result[3]),
|
|
119
|
+
};
|
|
120
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ContractType, getReyaNetwork } from '@reyaxyz/common';
|
|
2
|
+
import { getSdkEnvironment } from '../../config';
|
|
3
|
+
import { signAndBroadcastTransaction } from '../signAndBroadcastTransaction';
|
|
4
|
+
import { encodeSwap } from './encode';
|
|
5
|
+
import { SwapParams, SwapResult } from './types';
|
|
6
|
+
|
|
7
|
+
export const swap = async (params: SwapParams): Promise<SwapResult> => {
|
|
8
|
+
if (params.amountInBase === 0) {
|
|
9
|
+
throw new Error('Swap amount cannot be 0');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const env = getSdkEnvironment();
|
|
13
|
+
const chainId = getReyaNetwork(env === 'production' ? 'production' : 'test');
|
|
14
|
+
|
|
15
|
+
const {
|
|
16
|
+
calldata: data,
|
|
17
|
+
payload: eip712Payload,
|
|
18
|
+
signature: eip712Signature,
|
|
19
|
+
} = await encodeSwap(
|
|
20
|
+
params.signer,
|
|
21
|
+
chainId,
|
|
22
|
+
params.owner.coreSigNonce,
|
|
23
|
+
params.marginAccountId,
|
|
24
|
+
params.amountInBase,
|
|
25
|
+
params.amountOutBase,
|
|
26
|
+
params.path,
|
|
27
|
+
params.adapters,
|
|
28
|
+
params.recipients,
|
|
29
|
+
0, // 0 for marketID
|
|
30
|
+
0, // 0 for exchangeID
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const result = await signAndBroadcastTransaction(
|
|
34
|
+
data,
|
|
35
|
+
chainId,
|
|
36
|
+
ContractType.PERIPHERY_PROXY,
|
|
37
|
+
{
|
|
38
|
+
action: 'swap',
|
|
39
|
+
accountId: params.marginAccountId,
|
|
40
|
+
marketId: 0,
|
|
41
|
+
tradeSource: params.tradeSource,
|
|
42
|
+
matchOrder: {
|
|
43
|
+
userEIP712Signature: eip712Signature,
|
|
44
|
+
eip712Payload,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
transactionHash: result?.txHash || null,
|
|
51
|
+
coreSigNonce: Number(result?.coreSigNonce) || null,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
|
+
import { MarginAccountEntity, OwnerMetadataEntity } from '@reyaxyz/common';
|
|
3
|
+
|
|
4
|
+
export type SwapParams = {
|
|
5
|
+
signer: Signer | JsonRpcSigner;
|
|
6
|
+
owner: Pick<OwnerMetadataEntity, 'coreSigNonce'>;
|
|
7
|
+
marginAccountId: MarginAccountEntity['id'];
|
|
8
|
+
amountInBase: FindBestPathResult['amountInBase'];
|
|
9
|
+
amountOutBase: FindBestPathResult['amountOutBase'];
|
|
10
|
+
path: FindBestPathResult['path'];
|
|
11
|
+
adapters: FindBestPathResult['adapters'];
|
|
12
|
+
recipients: FindBestPathResult['recipients'];
|
|
13
|
+
tradeSource?: 'reya' | 'rage' | 'other';
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type SwapResult = {
|
|
17
|
+
transactionHash: string | null;
|
|
18
|
+
coreSigNonce: number | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type FindBestPathResult = {
|
|
22
|
+
amountFrom: number;
|
|
23
|
+
amountTo: number;
|
|
24
|
+
amountInBase: number;
|
|
25
|
+
amountOutBase: number;
|
|
26
|
+
path: string[];
|
|
27
|
+
adapters: string[];
|
|
28
|
+
recipients: string[];
|
|
29
|
+
};
|