@morpho-org/consumer-sdk 0.2.0 → 0.4.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.
Files changed (48) hide show
  1. package/README.md +178 -44
  2. package/lib/actions/index.d.ts +1 -0
  3. package/lib/actions/index.js +1 -0
  4. package/lib/actions/requirements/getRequirements.js +3 -0
  5. package/lib/actions/requirements/getRequirementsAction.d.ts +2 -2
  6. package/lib/actions/requirements/getRequirementsAction.js +7 -7
  7. package/lib/actions/vaultV1/deposit.d.ts +41 -0
  8. package/lib/actions/vaultV1/deposit.js +116 -0
  9. package/lib/actions/vaultV1/index.d.ts +3 -0
  10. package/lib/actions/vaultV1/index.js +19 -0
  11. package/lib/actions/vaultV1/redeem.d.ts +29 -0
  12. package/lib/actions/vaultV1/redeem.js +48 -0
  13. package/lib/actions/vaultV1/withdraw.d.ts +29 -0
  14. package/lib/actions/vaultV1/withdraw.js +48 -0
  15. package/lib/actions/vaultV2/deposit.d.ts +20 -25
  16. package/lib/actions/vaultV2/deposit.js +78 -40
  17. package/lib/actions/vaultV2/forceRedeem.d.ts +48 -0
  18. package/lib/actions/vaultV2/forceRedeem.js +85 -0
  19. package/lib/actions/vaultV2/forceWithdraw.d.ts +40 -0
  20. package/lib/actions/vaultV2/forceWithdraw.js +81 -0
  21. package/lib/actions/vaultV2/index.d.ts +2 -0
  22. package/lib/actions/vaultV2/index.js +2 -0
  23. package/lib/actions/vaultV2/redeem.js +2 -2
  24. package/lib/actions/vaultV2/withdraw.d.ts +3 -3
  25. package/lib/actions/vaultV2/withdraw.js +6 -6
  26. package/lib/client/morphoClient.d.ts +2 -1
  27. package/lib/client/morphoClient.js +3 -0
  28. package/lib/client/morphoViemExtension.d.ts +12 -9
  29. package/lib/client/morphoViemExtension.js +12 -9
  30. package/lib/entities/index.d.ts +1 -0
  31. package/lib/entities/index.js +1 -0
  32. package/lib/entities/vaultV1/index.d.ts +1 -0
  33. package/lib/entities/vaultV1/index.js +17 -0
  34. package/lib/entities/vaultV1/vaultV1.d.ts +95 -0
  35. package/lib/entities/vaultV1/vaultV1.js +125 -0
  36. package/lib/entities/vaultV2/vaultV2.d.ts +94 -18
  37. package/lib/entities/vaultV2/vaultV2.js +76 -15
  38. package/lib/helpers/encodeDeallocation.d.ts +10 -0
  39. package/lib/helpers/encodeDeallocation.js +37 -0
  40. package/lib/types/action.d.ts +59 -3
  41. package/lib/types/client.d.ts +2 -1
  42. package/lib/types/deallocation.d.ts +15 -0
  43. package/lib/types/deallocation.js +2 -0
  44. package/lib/types/error.d.ts +28 -4
  45. package/lib/types/error.js +59 -11
  46. package/lib/types/index.d.ts +1 -0
  47. package/lib/types/index.js +1 -0
  48. package/package.json +2 -2
@@ -0,0 +1,10 @@
1
+ import { type Address, type Hex } from "viem";
2
+ import { type Deallocation } from "../types";
3
+ /**
4
+ * Encodes a single `forceDeallocate` call as ABI-encoded calldata.
5
+ *
6
+ * @param deallocation - A deallocation entry.
7
+ * @param onBehalf - The address from which the penalty is taken (share owner).
8
+ * @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
9
+ */
10
+ export declare function encodeForceDeallocateCall(deallocation: Deallocation, onBehalf: Address): Hex;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeForceDeallocateCall = encodeForceDeallocateCall;
4
+ const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
+ const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
+ const viem_1 = require("viem");
7
+ const types_1 = require("../types");
8
+ /**
9
+ * Encodes the adapter-specific `data` bytes for a `forceDeallocate` call.
10
+ *
11
+ * @param deallocation - A deallocation entry.
12
+ * @returns The ABI-encoded bytes to pass as `data` to VaultV2.forceDeallocate.
13
+ */
14
+ function encodeDeallocateData(deallocation) {
15
+ if (deallocation.marketParams) {
16
+ return (0, viem_1.encodeAbiParameters)([blue_sdk_1.marketParamsAbi], [deallocation.marketParams]);
17
+ }
18
+ return "0x";
19
+ }
20
+ /**
21
+ * Encodes a single `forceDeallocate` call as ABI-encoded calldata.
22
+ *
23
+ * @param deallocation - A deallocation entry.
24
+ * @param onBehalf - The address from which the penalty is taken (share owner).
25
+ * @returns The ABI-encoded calldata for `VaultV2.forceDeallocate`.
26
+ */
27
+ function encodeForceDeallocateCall(deallocation, onBehalf) {
28
+ if (deallocation.amount <= 0n) {
29
+ throw new types_1.NonPositiveAssetAmountError(deallocation.adapter);
30
+ }
31
+ const data = encodeDeallocateData(deallocation);
32
+ return (0, viem_1.encodeFunctionData)({
33
+ abi: blue_sdk_viem_1.vaultV2Abi,
34
+ functionName: "forceDeallocate",
35
+ args: [deallocation.adapter, data, deallocation.amount, onBehalf],
36
+ });
37
+ }
@@ -1,4 +1,5 @@
1
1
  import type { Address, Client, Hex } from "viem";
2
+ import type { Deallocation } from "./deallocation";
2
3
  export interface BaseAction<TType extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> {
3
4
  readonly type: TType;
4
5
  readonly args: TArgs;
@@ -13,14 +14,15 @@ export interface ERC20PermitAction {
13
14
  }
14
15
  export interface VaultV2DepositAction extends BaseAction<"vaultV2Deposit", {
15
16
  vault: Address;
16
- assets: bigint;
17
+ amount: bigint;
17
18
  maxSharePrice: bigint;
18
19
  recipient: Address;
20
+ nativeAmount?: bigint;
19
21
  }> {
20
22
  }
21
23
  export interface VaultV2WithdrawAction extends BaseAction<"vaultV2Withdraw", {
22
24
  vault: Address;
23
- assets: bigint;
25
+ amount: bigint;
24
26
  recipient: Address;
25
27
  }> {
26
28
  }
@@ -30,13 +32,67 @@ export interface VaultV2RedeemAction extends BaseAction<"vaultV2Redeem", {
30
32
  recipient: Address;
31
33
  }> {
32
34
  }
33
- export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction;
35
+ export interface VaultV2ForceWithdrawAction extends BaseAction<"vaultV2ForceWithdraw", {
36
+ vault: Address;
37
+ deallocations: readonly Deallocation[];
38
+ withdraw: {
39
+ amount: bigint;
40
+ recipient: Address;
41
+ };
42
+ onBehalf: Address;
43
+ }> {
44
+ }
45
+ export interface VaultV2ForceRedeemAction extends BaseAction<"vaultV2ForceRedeem", {
46
+ vault: Address;
47
+ deallocations: readonly Deallocation[];
48
+ redeem: {
49
+ shares: bigint;
50
+ recipient: Address;
51
+ };
52
+ onBehalf: Address;
53
+ }> {
54
+ }
55
+ export interface VaultV1DepositAction extends BaseAction<"vaultV1Deposit", {
56
+ vault: Address;
57
+ amount: bigint;
58
+ maxSharePrice: bigint;
59
+ recipient: Address;
60
+ nativeAmount?: bigint;
61
+ }> {
62
+ }
63
+ export interface VaultV1WithdrawAction extends BaseAction<"vaultV1Withdraw", {
64
+ vault: Address;
65
+ amount: bigint;
66
+ recipient: Address;
67
+ }> {
68
+ }
69
+ export interface VaultV1RedeemAction extends BaseAction<"vaultV1Redeem", {
70
+ vault: Address;
71
+ shares: bigint;
72
+ recipient: Address;
73
+ }> {
74
+ }
75
+ export type TransactionAction = ERC20ApprovalAction | VaultV2DepositAction | VaultV2WithdrawAction | VaultV2RedeemAction | VaultV2ForceWithdrawAction | VaultV2ForceRedeemAction | VaultV1DepositAction | VaultV1WithdrawAction | VaultV1RedeemAction;
34
76
  export interface Transaction<TAction extends BaseAction = TransactionAction> {
35
77
  readonly to: Address;
36
78
  readonly value: bigint;
37
79
  readonly data: Hex;
38
80
  readonly action: TAction;
39
81
  }
82
+ /**
83
+ * Enforces that at least one deposit amount source is provided.
84
+ *
85
+ * - `amount` alone: standard ERC20 deposit.
86
+ * - `nativeAmount` alone: pure native-wrap deposit (vault asset must be wNative).
87
+ * - Both: mixed deposit (ERC20 transfer + native wrap).
88
+ */
89
+ export type DepositAmountArgs = {
90
+ amount: bigint;
91
+ nativeAmount?: bigint;
92
+ } | {
93
+ nativeAmount: bigint;
94
+ amount?: bigint;
95
+ };
40
96
  export interface PermitArgs {
41
97
  owner: Address;
42
98
  nonce: bigint;
@@ -1,5 +1,5 @@
1
1
  import type { Address, Client } from "viem";
2
- import type { VaultV2Actions } from "../entities";
2
+ import type { VaultV1Actions, VaultV2Actions } from "../entities";
3
3
  import type { Metadata } from "./index";
4
4
  export interface MorphoClientType {
5
5
  readonly viemClient: Client;
@@ -8,5 +8,6 @@ export interface MorphoClientType {
8
8
  readonly supportDeployless?: boolean;
9
9
  readonly metadata?: Metadata;
10
10
  };
11
+ vaultV1: (vault: Address, chainId: number) => VaultV1Actions;
11
12
  vaultV2: (vault: Address, chainId: number) => VaultV2Actions;
12
13
  }
@@ -0,0 +1,15 @@
1
+ import type { MarketParams } from "@morpho-org/blue-sdk";
2
+ import type { Address } from "viem";
3
+ /**
4
+ * A single deallocation entry for a `forceDeallocate` call.
5
+ *
6
+ * - When `marketParams` is provided, the adapter is treated as a Morpho Market V1 adapter
7
+ * and `data` is ABI-encoded from the given `MarketParams`.
8
+ * - When `marketParams` is omitted, empty bytes are passed as `data` (suitable for adapters
9
+ * such as Vault V1 that do not require market identification).
10
+ */
11
+ export interface Deallocation {
12
+ readonly adapter: Address;
13
+ readonly marketParams?: MarketParams;
14
+ readonly amount: bigint;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,11 +1,11 @@
1
1
  import type { Address } from "viem";
2
- export declare class ZeroAssetAmountError extends Error {
3
- constructor(asset: Address);
2
+ export declare class NonPositiveAssetAmountError extends Error {
3
+ constructor(origin: Address);
4
4
  }
5
- export declare class ZeroSharesAmountError extends Error {
5
+ export declare class NonPositiveSharesAmountError extends Error {
6
6
  constructor(vault: Address);
7
7
  }
8
- export declare class ZeroMaxSharePriceError extends Error {
8
+ export declare class NonPositiveMaxSharePriceError extends Error {
9
9
  constructor(vault: Address);
10
10
  }
11
11
  export declare class AddressMismatchError extends Error {
@@ -20,12 +20,36 @@ export declare class MissingClientPropertyError extends Error {
20
20
  export declare class ApprovalAmountLessThanSpendAmountError extends Error {
21
21
  constructor();
22
22
  }
23
+ export declare class NegativeSlippageToleranceError extends Error {
24
+ constructor(slippageTolerance: bigint);
25
+ }
23
26
  export declare class ExcessiveSlippageToleranceError extends Error {
24
27
  constructor(slippageTolerance: bigint);
25
28
  }
29
+ export declare class EmptyDeallocationsError extends Error {
30
+ constructor(vault: Address);
31
+ }
26
32
  export declare class DepositAmountMismatchError extends Error {
27
33
  constructor(depositAmount: bigint, signatureAmount: bigint);
28
34
  }
29
35
  export declare class DepositAssetMismatchError extends Error {
30
36
  constructor(depositAsset: Address, signatureAsset: Address);
31
37
  }
38
+ export declare class DeallocationsExceedWithdrawError extends Error {
39
+ constructor(vault: Address, withdrawAmount: bigint, totalDeallocated: bigint);
40
+ }
41
+ export declare class NativeAmountOnNonWNativeVaultError extends Error {
42
+ constructor(vaultAsset: Address, wNative: Address);
43
+ }
44
+ export declare class ChainWNativeMissingError extends Error {
45
+ constructor(chainId: number);
46
+ }
47
+ export declare class NegativeNativeAmountError extends Error {
48
+ constructor(nativeAmount: bigint);
49
+ }
50
+ export declare class ZeroDepositAmountError extends Error {
51
+ constructor(vault: Address);
52
+ }
53
+ export declare class VaultAddressMismatchError extends Error {
54
+ constructor(vaultAddress: Address, argsVaultAddress: Address);
55
+ }
@@ -1,24 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DepositAssetMismatchError = exports.DepositAmountMismatchError = exports.ExcessiveSlippageToleranceError = exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.ZeroMaxSharePriceError = exports.ZeroSharesAmountError = exports.ZeroAssetAmountError = void 0;
4
- class ZeroAssetAmountError extends Error {
5
- constructor(asset) {
6
- super(`Asset amount cannot be zero for address: ${asset}`);
3
+ exports.VaultAddressMismatchError = exports.ZeroDepositAmountError = exports.NegativeNativeAmountError = exports.ChainWNativeMissingError = exports.NativeAmountOnNonWNativeVaultError = exports.DeallocationsExceedWithdrawError = exports.DepositAssetMismatchError = exports.DepositAmountMismatchError = exports.EmptyDeallocationsError = exports.ExcessiveSlippageToleranceError = exports.NegativeSlippageToleranceError = exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.NonPositiveMaxSharePriceError = exports.NonPositiveSharesAmountError = exports.NonPositiveAssetAmountError = void 0;
4
+ class NonPositiveAssetAmountError extends Error {
5
+ constructor(origin) {
6
+ super(`Asset amount must be positive for address ${origin}`);
7
7
  }
8
8
  }
9
- exports.ZeroAssetAmountError = ZeroAssetAmountError;
10
- class ZeroSharesAmountError extends Error {
9
+ exports.NonPositiveAssetAmountError = NonPositiveAssetAmountError;
10
+ class NonPositiveSharesAmountError extends Error {
11
11
  constructor(vault) {
12
- super(`Shares amount cannot be zero for address: ${vault}`);
12
+ super(`Shares amount must be positive for address: ${vault}`);
13
13
  }
14
14
  }
15
- exports.ZeroSharesAmountError = ZeroSharesAmountError;
16
- class ZeroMaxSharePriceError extends Error {
15
+ exports.NonPositiveSharesAmountError = NonPositiveSharesAmountError;
16
+ class NonPositiveMaxSharePriceError extends Error {
17
17
  constructor(vault) {
18
- super(`Max share price cannot be zero for vault: ${vault}`);
18
+ super(`Max share price must be positive for vault: ${vault}`);
19
19
  }
20
20
  }
21
- exports.ZeroMaxSharePriceError = ZeroMaxSharePriceError;
21
+ exports.NonPositiveMaxSharePriceError = NonPositiveMaxSharePriceError;
22
22
  class AddressMismatchError extends Error {
23
23
  constructor(clientAddress, argsAddress) {
24
24
  super(`Address mismatch between client: ${clientAddress} and args: ${argsAddress}`);
@@ -43,12 +43,24 @@ class ApprovalAmountLessThanSpendAmountError extends Error {
43
43
  }
44
44
  }
45
45
  exports.ApprovalAmountLessThanSpendAmountError = ApprovalAmountLessThanSpendAmountError;
46
+ class NegativeSlippageToleranceError extends Error {
47
+ constructor(slippageTolerance) {
48
+ super(`Slippage tolerance ${slippageTolerance} must not be negative`);
49
+ }
50
+ }
51
+ exports.NegativeSlippageToleranceError = NegativeSlippageToleranceError;
46
52
  class ExcessiveSlippageToleranceError extends Error {
47
53
  constructor(slippageTolerance) {
48
54
  super(`Slippage tolerance ${slippageTolerance} exceeds maximum allowed (10%)`);
49
55
  }
50
56
  }
51
57
  exports.ExcessiveSlippageToleranceError = ExcessiveSlippageToleranceError;
58
+ class EmptyDeallocationsError extends Error {
59
+ constructor(vault) {
60
+ super(`Deallocations list cannot be empty for vault: ${vault}`);
61
+ }
62
+ }
63
+ exports.EmptyDeallocationsError = EmptyDeallocationsError;
52
64
  class DepositAmountMismatchError extends Error {
53
65
  constructor(depositAmount, signatureAmount) {
54
66
  super(`Deposit amount ${depositAmount} does not match requirement signature amount ${signatureAmount}`);
@@ -61,3 +73,39 @@ class DepositAssetMismatchError extends Error {
61
73
  }
62
74
  }
63
75
  exports.DepositAssetMismatchError = DepositAssetMismatchError;
76
+ class DeallocationsExceedWithdrawError extends Error {
77
+ constructor(vault, withdrawAmount, totalDeallocated) {
78
+ super(`Total deallocated amount (${totalDeallocated}) exceed withdraw amount (${withdrawAmount}) for vault: ${vault}`);
79
+ }
80
+ }
81
+ exports.DeallocationsExceedWithdrawError = DeallocationsExceedWithdrawError;
82
+ class NativeAmountOnNonWNativeVaultError extends Error {
83
+ constructor(vaultAsset, wNative) {
84
+ super(`Cannot use nativeAmount: vault asset ${vaultAsset} is not the wrapped native token ${wNative}`);
85
+ }
86
+ }
87
+ exports.NativeAmountOnNonWNativeVaultError = NativeAmountOnNonWNativeVaultError;
88
+ class ChainWNativeMissingError extends Error {
89
+ constructor(chainId) {
90
+ super(`Chain ${chainId} does not have a configured wrapped native token (wNative)`);
91
+ }
92
+ }
93
+ exports.ChainWNativeMissingError = ChainWNativeMissingError;
94
+ class NegativeNativeAmountError extends Error {
95
+ constructor(nativeAmount) {
96
+ super(`Native amount must not be negative, got ${nativeAmount}`);
97
+ }
98
+ }
99
+ exports.NegativeNativeAmountError = NegativeNativeAmountError;
100
+ class ZeroDepositAmountError extends Error {
101
+ constructor(vault) {
102
+ super(`Total deposit amount must be positive for vault: ${vault}. Both amount and nativeAmount are zero.`);
103
+ }
104
+ }
105
+ exports.ZeroDepositAmountError = ZeroDepositAmountError;
106
+ class VaultAddressMismatchError extends Error {
107
+ constructor(vaultAddress, argsVaultAddress) {
108
+ super(`Vault address mismatch between vault: ${vaultAddress} and args: ${argsVaultAddress}`);
109
+ }
110
+ }
111
+ exports.VaultAddressMismatchError = VaultAddressMismatchError;
@@ -1,5 +1,6 @@
1
1
  export * from "./action";
2
2
  export * from "./client";
3
+ export * from "./deallocation";
3
4
  export * from "./entity";
4
5
  export * from "./error";
5
6
  export * from "./metadata";
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./action"), exports);
18
18
  __exportStar(require("./client"), exports);
19
+ __exportStar(require("./deallocation"), exports);
19
20
  __exportStar(require("./entity"), exports);
20
21
  __exportStar(require("./error"), exports);
21
22
  __exportStar(require("./metadata"), exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@morpho-org/consumer-sdk",
3
3
  "description": "Abstraction layer for Morpho's complexity.",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "author": "Morpho Association <contact@morpho.org>",
6
6
  "contributors": [
7
7
  "Foulks-Plb <https://x.com/FoulkPlb>"
@@ -20,7 +20,7 @@
20
20
  "devDependencies": {
21
21
  "@biomejs/biome": "2.3.3",
22
22
  "@changesets/cli": "^2.29.8",
23
- "@morpho-org/test": "2.6.7",
23
+ "@morpho-org/test": "2.7.0",
24
24
  "@types/node": "^24.9.1",
25
25
  "@vitest/coverage-v8": "3.2.4",
26
26
  "@vitest/ui": "3.2.4",