@reyaxyz/common 0.372.0 → 0.373.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-54.25%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-53.78%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-41.55%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-57.7%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-56.9%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-57.43%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-44.35%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-60.52%25-red.svg?style=flat) |
10
10
 
@@ -44,4 +44,5 @@ __exportStar(require("./executeTransaction"), exports);
44
44
  __exportStar(require("./permissions"), exports);
45
45
  __exportStar(require("./buildMulticallTx"), exports);
46
46
  __exportStar(require("./encodeStorkUpdateTx"), exports);
47
+ __exportStar(require("./operations"), exports);
47
48
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,wDAAuD;AAA9C,yGAAA,GAAG,OAAW;AACvB,kDAAsD;AAA7C,wGAAA,GAAG,OAAa;AACzB,sEAAmE;AAA1D,qHAAA,GAAG,OAAgB;AAC5B,4EAA2E;AAAlE,6HAAA,GAAG,OAAqB;AACjC,wDAA4D;AAAnD,8GAAA,GAAG,OAAgB;AAC5B,6EAAwF;AAA/E,mIAAA,GAAG,OAA8B;AAC1C,gEAAoE;AAA3D,sHAAA,GAAG,OAAoB;AAChC,sEAAqE;AAA5D,uHAAA,GAAG,OAAkB;AAC9B,4CAAgD;AAAvC,kGAAA,GAAG,OAAU;AACtB,sDAAoC;AACpC,mDAAiC;AACjC,yCAAuB;AACvB,0CAAwB;AACxB,8CAA4B;AAC5B,2CAAyB;AACzB,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,wDAAsC","sourcesContent":["export * from './action';\nexport { abi as CoreAbi } from './abis/CoreProxy.json';\nexport { abi as ErrorsAbi } from './abis/Errors.json';\nexport { abi as MulticallAbi } from './abis/CustomMulticall3.json';\nexport { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';\nexport { abi as PeripheryAbi } from './abis/Periphery.json';\nexport { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';\nexport { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';\nexport { abi as PassivePerpAbi } from './abis/PassivePerpProxy.json';\nexport { abi as OFTAbi } from './abis/OFT.json';\nexport * from './contractAddresses';\nexport * from './routerCommands';\nexport * from './sign';\nexport * from './trade';\nexport * from './txHelpers';\nexport * from './consts';\nexport * from './executeTransaction';\nexport * from './permissions';\nexport * from './buildMulticallTx';\nexport * from './encodeStorkUpdateTx';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,wDAAuD;AAA9C,yGAAA,GAAG,OAAW;AACvB,kDAAsD;AAA7C,wGAAA,GAAG,OAAa;AACzB,sEAAmE;AAA1D,qHAAA,GAAG,OAAgB;AAC5B,4EAA2E;AAAlE,6HAAA,GAAG,OAAqB;AACjC,wDAA4D;AAAnD,8GAAA,GAAG,OAAgB;AAC5B,6EAAwF;AAA/E,mIAAA,GAAG,OAA8B;AAC1C,gEAAoE;AAA3D,sHAAA,GAAG,OAAoB;AAChC,sEAAqE;AAA5D,uHAAA,GAAG,OAAkB;AAC9B,4CAAgD;AAAvC,kGAAA,GAAG,OAAU;AACtB,sDAAoC;AACpC,mDAAiC;AACjC,yCAAuB;AACvB,0CAAwB;AACxB,8CAA4B;AAC5B,2CAAyB;AACzB,uDAAqC;AACrC,gDAA8B;AAC9B,qDAAmC;AACnC,wDAAsC;AACtC,+CAA6B","sourcesContent":["export * from './action';\nexport { abi as CoreAbi } from './abis/CoreProxy.json';\nexport { abi as ErrorsAbi } from './abis/Errors.json';\nexport { abi as MulticallAbi } from './abis/CustomMulticall3.json';\nexport { abi as OracleAdaptersAbi } from './abis/OracleAdaptersProxy.json';\nexport { abi as PeripheryAbi } from './abis/Periphery.json';\nexport { abi as Socket_VaultWithPayloadAbi } from './abis/socket/VaultWithPayload.json';\nexport { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';\nexport { abi as PassivePerpAbi } from './abis/PassivePerpProxy.json';\nexport { abi as OFTAbi } from './abis/OFT.json';\nexport * from './contractAddresses';\nexport * from './routerCommands';\nexport * from './sign';\nexport * from './trade';\nexport * from './txHelpers';\nexport * from './consts';\nexport * from './executeTransaction';\nexport * from './permissions';\nexport * from './buildMulticallTx';\nexport * from './encodeStorkUpdateTx';\nexport * from './operations';\n"]}
@@ -0,0 +1,254 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
34
+ }
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.buildTransactionOperation = exports.InvalidTransactionOperation = exports.TRANSACTION_OPERATIONS = void 0;
39
+ var ethers_1 = require("ethers");
40
+ var CoreProxy_json_1 = require("./abis/CoreProxy.json");
41
+ var Periphery_json_1 = require("./abis/Periphery.json");
42
+ var OrdersGateway_json_1 = require("./abis/OrdersGateway.json");
43
+ var contractAddresses_1 = require("./contractAddresses");
44
+ var routerCommands_1 = require("./routerCommands");
45
+ exports.TRANSACTION_OPERATIONS = [
46
+ 'create-account',
47
+ 'transfer',
48
+ 'withdraw',
49
+ 'withdraw-lz',
50
+ 'enable-api-wallet',
51
+ 'disable-api-wallet',
52
+ 'stake',
53
+ 'unstake',
54
+ 'deposit-pool',
55
+ 'withdraw-pool',
56
+ ];
57
+ var InvalidTransactionOperation = /** @class */ (function (_super) {
58
+ __extends(InvalidTransactionOperation, _super);
59
+ function InvalidTransactionOperation(message) {
60
+ var _this = _super.call(this, message) || this;
61
+ // common targets ES5. Restore Error subclass identity for consumers of
62
+ // the compiled package, so invalid HTTP input remains a 400, not a 500.
63
+ Object.setPrototypeOf(_this, InvalidTransactionOperation.prototype);
64
+ _this.name = 'InvalidTransactionOperation';
65
+ return _this;
66
+ }
67
+ return InvalidTransactionOperation;
68
+ }(Error));
69
+ exports.InvalidTransactionOperation = InvalidTransactionOperation;
70
+ var invalid = function () {
71
+ throw new InvalidTransactionOperation('Invalid transaction operation parameters');
72
+ };
73
+ // Reject extra fields as well as missing ones. Neither raw calldata nor client
74
+ // metadata may smuggle an alternate target, command batch, or nonce destination.
75
+ function fields(value, keys) {
76
+ if (!value || typeof value !== 'object' || Array.isArray(value))
77
+ invalid();
78
+ var object = value;
79
+ if (Object.keys(object).length !== keys.length ||
80
+ !keys.every(function (key) { return Object.prototype.hasOwnProperty.call(object, key); }))
81
+ invalid();
82
+ return object;
83
+ }
84
+ function uint(value, bits) {
85
+ if (bits === void 0) { bits = 256; }
86
+ if (typeof value !== 'string' ||
87
+ !/^(0|[1-9][0-9]{0,77})$/.test(value) ||
88
+ BigInt(value) >= BigInt(1) << BigInt(bits))
89
+ invalid();
90
+ return value;
91
+ }
92
+ function address(value) {
93
+ if (typeof value !== 'string' || !(0, ethers_1.isHexString)(value, 20) || !(0, ethers_1.isAddress)(value))
94
+ invalid();
95
+ return value;
96
+ }
97
+ function signature(value) {
98
+ var sig = fields(value, ['v', 'r', 's', 'deadline']);
99
+ if ((sig.v !== 27 && sig.v !== 28) ||
100
+ typeof sig.r !== 'string' ||
101
+ !(0, ethers_1.isHexString)(sig.r, 32) ||
102
+ typeof sig.s !== 'string' ||
103
+ !(0, ethers_1.isHexString)(sig.s, 32))
104
+ invalid();
105
+ return {
106
+ v: sig.v,
107
+ r: sig.r,
108
+ s: sig.s,
109
+ deadline: uint(sig.deadline),
110
+ };
111
+ }
112
+ var core = new ethers_1.Interface(CoreProxy_json_1.abi);
113
+ var periphery = new ethers_1.Interface(Periphery_json_1.abi);
114
+ var gateway = new ethers_1.Interface(OrdersGateway_json_1.abi);
115
+ /** Validates an untrusted HTTP body and constructs only the named operation. */
116
+ function buildTransactionOperation(operation, body, reyaChainId) {
117
+ var params = fields(body, ['params']).params;
118
+ var contract = contractAddresses_1.ContractType.PERIPHERY_PROXY;
119
+ var callData;
120
+ var metadata = { destinationType: 'account' };
121
+ switch (operation) {
122
+ case 'create-account': {
123
+ var p = fields(params, ['owner']);
124
+ contract = contractAddresses_1.ContractType.CORE_PROXY;
125
+ callData = core.encodeFunctionData('createAccount', [address(p.owner)]);
126
+ metadata.action = 'createAccount';
127
+ // Creating an account for an owner is permissionless on Core. Profile
128
+ // names require the authenticated account-edit endpoint (RET-200).
129
+ break;
130
+ }
131
+ case 'enable-api-wallet':
132
+ case 'disable-api-wallet': {
133
+ var p = fields(params, ['owner', 'apiWallet', 'sig']);
134
+ contract = contractAddresses_1.ContractType.ORDERS_GATEWAY_PROXY;
135
+ callData = gateway.encodeFunctionData('managePermissionBySig', [
136
+ address(p.owner),
137
+ address(p.apiWallet),
138
+ operation === 'enable-api-wallet',
139
+ signature(p.sig),
140
+ ]);
141
+ metadata.action = operation;
142
+ break;
143
+ }
144
+ case 'transfer': {
145
+ var p = fields(params, [
146
+ 'accountId',
147
+ 'toAccountId',
148
+ 'token',
149
+ 'tokenAmount',
150
+ 'sig',
151
+ ]);
152
+ callData = periphery.encodeFunctionData('transferFromMAToMA', [
153
+ {
154
+ accountId: uint(p.accountId, 128),
155
+ transfer: {
156
+ destAccountId: uint(p.toAccountId, 128),
157
+ collateral: address(p.token),
158
+ collateralAmount: uint(p.tokenAmount),
159
+ },
160
+ sig: signature(p.sig),
161
+ },
162
+ ]);
163
+ break;
164
+ }
165
+ case 'withdraw':
166
+ case 'withdraw-lz': {
167
+ var p = fields(params, __spreadArray([
168
+ 'accountId',
169
+ 'token',
170
+ 'tokenAmount',
171
+ 'sig',
172
+ 'receiver'
173
+ ], (operation === 'withdraw'
174
+ ? ['chainId', 'socketMsgGasLimit']
175
+ : ['dstEid']), true));
176
+ callData = periphery.encodeFunctionData(operation === 'withdraw' ? 'withdrawMA' : 'withdrawMALZ', [
177
+ __assign({ accountId: uint(p.accountId, 128), token: address(p.token), tokenAmount: uint(p.tokenAmount), sig: signature(p.sig), receiver: address(p.receiver) }, (operation === 'withdraw'
178
+ ? {
179
+ chainId: uint(p.chainId),
180
+ socketMsgGasLimit: uint(p.socketMsgGasLimit),
181
+ }
182
+ : { dstEid: uint(p.dstEid, 32) })),
183
+ ]);
184
+ break;
185
+ }
186
+ case 'stake':
187
+ case 'unstake': {
188
+ var p = fields(params, [
189
+ 'accountId',
190
+ 'poolId',
191
+ 'amount',
192
+ 'minimumOut',
193
+ 'sig',
194
+ ]);
195
+ var inputs = ethers_1.AbiCoder.defaultAbiCoder().encode(['uint128', 'uint256', 'uint256'], [uint(p.poolId, 128), uint(p.amount), uint(p.minimumOut)]);
196
+ var command = (0, routerCommands_1.getCommand)(routerCommands_1.CommandType.MANAGE_POOL_STAKE, [operation === 'stake' ? 0 : 1, inputs], 0, 0).command;
197
+ callData = periphery.encodeFunctionData('execute', [
198
+ {
199
+ accountId: uint(p.accountId, 128),
200
+ commands: [command],
201
+ sig: signature(p.sig),
202
+ },
203
+ ]);
204
+ break;
205
+ }
206
+ case 'deposit-pool': {
207
+ var p = fields(params, ['accountId', 'poolId', 'sharesAmount', 'sig']);
208
+ callData = periphery.encodeFunctionData('depositLiquidityToAccount', [
209
+ {
210
+ accountId: uint(p.accountId, 128),
211
+ poolId: uint(p.poolId, 128),
212
+ sharesAmount: uint(p.sharesAmount),
213
+ sig: signature(p.sig),
214
+ },
215
+ ]);
216
+ metadata.destinationType = 'pool';
217
+ break;
218
+ }
219
+ case 'withdraw-pool': {
220
+ var p = fields(params, [
221
+ 'owner',
222
+ 'poolId',
223
+ 'sharesAmount',
224
+ 'minOut',
225
+ 'sig',
226
+ 'socketMsgGasLimit',
227
+ 'chainId',
228
+ 'receiver',
229
+ ]);
230
+ callData = periphery.encodeFunctionData('withdrawPassivePool', [
231
+ {
232
+ owner: address(p.owner),
233
+ poolId: uint(p.poolId, 128),
234
+ sharesAmount: uint(p.sharesAmount),
235
+ minOut: uint(p.minOut),
236
+ sig: signature(p.sig),
237
+ socketMsgGasLimit: uint(p.socketMsgGasLimit),
238
+ chainId: uint(p.chainId),
239
+ receiver: address(p.receiver),
240
+ },
241
+ ]);
242
+ metadata.destinationType = 'pool';
243
+ break;
244
+ }
245
+ default:
246
+ return invalid();
247
+ }
248
+ return {
249
+ call: { target: (0, contractAddresses_1.getAddress)(reyaChainId, contract), callData: callData },
250
+ metadata: metadata,
251
+ };
252
+ }
253
+ exports.buildTransactionOperation = buildTransactionOperation;
254
+ //# sourceMappingURL=operations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operations.js","sourceRoot":"/","sources":["transactions/operations.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAqE;AACrE,wDAAuD;AACvD,wDAA4D;AAC5D,gEAAoE;AACpE,yDAA+D;AAC/D,mDAA2D;AAsE9C,QAAA,sBAAsB,GAAoC;IACrE,gBAAgB;IAChB,UAAU;IACV,UAAU;IACV,aAAa;IACb,mBAAmB;IACnB,oBAAoB;IACpB,OAAO;IACP,SAAS;IACT,cAAc;IACd,eAAe;CAChB,CAAC;AAEF;IAAiD,+CAAK;IACpD,qCAAY,OAAe;QACzB,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,uEAAuE;QACvE,wEAAwE;QACxE,MAAM,CAAC,cAAc,CAAC,KAAI,EAAE,2BAA2B,CAAC,SAAS,CAAC,CAAC;QACnE,KAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;;IAC5C,CAAC;IACH,kCAAC;AAAD,CAAC,AARD,CAAiD,KAAK,GAQrD;AARY,kEAA2B;AAUxC,IAAM,OAAO,GAAG;IACd,MAAM,IAAI,2BAA2B,CACnC,0CAA0C,CAC3C,CAAC;AACJ,CAAC,CAAC;AAEF,+EAA+E;AAC/E,iFAAiF;AACjF,SAAS,MAAM,CAAC,KAAc,EAAE,IAAc;IAC5C,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAC3E,IAAM,MAAM,GAAG,KAAgC,CAAC;IAChD,IACE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,KAAK;gBACL,mBAAmB;gBACnB,SAAS;gBACT,UAAU;aACX,CAAC,CAAC;YACH,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,qBAAqB,EAAE;gBAC7D;oBACE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC;oBACvB,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC;oBAC3B,YAAY,EAAE,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC;oBAClC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;oBACtB,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC;oBACrB,iBAAiB,EAAE,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC;oBAC5C,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC;oBACxB,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC;iBAC9B;aACF,CAAC,CAAC;YACH,QAAQ,CAAC,eAAe,GAAG,MAAM,CAAC;YAClC,MAAM;QACR,CAAC;QACD;YACE,OAAO,OAAO,EAAE,CAAC;IACrB,CAAC;IAED,OAAO;QACL,IAAI,EAAE,EAAE,MAAM,EAAE,IAAA,8BAAU,EAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,QAAQ,UAAA,EAAE;QAC7D,QAAQ,UAAA;KACT,CAAC;AACJ,CAAC;AAjKD,8DAiKC","sourcesContent":["import { AbiCoder, Interface, isAddress, isHexString } from 'ethers';\nimport { abi as CoreAbi } from './abis/CoreProxy.json';\nimport { abi as PeripheryAbi } from './abis/Periphery.json';\nimport { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';\nimport { ContractType, getAddress } from './contractAddresses';\nimport { CommandType, getCommand } from './routerCommands';\nimport type { TransactionExecutionMetadata } from '../types';\nimport type { Call } from './buildMulticallTx';\n\n/** Integers travel as decimal strings, including IDs, amounts and deadlines. */\nexport type TransactionOperationSignature = {\n v: 27 | 28;\n r: string;\n s: string;\n deadline: string;\n};\n\ntype SignedAccountOperation = {\n accountId: string;\n sig: TransactionOperationSignature;\n};\ntype WithdrawOperation = SignedAccountOperation & {\n token: string;\n tokenAmount: string;\n receiver: string;\n};\ntype ApiWalletOperation = {\n owner: string;\n apiWallet: string;\n sig: TransactionOperationSignature;\n};\ntype StakeOperation = SignedAccountOperation & {\n poolId: string;\n amount: string;\n minimumOut: string;\n};\n\nexport type TransactionOperationParams = {\n 'create-account': { owner: string };\n transfer: SignedAccountOperation & {\n toAccountId: string;\n token: string;\n tokenAmount: string;\n };\n withdraw: WithdrawOperation & {\n chainId: string;\n socketMsgGasLimit: string;\n };\n 'withdraw-lz': WithdrawOperation & { dstEid: string };\n 'enable-api-wallet': ApiWalletOperation;\n 'disable-api-wallet': ApiWalletOperation;\n stake: StakeOperation;\n unstake: StakeOperation;\n /** Deposit existing pool shares into a margin account; no source-chain relay. */\n 'deposit-pool': SignedAccountOperation & {\n poolId: string;\n sharesAmount: string;\n };\n 'withdraw-pool': {\n owner: string;\n poolId: string;\n sharesAmount: string;\n minOut: string;\n sig: TransactionOperationSignature;\n socketMsgGasLimit: string;\n chainId: string;\n receiver: string;\n };\n};\n\nexport type TransactionOperation = keyof TransactionOperationParams;\nexport type TransactionOperationRequest<K extends TransactionOperation> = {\n params: TransactionOperationParams[K];\n};\n\nexport const TRANSACTION_OPERATIONS: readonly TransactionOperation[] = [\n 'create-account',\n 'transfer',\n 'withdraw',\n 'withdraw-lz',\n 'enable-api-wallet',\n 'disable-api-wallet',\n 'stake',\n 'unstake',\n 'deposit-pool',\n 'withdraw-pool',\n];\n\nexport class InvalidTransactionOperation extends Error {\n constructor(message: string) {\n super(message);\n // common targets ES5. Restore Error subclass identity for consumers of\n // the compiled package, so invalid HTTP input remains a 400, not a 500.\n Object.setPrototypeOf(this, InvalidTransactionOperation.prototype);\n this.name = 'InvalidTransactionOperation';\n }\n}\n\nconst invalid = (): never => {\n throw new InvalidTransactionOperation(\n 'Invalid transaction operation parameters',\n );\n};\n\n// Reject extra fields as well as missing ones. Neither raw calldata nor client\n// metadata may smuggle an alternate target, command batch, or nonce destination.\nfunction fields(value: unknown, keys: string[]): Record<string, unknown> {\n if (!value || typeof value !== 'object' || Array.isArray(value)) invalid();\n const object = value as Record<string, unknown>;\n if (\n Object.keys(object).length !== keys.length ||\n !keys.every((key) => Object.prototype.hasOwnProperty.call(object, key))\n )\n invalid();\n return object;\n}\n\nfunction uint(value: unknown, bits = 256): string {\n if (\n typeof value !== 'string' ||\n !/^(0|[1-9][0-9]{0,77})$/.test(value) ||\n BigInt(value) >= BigInt(1) << BigInt(bits)\n )\n invalid();\n return value as string;\n}\n\nfunction address(value: unknown): string {\n if (typeof value !== 'string' || !isHexString(value, 20) || !isAddress(value))\n invalid();\n return value as string;\n}\n\nfunction signature(value: unknown): TransactionOperationSignature {\n const sig = fields(value, ['v', 'r', 's', 'deadline']);\n if (\n (sig.v !== 27 && sig.v !== 28) ||\n typeof sig.r !== 'string' ||\n !isHexString(sig.r, 32) ||\n typeof sig.s !== 'string' ||\n !isHexString(sig.s, 32)\n )\n invalid();\n return {\n v: sig.v as 27 | 28,\n r: sig.r as string,\n s: sig.s as string,\n deadline: uint(sig.deadline),\n };\n}\n\nconst core = new Interface(CoreAbi);\nconst periphery = new Interface(PeripheryAbi);\nconst gateway = new Interface(OrdersGatewayAbi);\n\n/** Validates an untrusted HTTP body and constructs only the named operation. */\nexport function buildTransactionOperation(\n operation: TransactionOperation,\n body: unknown,\n reyaChainId: number,\n): { call: Call; metadata: TransactionExecutionMetadata } {\n const { params } = fields(body, ['params']);\n let contract = ContractType.PERIPHERY_PROXY;\n let callData: string;\n const metadata: TransactionExecutionMetadata = { destinationType: 'account' };\n\n switch (operation) {\n case 'create-account': {\n const p = fields(params, ['owner']);\n contract = ContractType.CORE_PROXY;\n callData = core.encodeFunctionData('createAccount', [address(p.owner)]);\n metadata.action = 'createAccount';\n // Creating an account for an owner is permissionless on Core. Profile\n // names require the authenticated account-edit endpoint (RET-200).\n break;\n }\n case 'enable-api-wallet':\n case 'disable-api-wallet': {\n const p = fields(params, ['owner', 'apiWallet', 'sig']);\n contract = ContractType.ORDERS_GATEWAY_PROXY;\n callData = gateway.encodeFunctionData('managePermissionBySig', [\n address(p.owner),\n address(p.apiWallet),\n operation === 'enable-api-wallet',\n signature(p.sig),\n ]);\n metadata.action = operation;\n break;\n }\n case 'transfer': {\n const p = fields(params, [\n 'accountId',\n 'toAccountId',\n 'token',\n 'tokenAmount',\n 'sig',\n ]);\n callData = periphery.encodeFunctionData('transferFromMAToMA', [\n {\n accountId: uint(p.accountId, 128),\n transfer: {\n destAccountId: uint(p.toAccountId, 128),\n collateral: address(p.token),\n collateralAmount: uint(p.tokenAmount),\n },\n sig: signature(p.sig),\n },\n ]);\n break;\n }\n case 'withdraw':\n case 'withdraw-lz': {\n const p = fields(params, [\n 'accountId',\n 'token',\n 'tokenAmount',\n 'sig',\n 'receiver',\n ...(operation === 'withdraw'\n ? ['chainId', 'socketMsgGasLimit']\n : ['dstEid']),\n ]);\n callData = periphery.encodeFunctionData(\n operation === 'withdraw' ? 'withdrawMA' : 'withdrawMALZ',\n [\n {\n accountId: uint(p.accountId, 128),\n token: address(p.token),\n tokenAmount: uint(p.tokenAmount),\n sig: signature(p.sig),\n receiver: address(p.receiver),\n ...(operation === 'withdraw'\n ? {\n chainId: uint(p.chainId),\n socketMsgGasLimit: uint(p.socketMsgGasLimit),\n }\n : { dstEid: uint(p.dstEid, 32) }),\n },\n ],\n );\n break;\n }\n case 'stake':\n case 'unstake': {\n const p = fields(params, [\n 'accountId',\n 'poolId',\n 'amount',\n 'minimumOut',\n 'sig',\n ]);\n const inputs = AbiCoder.defaultAbiCoder().encode(\n ['uint128', 'uint256', 'uint256'],\n [uint(p.poolId, 128), uint(p.amount), uint(p.minimumOut)],\n );\n const { command } = getCommand(\n CommandType.MANAGE_POOL_STAKE,\n [operation === 'stake' ? 0 : 1, inputs],\n 0,\n 0,\n );\n callData = periphery.encodeFunctionData('execute', [\n {\n accountId: uint(p.accountId, 128),\n commands: [command],\n sig: signature(p.sig),\n },\n ]);\n break;\n }\n case 'deposit-pool': {\n const p = fields(params, ['accountId', 'poolId', 'sharesAmount', 'sig']);\n callData = periphery.encodeFunctionData('depositLiquidityToAccount', [\n {\n accountId: uint(p.accountId, 128),\n poolId: uint(p.poolId, 128),\n sharesAmount: uint(p.sharesAmount),\n sig: signature(p.sig),\n },\n ]);\n metadata.destinationType = 'pool';\n break;\n }\n case 'withdraw-pool': {\n const p = fields(params, [\n 'owner',\n 'poolId',\n 'sharesAmount',\n 'minOut',\n 'sig',\n 'socketMsgGasLimit',\n 'chainId',\n 'receiver',\n ]);\n callData = periphery.encodeFunctionData('withdrawPassivePool', [\n {\n owner: address(p.owner),\n poolId: uint(p.poolId, 128),\n sharesAmount: uint(p.sharesAmount),\n minOut: uint(p.minOut),\n sig: signature(p.sig),\n socketMsgGasLimit: uint(p.socketMsgGasLimit),\n chainId: uint(p.chainId),\n receiver: address(p.receiver),\n },\n ]);\n metadata.destinationType = 'pool';\n break;\n }\n default:\n return invalid();\n }\n\n return {\n call: { target: getAddress(reyaChainId, contract), callData },\n metadata,\n };\n}\n"]}
@@ -18,4 +18,5 @@ export * from './executeTransaction';
18
18
  export * from './permissions';
19
19
  export * from './buildMulticallTx';
20
20
  export * from './encodeStorkUpdateTx';
21
+ export * from './operations';
21
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["transactions/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,GAAG,IAAI,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,GAAG,IAAI,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,GAAG,IAA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AE,GAAG,IAAI,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAChD,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,cAAc,CAAC"}
@@ -0,0 +1,79 @@
1
+ import type { TransactionExecutionMetadata } from '../types';
2
+ import type { Call } from './buildMulticallTx';
3
+ /** Integers travel as decimal strings, including IDs, amounts and deadlines. */
4
+ export type TransactionOperationSignature = {
5
+ v: 27 | 28;
6
+ r: string;
7
+ s: string;
8
+ deadline: string;
9
+ };
10
+ type SignedAccountOperation = {
11
+ accountId: string;
12
+ sig: TransactionOperationSignature;
13
+ };
14
+ type WithdrawOperation = SignedAccountOperation & {
15
+ token: string;
16
+ tokenAmount: string;
17
+ receiver: string;
18
+ };
19
+ type ApiWalletOperation = {
20
+ owner: string;
21
+ apiWallet: string;
22
+ sig: TransactionOperationSignature;
23
+ };
24
+ type StakeOperation = SignedAccountOperation & {
25
+ poolId: string;
26
+ amount: string;
27
+ minimumOut: string;
28
+ };
29
+ export type TransactionOperationParams = {
30
+ 'create-account': {
31
+ owner: string;
32
+ };
33
+ transfer: SignedAccountOperation & {
34
+ toAccountId: string;
35
+ token: string;
36
+ tokenAmount: string;
37
+ };
38
+ withdraw: WithdrawOperation & {
39
+ chainId: string;
40
+ socketMsgGasLimit: string;
41
+ };
42
+ 'withdraw-lz': WithdrawOperation & {
43
+ dstEid: string;
44
+ };
45
+ 'enable-api-wallet': ApiWalletOperation;
46
+ 'disable-api-wallet': ApiWalletOperation;
47
+ stake: StakeOperation;
48
+ unstake: StakeOperation;
49
+ /** Deposit existing pool shares into a margin account; no source-chain relay. */
50
+ 'deposit-pool': SignedAccountOperation & {
51
+ poolId: string;
52
+ sharesAmount: string;
53
+ };
54
+ 'withdraw-pool': {
55
+ owner: string;
56
+ poolId: string;
57
+ sharesAmount: string;
58
+ minOut: string;
59
+ sig: TransactionOperationSignature;
60
+ socketMsgGasLimit: string;
61
+ chainId: string;
62
+ receiver: string;
63
+ };
64
+ };
65
+ export type TransactionOperation = keyof TransactionOperationParams;
66
+ export type TransactionOperationRequest<K extends TransactionOperation> = {
67
+ params: TransactionOperationParams[K];
68
+ };
69
+ export declare const TRANSACTION_OPERATIONS: readonly TransactionOperation[];
70
+ export declare class InvalidTransactionOperation extends Error {
71
+ constructor(message: string);
72
+ }
73
+ /** Validates an untrusted HTTP body and constructs only the named operation. */
74
+ export declare function buildTransactionOperation(operation: TransactionOperation, body: unknown, reyaChainId: number): {
75
+ call: Call;
76
+ metadata: TransactionExecutionMetadata;
77
+ };
78
+ export {};
79
+ //# sourceMappingURL=operations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operations.d.ts","sourceRoot":"/","sources":["transactions/operations.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,UAAU,CAAC;AAC7D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE/C,gFAAgF;AAChF,MAAM,MAAM,6BAA6B,GAAG;IAC1C,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC;IACX,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,KAAK,sBAAsB,GAAG;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,6BAA6B,CAAC;CACpC,CAAC;AACF,KAAK,iBAAiB,GAAG,sBAAsB,GAAG;IAChD,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AACF,KAA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G,MAAM,0BAA0B,CAAC;AACpE,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,oBAAoB,IAAI;IACxE,MAAM,EAAE,0BAA0B,CAAC,CAAC,CAAC,CAAC;CACvC,CAAC;AAEF,eAAO,MAAM,sBAAsB,EAAE,SAAS,oBAAoB,EAWjE,CAAC;AAEF,qBAAa,2BAA4B,SAAQ,KAAK;gBACxC,OAAO,EAAE,MAAM;CAO5B;AA2DD,gFAAgF;AAChF,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,oBAAoB,EAC/B,IAAI,EAAE,OAAO,EACb,WAAW,EAAE,MAAM,GAClB;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,QAAQ,EAAE,4BAA4B,CAAA;CAAE,CA6JxD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/common",
3
- "version": "0.372.0",
3
+ "version": "0.373.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -41,5 +41,5 @@
41
41
  "test:coverage": "npx jest test --coverage",
42
42
  "generate:coverage-badges": "npx istanbul-badges-readme --silent"
43
43
  },
44
- "gitHead": "cd2b35cf3089435fee2bafdeab9ae6d8e42ca506"
44
+ "gitHead": "3ba59c57f6493ac1dce9f8ef6f8ce2f1d4d3f399"
45
45
  }
@@ -18,3 +18,4 @@ export * from './executeTransaction';
18
18
  export * from './permissions';
19
19
  export * from './buildMulticallTx';
20
20
  export * from './encodeStorkUpdateTx';
21
+ export * from './operations';
@@ -0,0 +1,318 @@
1
+ import { AbiCoder, Interface, isAddress, isHexString } from 'ethers';
2
+ import { abi as CoreAbi } from './abis/CoreProxy.json';
3
+ import { abi as PeripheryAbi } from './abis/Periphery.json';
4
+ import { abi as OrdersGatewayAbi } from './abis/OrdersGateway.json';
5
+ import { ContractType, getAddress } from './contractAddresses';
6
+ import { CommandType, getCommand } from './routerCommands';
7
+ import type { TransactionExecutionMetadata } from '../types';
8
+ import type { Call } from './buildMulticallTx';
9
+
10
+ /** Integers travel as decimal strings, including IDs, amounts and deadlines. */
11
+ export type TransactionOperationSignature = {
12
+ v: 27 | 28;
13
+ r: string;
14
+ s: string;
15
+ deadline: string;
16
+ };
17
+
18
+ type SignedAccountOperation = {
19
+ accountId: string;
20
+ sig: TransactionOperationSignature;
21
+ };
22
+ type WithdrawOperation = SignedAccountOperation & {
23
+ token: string;
24
+ tokenAmount: string;
25
+ receiver: string;
26
+ };
27
+ type ApiWalletOperation = {
28
+ owner: string;
29
+ apiWallet: string;
30
+ sig: TransactionOperationSignature;
31
+ };
32
+ type StakeOperation = SignedAccountOperation & {
33
+ poolId: string;
34
+ amount: string;
35
+ minimumOut: string;
36
+ };
37
+
38
+ export type TransactionOperationParams = {
39
+ 'create-account': { owner: string };
40
+ transfer: SignedAccountOperation & {
41
+ toAccountId: string;
42
+ token: string;
43
+ tokenAmount: string;
44
+ };
45
+ withdraw: WithdrawOperation & {
46
+ chainId: string;
47
+ socketMsgGasLimit: string;
48
+ };
49
+ 'withdraw-lz': WithdrawOperation & { dstEid: string };
50
+ 'enable-api-wallet': ApiWalletOperation;
51
+ 'disable-api-wallet': ApiWalletOperation;
52
+ stake: StakeOperation;
53
+ unstake: StakeOperation;
54
+ /** Deposit existing pool shares into a margin account; no source-chain relay. */
55
+ 'deposit-pool': SignedAccountOperation & {
56
+ poolId: string;
57
+ sharesAmount: string;
58
+ };
59
+ 'withdraw-pool': {
60
+ owner: string;
61
+ poolId: string;
62
+ sharesAmount: string;
63
+ minOut: string;
64
+ sig: TransactionOperationSignature;
65
+ socketMsgGasLimit: string;
66
+ chainId: string;
67
+ receiver: string;
68
+ };
69
+ };
70
+
71
+ export type TransactionOperation = keyof TransactionOperationParams;
72
+ export type TransactionOperationRequest<K extends TransactionOperation> = {
73
+ params: TransactionOperationParams[K];
74
+ };
75
+
76
+ export const TRANSACTION_OPERATIONS: readonly TransactionOperation[] = [
77
+ 'create-account',
78
+ 'transfer',
79
+ 'withdraw',
80
+ 'withdraw-lz',
81
+ 'enable-api-wallet',
82
+ 'disable-api-wallet',
83
+ 'stake',
84
+ 'unstake',
85
+ 'deposit-pool',
86
+ 'withdraw-pool',
87
+ ];
88
+
89
+ export class InvalidTransactionOperation extends Error {
90
+ constructor(message: string) {
91
+ super(message);
92
+ // common targets ES5. Restore Error subclass identity for consumers of
93
+ // the compiled package, so invalid HTTP input remains a 400, not a 500.
94
+ Object.setPrototypeOf(this, InvalidTransactionOperation.prototype);
95
+ this.name = 'InvalidTransactionOperation';
96
+ }
97
+ }
98
+
99
+ const invalid = (): never => {
100
+ throw new InvalidTransactionOperation(
101
+ 'Invalid transaction operation parameters',
102
+ );
103
+ };
104
+
105
+ // Reject extra fields as well as missing ones. Neither raw calldata nor client
106
+ // metadata may smuggle an alternate target, command batch, or nonce destination.
107
+ function fields(value: unknown, keys: string[]): Record<string, unknown> {
108
+ if (!value || typeof value !== 'object' || Array.isArray(value)) invalid();
109
+ const object = value as Record<string, unknown>;
110
+ if (
111
+ Object.keys(object).length !== keys.length ||
112
+ !keys.every((key) => Object.prototype.hasOwnProperty.call(object, key))
113
+ )
114
+ invalid();
115
+ return object;
116
+ }
117
+
118
+ function uint(value: unknown, bits = 256): string {
119
+ if (
120
+ typeof value !== 'string' ||
121
+ !/^(0|[1-9][0-9]{0,77})$/.test(value) ||
122
+ BigInt(value) >= BigInt(1) << BigInt(bits)
123
+ )
124
+ invalid();
125
+ return value as string;
126
+ }
127
+
128
+ function address(value: unknown): string {
129
+ if (typeof value !== 'string' || !isHexString(value, 20) || !isAddress(value))
130
+ invalid();
131
+ return value as string;
132
+ }
133
+
134
+ function signature(value: unknown): TransactionOperationSignature {
135
+ const sig = fields(value, ['v', 'r', 's', 'deadline']);
136
+ if (
137
+ (sig.v !== 27 && sig.v !== 28) ||
138
+ typeof sig.r !== 'string' ||
139
+ !isHexString(sig.r, 32) ||
140
+ typeof sig.s !== 'string' ||
141
+ !isHexString(sig.s, 32)
142
+ )
143
+ invalid();
144
+ return {
145
+ v: sig.v as 27 | 28,
146
+ r: sig.r as string,
147
+ s: sig.s as string,
148
+ deadline: uint(sig.deadline),
149
+ };
150
+ }
151
+
152
+ const core = new Interface(CoreAbi);
153
+ const periphery = new Interface(PeripheryAbi);
154
+ const gateway = new Interface(OrdersGatewayAbi);
155
+
156
+ /** Validates an untrusted HTTP body and constructs only the named operation. */
157
+ export function buildTransactionOperation(
158
+ operation: TransactionOperation,
159
+ body: unknown,
160
+ reyaChainId: number,
161
+ ): { call: Call; metadata: TransactionExecutionMetadata } {
162
+ const { params } = fields(body, ['params']);
163
+ let contract = ContractType.PERIPHERY_PROXY;
164
+ let callData: string;
165
+ const metadata: TransactionExecutionMetadata = { destinationType: 'account' };
166
+
167
+ switch (operation) {
168
+ case 'create-account': {
169
+ const p = fields(params, ['owner']);
170
+ contract = ContractType.CORE_PROXY;
171
+ callData = core.encodeFunctionData('createAccount', [address(p.owner)]);
172
+ metadata.action = 'createAccount';
173
+ // Creating an account for an owner is permissionless on Core. Profile
174
+ // names require the authenticated account-edit endpoint (RET-200).
175
+ break;
176
+ }
177
+ case 'enable-api-wallet':
178
+ case 'disable-api-wallet': {
179
+ const p = fields(params, ['owner', 'apiWallet', 'sig']);
180
+ contract = ContractType.ORDERS_GATEWAY_PROXY;
181
+ callData = gateway.encodeFunctionData('managePermissionBySig', [
182
+ address(p.owner),
183
+ address(p.apiWallet),
184
+ operation === 'enable-api-wallet',
185
+ signature(p.sig),
186
+ ]);
187
+ metadata.action = operation;
188
+ break;
189
+ }
190
+ case 'transfer': {
191
+ const p = fields(params, [
192
+ 'accountId',
193
+ 'toAccountId',
194
+ 'token',
195
+ 'tokenAmount',
196
+ 'sig',
197
+ ]);
198
+ callData = periphery.encodeFunctionData('transferFromMAToMA', [
199
+ {
200
+ accountId: uint(p.accountId, 128),
201
+ transfer: {
202
+ destAccountId: uint(p.toAccountId, 128),
203
+ collateral: address(p.token),
204
+ collateralAmount: uint(p.tokenAmount),
205
+ },
206
+ sig: signature(p.sig),
207
+ },
208
+ ]);
209
+ break;
210
+ }
211
+ case 'withdraw':
212
+ case 'withdraw-lz': {
213
+ const p = fields(params, [
214
+ 'accountId',
215
+ 'token',
216
+ 'tokenAmount',
217
+ 'sig',
218
+ 'receiver',
219
+ ...(operation === 'withdraw'
220
+ ? ['chainId', 'socketMsgGasLimit']
221
+ : ['dstEid']),
222
+ ]);
223
+ callData = periphery.encodeFunctionData(
224
+ operation === 'withdraw' ? 'withdrawMA' : 'withdrawMALZ',
225
+ [
226
+ {
227
+ accountId: uint(p.accountId, 128),
228
+ token: address(p.token),
229
+ tokenAmount: uint(p.tokenAmount),
230
+ sig: signature(p.sig),
231
+ receiver: address(p.receiver),
232
+ ...(operation === 'withdraw'
233
+ ? {
234
+ chainId: uint(p.chainId),
235
+ socketMsgGasLimit: uint(p.socketMsgGasLimit),
236
+ }
237
+ : { dstEid: uint(p.dstEid, 32) }),
238
+ },
239
+ ],
240
+ );
241
+ break;
242
+ }
243
+ case 'stake':
244
+ case 'unstake': {
245
+ const p = fields(params, [
246
+ 'accountId',
247
+ 'poolId',
248
+ 'amount',
249
+ 'minimumOut',
250
+ 'sig',
251
+ ]);
252
+ const inputs = AbiCoder.defaultAbiCoder().encode(
253
+ ['uint128', 'uint256', 'uint256'],
254
+ [uint(p.poolId, 128), uint(p.amount), uint(p.minimumOut)],
255
+ );
256
+ const { command } = getCommand(
257
+ CommandType.MANAGE_POOL_STAKE,
258
+ [operation === 'stake' ? 0 : 1, inputs],
259
+ 0,
260
+ 0,
261
+ );
262
+ callData = periphery.encodeFunctionData('execute', [
263
+ {
264
+ accountId: uint(p.accountId, 128),
265
+ commands: [command],
266
+ sig: signature(p.sig),
267
+ },
268
+ ]);
269
+ break;
270
+ }
271
+ case 'deposit-pool': {
272
+ const p = fields(params, ['accountId', 'poolId', 'sharesAmount', 'sig']);
273
+ callData = periphery.encodeFunctionData('depositLiquidityToAccount', [
274
+ {
275
+ accountId: uint(p.accountId, 128),
276
+ poolId: uint(p.poolId, 128),
277
+ sharesAmount: uint(p.sharesAmount),
278
+ sig: signature(p.sig),
279
+ },
280
+ ]);
281
+ metadata.destinationType = 'pool';
282
+ break;
283
+ }
284
+ case 'withdraw-pool': {
285
+ const p = fields(params, [
286
+ 'owner',
287
+ 'poolId',
288
+ 'sharesAmount',
289
+ 'minOut',
290
+ 'sig',
291
+ 'socketMsgGasLimit',
292
+ 'chainId',
293
+ 'receiver',
294
+ ]);
295
+ callData = periphery.encodeFunctionData('withdrawPassivePool', [
296
+ {
297
+ owner: address(p.owner),
298
+ poolId: uint(p.poolId, 128),
299
+ sharesAmount: uint(p.sharesAmount),
300
+ minOut: uint(p.minOut),
301
+ sig: signature(p.sig),
302
+ socketMsgGasLimit: uint(p.socketMsgGasLimit),
303
+ chainId: uint(p.chainId),
304
+ receiver: address(p.receiver),
305
+ },
306
+ ]);
307
+ metadata.destinationType = 'pool';
308
+ break;
309
+ }
310
+ default:
311
+ return invalid();
312
+ }
313
+
314
+ return {
315
+ call: { target: getAddress(reyaChainId, contract), callData },
316
+ metadata,
317
+ };
318
+ }