@morpho-org/bundler-sdk-viem 2.1.4 → 2.2.1-next.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/lib/actions.js +4 -4
- package/lib/operations.js +19 -9
- package/package.json +10 -10
package/lib/actions.js
CHANGED
|
@@ -66,7 +66,7 @@ const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
|
66
66
|
export const encodeOperation = (operation, dataBefore, supportsSignature = true, index = 0) => {
|
|
67
67
|
const { chainId } = dataBefore;
|
|
68
68
|
const deadline = Time.timestamp() + Time.s.from.h(24n);
|
|
69
|
-
const { morpho, bundler,
|
|
69
|
+
const { morpho, bundler, permit2, publicAllocator, wNative, dai, wstEth, stEth, } = getChainAddresses(chainId);
|
|
70
70
|
let value = 0n;
|
|
71
71
|
const actions = [];
|
|
72
72
|
const requirements = {
|
|
@@ -230,7 +230,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
230
230
|
// Native token cannot be permitted.
|
|
231
231
|
if (address === NATIVE_ADDRESS)
|
|
232
232
|
break;
|
|
233
|
-
const { amount,
|
|
233
|
+
const { amount, expiration, nonce } = operation.args;
|
|
234
234
|
if (supportsSignature) {
|
|
235
235
|
const action = {
|
|
236
236
|
type: "approve2",
|
|
@@ -242,7 +242,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
242
242
|
nonce: Number(nonce),
|
|
243
243
|
expiration: Number(expiration),
|
|
244
244
|
},
|
|
245
|
-
spender,
|
|
245
|
+
spender: bundler,
|
|
246
246
|
sigDeadline: deadline,
|
|
247
247
|
},
|
|
248
248
|
null,
|
|
@@ -278,7 +278,7 @@ export const encodeOperation = (operation, dataBefore, supportsSignature = true,
|
|
|
278
278
|
break;
|
|
279
279
|
}
|
|
280
280
|
// Signatures are not supported, fallback to standard approval.
|
|
281
|
-
requirements.txs.push(...encodeErc20Approval(address, sender,
|
|
281
|
+
requirements.txs.push(...encodeErc20Approval(address, sender, bundler, amount, dataBefore));
|
|
282
282
|
break;
|
|
283
283
|
}
|
|
284
284
|
case "Erc20_Transfer": {
|
package/lib/operations.js
CHANGED
|
@@ -23,7 +23,7 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
25
|
];
|
|
26
|
-
const { erc20Allowances,
|
|
26
|
+
const { erc20Allowances, permit2BundlerAllowance, erc2612Nonce } = data.getHolding(from, address);
|
|
27
27
|
// ERC20 allowance to the bundler is enough, consume it.
|
|
28
28
|
if (erc20Allowances.bundler >= amount)
|
|
29
29
|
return [
|
|
@@ -90,17 +90,16 @@ export const populateInputTransfer = ({ address, args: { amount, from } }, data,
|
|
|
90
90
|
spender: permit2,
|
|
91
91
|
},
|
|
92
92
|
});
|
|
93
|
-
if (
|
|
94
|
-
|
|
93
|
+
if (permit2BundlerAllowance.amount < amount ||
|
|
94
|
+
permit2BundlerAllowance.expiration < data.block.timestamp)
|
|
95
95
|
operations.push({
|
|
96
96
|
type: "Erc20_Permit2",
|
|
97
97
|
sender: from,
|
|
98
98
|
address,
|
|
99
99
|
args: {
|
|
100
100
|
amount,
|
|
101
|
-
spender: bundler,
|
|
102
101
|
expiration: MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
103
|
-
nonce:
|
|
102
|
+
nonce: permit2BundlerAllowance.nonce,
|
|
104
103
|
},
|
|
105
104
|
});
|
|
106
105
|
operations.push({
|
|
@@ -209,12 +208,24 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
209
208
|
}) > supplyTargetUtilization) {
|
|
210
209
|
// Liquidity is insufficient: trigger a public reallocation and try to have a resulting utilization as low as possible, above the target.
|
|
211
210
|
// Solve: newTotalBorrowAssets / (newTotalSupplyAssets + reallocatedAssets) = supplyTargetUtilization
|
|
212
|
-
//
|
|
211
|
+
// We first try to find public reallocations that respect every markets targets.
|
|
212
|
+
// If this is not enough, the first market to be pushed above target is the supply market. Then we fully withdraw from every market.
|
|
213
213
|
let requiredAssets = supplyTargetUtilization === 0n
|
|
214
214
|
? MathLib.MAX_UINT_160
|
|
215
215
|
: MathLib.wDivDown(newTotalBorrowAssets, supplyTargetUtilization) -
|
|
216
216
|
newTotalSupplyAssets;
|
|
217
|
-
|
|
217
|
+
let { withdrawals, data: simulationStatePostFriendlyReallocation } = data.getMarketPublicReallocations(market.id, publicAllocatorOptions);
|
|
218
|
+
const marketPostFriendlyReallocation = simulationStatePostFriendlyReallocation.getMarket(market.id);
|
|
219
|
+
if (marketPostFriendlyReallocation.totalBorrowAssets + borrowedAssets >
|
|
220
|
+
marketPostFriendlyReallocation.totalSupplyAssets - withdrawnAssets) {
|
|
221
|
+
// If the "friendly" reallocations are not enough, we fully withdraw from every market.
|
|
222
|
+
requiredAssets = newTotalBorrowAssets - newTotalSupplyAssets;
|
|
223
|
+
({ withdrawals } = data.getMarketPublicReallocations(market.id, {
|
|
224
|
+
...publicAllocatorOptions,
|
|
225
|
+
defaultMaxWithdrawalUtilization: MathLib.WAD,
|
|
226
|
+
maxWithdrawalUtilization: {},
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
218
229
|
for (const { vault, ...withdrawal } of withdrawals) {
|
|
219
230
|
const vaultReallocations = (reallocations[vault] ??= []);
|
|
220
231
|
if (withdrawal.assets > requiredAssets) {
|
|
@@ -362,8 +373,7 @@ export const finalizeBundle = (operations, startData, receiver, unwrapTokens = n
|
|
|
362
373
|
}
|
|
363
374
|
case "Erc20_Permit2": {
|
|
364
375
|
const duplicatePermit2 = permit2s.find((permit2) => permit2.address === operation.address &&
|
|
365
|
-
permit2.sender === operation.sender
|
|
366
|
-
permit2.args.spender === operation.args.spender);
|
|
376
|
+
permit2.sender === operation.sender);
|
|
367
377
|
if (duplicatePermit2 == null) {
|
|
368
378
|
const lastPermit2 = permit2s.findLast((permit2) => permit2.address === operation.address &&
|
|
369
379
|
permit2.sender === operation.sender);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@morpho-org/bundler-sdk-viem",
|
|
3
3
|
"description": "Viem-based extension of `@morpho-org/simulation-sdk` that exports utilities to transform simple interactions on Morpho (such as `Blue_Borrow`) and Morpho Vaults (such as `MetaMorpho_Deposit`) into the required bundles (with ERC20 approvals, transfers, etc) to submit to the bundler onchain.",
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.2.1-next.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Rubilmax <rmilon@gmail.com>"
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
],
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"viem": "^2.0.0",
|
|
23
|
-
"@morpho-org/blue-sdk": "^2.3.
|
|
24
|
-
"@morpho-org/
|
|
25
|
-
"@morpho-org/
|
|
26
|
-
"@morpho-org/
|
|
23
|
+
"@morpho-org/blue-sdk": "^2.3.2-next.0",
|
|
24
|
+
"@morpho-org/morpho-ts": "^2.1.0",
|
|
25
|
+
"@morpho-org/simulation-sdk": "^2.1.4-next.0",
|
|
26
|
+
"@morpho-org/blue-sdk-viem": "^2.2.3-next.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@tanstack/query-core": "^5.62.16",
|
|
@@ -35,13 +35,13 @@
|
|
|
35
35
|
"typescript": "^5.7.2",
|
|
36
36
|
"viem": "^2.23.0",
|
|
37
37
|
"vitest": "^3.0.5",
|
|
38
|
-
"@morpho-org/blue-sdk": "^2.3.
|
|
39
|
-
"@morpho-org/blue-sdk-viem": "^2.2.
|
|
38
|
+
"@morpho-org/blue-sdk": "^2.3.2-next.0",
|
|
39
|
+
"@morpho-org/blue-sdk-viem": "^2.2.3-next.0",
|
|
40
40
|
"@morpho-org/morpho-test": "^2.2.1",
|
|
41
|
-
"@morpho-org/
|
|
42
|
-
"@morpho-org/
|
|
43
|
-
"@morpho-org/simulation-sdk-wagmi": "^2.0.5",
|
|
41
|
+
"@morpho-org/simulation-sdk": "^2.1.4-next.0",
|
|
42
|
+
"@morpho-org/morpho-ts": "^2.1.0",
|
|
44
43
|
"@morpho-org/test": "^2.0.6",
|
|
44
|
+
"@morpho-org/simulation-sdk-wagmi": "^2.0.6-next.0",
|
|
45
45
|
"@morpho-org/test-wagmi": "^2.0.4"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|