@morpho-org/consumer-sdk 0.0.2 → 0.1.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/README.md +6 -2
- package/lib/actions/requirements/{encodeErc20Approval.d.ts → encode/encodeErc20Approval.d.ts} +1 -1
- package/lib/actions/requirements/encode/encodeErc20Permit.d.ts +11 -0
- package/lib/actions/requirements/encode/encodeErc20Permit.js +86 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.d.ts +12 -0
- package/lib/actions/requirements/encode/encodeErc20Permit2.js +67 -0
- package/lib/actions/requirements/encode/index.d.ts +3 -0
- package/lib/{sdk → actions/requirements/encode}/index.js +3 -10
- package/lib/actions/requirements/getRequirements.d.ts +20 -2
- package/lib/actions/requirements/getRequirements.js +54 -21
- package/lib/actions/requirements/getRequirementsAction.d.ts +14 -0
- package/lib/actions/requirements/getRequirementsAction.js +91 -0
- package/lib/actions/requirements/getRequirementsApproval.d.ts +31 -0
- package/lib/actions/requirements/getRequirementsApproval.js +51 -0
- package/lib/actions/requirements/getRequirementsPermit.d.ts +25 -0
- package/lib/actions/requirements/getRequirementsPermit.js +37 -0
- package/lib/actions/requirements/getRequirementsPermit2.d.ts +35 -0
- package/lib/actions/requirements/getRequirementsPermit2.js +56 -0
- package/lib/actions/requirements/index.d.ts +4 -1
- package/lib/actions/requirements/index.js +4 -1
- package/lib/actions/vaultV2/deposit.d.ts +6 -2
- package/lib/actions/vaultV2/deposit.js +18 -10
- package/lib/client/morphoClient.d.ts +12 -2
- package/lib/client/morphoClient.js +8 -3
- package/lib/client/morphoViemExtension.d.ts +5 -2
- package/lib/client/morphoViemExtension.js +3 -3
- package/lib/entities/vaultV2/vaultV2.d.ts +5 -5
- package/lib/entities/vaultV2/vaultV2.js +14 -14
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -3
- package/lib/types/action.d.ts +44 -1
- package/lib/types/action.js +13 -0
- package/lib/types/client.d.ts +4 -1
- package/lib/types/error.d.ts +9 -6
- package/lib/types/error.js +19 -13
- package/package.json +10 -9
- package/lib/sdk/index.d.ts +0 -10
- /package/lib/actions/requirements/{encodeErc20Approval.js → encode/encodeErc20Approval.js} +0 -0
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Consumer sdk
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> ⚠️ **Experimental package**: This SDK is currently in experimental phase.
|
|
2
6
|
|
|
3
7
|
> **The abstraction layer that simplifies Morpho protocol**
|
|
4
8
|
|
|
5
|
-
## ✨ How to use it? (
|
|
9
|
+
## ✨ How to use it? (two ways to build transactions)
|
|
6
10
|
|
|
7
11
|
### 1️⃣ **With MorphoClient**
|
|
8
12
|
|
package/lib/actions/requirements/{encodeErc20Approval.d.ts → encode/encodeErc20Approval.d.ts}
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type { ERC20ApprovalAction, Transaction } from "
|
|
2
|
+
import type { ERC20ApprovalAction, Transaction } from "../../../types";
|
|
3
3
|
interface EncodeErc20ApprovalParams {
|
|
4
4
|
token: Address;
|
|
5
5
|
spender: Address;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Requirement } from "../../../types";
|
|
3
|
+
interface EncodeErc20PermitParams {
|
|
4
|
+
token: Address;
|
|
5
|
+
spender: Address;
|
|
6
|
+
amount: bigint;
|
|
7
|
+
chainId: number;
|
|
8
|
+
nonce: bigint;
|
|
9
|
+
}
|
|
10
|
+
export declare const encodeErc20Permit: (params: EncodeErc20PermitParams) => Requirement;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeErc20Permit = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const actions_1 = require("viem/actions");
|
|
9
|
+
const types_1 = require("../../../types");
|
|
10
|
+
const encodeErc20Permit = (params) => {
|
|
11
|
+
const { token, spender, amount, chainId, nonce } = params;
|
|
12
|
+
const now = morpho_ts_1.Time.timestamp();
|
|
13
|
+
const deadline = now + morpho_ts_1.Time.s.from.h(2n);
|
|
14
|
+
const action = {
|
|
15
|
+
type: "permit",
|
|
16
|
+
args: {
|
|
17
|
+
spender,
|
|
18
|
+
amount,
|
|
19
|
+
deadline,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
action,
|
|
24
|
+
async sign(client, userAddress) {
|
|
25
|
+
if (!client.account?.address) {
|
|
26
|
+
throw new types_1.MissingClientPropertyError("client.account.address");
|
|
27
|
+
}
|
|
28
|
+
if (client.account.address !== userAddress) {
|
|
29
|
+
throw new types_1.AddressMismatchError(client.account.address, userAddress);
|
|
30
|
+
}
|
|
31
|
+
const { dai } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
32
|
+
const isDai = dai != null && token === dai;
|
|
33
|
+
let signature;
|
|
34
|
+
if (isDai) {
|
|
35
|
+
const typedData = (0, blue_sdk_viem_1.getDaiPermitTypedData)({
|
|
36
|
+
owner: userAddress,
|
|
37
|
+
spender,
|
|
38
|
+
allowance: amount,
|
|
39
|
+
nonce,
|
|
40
|
+
deadline,
|
|
41
|
+
}, chainId);
|
|
42
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
43
|
+
...typedData,
|
|
44
|
+
account: client.account,
|
|
45
|
+
});
|
|
46
|
+
await (0, viem_1.verifyTypedData)({
|
|
47
|
+
...typedData,
|
|
48
|
+
address: userAddress,
|
|
49
|
+
signature,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const tokenData = await (0, blue_sdk_viem_1.fetchToken)(token, client);
|
|
54
|
+
const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
|
|
55
|
+
erc20: tokenData,
|
|
56
|
+
owner: userAddress,
|
|
57
|
+
spender,
|
|
58
|
+
allowance: amount,
|
|
59
|
+
nonce,
|
|
60
|
+
deadline,
|
|
61
|
+
}, chainId);
|
|
62
|
+
signature = await (0, actions_1.signTypedData)(client, {
|
|
63
|
+
...typedData,
|
|
64
|
+
account: client.account,
|
|
65
|
+
});
|
|
66
|
+
await (0, viem_1.verifyTypedData)({
|
|
67
|
+
...typedData,
|
|
68
|
+
address: userAddress, // Verify against the permit's owner.
|
|
69
|
+
signature,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
73
|
+
args: {
|
|
74
|
+
owner: userAddress,
|
|
75
|
+
signature,
|
|
76
|
+
deadline,
|
|
77
|
+
amount,
|
|
78
|
+
asset: token,
|
|
79
|
+
nonce,
|
|
80
|
+
},
|
|
81
|
+
action,
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
exports.encodeErc20Permit = encodeErc20Permit;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type Requirement } from "../../../types";
|
|
3
|
+
interface EncodeErc20Permit2Params {
|
|
4
|
+
token: Address;
|
|
5
|
+
spender: Address;
|
|
6
|
+
amount: bigint;
|
|
7
|
+
chainId: number;
|
|
8
|
+
nonce: bigint;
|
|
9
|
+
expiration: bigint;
|
|
10
|
+
}
|
|
11
|
+
export declare const encodeErc20Permit2: (params: EncodeErc20Permit2Params) => Requirement;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeErc20Permit2 = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
+
const viem_1 = require("viem");
|
|
8
|
+
const actions_1 = require("viem/actions");
|
|
9
|
+
const types_1 = require("../../../types");
|
|
10
|
+
const encodeErc20Permit2 = (params) => {
|
|
11
|
+
const { token, spender, amount, chainId, nonce, expiration = blue_sdk_1.MathLib.MAX_UINT_48, } = params;
|
|
12
|
+
const now = morpho_ts_1.Time.timestamp();
|
|
13
|
+
const deadline = now + morpho_ts_1.Time.s.from.h(2n);
|
|
14
|
+
const action = {
|
|
15
|
+
type: "permit2",
|
|
16
|
+
args: {
|
|
17
|
+
spender,
|
|
18
|
+
amount,
|
|
19
|
+
deadline,
|
|
20
|
+
expiration,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
action,
|
|
25
|
+
async sign(client, userAddress) {
|
|
26
|
+
if (!client.account?.address) {
|
|
27
|
+
throw new types_1.MissingClientPropertyError("client.account.address");
|
|
28
|
+
}
|
|
29
|
+
if (client.account.address !== userAddress) {
|
|
30
|
+
throw new types_1.AddressMismatchError(client.account.address, userAddress);
|
|
31
|
+
}
|
|
32
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
33
|
+
const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
|
|
34
|
+
// Never permit any other address than the GeneralAdapter1 otherwise
|
|
35
|
+
// the signature can be used independently.
|
|
36
|
+
spender: generalAdapter1,
|
|
37
|
+
allowance: amount,
|
|
38
|
+
erc20: token,
|
|
39
|
+
nonce: Number(nonce),
|
|
40
|
+
deadline,
|
|
41
|
+
expiration: Number(expiration),
|
|
42
|
+
}, chainId);
|
|
43
|
+
const signature = await (0, actions_1.signTypedData)(client, {
|
|
44
|
+
...typedData,
|
|
45
|
+
account: client.account,
|
|
46
|
+
});
|
|
47
|
+
await (0, viem_1.verifyTypedData)({
|
|
48
|
+
...typedData,
|
|
49
|
+
address: userAddress,
|
|
50
|
+
signature,
|
|
51
|
+
});
|
|
52
|
+
return (0, morpho_ts_1.deepFreeze)({
|
|
53
|
+
args: {
|
|
54
|
+
owner: userAddress,
|
|
55
|
+
signature,
|
|
56
|
+
deadline,
|
|
57
|
+
amount,
|
|
58
|
+
asset: token,
|
|
59
|
+
expiration,
|
|
60
|
+
nonce,
|
|
61
|
+
},
|
|
62
|
+
action,
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
exports.encodeErc20Permit2 = encodeErc20Permit2;
|
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Morpho SDK Unified Exports
|
|
4
|
-
*
|
|
5
|
-
* This module provides a unified interface to all Morpho packages.
|
|
6
|
-
* Users can import any functionality from any Morpho package through this single entry point.
|
|
7
|
-
*/
|
|
8
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
3
|
if (k2 === undefined) k2 = k;
|
|
10
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -20,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
20
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
15
|
};
|
|
22
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
__exportStar(require("
|
|
24
|
-
__exportStar(require("
|
|
25
|
-
__exportStar(require("
|
|
26
|
-
__exportStar(require("@morpho-org/morpho-ts"), exports);
|
|
17
|
+
__exportStar(require("./encodeErc20Approval"), exports);
|
|
18
|
+
__exportStar(require("./encodeErc20Permit"), exports);
|
|
19
|
+
__exportStar(require("./encodeErc20Permit2"), exports);
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
2
|
import type { Client } from "viem";
|
|
3
|
-
import { type ERC20ApprovalAction, type Transaction } from "../../types";
|
|
3
|
+
import { type ERC20ApprovalAction, type Requirement, type Transaction } from "../../types";
|
|
4
|
+
/**
|
|
5
|
+
* Get token "requirement" for approval/permit before interacting with protocol.
|
|
6
|
+
*
|
|
7
|
+
* Three flows:
|
|
8
|
+
* 1. If signature not supported, use classic approval (transaction).
|
|
9
|
+
* 2. If signature supported, try simple permit (EIP-2612), else fallback to permit2.
|
|
10
|
+
*
|
|
11
|
+
* @param viemClient - The connected viem Client instance, with the correct chain and account.
|
|
12
|
+
* @param params - Destructured object with:
|
|
13
|
+
* @param params.address - ERC20 token address.
|
|
14
|
+
* @param params.chainId - Chain/network id.
|
|
15
|
+
* @param params.args - Object with:
|
|
16
|
+
* @param params.args.amount - Required token amount.
|
|
17
|
+
* @param params.args.from - The account that will grant approval.
|
|
18
|
+
* @param supportSignature - Whether signature-based approvals are supported. If true, will try to use permit or permit2.
|
|
19
|
+
* @returns Promise of array of approval transaction or requirement objects.
|
|
20
|
+
*/
|
|
4
21
|
export declare const getRequirements: (viemClient: Client, params: {
|
|
5
22
|
address: Address;
|
|
6
23
|
chainId: number;
|
|
24
|
+
supportSignature: boolean;
|
|
7
25
|
args: {
|
|
8
26
|
amount: bigint;
|
|
9
27
|
from: Address;
|
|
10
28
|
};
|
|
11
|
-
}) => Promise<Readonly<Transaction<ERC20ApprovalAction
|
|
29
|
+
}) => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
@@ -4,34 +4,67 @@ exports.getRequirements = void 0;
|
|
|
4
4
|
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
5
|
const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
|
-
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
8
7
|
const types_1 = require("../../types");
|
|
9
|
-
const
|
|
8
|
+
const getRequirementsApproval_1 = require("./getRequirementsApproval");
|
|
9
|
+
const getRequirementsPermit_1 = require("./getRequirementsPermit");
|
|
10
|
+
const getRequirementsPermit2_1 = require("./getRequirementsPermit2");
|
|
11
|
+
/**
|
|
12
|
+
* Get token "requirement" for approval/permit before interacting with protocol.
|
|
13
|
+
*
|
|
14
|
+
* Three flows:
|
|
15
|
+
* 1. If signature not supported, use classic approval (transaction).
|
|
16
|
+
* 2. If signature supported, try simple permit (EIP-2612), else fallback to permit2.
|
|
17
|
+
*
|
|
18
|
+
* @param viemClient - The connected viem Client instance, with the correct chain and account.
|
|
19
|
+
* @param params - Destructured object with:
|
|
20
|
+
* @param params.address - ERC20 token address.
|
|
21
|
+
* @param params.chainId - Chain/network id.
|
|
22
|
+
* @param params.args - Object with:
|
|
23
|
+
* @param params.args.amount - Required token amount.
|
|
24
|
+
* @param params.args.from - The account that will grant approval.
|
|
25
|
+
* @param supportSignature - Whether signature-based approvals are supported. If true, will try to use permit or permit2.
|
|
26
|
+
* @returns Promise of array of approval transaction or requirement objects.
|
|
27
|
+
*/
|
|
10
28
|
const getRequirements = async (viemClient, params) => {
|
|
11
|
-
const { address, chainId, args: { amount, from }, } = params;
|
|
29
|
+
const { address, chainId, supportSignature, args: { amount, from }, } = params;
|
|
12
30
|
if (viemClient.chain?.id !== chainId) {
|
|
13
31
|
throw new types_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
14
32
|
}
|
|
15
|
-
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
16
|
-
const { erc20Allowances } = await (0, blue_sdk_viem_1.fetchHolding)(from, address, viemClient);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
token: address,
|
|
23
|
-
spender: generalAdapter1,
|
|
24
|
-
amount: 0n,
|
|
33
|
+
const { permit2, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
34
|
+
const { erc20Allowances, erc2612Nonce, permit2BundlerAllowance } = await (0, blue_sdk_viem_1.fetchHolding)(from, address, viemClient);
|
|
35
|
+
if (supportSignature) {
|
|
36
|
+
const supportSimplePermit = (0, morpho_ts_1.isDefined)(erc2612Nonce);
|
|
37
|
+
if (supportSimplePermit) {
|
|
38
|
+
return (0, getRequirementsPermit_1.getRequirementsPermit)({
|
|
39
|
+
address,
|
|
25
40
|
chainId,
|
|
26
|
-
|
|
41
|
+
args: { amount },
|
|
42
|
+
allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
|
|
43
|
+
nonce: erc2612Nonce,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (permit2) {
|
|
47
|
+
return (0, getRequirementsPermit2_1.getRequirementsPermit2)({
|
|
48
|
+
address,
|
|
49
|
+
chainId,
|
|
50
|
+
permit2,
|
|
51
|
+
args: { amount },
|
|
52
|
+
allowancesPermit2: erc20Allowances.permit2,
|
|
53
|
+
allowanceGeneralAdapterPermit2: permit2BundlerAllowance.amount,
|
|
54
|
+
allowanceGeneralAdapterExpiration: permit2BundlerAllowance.expiration,
|
|
55
|
+
nonce: permit2BundlerAllowance.nonce,
|
|
56
|
+
});
|
|
27
57
|
}
|
|
28
|
-
txs.push((0, encodeErc20Approval_1.encodeErc20Approval)({
|
|
29
|
-
token: address,
|
|
30
|
-
spender: generalAdapter1,
|
|
31
|
-
amount,
|
|
32
|
-
chainId,
|
|
33
|
-
}));
|
|
34
58
|
}
|
|
35
|
-
return (0,
|
|
59
|
+
return (0, getRequirementsApproval_1.getRequirementsApproval)({
|
|
60
|
+
address,
|
|
61
|
+
chainId,
|
|
62
|
+
args: {
|
|
63
|
+
spendAmount: amount,
|
|
64
|
+
approvalAmount: amount,
|
|
65
|
+
spender: generalAdapter1,
|
|
66
|
+
},
|
|
67
|
+
allowances: erc20Allowances["bundler3.generalAdapter1"],
|
|
68
|
+
});
|
|
36
69
|
};
|
|
37
70
|
exports.getRequirements = getRequirements;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Action } from "@morpho-org/bundler-sdk-viem";
|
|
2
|
+
import type { Permit2Action, PermitAction, PermitArgs } from "../../types";
|
|
3
|
+
interface GetRequirementsActionParams {
|
|
4
|
+
chainId: number;
|
|
5
|
+
requirementSignature: {
|
|
6
|
+
args: PermitArgs;
|
|
7
|
+
action: PermitAction | Permit2Action;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get the actions required to transfer the asset based on the requirement signature.
|
|
12
|
+
*/
|
|
13
|
+
export declare const getRequirementsAction: ({ chainId, requirementSignature, }: GetRequirementsActionParams) => Action[];
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsAction = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
/**
|
|
6
|
+
* Get the actions required to transfer the asset based on the requirement signature.
|
|
7
|
+
*/
|
|
8
|
+
const getRequirementsAction = ({ chainId, requirementSignature, }) => {
|
|
9
|
+
const { dai, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
10
|
+
if (requirementSignature.action.type === "permit2") {
|
|
11
|
+
if (!("expiration" in requirementSignature.args)) {
|
|
12
|
+
throw new Error("Expiration is not defined");
|
|
13
|
+
}
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
type: "approve2",
|
|
17
|
+
args: [
|
|
18
|
+
requirementSignature.args.owner,
|
|
19
|
+
{
|
|
20
|
+
details: {
|
|
21
|
+
token: requirementSignature.args.asset,
|
|
22
|
+
amount: requirementSignature.args.amount,
|
|
23
|
+
nonce: Number(requirementSignature.args.nonce),
|
|
24
|
+
expiration: Number(requirementSignature.args.expiration),
|
|
25
|
+
},
|
|
26
|
+
sigDeadline: requirementSignature.args.deadline,
|
|
27
|
+
},
|
|
28
|
+
requirementSignature.args.signature,
|
|
29
|
+
false,
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
type: "transferFrom2",
|
|
34
|
+
args: [
|
|
35
|
+
requirementSignature.args.asset,
|
|
36
|
+
requirementSignature.args.amount,
|
|
37
|
+
generalAdapter1,
|
|
38
|
+
false,
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
const isDai = dai != null && requirementSignature.args.asset === dai;
|
|
44
|
+
if (isDai) {
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
type: "permitDai",
|
|
48
|
+
args: [
|
|
49
|
+
requirementSignature.args.owner,
|
|
50
|
+
requirementSignature.args.nonce,
|
|
51
|
+
requirementSignature.args.deadline,
|
|
52
|
+
requirementSignature.args.amount > 0n,
|
|
53
|
+
requirementSignature.args.signature,
|
|
54
|
+
false,
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
type: "erc20TransferFrom",
|
|
59
|
+
args: [
|
|
60
|
+
requirementSignature.args.asset,
|
|
61
|
+
requirementSignature.args.amount,
|
|
62
|
+
generalAdapter1,
|
|
63
|
+
false,
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
return [
|
|
69
|
+
{
|
|
70
|
+
type: "permit",
|
|
71
|
+
args: [
|
|
72
|
+
requirementSignature.args.owner,
|
|
73
|
+
requirementSignature.args.asset,
|
|
74
|
+
requirementSignature.args.amount,
|
|
75
|
+
requirementSignature.args.deadline,
|
|
76
|
+
requirementSignature.args.signature,
|
|
77
|
+
false,
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
type: "erc20TransferFrom",
|
|
82
|
+
args: [
|
|
83
|
+
requirementSignature.args.asset,
|
|
84
|
+
requirementSignature.args.amount,
|
|
85
|
+
generalAdapter1,
|
|
86
|
+
false,
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
};
|
|
91
|
+
exports.getRequirementsAction = getRequirementsAction;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import { type ERC20ApprovalAction, type Transaction } from "../../types";
|
|
3
|
+
/**
|
|
4
|
+
* Get token "requirement" for approval.
|
|
5
|
+
*
|
|
6
|
+
* Verify if the allowance is enough on the spender contract.
|
|
7
|
+
* => If not, approve the token to the spender contract with classic approval transaction on the required amount.
|
|
8
|
+
* => If the allowance is enough, return an empty array.
|
|
9
|
+
*
|
|
10
|
+
* Handle logic for reset approval before approving (if needed).
|
|
11
|
+
*
|
|
12
|
+
* @param params - Destructured object with:
|
|
13
|
+
* @param params.address - ERC20 token address.
|
|
14
|
+
* @param params.chainId - Chain/network id.
|
|
15
|
+
* @param params.args - Object with:
|
|
16
|
+
* @param params.args.spendAmount - Spend amount required for the action.
|
|
17
|
+
* @param params.args.approvalAmount - Approval amount to be approved.
|
|
18
|
+
* @param params.args.spender - Spender contract address.
|
|
19
|
+
* @param params.allowances - Allowance for the spender contract.
|
|
20
|
+
* @returns An array of requirement transaction object.
|
|
21
|
+
*/
|
|
22
|
+
export declare const getRequirementsApproval: (params: {
|
|
23
|
+
address: Address;
|
|
24
|
+
chainId: number;
|
|
25
|
+
args: {
|
|
26
|
+
approvalAmount: bigint;
|
|
27
|
+
spendAmount: bigint;
|
|
28
|
+
spender: Address;
|
|
29
|
+
};
|
|
30
|
+
allowances: bigint;
|
|
31
|
+
}) => Readonly<Transaction<ERC20ApprovalAction>>[];
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsApproval = void 0;
|
|
4
|
+
const simulation_sdk_1 = require("@morpho-org/simulation-sdk");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
const encodeErc20Approval_1 = require("./encode/encodeErc20Approval");
|
|
7
|
+
/**
|
|
8
|
+
* Get token "requirement" for approval.
|
|
9
|
+
*
|
|
10
|
+
* Verify if the allowance is enough on the spender contract.
|
|
11
|
+
* => If not, approve the token to the spender contract with classic approval transaction on the required amount.
|
|
12
|
+
* => If the allowance is enough, return an empty array.
|
|
13
|
+
*
|
|
14
|
+
* Handle logic for reset approval before approving (if needed).
|
|
15
|
+
*
|
|
16
|
+
* @param params - Destructured object with:
|
|
17
|
+
* @param params.address - ERC20 token address.
|
|
18
|
+
* @param params.chainId - Chain/network id.
|
|
19
|
+
* @param params.args - Object with:
|
|
20
|
+
* @param params.args.spendAmount - Spend amount required for the action.
|
|
21
|
+
* @param params.args.approvalAmount - Approval amount to be approved.
|
|
22
|
+
* @param params.args.spender - Spender contract address.
|
|
23
|
+
* @param params.allowances - Allowance for the spender contract.
|
|
24
|
+
* @returns An array of requirement transaction object.
|
|
25
|
+
*/
|
|
26
|
+
const getRequirementsApproval = (params) => {
|
|
27
|
+
const { address, chainId, args: { spendAmount, approvalAmount, spender }, allowances, } = params;
|
|
28
|
+
if (approvalAmount < spendAmount) {
|
|
29
|
+
throw new types_1.ApprovalAmountLessThanSpendAmountError();
|
|
30
|
+
}
|
|
31
|
+
const approvals = [];
|
|
32
|
+
if (allowances < spendAmount) {
|
|
33
|
+
if (simulation_sdk_1.APPROVE_ONLY_ONCE_TOKENS[chainId]?.includes(address) &&
|
|
34
|
+
allowances > 0n) {
|
|
35
|
+
approvals.push((0, encodeErc20Approval_1.encodeErc20Approval)({
|
|
36
|
+
token: address,
|
|
37
|
+
spender,
|
|
38
|
+
amount: 0n,
|
|
39
|
+
chainId,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
approvals.push((0, encodeErc20Approval_1.encodeErc20Approval)({
|
|
43
|
+
token: address,
|
|
44
|
+
spender,
|
|
45
|
+
amount: approvalAmount,
|
|
46
|
+
chainId,
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
return approvals;
|
|
50
|
+
};
|
|
51
|
+
exports.getRequirementsApproval = getRequirementsApproval;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Get token "requirement" for permit (EIP-2612).
|
|
4
|
+
*
|
|
5
|
+
* Verify if the allowance is enough on general adapter from permit (EIP-2612) contract.
|
|
6
|
+
* => If not, approve the token to general adapter from permit via EIP-2612 with permit signature on the required amount.
|
|
7
|
+
*
|
|
8
|
+
* @param params - Destructured object with:
|
|
9
|
+
* @param params.address - ERC20 token address.
|
|
10
|
+
* @param params.chainId - Chain/network id.
|
|
11
|
+
* @param params.args - Object with:
|
|
12
|
+
* @param params.args.amount - Required token amount.
|
|
13
|
+
* @param params.allowancesGeneralAdapter - Allowance for general adapter from permit contract.
|
|
14
|
+
* @param params.nonce - Nonce for permit (EIP-2612).
|
|
15
|
+
* @returns An array of requirement signature object.
|
|
16
|
+
*/
|
|
17
|
+
export declare const getRequirementsPermit: (params: {
|
|
18
|
+
address: Address;
|
|
19
|
+
chainId: number;
|
|
20
|
+
args: {
|
|
21
|
+
amount: bigint;
|
|
22
|
+
};
|
|
23
|
+
allowancesGeneralAdapter: bigint;
|
|
24
|
+
nonce: bigint;
|
|
25
|
+
}) => import("../..").Requirement[];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsPermit = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const encode_1 = require("./encode");
|
|
6
|
+
/**
|
|
7
|
+
* Get token "requirement" for permit (EIP-2612).
|
|
8
|
+
*
|
|
9
|
+
* Verify if the allowance is enough on general adapter from permit (EIP-2612) contract.
|
|
10
|
+
* => If not, approve the token to general adapter from permit via EIP-2612 with permit signature on the required amount.
|
|
11
|
+
*
|
|
12
|
+
* @param params - Destructured object with:
|
|
13
|
+
* @param params.address - ERC20 token address.
|
|
14
|
+
* @param params.chainId - Chain/network id.
|
|
15
|
+
* @param params.args - Object with:
|
|
16
|
+
* @param params.args.amount - Required token amount.
|
|
17
|
+
* @param params.allowancesGeneralAdapter - Allowance for general adapter from permit contract.
|
|
18
|
+
* @param params.nonce - Nonce for permit (EIP-2612).
|
|
19
|
+
* @returns An array of requirement signature object.
|
|
20
|
+
*/
|
|
21
|
+
const getRequirementsPermit = (params) => {
|
|
22
|
+
const { address, chainId, args: { amount }, allowancesGeneralAdapter, nonce, } = params;
|
|
23
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
24
|
+
if (allowancesGeneralAdapter < amount) {
|
|
25
|
+
return [
|
|
26
|
+
(0, encode_1.encodeErc20Permit)({
|
|
27
|
+
token: address,
|
|
28
|
+
spender: generalAdapter1,
|
|
29
|
+
amount,
|
|
30
|
+
chainId,
|
|
31
|
+
nonce,
|
|
32
|
+
}),
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
return [];
|
|
36
|
+
};
|
|
37
|
+
exports.getRequirementsPermit = getRequirementsPermit;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Address } from "@morpho-org/blue-sdk";
|
|
2
|
+
import type { ERC20ApprovalAction, Requirement, Transaction } from "../../types";
|
|
3
|
+
/**
|
|
4
|
+
* Get token "requirement" for permit2.
|
|
5
|
+
*
|
|
6
|
+
* Two steps:
|
|
7
|
+
* 1. Verify if the allowance is enough on permit2 contract.
|
|
8
|
+
* => If not, approve the token to permit2 contract with classic approval on infinite amount.
|
|
9
|
+
* 2. Verify if the allowance is enough on general adapter from permit2 contract.
|
|
10
|
+
* => If not, approve the token to general adapter from permit2 contract with permit2 signature on the required amount.
|
|
11
|
+
*
|
|
12
|
+
* @param params - Destructured object with:
|
|
13
|
+
* @param params.address - ERC20 token address.
|
|
14
|
+
* @param params.chainId - Chain/network id.
|
|
15
|
+
* @param params.permit2 - Permit2 contract address.
|
|
16
|
+
* @param params.args - Object with:
|
|
17
|
+
* @param params.args.amount - Required token amount.
|
|
18
|
+
* @param params.allowancesPermit2 - Allowance for permit2.
|
|
19
|
+
* @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
|
|
20
|
+
* @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
|
|
21
|
+
* @param params.nonce - Nonce for permit2.
|
|
22
|
+
* @returns An array of approval transaction or requirement signatures objects.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getRequirementsPermit2: (params: {
|
|
25
|
+
address: Address;
|
|
26
|
+
chainId: number;
|
|
27
|
+
permit2: Address;
|
|
28
|
+
args: {
|
|
29
|
+
amount: bigint;
|
|
30
|
+
};
|
|
31
|
+
allowancesPermit2: bigint;
|
|
32
|
+
allowanceGeneralAdapterPermit2: bigint;
|
|
33
|
+
allowanceGeneralAdapterExpiration: bigint;
|
|
34
|
+
nonce: bigint;
|
|
35
|
+
}) => Readonly<Transaction<ERC20ApprovalAction> | Requirement>[];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRequirementsPermit2 = void 0;
|
|
4
|
+
const blue_sdk_1 = require("@morpho-org/blue-sdk");
|
|
5
|
+
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
6
|
+
const encodeErc20Permit2_1 = require("./encode/encodeErc20Permit2");
|
|
7
|
+
const getRequirementsApproval_1 = require("./getRequirementsApproval");
|
|
8
|
+
/**
|
|
9
|
+
* Get token "requirement" for permit2.
|
|
10
|
+
*
|
|
11
|
+
* Two steps:
|
|
12
|
+
* 1. Verify if the allowance is enough on permit2 contract.
|
|
13
|
+
* => If not, approve the token to permit2 contract with classic approval on infinite amount.
|
|
14
|
+
* 2. Verify if the allowance is enough on general adapter from permit2 contract.
|
|
15
|
+
* => If not, approve the token to general adapter from permit2 contract with permit2 signature on the required amount.
|
|
16
|
+
*
|
|
17
|
+
* @param params - Destructured object with:
|
|
18
|
+
* @param params.address - ERC20 token address.
|
|
19
|
+
* @param params.chainId - Chain/network id.
|
|
20
|
+
* @param params.permit2 - Permit2 contract address.
|
|
21
|
+
* @param params.args - Object with:
|
|
22
|
+
* @param params.args.amount - Required token amount.
|
|
23
|
+
* @param params.allowancesPermit2 - Allowance for permit2.
|
|
24
|
+
* @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
|
|
25
|
+
* @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
|
|
26
|
+
* @param params.nonce - Nonce for permit2.
|
|
27
|
+
* @returns An array of approval transaction or requirement signatures objects.
|
|
28
|
+
*/
|
|
29
|
+
const getRequirementsPermit2 = (params) => {
|
|
30
|
+
const { address, chainId, permit2, args: { amount }, allowancesPermit2, allowanceGeneralAdapterPermit2, allowanceGeneralAdapterExpiration, nonce, } = params;
|
|
31
|
+
const requirements = [];
|
|
32
|
+
const approvalRequirements = (0, getRequirementsApproval_1.getRequirementsApproval)({
|
|
33
|
+
address,
|
|
34
|
+
chainId,
|
|
35
|
+
args: {
|
|
36
|
+
approvalAmount: blue_sdk_1.MathLib.MAX_UINT_160, // Always approve infinite.
|
|
37
|
+
spendAmount: amount,
|
|
38
|
+
spender: permit2,
|
|
39
|
+
},
|
|
40
|
+
allowances: allowancesPermit2,
|
|
41
|
+
});
|
|
42
|
+
requirements.push(...approvalRequirements);
|
|
43
|
+
if (allowanceGeneralAdapterPermit2 < amount ||
|
|
44
|
+
allowanceGeneralAdapterExpiration < morpho_ts_1.Time.timestamp() + morpho_ts_1.Time.s.from.h(4n)) {
|
|
45
|
+
requirements.push((0, encodeErc20Permit2_1.encodeErc20Permit2)({
|
|
46
|
+
token: address,
|
|
47
|
+
spender: permit2,
|
|
48
|
+
amount,
|
|
49
|
+
chainId,
|
|
50
|
+
nonce,
|
|
51
|
+
expiration: blue_sdk_1.MathLib.MAX_UINT_48, // Always approve indefinitely.
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
return requirements;
|
|
55
|
+
};
|
|
56
|
+
exports.getRequirementsPermit2 = getRequirementsPermit2;
|
|
@@ -14,5 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./encode/index"), exports);
|
|
18
18
|
__exportStar(require("./getRequirements"), exports);
|
|
19
|
+
__exportStar(require("./getRequirementsApproval"), exports);
|
|
20
|
+
__exportStar(require("./getRequirementsPermit"), exports);
|
|
21
|
+
__exportStar(require("./getRequirementsPermit2"), exports);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
-
import { type Metadata, type Transaction, type VaultV2DepositAction } from "../../types";
|
|
2
|
+
import { type Metadata, type RequirementSignature, type Transaction, type VaultV2DepositAction } from "../../types";
|
|
3
3
|
export interface VaultV2DepositParams {
|
|
4
4
|
vault: {
|
|
5
5
|
chainId: number;
|
|
@@ -10,6 +10,7 @@ export interface VaultV2DepositParams {
|
|
|
10
10
|
assets: bigint;
|
|
11
11
|
maxSharePrice: bigint;
|
|
12
12
|
recipient: Address;
|
|
13
|
+
requirementSignature?: RequirementSignature;
|
|
13
14
|
};
|
|
14
15
|
metadata?: Metadata;
|
|
15
16
|
}
|
|
@@ -35,8 +36,11 @@ export interface VaultV2DepositParams {
|
|
|
35
36
|
* @param {bigint} params.args.assets - The amount of assets to deposit.
|
|
36
37
|
* @param {bigint} params.args.maxSharePrice - The maximum share price to accept for the deposit.
|
|
37
38
|
* @param {Address} params.args.recipient - The recipient address.
|
|
39
|
+
* @param {Object} params.args.requirementSignature - The requirement args and signature.
|
|
40
|
+
* @param {Object} params.args.requirementSignature.args - The requirement signature arguments.
|
|
41
|
+
* @param {Object} params.args.requirementSignature.action - The requirement signature action.
|
|
38
42
|
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
39
43
|
*
|
|
40
44
|
* @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
|
|
41
45
|
*/
|
|
42
|
-
export declare const vaultV2Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient }, metadata, }: VaultV2DepositParams) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
46
|
+
export declare const vaultV2Deposit: ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient, requirementSignature }, metadata, }: VaultV2DepositParams) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
@@ -6,6 +6,7 @@ const bundler_sdk_viem_1 = require("@morpho-org/bundler-sdk-viem");
|
|
|
6
6
|
const morpho_ts_1 = require("@morpho-org/morpho-ts");
|
|
7
7
|
const helpers_1 = require("../../helpers");
|
|
8
8
|
const types_1 = require("../../types");
|
|
9
|
+
const getRequirementsAction_1 = require("../requirements/getRequirementsAction");
|
|
9
10
|
/**
|
|
10
11
|
* Prepares a deposit transaction for the VaultV2 contract.
|
|
11
12
|
*
|
|
@@ -28,28 +29,35 @@ const types_1 = require("../../types");
|
|
|
28
29
|
* @param {bigint} params.args.assets - The amount of assets to deposit.
|
|
29
30
|
* @param {bigint} params.args.maxSharePrice - The maximum share price to accept for the deposit.
|
|
30
31
|
* @param {Address} params.args.recipient - The recipient address.
|
|
32
|
+
* @param {Object} params.args.requirementSignature - The requirement args and signature.
|
|
33
|
+
* @param {Object} params.args.requirementSignature.args - The requirement signature arguments.
|
|
34
|
+
* @param {Object} params.args.requirementSignature.action - The requirement signature action.
|
|
31
35
|
* @param {Metadata} [params.metadata] - Optional the metadata.
|
|
32
36
|
*
|
|
33
37
|
* @returns {Readonly<Transaction<VaultV2DepositAction>>} The prepared deposit transaction.
|
|
34
38
|
*/
|
|
35
|
-
const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient }, metadata, }) => {
|
|
39
|
+
const vaultV2Deposit = ({ vault: { chainId, address: vaultAddress, asset }, args: { assets, maxSharePrice, recipient, requirementSignature }, metadata, }) => {
|
|
36
40
|
if (assets === 0n) {
|
|
37
41
|
throw new types_1.ZeroAssetAmountError(asset);
|
|
38
42
|
}
|
|
39
43
|
if (maxSharePrice === 0n) {
|
|
40
44
|
throw new types_1.ZeroMaxSharePriceError(vaultAddress);
|
|
41
45
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
{
|
|
46
|
+
const actions = [];
|
|
47
|
+
if (requirementSignature) {
|
|
48
|
+
actions.push(...(0, getRequirementsAction_1.getRequirementsAction)({ chainId, requirementSignature }));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
|
|
52
|
+
actions.push({
|
|
45
53
|
type: "erc20TransferFrom",
|
|
46
54
|
args: [asset, assets, generalAdapter1, false],
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
actions.push({
|
|
58
|
+
type: "erc4626Deposit",
|
|
59
|
+
args: [vaultAddress, assets, maxSharePrice, recipient, false],
|
|
60
|
+
});
|
|
53
61
|
let tx = bundler_sdk_viem_1.BundlerAction.encodeBundle(chainId, actions);
|
|
54
62
|
if (metadata) {
|
|
55
63
|
tx = (0, helpers_1.addTransactionMetadata)(tx, metadata);
|
|
@@ -3,7 +3,17 @@ import { MorphoVaultV2 } from "../entities";
|
|
|
3
3
|
import type { Metadata, MorphoClientType } from "../types";
|
|
4
4
|
export declare class MorphoClient implements MorphoClientType {
|
|
5
5
|
readonly viemClient: Client;
|
|
6
|
-
readonly
|
|
7
|
-
|
|
6
|
+
readonly _options?: {
|
|
7
|
+
readonly supportSignature?: boolean;
|
|
8
|
+
readonly metadata?: Metadata;
|
|
9
|
+
} | undefined;
|
|
10
|
+
readonly options: {
|
|
11
|
+
readonly supportSignature: boolean;
|
|
12
|
+
readonly metadata?: Metadata;
|
|
13
|
+
};
|
|
14
|
+
constructor(viemClient: Client, _options?: {
|
|
15
|
+
readonly supportSignature?: boolean;
|
|
16
|
+
readonly metadata?: Metadata;
|
|
17
|
+
} | undefined);
|
|
8
18
|
vaultV2(vault: Address, chainId: number): MorphoVaultV2;
|
|
9
19
|
}
|
|
@@ -4,10 +4,15 @@ exports.MorphoClient = void 0;
|
|
|
4
4
|
const entities_1 = require("../entities");
|
|
5
5
|
class MorphoClient {
|
|
6
6
|
viemClient;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
_options;
|
|
8
|
+
options;
|
|
9
|
+
constructor(viemClient, _options) {
|
|
9
10
|
this.viemClient = viemClient;
|
|
10
|
-
this.
|
|
11
|
+
this._options = _options;
|
|
12
|
+
this.options = {
|
|
13
|
+
..._options,
|
|
14
|
+
supportSignature: _options?.supportSignature ?? false,
|
|
15
|
+
};
|
|
11
16
|
}
|
|
12
17
|
vaultV2(vault, chainId) {
|
|
13
18
|
return new entities_1.MorphoVaultV2(this, vault, chainId);
|
|
@@ -12,7 +12,7 @@ import { MorphoClient } from "./morphoClient";
|
|
|
12
12
|
* ```ts
|
|
13
13
|
* import { createWalletClient, http } from 'viem';
|
|
14
14
|
* import { mainnet } from 'viem/chains';
|
|
15
|
-
* import { morpho } from 'consumer-sdk';
|
|
15
|
+
* import { morpho } from '@morpho-org/consumer-sdk';
|
|
16
16
|
*
|
|
17
17
|
* const client = createWalletClient({
|
|
18
18
|
* chain: mainnet,
|
|
@@ -25,6 +25,9 @@ import { MorphoClient } from "./morphoClient";
|
|
|
25
25
|
* const deposit = await vault.deposit({ assets: 1000000000000000000n });
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export declare function morphoViemExtension(
|
|
28
|
+
export declare function morphoViemExtension(_options?: {
|
|
29
|
+
metadata?: Metadata;
|
|
30
|
+
supportSignature?: boolean;
|
|
31
|
+
}): <TClient extends Client>(client: TClient) => {
|
|
29
32
|
morpho: MorphoClient;
|
|
30
33
|
};
|
|
@@ -13,7 +13,7 @@ const morphoClient_1 = require("./morphoClient");
|
|
|
13
13
|
* ```ts
|
|
14
14
|
* import { createWalletClient, http } from 'viem';
|
|
15
15
|
* import { mainnet } from 'viem/chains';
|
|
16
|
-
* import { morpho } from 'consumer-sdk';
|
|
16
|
+
* import { morpho } from '@morpho-org/consumer-sdk';
|
|
17
17
|
*
|
|
18
18
|
* const client = createWalletClient({
|
|
19
19
|
* chain: mainnet,
|
|
@@ -26,10 +26,10 @@ const morphoClient_1 = require("./morphoClient");
|
|
|
26
26
|
* const deposit = await vault.deposit({ assets: 1000000000000000000n });
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
function morphoViemExtension(
|
|
29
|
+
function morphoViemExtension(_options) {
|
|
30
30
|
return (client) => {
|
|
31
31
|
return {
|
|
32
|
-
morpho: new morphoClient_1.MorphoClient(client,
|
|
32
|
+
morpho: new morphoClient_1.MorphoClient(client, _options),
|
|
33
33
|
};
|
|
34
34
|
};
|
|
35
35
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { fetchAccrualVaultV2 } from "@morpho-org/blue-sdk-viem";
|
|
2
2
|
import type { Address } from "viem";
|
|
3
|
-
import { type ERC20ApprovalAction, type MorphoClientType, type Transaction, type VaultV2DepositAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types";
|
|
3
|
+
import { type ERC20ApprovalAction, type MorphoClientType, type Requirement, type RequirementSignature, type Transaction, type VaultV2DepositAction, type VaultV2RedeemAction, type VaultV2WithdrawAction } from "../../types";
|
|
4
4
|
export interface VaultV2Actions {
|
|
5
5
|
/**
|
|
6
6
|
* Fetches the latest vault data.
|
|
@@ -31,8 +31,8 @@ export interface VaultV2Actions {
|
|
|
31
31
|
userAddress: Address;
|
|
32
32
|
slippageTolerance?: bigint;
|
|
33
33
|
}) => Promise<{
|
|
34
|
-
buildTx: () => Readonly<Transaction<VaultV2DepositAction>>;
|
|
35
|
-
getRequirements: () => Promise<Readonly<Transaction<ERC20ApprovalAction
|
|
34
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
35
|
+
getRequirements: () => Promise<(Readonly<Transaction<ERC20ApprovalAction>> | Requirement)[]>;
|
|
36
36
|
}>;
|
|
37
37
|
/**
|
|
38
38
|
* Prepares a withdraw transaction for the VaultV2 contract.
|
|
@@ -80,8 +80,8 @@ export declare class MorphoVaultV2 implements VaultV2Actions {
|
|
|
80
80
|
userAddress: Address;
|
|
81
81
|
slippageTolerance?: bigint;
|
|
82
82
|
}): Promise<{
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
getRequirements: () => Promise<(Requirement | Readonly<Transaction<ERC20ApprovalAction>>)[]>;
|
|
84
|
+
buildTx: (requirementSignature?: RequirementSignature) => Readonly<Transaction<VaultV2DepositAction>>;
|
|
85
85
|
}>;
|
|
86
86
|
withdraw({ assets, userAddress }: {
|
|
87
87
|
assets: bigint;
|
|
@@ -24,7 +24,16 @@ class MorphoVaultV2 {
|
|
|
24
24
|
const vaultData = await (0, blue_sdk_viem_1.fetchVaultV2)(this.vault, this.client.viemClient);
|
|
25
25
|
const maxSharePrice = blue_sdk_1.MathLib.min(blue_sdk_1.MathLib.mulDivUp(assets, blue_sdk_1.MathLib.wToRay(blue_sdk_1.MathLib.WAD + slippageTolerance), vaultData.toShares(assets)), blue_sdk_1.MathLib.RAY * 100n);
|
|
26
26
|
return {
|
|
27
|
-
|
|
27
|
+
getRequirements: async () => await (0, actions_1.getRequirements)(this.client.viemClient, {
|
|
28
|
+
address: vaultData.asset,
|
|
29
|
+
chainId: this.chainId,
|
|
30
|
+
supportSignature: this.client.options.supportSignature,
|
|
31
|
+
args: {
|
|
32
|
+
amount: assets,
|
|
33
|
+
from: userAddress,
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
buildTx: (requirementSignature) => (0, actions_1.vaultV2Deposit)({
|
|
28
37
|
vault: {
|
|
29
38
|
chainId: this.chainId,
|
|
30
39
|
address: this.vault,
|
|
@@ -34,19 +43,10 @@ class MorphoVaultV2 {
|
|
|
34
43
|
assets,
|
|
35
44
|
maxSharePrice,
|
|
36
45
|
recipient: userAddress,
|
|
46
|
+
requirementSignature,
|
|
37
47
|
},
|
|
38
|
-
metadata: this.client.metadata,
|
|
48
|
+
metadata: this.client.options.metadata,
|
|
39
49
|
}),
|
|
40
|
-
getRequirements: async () => {
|
|
41
|
-
return (0, actions_1.getRequirements)(this.client.viemClient, {
|
|
42
|
-
address: vaultData.asset,
|
|
43
|
-
chainId: this.chainId,
|
|
44
|
-
args: {
|
|
45
|
-
amount: assets,
|
|
46
|
-
from: userAddress,
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
},
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
52
|
withdraw({ assets, userAddress }) {
|
|
@@ -58,7 +58,7 @@ class MorphoVaultV2 {
|
|
|
58
58
|
recipient: userAddress,
|
|
59
59
|
onBehalf: userAddress,
|
|
60
60
|
},
|
|
61
|
-
metadata: this.client.metadata,
|
|
61
|
+
metadata: this.client.options.metadata,
|
|
62
62
|
}),
|
|
63
63
|
};
|
|
64
64
|
}
|
|
@@ -71,7 +71,7 @@ class MorphoVaultV2 {
|
|
|
71
71
|
recipient: userAddress,
|
|
72
72
|
onBehalf: userAddress,
|
|
73
73
|
},
|
|
74
|
-
metadata: this.client.metadata,
|
|
74
|
+
metadata: this.client.options.metadata,
|
|
75
75
|
}),
|
|
76
76
|
};
|
|
77
77
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -18,7 +18,4 @@ __exportStar(require("./actions"), exports);
|
|
|
18
18
|
__exportStar(require("./client"), exports);
|
|
19
19
|
__exportStar(require("./entities"), exports);
|
|
20
20
|
__exportStar(require("./helpers"), exports);
|
|
21
|
-
// Unified Morpho SDK exports
|
|
22
|
-
// All Morpho packages are available through this single import
|
|
23
|
-
__exportStar(require("./sdk"), exports);
|
|
24
21
|
__exportStar(require("./types"), exports);
|
package/lib/types/action.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Address, Hex } from "viem";
|
|
1
|
+
import type { Address, Client, Hex } from "viem";
|
|
2
2
|
export interface BaseAction<TType extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> {
|
|
3
3
|
readonly type: TType;
|
|
4
4
|
readonly args: TArgs;
|
|
@@ -8,6 +8,9 @@ export interface ERC20ApprovalAction extends BaseAction<"erc20Approval", {
|
|
|
8
8
|
amount: bigint;
|
|
9
9
|
}> {
|
|
10
10
|
}
|
|
11
|
+
export interface ERC20PermitAction {
|
|
12
|
+
sign: (client: Client, userAddress: Address) => Promise<Hex>;
|
|
13
|
+
}
|
|
11
14
|
export interface VaultV2DepositAction extends BaseAction<"vaultV2Deposit", {
|
|
12
15
|
vault: Address;
|
|
13
16
|
assets: bigint;
|
|
@@ -34,3 +37,43 @@ export interface Transaction<TAction extends BaseAction = TransactionAction> {
|
|
|
34
37
|
readonly data: Hex;
|
|
35
38
|
readonly action: TAction;
|
|
36
39
|
}
|
|
40
|
+
export interface PermitArgs {
|
|
41
|
+
owner: Address;
|
|
42
|
+
nonce: bigint;
|
|
43
|
+
asset: Address;
|
|
44
|
+
signature: Hex;
|
|
45
|
+
amount: bigint;
|
|
46
|
+
deadline: bigint;
|
|
47
|
+
}
|
|
48
|
+
export interface Permit2Args {
|
|
49
|
+
owner: Address;
|
|
50
|
+
nonce: bigint;
|
|
51
|
+
asset: Address;
|
|
52
|
+
signature: Hex;
|
|
53
|
+
amount: bigint;
|
|
54
|
+
deadline: bigint;
|
|
55
|
+
expiration: bigint;
|
|
56
|
+
}
|
|
57
|
+
export interface Requirement {
|
|
58
|
+
sign: (client: Client, userAddress: Address) => Promise<RequirementSignature>;
|
|
59
|
+
action: PermitAction | Permit2Action;
|
|
60
|
+
}
|
|
61
|
+
export interface PermitAction extends BaseAction<"permit", {
|
|
62
|
+
spender: Address;
|
|
63
|
+
amount: bigint;
|
|
64
|
+
deadline: bigint;
|
|
65
|
+
}> {
|
|
66
|
+
}
|
|
67
|
+
export interface Permit2Action extends BaseAction<"permit2", {
|
|
68
|
+
spender: Address;
|
|
69
|
+
amount: bigint;
|
|
70
|
+
deadline: bigint;
|
|
71
|
+
expiration: bigint;
|
|
72
|
+
}> {
|
|
73
|
+
}
|
|
74
|
+
export interface RequirementSignature {
|
|
75
|
+
args: PermitArgs | Permit2Args;
|
|
76
|
+
action: PermitAction | Permit2Action;
|
|
77
|
+
}
|
|
78
|
+
export declare function isRequirementApproval(requirement: Transaction<ERC20ApprovalAction> | Requirement | undefined): requirement is Transaction<ERC20ApprovalAction>;
|
|
79
|
+
export declare function isRequirementSignature(requirement: Transaction<ERC20ApprovalAction> | Requirement | undefined): requirement is Requirement;
|
package/lib/types/action.js
CHANGED
|
@@ -1,2 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRequirementApproval = isRequirementApproval;
|
|
4
|
+
exports.isRequirementSignature = isRequirementSignature;
|
|
5
|
+
function isRequirementApproval(requirement) {
|
|
6
|
+
return (requirement !== undefined &&
|
|
7
|
+
"to" in requirement &&
|
|
8
|
+
"value" in requirement &&
|
|
9
|
+
"data" in requirement);
|
|
10
|
+
}
|
|
11
|
+
function isRequirementSignature(requirement) {
|
|
12
|
+
return (requirement !== undefined &&
|
|
13
|
+
"sign" in requirement &&
|
|
14
|
+
typeof requirement.sign === "function");
|
|
15
|
+
}
|
package/lib/types/client.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ import type { VaultV2Actions } from "../entities";
|
|
|
3
3
|
import type { Metadata } from "./index";
|
|
4
4
|
export interface MorphoClientType {
|
|
5
5
|
readonly viemClient: Client;
|
|
6
|
-
readonly
|
|
6
|
+
readonly options: {
|
|
7
|
+
readonly supportSignature: boolean;
|
|
8
|
+
readonly metadata?: Metadata;
|
|
9
|
+
};
|
|
7
10
|
vaultV2: (vault: Address, chainId: number) => VaultV2Actions;
|
|
8
11
|
}
|
package/lib/types/error.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import type { Address } from "viem";
|
|
2
|
-
export declare class MissingAddressError extends Error {
|
|
3
|
-
constructor();
|
|
4
|
-
}
|
|
5
|
-
export declare class MissingChainIdError extends Error {
|
|
6
|
-
constructor();
|
|
7
|
-
}
|
|
8
2
|
export declare class ZeroAssetAmountError extends Error {
|
|
9
3
|
constructor(asset: Address);
|
|
10
4
|
}
|
|
@@ -14,6 +8,15 @@ export declare class ZeroSharesAmountError extends Error {
|
|
|
14
8
|
export declare class ZeroMaxSharePriceError extends Error {
|
|
15
9
|
constructor(vault: Address);
|
|
16
10
|
}
|
|
11
|
+
export declare class AddressMismatchError extends Error {
|
|
12
|
+
constructor(clientAddress: Address, argsAddress: Address);
|
|
13
|
+
}
|
|
17
14
|
export declare class ChainIdMismatchError extends Error {
|
|
18
15
|
constructor(clientChainId: number | undefined, argsChainId: number);
|
|
19
16
|
}
|
|
17
|
+
export declare class MissingClientPropertyError extends Error {
|
|
18
|
+
constructor(property: string);
|
|
19
|
+
}
|
|
20
|
+
export declare class ApprovalAmountLessThanSpendAmountError extends Error {
|
|
21
|
+
constructor();
|
|
22
|
+
}
|
package/lib/types/error.js
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class MissingAddressError extends Error {
|
|
5
|
-
constructor() {
|
|
6
|
-
super("User address not found.");
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.MissingAddressError = MissingAddressError;
|
|
10
|
-
class MissingChainIdError extends Error {
|
|
11
|
-
constructor() {
|
|
12
|
-
super("Chain ID not found.");
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.MissingChainIdError = MissingChainIdError;
|
|
3
|
+
exports.ApprovalAmountLessThanSpendAmountError = exports.MissingClientPropertyError = exports.ChainIdMismatchError = exports.AddressMismatchError = exports.ZeroMaxSharePriceError = exports.ZeroSharesAmountError = exports.ZeroAssetAmountError = void 0;
|
|
16
4
|
class ZeroAssetAmountError extends Error {
|
|
17
5
|
constructor(asset) {
|
|
18
6
|
super(`Asset amount cannot be zero for address: ${asset}`);
|
|
@@ -31,9 +19,27 @@ class ZeroMaxSharePriceError extends Error {
|
|
|
31
19
|
}
|
|
32
20
|
}
|
|
33
21
|
exports.ZeroMaxSharePriceError = ZeroMaxSharePriceError;
|
|
22
|
+
class AddressMismatchError extends Error {
|
|
23
|
+
constructor(clientAddress, argsAddress) {
|
|
24
|
+
super(`Address mismatch between client: ${clientAddress} and args: ${argsAddress}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.AddressMismatchError = AddressMismatchError;
|
|
34
28
|
class ChainIdMismatchError extends Error {
|
|
35
29
|
constructor(clientChainId, argsChainId) {
|
|
36
30
|
super(`Chain ID mismatch between client: ${clientChainId} and args: ${argsChainId}`);
|
|
37
31
|
}
|
|
38
32
|
}
|
|
39
33
|
exports.ChainIdMismatchError = ChainIdMismatchError;
|
|
34
|
+
class MissingClientPropertyError extends Error {
|
|
35
|
+
constructor(property) {
|
|
36
|
+
super(`A required ${property} is missing from the client.`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.MissingClientPropertyError = MissingClientPropertyError;
|
|
40
|
+
class ApprovalAmountLessThanSpendAmountError extends Error {
|
|
41
|
+
constructor() {
|
|
42
|
+
super("Approval amount is less than spend amount");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.ApprovalAmountLessThanSpendAmountError = ApprovalAmountLessThanSpendAmountError;
|
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.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"author": "Morpho Association <contact@morpho.org>",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Foulks-Plb <https://x.com/FoulkPlb>"
|
|
@@ -18,29 +18,27 @@
|
|
|
18
18
|
"lib"
|
|
19
19
|
],
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@actions/core": "^1.11.1",
|
|
22
21
|
"@biomejs/biome": "2.3.3",
|
|
23
|
-
"@
|
|
22
|
+
"@changesets/cli": "^2.29.8",
|
|
24
23
|
"@morpho-org/test": "2.6.3",
|
|
25
24
|
"@types/node": "^24.9.1",
|
|
26
25
|
"@vitest/coverage-v8": "3.2.4",
|
|
27
26
|
"@vitest/ui": "3.2.4",
|
|
28
|
-
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
29
|
-
"conventional-changelog-writer": "^8.2.0",
|
|
30
|
-
"conventional-recommended-bump": "^11.2.0",
|
|
31
27
|
"dotenv": "^17.2.3",
|
|
32
28
|
"husky": "^9.1.7",
|
|
33
29
|
"typescript": "^5.7.2",
|
|
34
|
-
"viem": "^2.
|
|
30
|
+
"viem": "^2.41.2",
|
|
35
31
|
"vitest": "^3.0.5"
|
|
36
32
|
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
37
36
|
"dependencies": {
|
|
38
37
|
"@morpho-org/blue-sdk": "5.7.0",
|
|
39
38
|
"@morpho-org/blue-sdk-viem": "4.1.2",
|
|
40
39
|
"@morpho-org/bundler-sdk-viem": "4.1.2",
|
|
41
40
|
"@morpho-org/morpho-ts": "^2.4.5",
|
|
42
41
|
"@morpho-org/simulation-sdk": "3.2.2",
|
|
43
|
-
"semver": "^7.7.3",
|
|
44
42
|
"zod": "^4.1.12"
|
|
45
43
|
},
|
|
46
44
|
"scripts": {
|
|
@@ -52,7 +50,10 @@
|
|
|
52
50
|
"build:link": "pnpm run build && pnpm link --global",
|
|
53
51
|
"test": "vitest",
|
|
54
52
|
"test:ui": "vitest --ui",
|
|
55
|
-
"test:coverage": "vitest --coverage"
|
|
53
|
+
"test:coverage": "vitest --coverage",
|
|
54
|
+
"changeset": "changeset",
|
|
55
|
+
"version": "changeset version",
|
|
56
|
+
"release": "changeset publish"
|
|
56
57
|
},
|
|
57
58
|
"types": "lib/index.d.ts"
|
|
58
59
|
}
|
package/lib/sdk/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Morpho SDK Unified Exports
|
|
3
|
-
*
|
|
4
|
-
* This module provides a unified interface to all Morpho packages.
|
|
5
|
-
* Users can import any functionality from any Morpho package through this single entry point.
|
|
6
|
-
*/
|
|
7
|
-
export * from "@morpho-org/blue-sdk";
|
|
8
|
-
export * from "@morpho-org/blue-sdk-viem";
|
|
9
|
-
export * from "@morpho-org/bundler-sdk-viem";
|
|
10
|
-
export * from "@morpho-org/morpho-ts";
|
|
File without changes
|