@morpho-org/bundler-sdk-viem 3.0.0-next.9 → 3.0.1
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/lib/ActionBundle.d.ts +3 -1
- package/lib/ActionBundle.js +19 -7
- package/lib/BundlerAction.d.ts +137 -104
- package/lib/BundlerAction.js +468 -424
- package/lib/abis.js +80 -77
- package/lib/actions.js +194 -99
- package/lib/bundle.js +10 -6
- package/lib/errors.d.ts +4 -0
- package/lib/errors.js +11 -2
- package/lib/index.js +24 -8
- package/lib/operations.js +95 -79
- package/lib/types/actions.d.ts +87 -46
- package/lib/types/actions.js +2 -1
- package/lib/types/index.js +18 -2
- package/lib/types/operations.js +16 -10
- package/package.json +12 -13
package/lib/actions.js
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeOperation = exports.MAX_TOKEN_APPROVALS = exports.APPROVE_ONLY_ONCE_TOKENS = void 0;
|
|
4
|
+
exports.encodeBundle = encodeBundle;
|
|
5
|
+
const viem_1 = require("viem");
|
|
6
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
7
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
8
|
+
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
9
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
10
|
+
const actions_1 = require("viem/actions");
|
|
11
|
+
const ActionBundle_js_1 = require("./ActionBundle.js");
|
|
12
|
+
const errors_js_1 = require("./errors.js");
|
|
13
|
+
exports.APPROVE_ONLY_ONCE_TOKENS = {
|
|
14
|
+
[blue_sdk_1.ChainId.EthMainnet]: [
|
|
10
15
|
"0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT
|
|
11
16
|
"0xD533a949740bb3306d119CC777fa900bA034cd52", // CRV
|
|
12
17
|
],
|
|
13
18
|
};
|
|
14
|
-
|
|
15
|
-
[ChainId.EthMainnet]: {
|
|
16
|
-
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": MathLib.maxUint(96), // UNI --> see https://github.com/Uniswap/governance/blob/eabd8c71ad01f61fb54ed6945162021ee419998e/contracts/Uni.sol#L154
|
|
19
|
+
exports.MAX_TOKEN_APPROVALS = {
|
|
20
|
+
[blue_sdk_1.ChainId.EthMainnet]: {
|
|
21
|
+
"0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984": blue_sdk_1.MathLib.maxUint(96), // UNI --> see https://github.com/Uniswap/governance/blob/eabd8c71ad01f61fb54ed6945162021ee419998e/contracts/Uni.sol#L154
|
|
17
22
|
},
|
|
18
23
|
};
|
|
19
24
|
const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
20
25
|
const { chainId } = data;
|
|
21
|
-
const { morpho, bundler3: { generalAdapter1 }, permit2, } = getChainAddresses(chainId);
|
|
22
|
-
amount = MathLib.min(amount, MAX_TOKEN_APPROVALS[chainId]?.[token] ?? maxUint256);
|
|
26
|
+
const { morpho, bundler3: { generalAdapter1 }, permit2, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
27
|
+
amount = blue_sdk_1.MathLib.min(amount, exports.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
|
|
23
28
|
const txRequirements = [];
|
|
24
|
-
if (APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(token)) {
|
|
29
|
+
if (exports.APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(token)) {
|
|
25
30
|
const contract = spender === morpho
|
|
26
31
|
? "morpho"
|
|
27
32
|
: spender === generalAdapter1
|
|
@@ -40,8 +45,8 @@ const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
|
40
45
|
args: [token, spender, 0n],
|
|
41
46
|
tx: {
|
|
42
47
|
to: token,
|
|
43
|
-
data: encodeFunctionData({
|
|
44
|
-
abi: erc20Abi,
|
|
48
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
49
|
+
abi: viem_1.erc20Abi,
|
|
45
50
|
functionName: "approve",
|
|
46
51
|
args: [spender, 0n],
|
|
47
52
|
}),
|
|
@@ -53,8 +58,8 @@ const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
|
53
58
|
args: [token, spender, amount],
|
|
54
59
|
tx: {
|
|
55
60
|
to: token,
|
|
56
|
-
data: encodeFunctionData({
|
|
57
|
-
abi: erc20Abi,
|
|
61
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
62
|
+
abi: viem_1.erc20Abi,
|
|
58
63
|
functionName: "approve",
|
|
59
64
|
args: [spender, amount],
|
|
60
65
|
}),
|
|
@@ -62,28 +67,28 @@ const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
|
62
67
|
});
|
|
63
68
|
return txRequirements;
|
|
64
69
|
};
|
|
65
|
-
|
|
70
|
+
const encodeOperation = (operation, dataBefore, supportsSignature = true, index = 0) => {
|
|
66
71
|
const { chainId } = dataBefore;
|
|
67
|
-
const deadline = Time.timestamp() + Time.s.from.h(24n);
|
|
68
|
-
const { morpho, bundler3: { bundler3, generalAdapter1 }, permit2, wNative, dai, wstEth, stEth, } = getChainAddresses(chainId);
|
|
72
|
+
const deadline = morpho_ts_1.Time.timestamp() + morpho_ts_1.Time.s.from.h(24n);
|
|
73
|
+
const { morpho, bundler3: { bundler3, generalAdapter1 }, permit2, wNative, dai, wstEth, stEth, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
69
74
|
const actions = [];
|
|
70
|
-
const requirements = new ActionBundleRequirements();
|
|
75
|
+
const requirements = new ActionBundle_js_1.ActionBundleRequirements();
|
|
71
76
|
let callbackBundle;
|
|
72
|
-
const callback = getValue(operation.args, "callback");
|
|
77
|
+
const callback = (0, morpho_ts_1.getValue)(operation.args, "callback");
|
|
73
78
|
const simulatedOperation = {
|
|
74
79
|
...operation,
|
|
75
80
|
args: {
|
|
76
81
|
...operation.args,
|
|
77
82
|
...(callback && {
|
|
78
83
|
callback: (dataBefore) => {
|
|
79
|
-
callbackBundle = encodeBundle(callback, getCurrent(dataBefore), supportsSignature);
|
|
84
|
+
callbackBundle = encodeBundle(callback, (0, simulation_sdk_1.getCurrent)(dataBefore), supportsSignature);
|
|
80
85
|
return callback;
|
|
81
86
|
},
|
|
82
87
|
}),
|
|
83
88
|
},
|
|
84
89
|
};
|
|
85
90
|
// Operations with callbacks are encoded recursively as a side-effect of the simulation, within the callback itself.
|
|
86
|
-
const dataAfter = simulateOperation(simulatedOperation, dataBefore, index);
|
|
91
|
+
const dataAfter = (0, simulation_sdk_1.simulateOperation)(simulatedOperation, dataBefore, index);
|
|
87
92
|
if (callbackBundle) {
|
|
88
93
|
requirements.txs.push(...callbackBundle.requirements.txs);
|
|
89
94
|
requirements.signatures.push(...callbackBundle.requirements.signatures);
|
|
@@ -92,6 +97,9 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
92
97
|
switch (operation.type) {
|
|
93
98
|
case "Blue_SetAuthorization": {
|
|
94
99
|
const { owner, isAuthorized, authorized } = operation.args;
|
|
100
|
+
// Never authorize bundler3 otherwise the signature can be used independently.
|
|
101
|
+
if (authorized === bundler3)
|
|
102
|
+
throw new errors_js_1.BundlerErrors.UnexpectedSignature(authorized);
|
|
95
103
|
if (supportsSignature) {
|
|
96
104
|
const ownerData = dataBefore.getUser(owner);
|
|
97
105
|
const authorization = {
|
|
@@ -103,7 +111,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
103
111
|
};
|
|
104
112
|
const action = {
|
|
105
113
|
type: "morphoSetAuthorizationWithSig",
|
|
106
|
-
args: [authorization, null],
|
|
114
|
+
args: [authorization, null, operation.skipRevert],
|
|
107
115
|
};
|
|
108
116
|
actions.push(action);
|
|
109
117
|
requirements.signatures.push({
|
|
@@ -112,12 +120,12 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
112
120
|
let signature = action.args[1];
|
|
113
121
|
if (signature != null)
|
|
114
122
|
return signature;
|
|
115
|
-
const typedData = getAuthorizationTypedData(authorization, chainId);
|
|
116
|
-
signature = await signTypedData(client, {
|
|
123
|
+
const typedData = (0, blue_sdk_viem_1.getAuthorizationTypedData)(authorization, chainId);
|
|
124
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
117
125
|
...typedData,
|
|
118
126
|
account,
|
|
119
127
|
});
|
|
120
|
-
await verifyTypedData({
|
|
128
|
+
await (0, viem_1.verifyTypedData)({
|
|
121
129
|
...typedData,
|
|
122
130
|
address: sender, // Verify against the authorization's owner.
|
|
123
131
|
signature,
|
|
@@ -133,8 +141,8 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
133
141
|
args: [authorized, isAuthorized],
|
|
134
142
|
tx: {
|
|
135
143
|
to: morpho,
|
|
136
|
-
data: encodeFunctionData({
|
|
137
|
-
abi: blueAbi,
|
|
144
|
+
data: (0, viem_1.encodeFunctionData)({
|
|
145
|
+
abi: blue_sdk_viem_1.blueAbi,
|
|
138
146
|
functionName: "setAuthorization",
|
|
139
147
|
args: [authorized, isAuthorized],
|
|
140
148
|
}),
|
|
@@ -144,7 +152,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
144
152
|
}
|
|
145
153
|
case "Erc20_Approve": {
|
|
146
154
|
// Native token cannot be approved.
|
|
147
|
-
if (address === NATIVE_ADDRESS)
|
|
155
|
+
if (address === blue_sdk_1.NATIVE_ADDRESS)
|
|
148
156
|
break;
|
|
149
157
|
const { amount, spender } = operation.args;
|
|
150
158
|
// Signatures are not supported, skip Permit2 approval.
|
|
@@ -155,18 +163,36 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
155
163
|
}
|
|
156
164
|
case "Erc20_Permit": {
|
|
157
165
|
// Native token cannot be permitted.
|
|
158
|
-
if (address === NATIVE_ADDRESS)
|
|
166
|
+
if (address === blue_sdk_1.NATIVE_ADDRESS)
|
|
159
167
|
break;
|
|
160
168
|
const { amount, spender, nonce } = operation.args;
|
|
169
|
+
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
170
|
+
// the signature can be used independently.
|
|
171
|
+
if (spender !== generalAdapter1)
|
|
172
|
+
throw new errors_js_1.BundlerErrors.UnexpectedSignature(spender);
|
|
161
173
|
if (supportsSignature) {
|
|
162
174
|
const action = address === dai
|
|
163
175
|
? {
|
|
164
176
|
type: "permitDai",
|
|
165
|
-
args: [
|
|
177
|
+
args: [
|
|
178
|
+
sender,
|
|
179
|
+
nonce,
|
|
180
|
+
deadline,
|
|
181
|
+
true,
|
|
182
|
+
null,
|
|
183
|
+
operation.skipRevert,
|
|
184
|
+
],
|
|
166
185
|
}
|
|
167
186
|
: {
|
|
168
187
|
type: "permit",
|
|
169
|
-
args: [
|
|
188
|
+
args: [
|
|
189
|
+
sender,
|
|
190
|
+
address,
|
|
191
|
+
amount,
|
|
192
|
+
deadline,
|
|
193
|
+
null,
|
|
194
|
+
operation.skipRevert,
|
|
195
|
+
],
|
|
170
196
|
};
|
|
171
197
|
actions.push(action);
|
|
172
198
|
const tokenData = dataBefore.getToken(address);
|
|
@@ -177,25 +203,25 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
177
203
|
if (signature != null)
|
|
178
204
|
return signature; // action is already signed
|
|
179
205
|
if (address === dai) {
|
|
180
|
-
const typedData = getDaiPermitTypedData({
|
|
206
|
+
const typedData = (0, blue_sdk_viem_1.getDaiPermitTypedData)({
|
|
181
207
|
owner: sender,
|
|
182
208
|
spender,
|
|
183
209
|
allowance: amount,
|
|
184
210
|
nonce,
|
|
185
211
|
deadline,
|
|
186
212
|
}, chainId);
|
|
187
|
-
signature = await signTypedData(client, {
|
|
213
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
188
214
|
...typedData,
|
|
189
215
|
account,
|
|
190
216
|
});
|
|
191
|
-
await verifyTypedData({
|
|
217
|
+
await (0, viem_1.verifyTypedData)({
|
|
192
218
|
...typedData,
|
|
193
219
|
address: account.address,
|
|
194
220
|
signature,
|
|
195
221
|
});
|
|
196
222
|
}
|
|
197
223
|
else {
|
|
198
|
-
const typedData = getPermitTypedData({
|
|
224
|
+
const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
|
|
199
225
|
erc20: tokenData,
|
|
200
226
|
owner: sender,
|
|
201
227
|
spender,
|
|
@@ -203,11 +229,11 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
203
229
|
nonce,
|
|
204
230
|
deadline,
|
|
205
231
|
}, chainId);
|
|
206
|
-
signature = await signTypedData(client, {
|
|
232
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
207
233
|
...typedData,
|
|
208
234
|
account,
|
|
209
235
|
});
|
|
210
|
-
await verifyTypedData({
|
|
236
|
+
await (0, viem_1.verifyTypedData)({
|
|
211
237
|
...typedData,
|
|
212
238
|
address: sender, // Verify against the permit's owner.
|
|
213
239
|
signature,
|
|
@@ -224,7 +250,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
224
250
|
}
|
|
225
251
|
case "Erc20_Permit2": {
|
|
226
252
|
// Native token cannot be permitted.
|
|
227
|
-
if (address === NATIVE_ADDRESS)
|
|
253
|
+
if (address === blue_sdk_1.NATIVE_ADDRESS)
|
|
228
254
|
break;
|
|
229
255
|
const { amount, expiration, nonce } = operation.args;
|
|
230
256
|
if (supportsSignature) {
|
|
@@ -239,33 +265,35 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
239
265
|
nonce: Number(nonce),
|
|
240
266
|
expiration: Number(expiration),
|
|
241
267
|
},
|
|
242
|
-
spender: bundler3,
|
|
243
268
|
sigDeadline: deadline,
|
|
244
269
|
},
|
|
245
270
|
null,
|
|
271
|
+
operation.skipRevert,
|
|
246
272
|
],
|
|
247
273
|
};
|
|
248
274
|
actions.push(action);
|
|
249
275
|
requirements.signatures.push({
|
|
250
276
|
action,
|
|
251
277
|
async sign(client, account = client.account) {
|
|
252
|
-
const { details,
|
|
278
|
+
const { details, sigDeadline } = action.args[1];
|
|
253
279
|
let signature = action.args[2];
|
|
254
280
|
if (signature != null)
|
|
255
281
|
return signature; // action is already signed
|
|
256
|
-
const typedData = getPermit2PermitTypedData({
|
|
257
|
-
|
|
282
|
+
const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
|
|
283
|
+
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
284
|
+
// the signature can be used independently.
|
|
285
|
+
spender: generalAdapter1,
|
|
258
286
|
allowance: details.amount,
|
|
259
287
|
erc20: details.token,
|
|
260
288
|
nonce: details.nonce,
|
|
261
289
|
deadline: sigDeadline,
|
|
262
290
|
expiration: details.expiration,
|
|
263
291
|
}, chainId);
|
|
264
|
-
signature = await signTypedData(client, {
|
|
292
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
265
293
|
...typedData,
|
|
266
294
|
account,
|
|
267
295
|
});
|
|
268
|
-
await verifyTypedData({
|
|
296
|
+
await (0, viem_1.verifyTypedData)({
|
|
269
297
|
...typedData,
|
|
270
298
|
address: sender, // Verify against the permit's owner.
|
|
271
299
|
signature,
|
|
@@ -281,10 +309,10 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
281
309
|
}
|
|
282
310
|
case "Erc20_Transfer": {
|
|
283
311
|
const { amount, from, to } = operation.args;
|
|
284
|
-
if (address === NATIVE_ADDRESS) {
|
|
312
|
+
if (address === blue_sdk_1.NATIVE_ADDRESS) {
|
|
285
313
|
actions.push({
|
|
286
314
|
type: "nativeTransfer",
|
|
287
|
-
args: [from, to, amount],
|
|
315
|
+
args: [from, to, amount, operation.skipRevert],
|
|
288
316
|
});
|
|
289
317
|
break;
|
|
290
318
|
}
|
|
@@ -292,29 +320,29 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
292
320
|
if (from === generalAdapter1) {
|
|
293
321
|
actions.push({
|
|
294
322
|
type: "erc20Transfer",
|
|
295
|
-
args: [address, to, amount],
|
|
323
|
+
args: [address, to, amount, generalAdapter1, operation.skipRevert],
|
|
296
324
|
});
|
|
297
325
|
break;
|
|
298
326
|
}
|
|
299
327
|
actions.push({
|
|
300
328
|
type: "erc20TransferFrom",
|
|
301
|
-
args: [address, amount, to],
|
|
329
|
+
args: [address, amount, to, operation.skipRevert],
|
|
302
330
|
});
|
|
303
331
|
break;
|
|
304
332
|
}
|
|
305
333
|
case "Erc20_Transfer2": {
|
|
306
|
-
const { amount,
|
|
334
|
+
const { amount, to } = operation.args;
|
|
307
335
|
if (supportsSignature) {
|
|
308
336
|
actions.push({
|
|
309
337
|
type: "transferFrom2",
|
|
310
|
-
args: [address,
|
|
338
|
+
args: [address, amount, to, operation.skipRevert],
|
|
311
339
|
});
|
|
312
340
|
break;
|
|
313
341
|
}
|
|
314
342
|
// Signatures are not supported, fallback to standard transfer.
|
|
315
343
|
actions.push({
|
|
316
344
|
type: "erc20TransferFrom",
|
|
317
|
-
args: [address, amount, to],
|
|
345
|
+
args: [address, amount, to, operation.skipRevert],
|
|
318
346
|
});
|
|
319
347
|
break;
|
|
320
348
|
}
|
|
@@ -324,37 +352,43 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
324
352
|
case wNative: {
|
|
325
353
|
actions.push({
|
|
326
354
|
type: "wrapNative",
|
|
327
|
-
args: [amount],
|
|
355
|
+
args: [amount, generalAdapter1, operation.skipRevert],
|
|
328
356
|
});
|
|
329
357
|
break;
|
|
330
358
|
}
|
|
331
359
|
case wstEth: {
|
|
332
360
|
actions.push({
|
|
333
361
|
type: "wrapStEth",
|
|
334
|
-
args: [amount],
|
|
362
|
+
args: [amount, generalAdapter1, operation.skipRevert],
|
|
335
363
|
});
|
|
336
364
|
break;
|
|
337
365
|
}
|
|
338
366
|
case stEth: {
|
|
339
367
|
actions.push({
|
|
340
368
|
type: "stakeEth",
|
|
341
|
-
args: [
|
|
369
|
+
args: [
|
|
370
|
+
amount,
|
|
371
|
+
blue_sdk_1.MathLib.MAX_UINT_256,
|
|
372
|
+
viem_1.zeroAddress,
|
|
373
|
+
generalAdapter1,
|
|
374
|
+
operation.skipRevert,
|
|
375
|
+
],
|
|
342
376
|
});
|
|
343
377
|
break;
|
|
344
378
|
}
|
|
345
379
|
default: {
|
|
346
|
-
if (erc20WrapperTokens[chainId]?.has(address)) {
|
|
347
|
-
const underlying = getUnwrappedToken(address, chainId);
|
|
380
|
+
if (blue_sdk_1.erc20WrapperTokens[chainId]?.has(address)) {
|
|
381
|
+
const underlying = (0, blue_sdk_1.getUnwrappedToken)(address, chainId);
|
|
348
382
|
if (underlying == null)
|
|
349
383
|
throw Error(`unknown wrapped token: ${address}`);
|
|
350
384
|
actions.push({
|
|
351
385
|
type: "erc20WrapperDepositFor",
|
|
352
|
-
args: [address, underlying, amount],
|
|
386
|
+
args: [address, underlying, amount, operation.skipRevert],
|
|
353
387
|
});
|
|
354
388
|
break;
|
|
355
389
|
}
|
|
356
390
|
// Convex token wrapping is executed onchain along with supplyCollateral, via depositFor.
|
|
357
|
-
if (!convexWrapperTokens[chainId]?.has(address))
|
|
391
|
+
if (!blue_sdk_1.convexWrapperTokens[chainId]?.has(address))
|
|
358
392
|
throw Error(`unexpected token wrap: ${address}`);
|
|
359
393
|
}
|
|
360
394
|
}
|
|
@@ -366,32 +400,35 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
366
400
|
case wNative: {
|
|
367
401
|
actions.push({
|
|
368
402
|
type: "unwrapNative",
|
|
369
|
-
args: [amount],
|
|
403
|
+
args: [amount, generalAdapter1, operation.skipRevert],
|
|
370
404
|
});
|
|
371
405
|
break;
|
|
372
406
|
}
|
|
373
407
|
case wstEth: {
|
|
374
408
|
actions.push({
|
|
375
409
|
type: "unwrapStEth",
|
|
376
|
-
args: [amount],
|
|
410
|
+
args: [amount, generalAdapter1, operation.skipRevert],
|
|
377
411
|
});
|
|
378
412
|
break;
|
|
379
413
|
}
|
|
380
414
|
default: {
|
|
381
|
-
if (!erc20WrapperTokens[chainId]?.has(address))
|
|
415
|
+
if (!blue_sdk_1.erc20WrapperTokens[chainId]?.has(address))
|
|
382
416
|
throw Error(`unexpected token unwrap: ${address}`);
|
|
383
417
|
actions.push({
|
|
384
418
|
type: "erc20WrapperWithdrawTo",
|
|
385
|
-
args: [address, receiver, amount],
|
|
419
|
+
args: [address, receiver, amount, operation.skipRevert],
|
|
386
420
|
});
|
|
387
421
|
}
|
|
388
422
|
}
|
|
389
423
|
break;
|
|
390
424
|
}
|
|
391
425
|
case "Blue_Supply": {
|
|
392
|
-
const { id, assets = 0n, shares = 0n, onBehalf, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
393
|
-
|
|
394
|
-
const
|
|
426
|
+
const { id, assets = 0n, shares = 0n, onBehalf, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
427
|
+
// Accrue interest to calculate the expected share price.
|
|
428
|
+
const market = dataBefore
|
|
429
|
+
.getMarket(id)
|
|
430
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
431
|
+
const maxSharePrice = market.toSupplyAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippage));
|
|
395
432
|
actions.push({
|
|
396
433
|
type: "morphoSupply",
|
|
397
434
|
args: [
|
|
@@ -401,34 +438,58 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
401
438
|
maxSharePrice,
|
|
402
439
|
onBehalf,
|
|
403
440
|
callbackBundle?.actions ?? [],
|
|
441
|
+
operation.skipRevert,
|
|
404
442
|
],
|
|
405
443
|
});
|
|
406
444
|
break;
|
|
407
445
|
}
|
|
408
446
|
case "Blue_Withdraw": {
|
|
409
|
-
const { id, assets = 0n, shares = 0n, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
410
|
-
|
|
411
|
-
const
|
|
447
|
+
const { id, assets = 0n, shares = 0n, receiver, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
448
|
+
// Accrue interest to calculate the expected share price.
|
|
449
|
+
const market = dataBefore
|
|
450
|
+
.getMarket(id)
|
|
451
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
452
|
+
const minSharePrice = market.toSupplyAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD - slippage));
|
|
412
453
|
actions.push({
|
|
413
454
|
type: "morphoWithdraw",
|
|
414
|
-
args: [
|
|
455
|
+
args: [
|
|
456
|
+
market.params,
|
|
457
|
+
assets,
|
|
458
|
+
shares,
|
|
459
|
+
minSharePrice,
|
|
460
|
+
receiver,
|
|
461
|
+
operation.skipRevert,
|
|
462
|
+
],
|
|
415
463
|
});
|
|
416
464
|
break;
|
|
417
465
|
}
|
|
418
466
|
case "Blue_Borrow": {
|
|
419
|
-
const { id, assets = 0n, shares = 0n, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
420
|
-
|
|
421
|
-
const
|
|
467
|
+
const { id, assets = 0n, shares = 0n, receiver, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
468
|
+
// Accrue interest to calculate the expected share price.
|
|
469
|
+
const market = dataBefore
|
|
470
|
+
.getMarket(id)
|
|
471
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
472
|
+
const minSharePrice = market.toBorrowAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD - slippage));
|
|
422
473
|
actions.push({
|
|
423
474
|
type: "morphoBorrow",
|
|
424
|
-
args: [
|
|
475
|
+
args: [
|
|
476
|
+
market.params,
|
|
477
|
+
assets,
|
|
478
|
+
shares,
|
|
479
|
+
minSharePrice,
|
|
480
|
+
receiver,
|
|
481
|
+
operation.skipRevert,
|
|
482
|
+
],
|
|
425
483
|
});
|
|
426
484
|
break;
|
|
427
485
|
}
|
|
428
486
|
case "Blue_Repay": {
|
|
429
|
-
const { id, assets = 0n, shares = 0n, onBehalf, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
430
|
-
|
|
431
|
-
const
|
|
487
|
+
const { id, assets = 0n, shares = 0n, onBehalf, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
488
|
+
// Accrue interest to calculate the expected share price.
|
|
489
|
+
const market = dataBefore
|
|
490
|
+
.getMarket(id)
|
|
491
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
492
|
+
const maxSharePrice = market.toBorrowAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippage));
|
|
432
493
|
actions.push({
|
|
433
494
|
type: "morphoRepay",
|
|
434
495
|
args: [
|
|
@@ -438,6 +499,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
438
499
|
maxSharePrice,
|
|
439
500
|
onBehalf,
|
|
440
501
|
callbackBundle?.actions ?? [],
|
|
502
|
+
operation.skipRevert,
|
|
441
503
|
],
|
|
442
504
|
});
|
|
443
505
|
break;
|
|
@@ -445,19 +507,30 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
445
507
|
case "Blue_SupplyCollateral": {
|
|
446
508
|
const { id, assets, onBehalf } = operation.args;
|
|
447
509
|
const { params } = dataBefore.getMarket(id);
|
|
448
|
-
if (convexWrapperTokens[chainId]?.has(params.collateralToken)) {
|
|
449
|
-
const underlying = getUnwrappedToken(address, chainId);
|
|
510
|
+
if (blue_sdk_1.convexWrapperTokens[chainId]?.has(params.collateralToken)) {
|
|
511
|
+
const underlying = (0, blue_sdk_1.getUnwrappedToken)(address, chainId);
|
|
450
512
|
if (underlying == null)
|
|
451
513
|
throw Error(`unknown wrapped token: ${address}`);
|
|
452
514
|
actions.push({
|
|
453
515
|
type: "erc20WrapperDepositFor",
|
|
454
|
-
args: [
|
|
516
|
+
args: [
|
|
517
|
+
params.collateralToken,
|
|
518
|
+
underlying,
|
|
519
|
+
assets,
|
|
520
|
+
operation.skipRevert,
|
|
521
|
+
],
|
|
455
522
|
});
|
|
456
523
|
break;
|
|
457
524
|
}
|
|
458
525
|
actions.push({
|
|
459
526
|
type: "morphoSupplyCollateral",
|
|
460
|
-
args: [
|
|
527
|
+
args: [
|
|
528
|
+
params,
|
|
529
|
+
assets,
|
|
530
|
+
onBehalf,
|
|
531
|
+
callbackBundle?.actions ?? [],
|
|
532
|
+
operation.skipRevert,
|
|
533
|
+
],
|
|
461
534
|
});
|
|
462
535
|
break;
|
|
463
536
|
}
|
|
@@ -466,39 +539,59 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
466
539
|
const { params } = dataBefore.getMarket(id);
|
|
467
540
|
actions.push({
|
|
468
541
|
type: "morphoWithdrawCollateral",
|
|
469
|
-
args: [params, assets, receiver],
|
|
542
|
+
args: [params, assets, receiver, operation.skipRevert],
|
|
470
543
|
});
|
|
471
544
|
break;
|
|
472
545
|
}
|
|
473
546
|
case "MetaMorpho_Deposit": {
|
|
474
|
-
const { assets = 0n, shares = 0n, owner, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
475
|
-
|
|
476
|
-
const
|
|
547
|
+
const { assets = 0n, shares = 0n, owner, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
548
|
+
// Accrue interest to calculate the expected share price.
|
|
549
|
+
const vault = dataBefore
|
|
550
|
+
.getAccrualVault(address)
|
|
551
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
552
|
+
const maxSharePrice = vault.toAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippage));
|
|
477
553
|
if (shares === 0n)
|
|
478
554
|
actions.push({
|
|
479
555
|
type: "erc4626Deposit",
|
|
480
|
-
args: [address, assets, maxSharePrice, owner],
|
|
556
|
+
args: [address, assets, maxSharePrice, owner, operation.skipRevert],
|
|
481
557
|
});
|
|
482
558
|
else
|
|
483
559
|
actions.push({
|
|
484
560
|
type: "erc4626Mint",
|
|
485
|
-
args: [address, shares, maxSharePrice, owner],
|
|
561
|
+
args: [address, shares, maxSharePrice, owner, operation.skipRevert],
|
|
486
562
|
});
|
|
487
563
|
break;
|
|
488
564
|
}
|
|
489
565
|
case "MetaMorpho_Withdraw": {
|
|
490
|
-
const { assets = 0n, shares = 0n, owner, receiver, slippage = DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
491
|
-
|
|
492
|
-
const
|
|
566
|
+
const { assets = 0n, shares = 0n, owner, receiver, slippage = blue_sdk_1.DEFAULT_SLIPPAGE_TOLERANCE, } = operation.args;
|
|
567
|
+
// Accrue interest to calculate the expected share price.
|
|
568
|
+
const vault = dataBefore
|
|
569
|
+
.getAccrualVault(address)
|
|
570
|
+
.accrueInterest(dataBefore.block.timestamp);
|
|
571
|
+
const minSharePrice = vault.toAssets(blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD - slippage));
|
|
493
572
|
if (assets > 0n)
|
|
494
573
|
actions.push({
|
|
495
574
|
type: "erc4626Withdraw",
|
|
496
|
-
args: [
|
|
575
|
+
args: [
|
|
576
|
+
address,
|
|
577
|
+
assets,
|
|
578
|
+
minSharePrice,
|
|
579
|
+
receiver,
|
|
580
|
+
owner,
|
|
581
|
+
operation.skipRevert,
|
|
582
|
+
],
|
|
497
583
|
});
|
|
498
584
|
else
|
|
499
585
|
actions.push({
|
|
500
586
|
type: "erc4626Redeem",
|
|
501
|
-
args: [
|
|
587
|
+
args: [
|
|
588
|
+
address,
|
|
589
|
+
shares,
|
|
590
|
+
minSharePrice,
|
|
591
|
+
receiver,
|
|
592
|
+
owner,
|
|
593
|
+
operation.skipRevert,
|
|
594
|
+
],
|
|
502
595
|
});
|
|
503
596
|
break;
|
|
504
597
|
}
|
|
@@ -516,6 +609,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
516
609
|
amount: assets,
|
|
517
610
|
})),
|
|
518
611
|
dataBefore.getMarket(supplyMarketId).params,
|
|
612
|
+
operation.skipRevert,
|
|
519
613
|
],
|
|
520
614
|
});
|
|
521
615
|
break;
|
|
@@ -527,10 +621,11 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
527
621
|
requirements,
|
|
528
622
|
};
|
|
529
623
|
};
|
|
530
|
-
|
|
531
|
-
|
|
624
|
+
exports.encodeOperation = encodeOperation;
|
|
625
|
+
function encodeBundle(operations, startData, supportsSignature = true) {
|
|
626
|
+
const bundle = new ActionBundle_js_1.ActionBundle([startData]);
|
|
532
627
|
for (let index = 0; index < operations.length; ++index) {
|
|
533
|
-
const { dataAfter, actions, requirements } = encodeOperation(operations[index], bundle.steps[index], supportsSignature, index);
|
|
628
|
+
const { dataAfter, actions, requirements } = (0, exports.encodeOperation)(operations[index], bundle.steps[index], supportsSignature, index);
|
|
534
629
|
bundle.steps.push(dataAfter);
|
|
535
630
|
bundle.actions.push(...actions);
|
|
536
631
|
bundle.requirements.signatures.push(...requirements.signatures);
|
package/lib/bundle.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupBundle = void 0;
|
|
4
|
+
const actions_js_1 = require("./actions.js");
|
|
5
|
+
const operations_js_1 = require("./operations.js");
|
|
6
|
+
const setupBundle = (inputOperations, startData, receiver, { supportsSignature, unwrapTokens, unwrapSlippage, ...options } = {}) => {
|
|
7
|
+
let { operations } = (0, operations_js_1.populateBundle)(inputOperations, startData, options);
|
|
8
|
+
operations = (0, operations_js_1.finalizeBundle)(operations, startData, receiver, unwrapTokens, unwrapSlippage);
|
|
9
|
+
const bundle = (0, actions_js_1.encodeBundle)(operations, startData, supportsSignature);
|
|
7
10
|
return {
|
|
8
11
|
operations,
|
|
9
12
|
bundle,
|
|
10
13
|
};
|
|
11
14
|
};
|
|
15
|
+
exports.setupBundle = setupBundle;
|