@morpho-org/consumer-sdk 0.1.0 → 0.1.2

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.
@@ -1,4 +1,4 @@
1
- import { type Address } from "@morpho-org/blue-sdk";
1
+ import type { Address } from "@morpho-org/blue-sdk";
2
2
  import { type Requirement } from "../../../types";
3
3
  interface EncodeErc20PermitParams {
4
4
  token: Address;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.encodeErc20Permit = void 0;
4
- const blue_sdk_1 = require("@morpho-org/blue-sdk");
5
4
  const blue_sdk_viem_1 = require("@morpho-org/blue-sdk-viem");
6
5
  const morpho_ts_1 = require("@morpho-org/morpho-ts");
7
6
  const viem_1 = require("viem");
@@ -28,47 +27,24 @@ const encodeErc20Permit = (params) => {
28
27
  if (client.account.address !== userAddress) {
29
28
  throw new types_1.AddressMismatchError(client.account.address, userAddress);
30
29
  }
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
- }
30
+ const tokenData = await (0, blue_sdk_viem_1.fetchToken)(token, client);
31
+ const typedData = (0, blue_sdk_viem_1.getPermitTypedData)({
32
+ erc20: tokenData,
33
+ owner: userAddress,
34
+ spender,
35
+ allowance: amount,
36
+ nonce,
37
+ deadline,
38
+ }, chainId);
39
+ const signature = await (0, actions_1.signTypedData)(client, {
40
+ ...typedData,
41
+ account: client.account,
42
+ });
43
+ await (0, viem_1.verifyTypedData)({
44
+ ...typedData,
45
+ address: userAddress, // Verify against the permit's owner.
46
+ signature,
47
+ });
72
48
  return (0, morpho_ts_1.deepFreeze)({
73
49
  args: {
74
50
  owner: userAddress,
@@ -2,7 +2,6 @@ import { type Address } from "@morpho-org/blue-sdk";
2
2
  import { type Requirement } from "../../../types";
3
3
  interface EncodeErc20Permit2Params {
4
4
  token: Address;
5
- spender: Address;
6
5
  amount: bigint;
7
6
  chainId: number;
8
7
  nonce: bigint;
@@ -8,13 +8,14 @@ const viem_1 = require("viem");
8
8
  const actions_1 = require("viem/actions");
9
9
  const types_1 = require("../../../types");
10
10
  const encodeErc20Permit2 = (params) => {
11
- const { token, spender, amount, chainId, nonce, expiration = blue_sdk_1.MathLib.MAX_UINT_48, } = params;
11
+ const { token, amount, chainId, nonce, expiration = blue_sdk_1.MathLib.MAX_UINT_48, } = params;
12
12
  const now = morpho_ts_1.Time.timestamp();
13
13
  const deadline = now + morpho_ts_1.Time.s.from.h(2n);
14
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
14
15
  const action = {
15
16
  type: "permit2",
16
17
  args: {
17
- spender,
18
+ spender: generalAdapter1,
18
19
  amount,
19
20
  deadline,
20
21
  expiration,
@@ -29,7 +30,6 @@ const encodeErc20Permit2 = (params) => {
29
30
  if (client.account.address !== userAddress) {
30
31
  throw new types_1.AddressMismatchError(client.account.address, userAddress);
31
32
  }
32
- const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
33
33
  const typedData = (0, blue_sdk_viem_1.getPermit2PermitTypedData)({
34
34
  // Never permit any other address than the GeneralAdapter1 otherwise
35
35
  // the signature can be used independently.
@@ -30,10 +30,10 @@ const getRequirements = async (viemClient, params) => {
30
30
  if (viemClient.chain?.id !== chainId) {
31
31
  throw new types_1.ChainIdMismatchError(viemClient.chain?.id, chainId);
32
32
  }
33
- const { permit2, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
33
+ const { permit2, dai, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
34
34
  const { erc20Allowances, erc2612Nonce, permit2BundlerAllowance } = await (0, blue_sdk_viem_1.fetchHolding)(from, address, viemClient);
35
35
  if (supportSignature) {
36
- const supportSimplePermit = (0, morpho_ts_1.isDefined)(erc2612Nonce);
36
+ const supportSimplePermit = (0, morpho_ts_1.isDefined)(erc2612Nonce) && address !== dai;
37
37
  if (supportSimplePermit) {
38
38
  return (0, getRequirementsPermit_1.getRequirementsPermit)({
39
39
  address,
@@ -49,6 +49,7 @@ const getRequirements = async (viemClient, params) => {
49
49
  chainId,
50
50
  permit2,
51
51
  args: { amount },
52
+ allowancesGeneralAdapter: erc20Allowances["bundler3.generalAdapter1"],
52
53
  allowancesPermit2: erc20Allowances.permit2,
53
54
  allowanceGeneralAdapterPermit2: permit2BundlerAllowance.amount,
54
55
  allowanceGeneralAdapterExpiration: permit2BundlerAllowance.expiration,
@@ -6,7 +6,7 @@ const blue_sdk_1 = require("@morpho-org/blue-sdk");
6
6
  * Get the actions required to transfer the asset based on the requirement signature.
7
7
  */
8
8
  const getRequirementsAction = ({ chainId, requirementSignature, }) => {
9
- const { dai, bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
9
+ const { bundler3: { generalAdapter1 }, } = (0, blue_sdk_1.getChainAddresses)(chainId);
10
10
  if (requirementSignature.action.type === "permit2") {
11
11
  if (!("expiration" in requirementSignature.args)) {
12
12
  throw new Error("Expiration is not defined");
@@ -40,31 +40,6 @@ const getRequirementsAction = ({ chainId, requirementSignature, }) => {
40
40
  },
41
41
  ];
42
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
43
  return [
69
44
  {
70
45
  type: "permit",
@@ -15,6 +15,7 @@ import type { ERC20ApprovalAction, Requirement, Transaction } from "../../types"
15
15
  * @param params.permit2 - Permit2 contract address.
16
16
  * @param params.args - Object with:
17
17
  * @param params.args.amount - Required token amount.
18
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit2 contract.
18
19
  * @param params.allowancesPermit2 - Allowance for permit2.
19
20
  * @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
20
21
  * @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
@@ -29,6 +30,7 @@ export declare const getRequirementsPermit2: (params: {
29
30
  amount: bigint;
30
31
  };
31
32
  allowancesPermit2: bigint;
33
+ allowancesGeneralAdapter: bigint;
32
34
  allowanceGeneralAdapterPermit2: bigint;
33
35
  allowanceGeneralAdapterExpiration: bigint;
34
36
  nonce: bigint;
@@ -20,6 +20,7 @@ const getRequirementsApproval_1 = require("./getRequirementsApproval");
20
20
  * @param params.permit2 - Permit2 contract address.
21
21
  * @param params.args - Object with:
22
22
  * @param params.args.amount - Required token amount.
23
+ * @param params.allowancesGeneralAdapter - Allowance for general adapter from permit2 contract.
23
24
  * @param params.allowancesPermit2 - Allowance for permit2.
24
25
  * @param params.allowanceGeneralAdapterPermit2 - Allowance for general adapter from permit2 contract.
25
26
  * @param params.allowanceGeneralAdapterExpiration - Expiration for general adapter from permit2 contract.
@@ -27,7 +28,10 @@ const getRequirementsApproval_1 = require("./getRequirementsApproval");
27
28
  * @returns An array of approval transaction or requirement signatures objects.
28
29
  */
29
30
  const getRequirementsPermit2 = (params) => {
30
- const { address, chainId, permit2, args: { amount }, allowancesPermit2, allowanceGeneralAdapterPermit2, allowanceGeneralAdapterExpiration, nonce, } = params;
31
+ const { address, chainId, permit2, args: { amount }, allowancesPermit2, allowancesGeneralAdapter, allowanceGeneralAdapterPermit2, allowanceGeneralAdapterExpiration, nonce, } = params;
32
+ if (allowancesGeneralAdapter >= amount) {
33
+ return [];
34
+ }
31
35
  const requirements = [];
32
36
  const approvalRequirements = (0, getRequirementsApproval_1.getRequirementsApproval)({
33
37
  address,
@@ -44,7 +48,6 @@ const getRequirementsPermit2 = (params) => {
44
48
  allowanceGeneralAdapterExpiration < morpho_ts_1.Time.timestamp() + morpho_ts_1.Time.s.from.h(4n)) {
45
49
  requirements.push((0, encodeErc20Permit2_1.encodeErc20Permit2)({
46
50
  token: address,
47
- spender: permit2,
48
51
  amount,
49
52
  chainId,
50
53
  nonce,
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.1.0",
4
+ "version": "0.1.2",
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.3",
23
+ "@morpho-org/test": "2.6.5",
24
24
  "@types/node": "^24.9.1",
25
25
  "@vitest/coverage-v8": "3.2.4",
26
26
  "@vitest/ui": "3.2.4",
@@ -34,11 +34,11 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@morpho-org/blue-sdk": "5.7.0",
38
- "@morpho-org/blue-sdk-viem": "4.1.2",
39
- "@morpho-org/bundler-sdk-viem": "4.1.2",
40
- "@morpho-org/morpho-ts": "^2.4.5",
41
- "@morpho-org/simulation-sdk": "3.2.2",
37
+ "@morpho-org/blue-sdk": "^5.9.1",
38
+ "@morpho-org/blue-sdk-viem": "^4.1.4",
39
+ "@morpho-org/bundler-sdk-viem": "^4.1.3",
40
+ "@morpho-org/morpho-ts": "^2.4.6",
41
+ "@morpho-org/simulation-sdk": "^3.2.3",
42
42
  "zod": "^4.1.12"
43
43
  },
44
44
  "scripts": {