@morpho-org/bundler-sdk-viem 3.2.0-next.1 → 3.2.0-next.3
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.d.ts +0 -3
- package/lib/actions.js +14 -19
- package/lib/operations.js +22 -8
- package/package.json +8 -8
package/lib/actions.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { type Address } from "viem";
|
|
2
1
|
import { type MaybeDraft, type SimulationState } from "@morpho-org/simulation-sdk";
|
|
3
2
|
import { ActionBundle, ActionBundleRequirements } from "./ActionBundle.js";
|
|
4
3
|
import type { Action, BundlerOperation, TransactionRequirement } from "./types/index.js";
|
|
5
|
-
export declare const APPROVE_ONLY_ONCE_TOKENS: Partial<Record<number, Address[]>>;
|
|
6
|
-
export declare const MAX_TOKEN_APPROVALS: Partial<Record<number, Record<Address, bigint>>>;
|
|
7
4
|
export declare const encodeOperation: (operation: BundlerOperation, dataBefore: MaybeDraft<SimulationState>, supportsSignature?: boolean, index?: number) => {
|
|
8
5
|
dataAfter: MaybeDraft<SimulationState>;
|
|
9
6
|
actions: Action[];
|
package/lib/actions.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.encodeOperation =
|
|
3
|
+
exports.encodeOperation = void 0;
|
|
4
4
|
exports.encodeBundle = encodeBundle;
|
|
5
5
|
const viem_1 = require("viem");
|
|
6
6
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
@@ -10,23 +10,12 @@ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
|
10
10
|
const actions_1 = require("viem/actions");
|
|
11
11
|
const ActionBundle_js_1 = require("./ActionBundle.js");
|
|
12
12
|
const errors_js_1 = require("./errors.js");
|
|
13
|
-
exports.APPROVE_ONLY_ONCE_TOKENS = {
|
|
14
|
-
[blue_sdk_1.ChainId.EthMainnet]: [
|
|
15
|
-
"0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT
|
|
16
|
-
"0xD533a949740bb3306d119CC777fa900bA034cd52", // CRV
|
|
17
|
-
],
|
|
18
|
-
};
|
|
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
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
13
|
const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
25
14
|
const { chainId } = data;
|
|
26
15
|
const { morpho, bundler3: { generalAdapter1 }, permit2, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
27
|
-
amount = blue_sdk_1.MathLib.min(amount,
|
|
16
|
+
amount = blue_sdk_1.MathLib.min(amount, simulation_sdk_1.MAX_TOKEN_APPROVALS[chainId]?.[token] ?? viem_1.maxUint256);
|
|
28
17
|
const txRequirements = [];
|
|
29
|
-
if (
|
|
18
|
+
if (simulation_sdk_1.APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(token)) {
|
|
30
19
|
const contract = spender === morpho
|
|
31
20
|
? "morpho"
|
|
32
21
|
: spender === generalAdapter1
|
|
@@ -69,7 +58,6 @@ const encodeErc20Approval = (token, sender, spender, amount, data) => {
|
|
|
69
58
|
};
|
|
70
59
|
const encodeOperation = (operation, dataBefore, supportsSignature = true, index = 0) => {
|
|
71
60
|
const { chainId } = dataBefore;
|
|
72
|
-
const deadline = morpho_ts_1.Time.timestamp() + morpho_ts_1.Time.s.from.h(24n);
|
|
73
61
|
const { morpho, bundler3: { bundler3, generalAdapter1, paraswapAdapter }, permit2, wNative, dai, wstEth, stEth, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
74
62
|
const actions = [];
|
|
75
63
|
const requirements = new ActionBundle_js_1.ActionBundleRequirements();
|
|
@@ -100,7 +88,7 @@ const encodeOperation = (operation, dataBefore, supportsSignature = true, index
|
|
|
100
88
|
const { sender } = operation;
|
|
101
89
|
switch (operation.type) {
|
|
102
90
|
case "Blue_SetAuthorization": {
|
|
103
|
-
const { owner, isAuthorized, authorized } = operation.args;
|
|
91
|
+
const { owner, isAuthorized, authorized, deadline = dataBefore.block.timestamp + morpho_ts_1.Time.s.from.h(2n), } = operation.args;
|
|
104
92
|
// Never authorize bundler3 otherwise the signature can be used independently.
|
|
105
93
|
if (authorized === bundler3)
|
|
106
94
|
throw new errors_js_1.BundlerErrors.UnexpectedSignature(authorized);
|
|
@@ -169,7 +157,7 @@ const encodeOperation = (operation, dataBefore, supportsSignature = true, index
|
|
|
169
157
|
// Native token cannot be permitted.
|
|
170
158
|
if (operation.address === blue_sdk_1.NATIVE_ADDRESS)
|
|
171
159
|
break;
|
|
172
|
-
const { amount, spender, nonce } = operation.args;
|
|
160
|
+
const { amount, spender, nonce, deadline = dataBefore.block.timestamp + morpho_ts_1.Time.s.from.h(2n), } = operation.args;
|
|
173
161
|
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
174
162
|
// the signature can be used independently.
|
|
175
163
|
if (spender !== generalAdapter1)
|
|
@@ -179,7 +167,14 @@ const encodeOperation = (operation, dataBefore, supportsSignature = true, index
|
|
|
179
167
|
const action = isDai
|
|
180
168
|
? {
|
|
181
169
|
type: "permitDai",
|
|
182
|
-
args: [
|
|
170
|
+
args: [
|
|
171
|
+
sender,
|
|
172
|
+
nonce,
|
|
173
|
+
deadline,
|
|
174
|
+
amount > 0n,
|
|
175
|
+
null,
|
|
176
|
+
operation.skipRevert,
|
|
177
|
+
],
|
|
183
178
|
}
|
|
184
179
|
: {
|
|
185
180
|
type: "permit",
|
|
@@ -250,7 +245,7 @@ const encodeOperation = (operation, dataBefore, supportsSignature = true, index
|
|
|
250
245
|
// Native token cannot be permitted.
|
|
251
246
|
if (operation.address === blue_sdk_1.NATIVE_ADDRESS)
|
|
252
247
|
break;
|
|
253
|
-
const { amount, expiration, nonce } = operation.args;
|
|
248
|
+
const { amount, expiration, nonce, deadline = dataBefore.block.timestamp + morpho_ts_1.Time.s.from.h(2n), } = operation.args;
|
|
254
249
|
if (supportsSignature) {
|
|
255
250
|
const action = {
|
|
256
251
|
type: "approve2",
|
package/lib/operations.js
CHANGED
|
@@ -358,7 +358,7 @@ const finalizeBundle = (operations, startData, receiver, unwrapTokens = new Set(
|
|
|
358
358
|
const nbOperations = operations.length;
|
|
359
359
|
if (nbOperations === 0)
|
|
360
360
|
return operations;
|
|
361
|
-
const { bundler3: { bundler3, generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(startData.chainId);
|
|
361
|
+
const { bundler3: { bundler3, generalAdapter1 }, dai, } = (0, blue_sdk_1.getChainAddresses)(startData.chainId);
|
|
362
362
|
if ((0, viem_1.isAddressEqual)(receiver, bundler3) ||
|
|
363
363
|
(0, viem_1.isAddressEqual)(receiver, generalAdapter1))
|
|
364
364
|
throw Error(`receiver is bundler`);
|
|
@@ -541,9 +541,25 @@ const finalizeBundle = (operations, startData, receiver, unwrapTokens = new Set(
|
|
|
541
541
|
});
|
|
542
542
|
// Simulate without slippage to skim the bundler of all possible surplus of shares & assets.
|
|
543
543
|
steps = (0, exports.simulateBundlerOperations)(operations, startData, { slippage: 0n });
|
|
544
|
+
const lastStep = (0, morpho_ts_1.getLast)(steps);
|
|
545
|
+
const daiPermit = dai != null
|
|
546
|
+
? operations.find(
|
|
547
|
+
// There should exist only one dai permit operation in the bundle thanks to the first optimization step.
|
|
548
|
+
(operation) => operation.type === "Erc20_Permit" && operation.address === dai)
|
|
549
|
+
: undefined;
|
|
550
|
+
// If the bundle approves dai, reset the dai allowance at the end of the bundle.
|
|
551
|
+
if (daiPermit != null)
|
|
552
|
+
operations.push({
|
|
553
|
+
...daiPermit,
|
|
554
|
+
args: {
|
|
555
|
+
amount: 0n,
|
|
556
|
+
spender: daiPermit.args.spender,
|
|
557
|
+
nonce: daiPermit.args.nonce + 1n,
|
|
558
|
+
},
|
|
559
|
+
});
|
|
544
560
|
// Unwrap requested remaining wrapped tokens.
|
|
545
561
|
const unwraps = [];
|
|
546
|
-
const endBundlerTokenData =
|
|
562
|
+
const endBundlerTokenData = lastStep.holdings[generalAdapter1] ?? {};
|
|
547
563
|
unwrapTokens.forEach((wrappedToken) => {
|
|
548
564
|
const remaining = endBundlerTokenData[wrappedToken]?.balance ?? 0n;
|
|
549
565
|
if (remaining <= 5n)
|
|
@@ -635,13 +651,11 @@ const simulateRequiredTokenAmounts = (operations, data) => {
|
|
|
635
651
|
});
|
|
636
652
|
// Simulate the operations to calculate the required token amounts.
|
|
637
653
|
const steps = (0, simulation_sdk_1.simulateOperations)(operations, virtualBundlerData);
|
|
638
|
-
return (0, morpho_ts_1.entries)((0, morpho_ts_1.getLast)(steps).holdings[generalAdapter1] ?? {})
|
|
639
|
-
.map(([token, holding]) => ({
|
|
640
|
-
token,
|
|
654
|
+
return ((0, morpho_ts_1.entries)((0, morpho_ts_1.getLast)(steps).holdings[generalAdapter1] ?? {})
|
|
641
655
|
// Safe cast because the holding was transformed to a VirtualHolding.
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
.filter(({ required }) => required > 0n);
|
|
656
|
+
.filter((entry) => entry[1] != null)
|
|
657
|
+
.map(([token, { required }]) => ({ token, required }))
|
|
658
|
+
.filter(({ required }) => required > 0n));
|
|
645
659
|
};
|
|
646
660
|
exports.simulateRequiredTokenAmounts = simulateRequiredTokenAmounts;
|
|
647
661
|
const getSimulatedBundlerOperation = (operation, { slippage } = {}) => {
|
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": "3.2.0-next.
|
|
4
|
+
"version": "3.2.0-next.3",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Rubilmax <rmilon@gmail.com>"
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
],
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"viem": "^2.0.0",
|
|
22
|
-
"@morpho-org/blue-sdk": "^
|
|
23
|
-
"@morpho-org/blue-sdk-viem": "^3.
|
|
24
|
-
"@morpho-org/
|
|
25
|
-
"@morpho-org/
|
|
22
|
+
"@morpho-org/blue-sdk": "^4.0.0-next.3",
|
|
23
|
+
"@morpho-org/blue-sdk-viem": "^3.1.1",
|
|
24
|
+
"@morpho-org/morpho-ts": "^2.3.0",
|
|
25
|
+
"@morpho-org/simulation-sdk": "^3.1.0-next.2"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@tanstack/query-core": "^5.62.16",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
"typescript": "^5.7.2",
|
|
35
35
|
"viem": "^2.23.0",
|
|
36
36
|
"vitest": "^3.0.5",
|
|
37
|
-
"@morpho-org/blue-sdk-viem": "^3.
|
|
37
|
+
"@morpho-org/blue-sdk-viem": "^3.1.1",
|
|
38
|
+
"@morpho-org/blue-sdk": "^4.0.0-next.3",
|
|
38
39
|
"@morpho-org/morpho-test": "^2.3.0",
|
|
39
40
|
"@morpho-org/morpho-ts": "^2.3.0",
|
|
40
|
-
"@morpho-org/simulation-sdk": "^3.0.
|
|
41
|
-
"@morpho-org/blue-sdk": "^3.0.6",
|
|
41
|
+
"@morpho-org/simulation-sdk": "^3.1.0-next.2",
|
|
42
42
|
"@morpho-org/simulation-sdk-wagmi": "^3.0.1",
|
|
43
43
|
"@morpho-org/test": "^2.1.3",
|
|
44
44
|
"@morpho-org/test-wagmi": "^2.0.4"
|