@morpho-org/bundler-sdk-viem 2.0.0-next.25 → 2.0.0-next.26
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/operations.d.ts +4 -0
- package/lib/operations.js +31 -5
- package/package.json +7 -7
package/lib/operations.d.ts
CHANGED
|
@@ -45,6 +45,10 @@ export declare const populateBundle: (inputOperations: InputBundlerOperation[],
|
|
|
45
45
|
operations: BundlerOperation[];
|
|
46
46
|
steps: SimulationResult;
|
|
47
47
|
};
|
|
48
|
+
export declare const simulateRequiredTokenAmounts: (operations: Operation[], data: MaybeDraft<SimulationState>) => {
|
|
49
|
+
token: `0x${string}`;
|
|
50
|
+
required: bigint;
|
|
51
|
+
}[];
|
|
48
52
|
export declare const getSimulatedBundlerOperation: (operation: BundlerOperation, { slippage }?: {
|
|
49
53
|
slippage?: bigint;
|
|
50
54
|
}) => Operation;
|
package/lib/operations.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DEFAULT_SLIPPAGE_TOLERANCE, DEFAULT_SUPPLY_TARGET_UTILIZATION, MarketUtils, MathLib, NATIVE_ADDRESS, erc20WrapperTokens, getChainAddresses, getUnwrappedToken, permissionedBackedTokens, permissionedWrapperTokens, } from "@morpho-org/blue-sdk";
|
|
2
|
-
import { entries, getLast, getValue, keys } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { handleOperation, handleOperations, produceImmutable, simulateOperation, } from "@morpho-org/simulation-sdk";
|
|
2
|
+
import { bigIntComparator, entries, getLast, getValue, keys, } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { handleOperation, handleOperations, produceImmutable, simulateOperation, simulateOperations, } from "@morpho-org/simulation-sdk";
|
|
4
4
|
import { maxUint256 } from "viem";
|
|
5
5
|
import { BundlerErrors } from "./errors.js";
|
|
6
6
|
export const populateInputTransfer = ({ address, args: { amount, from } }, data, { hasSimplePermit = false } = {}) => {
|
|
@@ -268,7 +268,7 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
268
268
|
},
|
|
269
269
|
};
|
|
270
270
|
// Operations with callbacks are populated recursively as a side-effect of the simulation, within the callback itself.
|
|
271
|
-
let
|
|
271
|
+
let requiredTokenAmounts = simulateRequiredTokenAmounts(operations.concat([simulatedOperation]), data);
|
|
272
272
|
const allOperations = operations.concat([
|
|
273
273
|
mainOperation,
|
|
274
274
|
]);
|
|
@@ -283,9 +283,9 @@ export const populateSubBundle = (inputOperation, data, options = {}) => {
|
|
|
283
283
|
return allOperations;
|
|
284
284
|
}
|
|
285
285
|
const requirementOperations = getRequirementOperations?.(requiredTokenAmounts) ?? [];
|
|
286
|
-
|
|
286
|
+
requiredTokenAmounts = simulateRequiredTokenAmounts(requirementOperations
|
|
287
287
|
.concat(allOperations)
|
|
288
|
-
.map((operation) => getSimulatedBundlerOperation(operation)),
|
|
288
|
+
.map((operation) => getSimulatedBundlerOperation(operation)), data);
|
|
289
289
|
// Append required input transfers.
|
|
290
290
|
requiredTokenAmounts.forEach(({ token, required }) => {
|
|
291
291
|
requirementOperations.push(...populateInputTransfer({
|
|
@@ -562,6 +562,32 @@ export const populateBundle = (inputOperations, data, options) => {
|
|
|
562
562
|
});
|
|
563
563
|
return { operations, steps };
|
|
564
564
|
};
|
|
565
|
+
export const simulateRequiredTokenAmounts = (operations, data) => {
|
|
566
|
+
const { bundler } = getChainAddresses(data.chainId);
|
|
567
|
+
const virtualBundlerData = produceImmutable(data, (draft) => {
|
|
568
|
+
Object.values(draft.holdings[bundler] ?? {}).forEach((bundlerTokenData) => {
|
|
569
|
+
// Virtual balance to calculate the amount required.
|
|
570
|
+
bundlerTokenData.balance += MathLib.MAX_UINT_160;
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
const steps = simulateOperations(operations, virtualBundlerData);
|
|
574
|
+
const bundlerTokenDiffs = keys(virtualBundlerData.holdings[bundler]).map((token) => ({
|
|
575
|
+
token,
|
|
576
|
+
required: steps
|
|
577
|
+
.map((step) =>
|
|
578
|
+
// When recursively simulated, this will cause tokens to be required at the highest recursion level.
|
|
579
|
+
// For example: supplyCollateral(x, supplyCollateral(y, borrow(z))) [provided x, y, z < MAX_UINT_160]
|
|
580
|
+
// | | |=> MAX_UINT_160 - (3 * MAX_UINT_160 + z) < 0
|
|
581
|
+
// | |=> MAX_UINT_160 - (2 * MAX_UINT_160 - y) < 0
|
|
582
|
+
// |=> MAX_UINT_160 - (MAX_UINT_160 - y - x) > 0
|
|
583
|
+
MathLib.MAX_UINT_160 -
|
|
584
|
+
(step.holdings[bundler]?.[token]?.balance ?? 0n))
|
|
585
|
+
.sort(bigIntComparator((required) => required,
|
|
586
|
+
// Take the highest required amount among all operations.
|
|
587
|
+
"desc"))[0],
|
|
588
|
+
}));
|
|
589
|
+
return bundlerTokenDiffs.filter(({ required }) => required > 0n);
|
|
590
|
+
};
|
|
565
591
|
export const getSimulatedBundlerOperation = (operation, { slippage } = {}) => {
|
|
566
592
|
const callback = getValue(operation.args, "callback");
|
|
567
593
|
const simulatedOperation = {
|
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.0.0-next.
|
|
4
|
+
"version": "2.0.0-next.26",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Rubilmax <rmilon@gmail.com>"
|
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"viem": "^2.0.0",
|
|
23
23
|
"@morpho-org/blue-sdk": "^2.0.0-next.24",
|
|
24
|
-
"@morpho-org/simulation-sdk": "^2.0.0-next.
|
|
25
|
-
"@morpho-org/
|
|
26
|
-
"@morpho-org/
|
|
24
|
+
"@morpho-org/simulation-sdk": "^2.0.0-next.22",
|
|
25
|
+
"@morpho-org/morpho-ts": "^2.0.0-next.12",
|
|
26
|
+
"@morpho-org/blue-sdk-viem": "^2.0.0-next.22"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@tanstack/query-core": "^5.59.13",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"@morpho-org/blue-sdk-viem": "^2.0.0-next.22",
|
|
40
40
|
"@morpho-org/morpho-ts": "^2.0.0-next.12",
|
|
41
41
|
"@morpho-org/morpho-test": "^2.0.0-next.14",
|
|
42
|
-
"@morpho-org/simulation-sdk": "^2.0.0-next.
|
|
42
|
+
"@morpho-org/simulation-sdk": "^2.0.0-next.22",
|
|
43
|
+
"@morpho-org/simulation-sdk-wagmi": "^2.0.0-next.21",
|
|
43
44
|
"@morpho-org/test": "^2.0.0-next.20",
|
|
44
|
-
"@morpho-org/test-wagmi": "^2.0.0-next.20"
|
|
45
|
-
"@morpho-org/simulation-sdk-wagmi": "^2.0.0-next.21"
|
|
45
|
+
"@morpho-org/test-wagmi": "^2.0.0-next.20"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"prepublish": "$npm_execpath build",
|