@morpho-org/morpho-sdk 5.9.0 → 6.0.0-next.1
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/MIGRATION-v5-to-v6.md +404 -0
- package/README.md +127 -69
- package/lib/cjs/abis.d.ts +210 -0
- package/lib/cjs/abis.js +142 -2
- package/lib/cjs/actions/blue/borrow.d.ts +62 -61
- package/lib/cjs/actions/blue/borrow.js +48 -80
- package/lib/cjs/actions/blue/common.d.ts +86 -0
- package/lib/cjs/actions/blue/common.js +205 -0
- package/lib/cjs/actions/blue/refinance.d.ts +72 -112
- package/lib/cjs/actions/blue/refinance.js +97 -199
- package/lib/cjs/actions/blue/repay.d.ts +72 -68
- package/lib/cjs/actions/blue/repay.js +56 -118
- package/lib/cjs/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/cjs/actions/blue/repayWithdrawCollateral.js +148 -137
- package/lib/cjs/actions/blue/supply.d.ts +62 -45
- package/lib/cjs/actions/blue/supply.js +94 -70
- package/lib/cjs/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/cjs/actions/blue/supplyCollateral.js +52 -69
- package/lib/cjs/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/cjs/actions/blue/supplyCollateralBorrow.js +133 -120
- package/lib/cjs/actions/blue/withdraw.d.ts +68 -79
- package/lib/cjs/actions/blue/withdraw.js +101 -97
- package/lib/cjs/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/cjs/actions/blue/withdrawCollateral.js +45 -50
- package/lib/cjs/actions/bundles/common.d.ts +200 -0
- package/lib/cjs/actions/bundles/common.js +469 -0
- package/lib/cjs/actions/bundles/index.d.ts +3 -0
- package/lib/cjs/actions/bundles/index.js +23 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/cjs/actions/bundles/resolveBundlesTokenRequirements.js +125 -0
- package/lib/cjs/actions/index.d.ts +2 -1
- package/lib/cjs/actions/index.js +1 -0
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeBorrow.js +2 -2
- package/lib/cjs/actions/midnight/takeLend.d.ts +2 -2
- package/lib/cjs/actions/midnight/takeLend.js +2 -2
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/cjs/actions/requirements/blue/getBlueAuthorizationRequirement.js +60 -19
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/cjs/actions/requirements/blue/getUnusedPermit2Nonce.js +72 -0
- package/lib/cjs/actions/requirements/blue/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/blue/index.js +1 -0
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/cjs/actions/requirements/encode/encodeBlueSignatureAuthorization.js +30 -5
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/cjs/actions/requirements/encode/encodeErc20Approval.js +8 -2
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit.js +39 -6
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/cjs/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +123 -0
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/cjs/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/cjs/actions/requirements/encode/index.d.ts +1 -0
- package/lib/cjs/actions/requirements/encode/index.js +1 -0
- package/lib/cjs/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/cjs/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/cjs/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/cjs/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/cjs/actions/signatures/getTokenRequirementActions.js +4 -0
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/cjs/actions/signatures/getVaultExitBundlesV1PermitStruct.js +18 -66
- package/lib/cjs/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/cjs/actions/vaultV1/deposit.js +110 -109
- package/lib/cjs/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/cjs/actions/vaultV1/migrateToV2.js +91 -104
- package/lib/cjs/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/cjs/actions/vaultV1/redeem.js +56 -33
- package/lib/cjs/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/cjs/actions/vaultV1/withdraw.js +86 -34
- package/lib/cjs/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/cjs/actions/vaultV2/deposit.js +111 -109
- package/lib/cjs/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/cjs/actions/vaultV2/redeem.js +56 -34
- package/lib/cjs/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/cjs/actions/vaultV2/withdraw.js +86 -35
- package/lib/cjs/bundler/actions.d.ts +4 -4
- package/lib/cjs/bundler/actions.js +4 -4
- package/lib/cjs/client/morphoViemExtension.d.ts +2 -1
- package/lib/cjs/client/morphoViemExtension.js +2 -1
- package/lib/cjs/entities/blue/blue.d.ts +806 -374
- package/lib/cjs/entities/blue/blue.js +449 -696
- package/lib/cjs/entities/index.d.ts +2 -0
- package/lib/cjs/entities/index.js +15 -0
- package/lib/cjs/entities/midnight/midnight.d.ts +2 -2
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/cjs/entities/requirements/getBundlesTokenRequirements.js +203 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/cjs/entities/requirements/getVaultBundlesSharesRequirements.js +74 -0
- package/lib/cjs/entities/requirements/index.d.ts +1 -0
- package/lib/cjs/entities/requirements/index.js +17 -0
- package/lib/cjs/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/cjs/entities/vaultV1/vaultV1.js +322 -120
- package/lib/cjs/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/cjs/entities/vaultV2/vaultV2.js +219 -77
- package/lib/cjs/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/cjs/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/cjs/helpers/index.d.ts +2 -1
- package/lib/cjs/helpers/index.js +5 -1
- package/lib/cjs/helpers/referralFee.d.ts +21 -0
- package/lib/cjs/helpers/referralFee.js +35 -0
- package/lib/cjs/helpers/slippage.d.ts +90 -1
- package/lib/cjs/helpers/slippage.js +123 -0
- package/lib/cjs/helpers/validate.d.ts +16 -23
- package/lib/cjs/helpers/validate.js +41 -48
- package/lib/cjs/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/cjs/helpers/validateRequirementSpender.js +3 -0
- package/lib/cjs/index.d.ts +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/types/action.d.ts +303 -210
- package/lib/cjs/types/action.js +29 -2
- package/lib/cjs/types/client.d.ts +9 -0
- package/lib/cjs/types/error.d.ts +193 -18
- package/lib/cjs/types/error.js +267 -24
- package/lib/cjs/types/sharedLiquidity.d.ts +0 -7
- package/lib/esm/abis.d.ts +210 -0
- package/lib/esm/abis.js +140 -0
- package/lib/esm/actions/blue/borrow.d.ts +62 -61
- package/lib/esm/actions/blue/borrow.js +48 -80
- package/lib/esm/actions/blue/common.d.ts +86 -0
- package/lib/esm/actions/blue/common.js +193 -0
- package/lib/esm/actions/blue/refinance.d.ts +72 -112
- package/lib/esm/actions/blue/refinance.js +98 -200
- package/lib/esm/actions/blue/repay.d.ts +72 -68
- package/lib/esm/actions/blue/repay.js +56 -118
- package/lib/esm/actions/blue/repayWithdrawCollateral.d.ts +85 -90
- package/lib/esm/actions/blue/repayWithdrawCollateral.js +149 -138
- package/lib/esm/actions/blue/supply.d.ts +62 -45
- package/lib/esm/actions/blue/supply.js +94 -70
- package/lib/esm/actions/blue/supplyCollateral.d.ts +63 -40
- package/lib/esm/actions/blue/supplyCollateral.js +52 -69
- package/lib/esm/actions/blue/supplyCollateralBorrow.d.ts +82 -90
- package/lib/esm/actions/blue/supplyCollateralBorrow.js +134 -121
- package/lib/esm/actions/blue/withdraw.d.ts +68 -79
- package/lib/esm/actions/blue/withdraw.js +100 -96
- package/lib/esm/actions/blue/withdrawCollateral.d.ts +56 -28
- package/lib/esm/actions/blue/withdrawCollateral.js +45 -50
- package/lib/esm/actions/bundles/common.d.ts +200 -0
- package/lib/esm/actions/bundles/common.js +457 -0
- package/lib/esm/actions/bundles/index.d.ts +3 -0
- package/lib/esm/actions/bundles/index.js +2 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.d.ts +76 -0
- package/lib/esm/actions/bundles/resolveBundlesTokenRequirements.js +121 -0
- package/lib/esm/actions/index.d.ts +2 -1
- package/lib/esm/actions/index.js +1 -0
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/supplyCollateralTakeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeBorrow.d.ts +2 -2
- package/lib/esm/actions/midnight/takeBorrow.js +2 -2
- package/lib/esm/actions/midnight/takeLend.d.ts +2 -2
- package/lib/esm/actions/midnight/takeLend.js +2 -2
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.d.ts +29 -17
- package/lib/esm/actions/requirements/blue/getBlueAuthorizationRequirement.js +63 -22
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.d.ts +48 -0
- package/lib/esm/actions/requirements/blue/getUnusedPermit2Nonce.js +68 -0
- package/lib/esm/actions/requirements/blue/index.d.ts +1 -0
- package/lib/esm/actions/requirements/blue/index.js +1 -0
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.d.ts +10 -7
- package/lib/esm/actions/requirements/encode/encodeBlueSignatureAuthorization.js +31 -6
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.d.ts +2 -1
- package/lib/esm/actions/requirements/encode/encodeErc20Approval.js +9 -3
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.d.ts +16 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit.js +40 -7
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.d.ts +61 -0
- package/lib/esm/actions/requirements/encode/encodeErc20Permit2SignatureTransfer.js +119 -0
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.d.ts +4 -4
- package/lib/esm/actions/requirements/encode/encodeVaultSharesPermit.js +6 -5
- package/lib/esm/actions/requirements/encode/index.d.ts +1 -0
- package/lib/esm/actions/requirements/encode/index.js +1 -0
- package/lib/esm/actions/requirements/generalAdapter/getGeneralAdapterRequirementsPermit.d.ts +1 -1
- package/lib/esm/actions/requirements/getRequirementsApproval.d.ts +6 -4
- package/lib/esm/actions/requirements/getRequirementsApproval.js +6 -4
- package/lib/esm/actions/signatures/getTokenRequirementActions.d.ts +1 -0
- package/lib/esm/actions/signatures/getTokenRequirementActions.js +5 -1
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.d.ts +22 -30
- package/lib/esm/actions/signatures/getVaultExitBundlesV1PermitStruct.js +19 -67
- package/lib/esm/actions/vaultV1/deposit.d.ts +73 -57
- package/lib/esm/actions/vaultV1/deposit.js +112 -111
- package/lib/esm/actions/vaultV1/migrateToV2.d.ts +43 -66
- package/lib/esm/actions/vaultV1/migrateToV2.js +93 -106
- package/lib/esm/actions/vaultV1/redeem.d.ts +30 -23
- package/lib/esm/actions/vaultV1/redeem.js +56 -33
- package/lib/esm/actions/vaultV1/withdraw.d.ts +58 -24
- package/lib/esm/actions/vaultV1/withdraw.js +86 -34
- package/lib/esm/actions/vaultV2/deposit.d.ts +74 -57
- package/lib/esm/actions/vaultV2/deposit.js +113 -111
- package/lib/esm/actions/vaultV2/redeem.d.ts +30 -24
- package/lib/esm/actions/vaultV2/redeem.js +56 -34
- package/lib/esm/actions/vaultV2/withdraw.d.ts +58 -25
- package/lib/esm/actions/vaultV2/withdraw.js +86 -35
- package/lib/esm/bundler/actions.d.ts +4 -4
- package/lib/esm/bundler/actions.js +4 -4
- package/lib/esm/client/morphoViemExtension.d.ts +2 -1
- package/lib/esm/client/morphoViemExtension.js +2 -1
- package/lib/esm/entities/blue/blue.d.ts +806 -374
- package/lib/esm/entities/blue/blue.js +455 -702
- package/lib/esm/entities/index.d.ts +2 -0
- package/lib/esm/entities/index.js +1 -0
- package/lib/esm/entities/midnight/midnight.d.ts +2 -2
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.d.ts +87 -0
- package/lib/esm/entities/requirements/getBundlesTokenRequirements.js +199 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.d.ts +28 -0
- package/lib/esm/entities/requirements/getVaultBundlesSharesRequirements.js +70 -0
- package/lib/esm/entities/requirements/index.d.ts +1 -0
- package/lib/esm/entities/requirements/index.js +1 -0
- package/lib/esm/entities/vaultV1/vaultV1.d.ts +348 -98
- package/lib/esm/entities/vaultV1/vaultV1.js +326 -124
- package/lib/esm/entities/vaultV2/vaultV2.d.ts +221 -68
- package/lib/esm/entities/vaultV2/vaultV2.js +222 -80
- package/lib/esm/helpers/computeVaultV1Reallocations.d.ts +3 -9
- package/lib/esm/helpers/computeVaultV1Reallocations.js +3 -9
- package/lib/esm/helpers/index.d.ts +2 -1
- package/lib/esm/helpers/index.js +2 -1
- package/lib/esm/helpers/referralFee.d.ts +21 -0
- package/lib/esm/helpers/referralFee.js +31 -0
- package/lib/esm/helpers/slippage.d.ts +90 -1
- package/lib/esm/helpers/slippage.js +122 -2
- package/lib/esm/helpers/validate.d.ts +16 -23
- package/lib/esm/helpers/validate.js +40 -48
- package/lib/esm/helpers/validateRequirementSpender.d.ts +2 -1
- package/lib/esm/helpers/validateRequirementSpender.js +3 -0
- package/lib/esm/index.d.ts +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/types/action.d.ts +303 -210
- package/lib/esm/types/action.js +28 -2
- package/lib/esm/types/client.d.ts +9 -0
- package/lib/esm/types/error.d.ts +193 -18
- package/lib/esm/types/error.js +252 -21
- package/lib/esm/types/sharedLiquidity.d.ts +0 -7
- package/package.json +6 -5
- package/lib/cjs/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/cjs/actions/blue/buildAssetFundingActions.js +0 -63
- package/lib/cjs/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/cjs/actions/blue/buildReallocationActions.js +0 -141
- package/lib/cjs/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/cjs/actions/blue/resolveRepayFunding.js +0 -85
- package/lib/esm/actions/blue/buildAssetFundingActions.d.ts +0 -47
- package/lib/esm/actions/blue/buildAssetFundingActions.js +0 -59
- package/lib/esm/actions/blue/buildReallocationActions.d.ts +0 -69
- package/lib/esm/actions/blue/buildReallocationActions.js +0 -135
- package/lib/esm/actions/blue/resolveRepayFunding.d.ts +0 -66
- package/lib/esm/actions/blue/resolveRepayFunding.js +0 -81
package/lib/esm/actions/index.js
CHANGED
|
@@ -40,8 +40,8 @@ export interface MidnightSupplyCollateralTakeBorrowParams extends MidnightTakeBo
|
|
|
40
40
|
* chainId: 8453,
|
|
41
41
|
* market: marketData.params,
|
|
42
42
|
* collateralAssets: 2_000_000n,
|
|
43
|
-
* loanAssets:
|
|
44
|
-
* maxUnits:
|
|
43
|
+
* loanAssets: 1_000_000n,
|
|
44
|
+
* maxUnits: 1_100_000n,
|
|
45
45
|
* taker: borrower,
|
|
46
46
|
* takeableOffers: quote.data.takeableOffers,
|
|
47
47
|
* deadline: maxUint256,
|
|
@@ -41,8 +41,8 @@ import { PermitKind } from "./types.js";
|
|
|
41
41
|
* chainId: 8453,
|
|
42
42
|
* market: marketData.params,
|
|
43
43
|
* collateralAssets: 2_000_000n,
|
|
44
|
-
* loanAssets:
|
|
45
|
-
* maxUnits:
|
|
44
|
+
* loanAssets: 1_000_000n,
|
|
45
|
+
* maxUnits: 1_100_000n,
|
|
46
46
|
* taker: borrower,
|
|
47
47
|
* takeableOffers: quote.data.takeableOffers,
|
|
48
48
|
* deadline: maxUint256,
|
|
@@ -46,8 +46,8 @@ export interface MidnightTakeBorrowParams {
|
|
|
46
46
|
* const tx = midnightTakeBorrow({
|
|
47
47
|
* chainId: 8453,
|
|
48
48
|
* market: marketData.params,
|
|
49
|
-
* loanAssets:
|
|
50
|
-
* maxUnits:
|
|
49
|
+
* loanAssets: 1_000_000n,
|
|
50
|
+
* maxUnits: 1_100_000n,
|
|
51
51
|
* taker: borrower,
|
|
52
52
|
* takeableOffers: quote.data.takeableOffers,
|
|
53
53
|
* deadline: maxUint256,
|
|
@@ -37,8 +37,8 @@ import { NegativeInputError, NonPositiveInputError, } from "../../types/index.js
|
|
|
37
37
|
* const tx = midnightTakeBorrow({
|
|
38
38
|
* chainId: 8453,
|
|
39
39
|
* market: marketData.params,
|
|
40
|
-
* loanAssets:
|
|
41
|
-
* maxUnits:
|
|
40
|
+
* loanAssets: 1_000_000n,
|
|
41
|
+
* maxUnits: 1_100_000n,
|
|
42
42
|
* taker: borrower,
|
|
43
43
|
* takeableOffers: quote.data.takeableOffers,
|
|
44
44
|
* deadline: maxUint256,
|
|
@@ -46,8 +46,8 @@ export interface MidnightTakeLendParams {
|
|
|
46
46
|
* const tx = midnightTakeLend({
|
|
47
47
|
* chainId: 8453,
|
|
48
48
|
* market: marketData.params,
|
|
49
|
-
* assets:
|
|
50
|
-
* minUnits:
|
|
49
|
+
* assets: 1_000_000n,
|
|
50
|
+
* minUnits: 900_000n,
|
|
51
51
|
* taker: lender,
|
|
52
52
|
* takeableOffers: quote.data.takeableOffers,
|
|
53
53
|
* deadline: maxUint256,
|
|
@@ -38,8 +38,8 @@ import { PermitKind } from "./types.js";
|
|
|
38
38
|
* const tx = midnightTakeLend({
|
|
39
39
|
* chainId: 8453,
|
|
40
40
|
* market: marketData.params,
|
|
41
|
-
* assets:
|
|
42
|
-
* minUnits:
|
|
41
|
+
* assets: 1_000_000n,
|
|
42
|
+
* minUnits: 900_000n,
|
|
43
43
|
* taker: lender,
|
|
44
44
|
* takeableOffers: quote.data.takeableOffers,
|
|
45
45
|
* deadline: maxUint256,
|
|
@@ -2,48 +2,60 @@ import type { Client } from "viem";
|
|
|
2
2
|
import { type Address } from "viem";
|
|
3
3
|
import { type AuthorizationRequirementSignature, type BlueAuthorizationAction, type Requirement, type Transaction } from "../../../types/index.js";
|
|
4
4
|
/**
|
|
5
|
-
* Resolves whether
|
|
5
|
+
* Resolves whether a supported operator needs Blue authorization for the given user, and returns
|
|
6
6
|
* the requirement to satisfy it when it does.
|
|
7
7
|
*
|
|
8
|
-
* Reads `Morpho.isAuthorized(userAddress,
|
|
9
|
-
* any bundled Blue path that operates on behalf of the user (`borrow`, `withdraw`,
|
|
10
|
-
* `supplyCollateralBorrow`, `repayWithdrawCollateral`, `refinance`).
|
|
8
|
+
* Reads `Morpho.isAuthorized(userAddress, authorized)` on the target chain.
|
|
11
9
|
*
|
|
12
10
|
* - When `supportSignature` is falsy (default), returns the
|
|
13
|
-
* `setAuthorization(
|
|
11
|
+
* `setAuthorization(authorized, true)` transaction the user submits before the operation.
|
|
14
12
|
* - When `supportSignature` is `true`, reads the user's Morpho `nonce` and returns a signable
|
|
15
|
-
* `Requirement
|
|
16
|
-
* `setAuthorizationWithSig`,
|
|
13
|
+
* `Requirement`. The selected route consumes the result: Bundler3 emits
|
|
14
|
+
* `setAuthorizationWithSig`, while BlueBundlesV1 embeds its signed-authorization struct.
|
|
17
15
|
*
|
|
18
16
|
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
19
|
-
* @param params.chainId - Target chain id
|
|
20
|
-
* @param params.userAddress - The user
|
|
17
|
+
* @param params.chainId - Target chain id used to resolve Morpho and the default GeneralAdapter1.
|
|
18
|
+
* @param params.userAddress - The user granting authorization.
|
|
19
|
+
* @param params.authorized - Operator to authorize. Defaults to GeneralAdapter1; direct Blue
|
|
20
|
+
* writes pass the registered BlueBundlesV1 deployment. Must be the chain's registered
|
|
21
|
+
* GeneralAdapter1 or BlueBundlesV1 operator.
|
|
22
|
+
* @param params.deadline - Optional signature deadline forwarded to the authorization encoder.
|
|
21
23
|
* @param params.supportSignature - When `true`, return a signable `Requirement` instead of a
|
|
22
|
-
* transaction so
|
|
24
|
+
* transaction so the destination route can consume the signed authorization.
|
|
23
25
|
* @returns A deep-frozen `Transaction<BlueAuthorizationAction>`, a signable authorization
|
|
24
26
|
* `Requirement` (when `supportSignature` is `true`), or `null` when authorization is already in
|
|
25
27
|
* place.
|
|
26
28
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
29
|
+
* @throws {UnsupportedAuthorizationOperatorError} when `authorized` is neither the chain's
|
|
30
|
+
* GeneralAdapter1 nor its BlueBundlesV1 operator.
|
|
31
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
32
|
+
* @throws {viem.BaseError} when an authorization or nonce RPC read fails.
|
|
27
33
|
* @example
|
|
28
34
|
* ```ts
|
|
29
|
-
* import { createPublicClient, http } from "viem";
|
|
35
|
+
* import { createPublicClient, http, zeroAddress } from "viem";
|
|
30
36
|
* import { mainnet } from "viem/chains";
|
|
31
37
|
* import { getBlueAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
38
|
+
* import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
32
39
|
*
|
|
33
40
|
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
41
|
+
* const blueBundlesV1 = getChainAddress(mainnet.id, "bundles.blueBundlesV1");
|
|
34
42
|
* const requirement = await getBlueAuthorizationRequirement({
|
|
35
43
|
* viemClient: client,
|
|
36
|
-
* chainId:
|
|
37
|
-
* userAddress:
|
|
44
|
+
* chainId: mainnet.id,
|
|
45
|
+
* userAddress: zeroAddress,
|
|
38
46
|
* supportSignature: true,
|
|
47
|
+
* authorized: blueBundlesV1,
|
|
48
|
+
* deadline: 1_900_000_000n,
|
|
39
49
|
* });
|
|
40
50
|
* // requirement is null when already authorized, a Requirement when supportSignature is true,
|
|
41
51
|
* // otherwise Readonly<Transaction<BlueAuthorizationAction>>
|
|
42
52
|
* ```
|
|
43
53
|
*/
|
|
44
54
|
export declare const getBlueAuthorizationRequirement: (params: {
|
|
45
|
-
viemClient: Client;
|
|
46
|
-
chainId: number;
|
|
47
|
-
userAddress: Address;
|
|
48
|
-
supportSignature?: boolean;
|
|
55
|
+
readonly viemClient: Client;
|
|
56
|
+
readonly chainId: number;
|
|
57
|
+
readonly userAddress: Address;
|
|
58
|
+
readonly supportSignature?: boolean;
|
|
59
|
+
readonly authorized?: Address;
|
|
60
|
+
readonly deadline?: bigint;
|
|
49
61
|
}) => Promise<Readonly<Transaction<BlueAuthorizationAction>> | Requirement<AuthorizationRequirementSignature> | null>;
|
|
@@ -1,44 +1,54 @@
|
|
|
1
1
|
import { getChainAddresses } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { blueAbi } from "@morpho-org/blue-sdk-viem";
|
|
3
|
-
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
4
|
-
import { encodeFunctionData, publicActions } from "viem";
|
|
5
|
-
import { ChainIdMismatchError, } from "../../../types/index.js";
|
|
3
|
+
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
4
|
+
import { encodeFunctionData, isAddressEqual, maxUint256, publicActions, } from "viem";
|
|
5
|
+
import { ChainIdMismatchError, ExpiredDeadlineError, InputExceedsMaxError, NonPositiveInputError, UnsupportedAuthorizationOperatorError, } from "../../../types/index.js";
|
|
6
6
|
import { encodeBlueSignatureAuthorization } from "../encode/encodeBlueSignatureAuthorization.js";
|
|
7
7
|
/**
|
|
8
|
-
* Resolves whether
|
|
8
|
+
* Resolves whether a supported operator needs Blue authorization for the given user, and returns
|
|
9
9
|
* the requirement to satisfy it when it does.
|
|
10
10
|
*
|
|
11
|
-
* Reads `Morpho.isAuthorized(userAddress,
|
|
12
|
-
* any bundled Blue path that operates on behalf of the user (`borrow`, `withdraw`,
|
|
13
|
-
* `supplyCollateralBorrow`, `repayWithdrawCollateral`, `refinance`).
|
|
11
|
+
* Reads `Morpho.isAuthorized(userAddress, authorized)` on the target chain.
|
|
14
12
|
*
|
|
15
13
|
* - When `supportSignature` is falsy (default), returns the
|
|
16
|
-
* `setAuthorization(
|
|
14
|
+
* `setAuthorization(authorized, true)` transaction the user submits before the operation.
|
|
17
15
|
* - When `supportSignature` is `true`, reads the user's Morpho `nonce` and returns a signable
|
|
18
|
-
* `Requirement
|
|
19
|
-
* `setAuthorizationWithSig`,
|
|
16
|
+
* `Requirement`. The selected route consumes the result: Bundler3 emits
|
|
17
|
+
* `setAuthorizationWithSig`, while BlueBundlesV1 embeds its signed-authorization struct.
|
|
20
18
|
*
|
|
21
19
|
* @param params.viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
22
|
-
* @param params.chainId - Target chain id
|
|
23
|
-
* @param params.userAddress - The user
|
|
20
|
+
* @param params.chainId - Target chain id used to resolve Morpho and the default GeneralAdapter1.
|
|
21
|
+
* @param params.userAddress - The user granting authorization.
|
|
22
|
+
* @param params.authorized - Operator to authorize. Defaults to GeneralAdapter1; direct Blue
|
|
23
|
+
* writes pass the registered BlueBundlesV1 deployment. Must be the chain's registered
|
|
24
|
+
* GeneralAdapter1 or BlueBundlesV1 operator.
|
|
25
|
+
* @param params.deadline - Optional signature deadline forwarded to the authorization encoder.
|
|
24
26
|
* @param params.supportSignature - When `true`, return a signable `Requirement` instead of a
|
|
25
|
-
* transaction so
|
|
27
|
+
* transaction so the destination route can consume the signed authorization.
|
|
26
28
|
* @returns A deep-frozen `Transaction<BlueAuthorizationAction>`, a signable authorization
|
|
27
29
|
* `Requirement` (when `supportSignature` is `true`), or `null` when authorization is already in
|
|
28
30
|
* place.
|
|
29
31
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
32
|
+
* @throws {UnsupportedAuthorizationOperatorError} when `authorized` is neither the chain's
|
|
33
|
+
* GeneralAdapter1 nor its BlueBundlesV1 operator.
|
|
34
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
35
|
+
* @throws {viem.BaseError} when an authorization or nonce RPC read fails.
|
|
30
36
|
* @example
|
|
31
37
|
* ```ts
|
|
32
|
-
* import { createPublicClient, http } from "viem";
|
|
38
|
+
* import { createPublicClient, http, zeroAddress } from "viem";
|
|
33
39
|
* import { mainnet } from "viem/chains";
|
|
34
40
|
* import { getBlueAuthorizationRequirement } from "@morpho-org/morpho-sdk";
|
|
41
|
+
* import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
35
42
|
*
|
|
36
43
|
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
44
|
+
* const blueBundlesV1 = getChainAddress(mainnet.id, "bundles.blueBundlesV1");
|
|
37
45
|
* const requirement = await getBlueAuthorizationRequirement({
|
|
38
46
|
* viemClient: client,
|
|
39
|
-
* chainId:
|
|
40
|
-
* userAddress:
|
|
47
|
+
* chainId: mainnet.id,
|
|
48
|
+
* userAddress: zeroAddress,
|
|
41
49
|
* supportSignature: true,
|
|
50
|
+
* authorized: blueBundlesV1,
|
|
51
|
+
* deadline: 1_900_000_000n,
|
|
42
52
|
* });
|
|
43
53
|
* // requirement is null when already authorized, a Requirement when supportSignature is true,
|
|
44
54
|
* // otherwise Readonly<Transaction<BlueAuthorizationAction>>
|
|
@@ -49,9 +59,39 @@ export const getBlueAuthorizationRequirement = async (params) => {
|
|
|
49
59
|
if (viemClient.chain?.id !== chainId) {
|
|
50
60
|
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
51
61
|
}
|
|
52
|
-
const { morpho, bundler3: { generalAdapter1 }, } = getChainAddresses(chainId);
|
|
62
|
+
const { morpho, bundler3: { generalAdapter1 }, bundles, } = getChainAddresses(chainId);
|
|
63
|
+
const authorized = params.authorized ?? generalAdapter1;
|
|
64
|
+
// The SDK only ever authorizes the chain's registered GeneralAdapter1 or BlueBundlesV1 operator;
|
|
65
|
+
// reject any other override so a misconfigured `authorized` cannot grant an arbitrary address
|
|
66
|
+
// control over the user's Morpho positions.
|
|
67
|
+
const supportedOperators = [generalAdapter1];
|
|
68
|
+
if (bundles?.blueBundlesV1 != null) {
|
|
69
|
+
supportedOperators.push(bundles.blueBundlesV1);
|
|
70
|
+
}
|
|
71
|
+
if (!supportedOperators.some((operator) => isAddressEqual(operator, authorized))) {
|
|
72
|
+
throw new UnsupportedAuthorizationOperatorError(authorized, chainId);
|
|
73
|
+
}
|
|
53
74
|
const pc = viemClient.extend(publicActions);
|
|
54
75
|
if (supportSignature) {
|
|
76
|
+
// The forwarded deadline is only consumed on the signable path; reject an invalid or expired
|
|
77
|
+
// one before the RPC reads so the caller never signs an authorization that cannot be encoded
|
|
78
|
+
// or would revert on-chain. An omitted deadline defaults downstream to two hours from now.
|
|
79
|
+
if (params.deadline != null) {
|
|
80
|
+
if (params.deadline <= 0n) {
|
|
81
|
+
throw new NonPositiveInputError("deadline", params.deadline);
|
|
82
|
+
}
|
|
83
|
+
if (params.deadline > maxUint256) {
|
|
84
|
+
throw new InputExceedsMaxError({
|
|
85
|
+
field: "deadline",
|
|
86
|
+
value: params.deadline,
|
|
87
|
+
max: maxUint256,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const timestamp = Time.timestamp();
|
|
91
|
+
if (params.deadline <= timestamp) {
|
|
92
|
+
throw new ExpiredDeadlineError(params.deadline, timestamp);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
55
95
|
// The signable path needs the user's Morpho nonce; fetch it alongside the
|
|
56
96
|
// authorization status so both reads share a round-trip (batched into a
|
|
57
97
|
// single multicall when the client enables batching) instead of
|
|
@@ -61,7 +101,7 @@ export const getBlueAuthorizationRequirement = async (params) => {
|
|
|
61
101
|
address: morpho,
|
|
62
102
|
abi: blueAbi,
|
|
63
103
|
functionName: "isAuthorized",
|
|
64
|
-
args: [userAddress,
|
|
104
|
+
args: [userAddress, authorized],
|
|
65
105
|
}),
|
|
66
106
|
pc.readContract({
|
|
67
107
|
address: morpho,
|
|
@@ -74,16 +114,17 @@ export const getBlueAuthorizationRequirement = async (params) => {
|
|
|
74
114
|
return null;
|
|
75
115
|
}
|
|
76
116
|
return encodeBlueSignatureAuthorization(viemClient, {
|
|
77
|
-
authorized
|
|
117
|
+
authorized,
|
|
78
118
|
chainId,
|
|
79
119
|
nonce,
|
|
120
|
+
deadline: params.deadline,
|
|
80
121
|
});
|
|
81
122
|
}
|
|
82
123
|
const isAuthorized = await pc.readContract({
|
|
83
124
|
address: morpho,
|
|
84
125
|
abi: blueAbi,
|
|
85
126
|
functionName: "isAuthorized",
|
|
86
|
-
args: [userAddress,
|
|
127
|
+
args: [userAddress, authorized],
|
|
87
128
|
});
|
|
88
129
|
if (isAuthorized) {
|
|
89
130
|
return null;
|
|
@@ -93,13 +134,13 @@ export const getBlueAuthorizationRequirement = async (params) => {
|
|
|
93
134
|
data: encodeFunctionData({
|
|
94
135
|
abi: blueAbi,
|
|
95
136
|
functionName: "setAuthorization",
|
|
96
|
-
args: [
|
|
137
|
+
args: [authorized, true],
|
|
97
138
|
}),
|
|
98
139
|
value: 0n,
|
|
99
140
|
action: {
|
|
100
141
|
type: "blueAuthorization",
|
|
101
142
|
args: {
|
|
102
|
-
authorized
|
|
143
|
+
authorized,
|
|
103
144
|
isAuthorized: true,
|
|
104
145
|
},
|
|
105
146
|
},
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Address, type Client } from "viem";
|
|
2
|
+
/** Parameters for {@link getUnusedPermit2Nonce}. */
|
|
3
|
+
export interface GetUnusedPermit2NonceParams {
|
|
4
|
+
/** Account that will sign the Permit2 SignatureTransfer. */
|
|
5
|
+
readonly owner: Address;
|
|
6
|
+
/** Target chain id; must match the connected client chain. */
|
|
7
|
+
readonly chainId: number;
|
|
8
|
+
/** Lowest nonce to consider; defaults to 0. */
|
|
9
|
+
readonly startNonce?: bigint;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Finds the lowest unused Permit2 unordered nonce for `owner`, ready to pass as `permit2Nonce` to
|
|
13
|
+
* {@link getBundlesTokenRequirements} or as `nonce` to {@link encodeErc20Permit2SignatureTransfer}.
|
|
14
|
+
*
|
|
15
|
+
* Permit2 SignatureTransfer consumes an unordered 256-bit nonce; a nonce is free when its bit in
|
|
16
|
+
* `nonceBitmap(owner, word)` is unset. This scans consecutive bitmap words starting at `startNonce`
|
|
17
|
+
* (one RPC read per 256 nonces) and returns the first free nonce, so integrators do not reimplement
|
|
18
|
+
* the scan. Concurrent flows for the same owner should advance `startNonce` (or read once, then
|
|
19
|
+
* partition locally) so two signatures never race on the same owner-global nonce.
|
|
20
|
+
*
|
|
21
|
+
* @param viemClient - Connected viem client used for the bitmap reads.
|
|
22
|
+
* @param params - Lookup parameters.
|
|
23
|
+
* @param params.owner - Account that will sign the SignatureTransfer.
|
|
24
|
+
* @param params.chainId - Target chain id; must match the client chain.
|
|
25
|
+
* @param params.startNonce - Lowest nonce to consider; defaults to 0.
|
|
26
|
+
* @returns The lowest unused Permit2 nonce at or after `startNonce`.
|
|
27
|
+
* @throws {ChainIdMismatchError} when the connected client targets another chain.
|
|
28
|
+
* @throws {NegativeInputError} when `startNonce` is negative.
|
|
29
|
+
* @throws {InputExceedsMaxError} when `startNonce` exceeds `uint256`.
|
|
30
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
31
|
+
* @throws {UnknownAddressError} when the chain has no registered canonical Permit2 deployment.
|
|
32
|
+
* @throws {viem.BaseError} when a Permit2 nonce-bitmap RPC read fails.
|
|
33
|
+
* @throws {NoUnusedPermit2NonceError} when every nonce at or after `startNonce` is consumed.
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { createPublicClient, http, zeroAddress } from "viem";
|
|
37
|
+
* import { mainnet } from "viem/chains";
|
|
38
|
+
* import { getUnusedPermit2Nonce } from "@morpho-org/morpho-sdk";
|
|
39
|
+
*
|
|
40
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
41
|
+
* const permit2Nonce = await getUnusedPermit2Nonce(client, {
|
|
42
|
+
* owner: zeroAddress,
|
|
43
|
+
* chainId: mainnet.id,
|
|
44
|
+
* });
|
|
45
|
+
* // permit2Nonce satisfies bigint
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export declare const getUnusedPermit2Nonce: (viemClient: Client, params: GetUnusedPermit2NonceParams) => Promise<bigint>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { permit2Abi } from "@morpho-org/blue-sdk-viem";
|
|
2
|
+
import { getChainAddress } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { maxUint256 } from "viem";
|
|
4
|
+
import { readContract } from "viem/actions";
|
|
5
|
+
import { validateChainId } from "../../../helpers/index.js";
|
|
6
|
+
import { validateUint256Field } from "../../../helpers/validate.js";
|
|
7
|
+
import { NoUnusedPermit2NonceError } from "../../../types/index.js";
|
|
8
|
+
/** Highest Permit2 nonce-bitmap word index; each word covers 256 unordered nonces. */
|
|
9
|
+
const MAX_PERMIT2_NONCE_WORD = maxUint256 >> 8n;
|
|
10
|
+
/**
|
|
11
|
+
* Finds the lowest unused Permit2 unordered nonce for `owner`, ready to pass as `permit2Nonce` to
|
|
12
|
+
* {@link getBundlesTokenRequirements} or as `nonce` to {@link encodeErc20Permit2SignatureTransfer}.
|
|
13
|
+
*
|
|
14
|
+
* Permit2 SignatureTransfer consumes an unordered 256-bit nonce; a nonce is free when its bit in
|
|
15
|
+
* `nonceBitmap(owner, word)` is unset. This scans consecutive bitmap words starting at `startNonce`
|
|
16
|
+
* (one RPC read per 256 nonces) and returns the first free nonce, so integrators do not reimplement
|
|
17
|
+
* the scan. Concurrent flows for the same owner should advance `startNonce` (or read once, then
|
|
18
|
+
* partition locally) so two signatures never race on the same owner-global nonce.
|
|
19
|
+
*
|
|
20
|
+
* @param viemClient - Connected viem client used for the bitmap reads.
|
|
21
|
+
* @param params - Lookup parameters.
|
|
22
|
+
* @param params.owner - Account that will sign the SignatureTransfer.
|
|
23
|
+
* @param params.chainId - Target chain id; must match the client chain.
|
|
24
|
+
* @param params.startNonce - Lowest nonce to consider; defaults to 0.
|
|
25
|
+
* @returns The lowest unused Permit2 nonce at or after `startNonce`.
|
|
26
|
+
* @throws {ChainIdMismatchError} when the connected client targets another chain.
|
|
27
|
+
* @throws {NegativeInputError} when `startNonce` is negative.
|
|
28
|
+
* @throws {InputExceedsMaxError} when `startNonce` exceeds `uint256`.
|
|
29
|
+
* @throws {UnsupportedChainIdError} when the chain is absent from the address registry.
|
|
30
|
+
* @throws {UnknownAddressError} when the chain has no registered canonical Permit2 deployment.
|
|
31
|
+
* @throws {viem.BaseError} when a Permit2 nonce-bitmap RPC read fails.
|
|
32
|
+
* @throws {NoUnusedPermit2NonceError} when every nonce at or after `startNonce` is consumed.
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { createPublicClient, http, zeroAddress } from "viem";
|
|
36
|
+
* import { mainnet } from "viem/chains";
|
|
37
|
+
* import { getUnusedPermit2Nonce } from "@morpho-org/morpho-sdk";
|
|
38
|
+
*
|
|
39
|
+
* const client = createPublicClient({ chain: mainnet, transport: http() });
|
|
40
|
+
* const permit2Nonce = await getUnusedPermit2Nonce(client, {
|
|
41
|
+
* owner: zeroAddress,
|
|
42
|
+
* chainId: mainnet.id,
|
|
43
|
+
* });
|
|
44
|
+
* // permit2Nonce satisfies bigint
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export const getUnusedPermit2Nonce = async (viemClient, params) => {
|
|
48
|
+
validateChainId(viemClient.chain?.id, params.chainId);
|
|
49
|
+
const startNonce = params.startNonce ?? 0n;
|
|
50
|
+
validateUint256Field("startNonce", startNonce);
|
|
51
|
+
const permit2 = getChainAddress(params.chainId, "permit2");
|
|
52
|
+
const startWord = startNonce >> 8n;
|
|
53
|
+
for (let word = startWord; word <= MAX_PERMIT2_NONCE_WORD; word++) {
|
|
54
|
+
const bitmap = await readContract(viemClient, {
|
|
55
|
+
abi: permit2Abi,
|
|
56
|
+
address: permit2,
|
|
57
|
+
functionName: "nonceBitmap",
|
|
58
|
+
args: [params.owner, word],
|
|
59
|
+
});
|
|
60
|
+
const firstBit = word === startWord ? startNonce & 255n : 0n;
|
|
61
|
+
for (let bit = firstBit; bit <= 255n; bit++) {
|
|
62
|
+
if ((bitmap & (1n << bit)) === 0n) {
|
|
63
|
+
return (word << 8n) | bit;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
throw new NoUnusedPermit2NonceError(params.owner, startNonce);
|
|
68
|
+
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Address } from "@morpho-org/blue-sdk";
|
|
2
|
-
import type
|
|
2
|
+
import { type Client } from "viem";
|
|
3
3
|
import { type AuthorizationRequirementSignature, type Requirement } from "../../../types/index.js";
|
|
4
4
|
/** Parameters for {@link encodeBlueSignatureAuthorization}. */
|
|
5
5
|
interface EncodeBlueSignatureAuthorizationParams {
|
|
6
|
-
/**
|
|
6
|
+
/** Operator to authorize on Morpho, such as GeneralAdapter1 or BlueBundlesV1. */
|
|
7
7
|
authorized: Address;
|
|
8
8
|
/** Target chain id; must match `viemClient.chain.id`. */
|
|
9
9
|
chainId: number;
|
|
@@ -16,23 +16,26 @@ interface EncodeBlueSignatureAuthorizationParams {
|
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
|
|
19
|
-
* Morpho on the signer's behalf through `setAuthorizationWithSig
|
|
20
|
-
*
|
|
19
|
+
* Morpho on the signer's behalf. Bundler3 consumes the result through `setAuthorizationWithSig`;
|
|
20
|
+
* BlueBundlesV1 embeds the same signed authorization in its direct-call struct.
|
|
21
21
|
*
|
|
22
22
|
* The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
|
|
23
23
|
* typed data, verifies it against the connected account, and returns a deep-frozen
|
|
24
|
-
* `RequirementSignature` the
|
|
25
|
-
* `Time.timestamp()`.
|
|
24
|
+
* `RequirementSignature` the selected transaction route consumes. Deadline defaults to two hours
|
|
25
|
+
* from `Time.timestamp()`.
|
|
26
26
|
*
|
|
27
27
|
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
28
28
|
* @param params - Authorization encoding parameters.
|
|
29
|
-
* @param params.authorized -
|
|
29
|
+
* @param params.authorized - Operator to authorize, such as GeneralAdapter1 or BlueBundlesV1.
|
|
30
30
|
* @param params.chainId - Target chain id.
|
|
31
31
|
* @param params.nonce - The signer's current Morpho authorization nonce.
|
|
32
32
|
* @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
|
|
33
33
|
* @param params.deadline - Optional signature deadline in seconds.
|
|
34
34
|
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
35
35
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
36
|
+
* @throws {NonPositiveInputError} when a provided `deadline` is not positive.
|
|
37
|
+
* @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
|
|
38
|
+
* @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
|
|
36
39
|
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
37
40
|
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
38
41
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
import { getAuthorizationTypedData } from "@morpho-org/blue-sdk-viem";
|
|
2
2
|
import { deepFreeze, Time } from "@morpho-org/morpho-ts";
|
|
3
|
+
import { maxUint256 } from "viem";
|
|
3
4
|
import { signAndVerifyTypedData } from "../../../helpers/signAndVerifyTypedData.js";
|
|
4
|
-
import { ChainIdMismatchError, } from "../../../types/index.js";
|
|
5
|
+
import { ChainIdMismatchError, ExpiredDeadlineError, InputExceedsMaxError, NonPositiveInputError, } from "../../../types/index.js";
|
|
5
6
|
/**
|
|
6
7
|
* Builds a Morpho authorization `Requirement` that, once signed, lets `authorized` operate on
|
|
7
|
-
* Morpho on the signer's behalf through `setAuthorizationWithSig
|
|
8
|
-
*
|
|
8
|
+
* Morpho on the signer's behalf. Bundler3 consumes the result through `setAuthorizationWithSig`;
|
|
9
|
+
* BlueBundlesV1 embeds the same signed authorization in its direct-call struct.
|
|
9
10
|
*
|
|
10
11
|
* The returned `Requirement.sign()` produces the EIP-712 signature over Morpho's `Authorization`
|
|
11
12
|
* typed data, verifies it against the connected account, and returns a deep-frozen
|
|
12
|
-
* `RequirementSignature` the
|
|
13
|
-
* `Time.timestamp()`.
|
|
13
|
+
* `RequirementSignature` the selected transaction route consumes. Deadline defaults to two hours
|
|
14
|
+
* from `Time.timestamp()`.
|
|
14
15
|
*
|
|
15
16
|
* @param viemClient - Connected viem `Client` whose `chain.id` matches `params.chainId`.
|
|
16
17
|
* @param params - Authorization encoding parameters.
|
|
17
|
-
* @param params.authorized -
|
|
18
|
+
* @param params.authorized - Operator to authorize, such as GeneralAdapter1 or BlueBundlesV1.
|
|
18
19
|
* @param params.chainId - Target chain id.
|
|
19
20
|
* @param params.nonce - The signer's current Morpho authorization nonce.
|
|
20
21
|
* @param params.isAuthorized - Grant (`true`, default) or revoke (`false`).
|
|
21
22
|
* @param params.deadline - Optional signature deadline in seconds.
|
|
22
23
|
* @returns A `Requirement` whose `sign(client, userAddress)` produces the deep-frozen signature.
|
|
23
24
|
* @throws {ChainIdMismatchError} when `viemClient.chain?.id !== params.chainId`.
|
|
25
|
+
* @throws {NonPositiveInputError} when a provided `deadline` is not positive.
|
|
26
|
+
* @throws {InputExceedsMaxError} when a provided `deadline` exceeds `uint256`.
|
|
27
|
+
* @throws {ExpiredDeadlineError} when a provided `deadline` is positive but not in the future.
|
|
24
28
|
* @throws {MissingClientPropertyError} from `sign()` when the client has no `account.address`.
|
|
25
29
|
* @throws {AddressMismatchError} from `sign()` when the client account differs from `userAddress`.
|
|
26
30
|
* @throws {InvalidSignatureError} from `sign()` when EIP-712 verification fails.
|
|
@@ -44,6 +48,27 @@ export const encodeBlueSignatureAuthorization = async (viemClient, params) => {
|
|
|
44
48
|
if (viemClient.chain?.id !== chainId) {
|
|
45
49
|
throw new ChainIdMismatchError(viemClient.chain?.id, chainId);
|
|
46
50
|
}
|
|
51
|
+
// Reject an invalid or already-expired caller-supplied deadline before signing, so a direct caller
|
|
52
|
+
// is never walked through a wallet EIP-712 prompt for an authorization Morpho would reject with
|
|
53
|
+
// `SIGNATURE_EXPIRED`. Mirrors the sibling `encodeErc20Permit2SignatureTransfer` and the
|
|
54
|
+
// `getBlueAuthorizationRequirement` resolver guards. An omitted deadline defaults to two hours
|
|
55
|
+
// from now and is always valid.
|
|
56
|
+
if (params.deadline != null) {
|
|
57
|
+
if (params.deadline <= 0n) {
|
|
58
|
+
throw new NonPositiveInputError("deadline", params.deadline);
|
|
59
|
+
}
|
|
60
|
+
if (params.deadline > maxUint256) {
|
|
61
|
+
throw new InputExceedsMaxError({
|
|
62
|
+
field: "deadline",
|
|
63
|
+
value: params.deadline,
|
|
64
|
+
max: maxUint256,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const timestamp = Time.timestamp();
|
|
68
|
+
if (params.deadline <= timestamp) {
|
|
69
|
+
throw new ExpiredDeadlineError(params.deadline, timestamp);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
47
72
|
const deadline = params.deadline ?? Time.timestamp() + Time.s.from.h(2n);
|
|
48
73
|
const action = {
|
|
49
74
|
type: "authorization",
|
|
@@ -16,10 +16,11 @@ interface EncodeErc20ApprovalParams {
|
|
|
16
16
|
* @param params - Encoding parameters.
|
|
17
17
|
* @param params.token - ERC-20 token address to approve.
|
|
18
18
|
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
|
|
19
|
-
* Midnight, MidnightBundles, or
|
|
19
|
+
* Midnight, MidnightBundles, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for the chain.
|
|
20
20
|
* @param params.amount - Allowance amount before per-token cap.
|
|
21
21
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
22
22
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
23
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
23
24
|
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
|
|
24
25
|
* @example
|
|
25
26
|
* ```ts
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MathLib } from "@morpho-org/blue-sdk";
|
|
2
2
|
import { deepFreeze } from "@morpho-org/morpho-ts";
|
|
3
|
-
import { encodeFunctionData, erc20Abi, maxUint256 } from "viem";
|
|
3
|
+
import { encodeFunctionData, erc20Abi, getAddress, maxUint256 } from "viem";
|
|
4
4
|
import { MAX_TOKEN_APPROVALS } from "../../../helpers/constant.js";
|
|
5
5
|
import { validateRequirementSpender } from "../../../helpers/validateRequirementSpender.js";
|
|
6
6
|
/**
|
|
@@ -12,10 +12,11 @@ import { validateRequirementSpender } from "../../../helpers/validateRequirement
|
|
|
12
12
|
* @param params - Encoding parameters.
|
|
13
13
|
* @param params.token - ERC-20 token address to approve.
|
|
14
14
|
* @param params.spender - Address granted the allowance. Must be GeneralAdapter1, Permit2,
|
|
15
|
-
* Midnight, MidnightBundles, or
|
|
15
|
+
* Midnight, MidnightBundles, VaultExitBundlesV1, VaultBundlesV1, or BlueBundlesV1 for the chain.
|
|
16
16
|
* @param params.amount - Allowance amount before per-token cap.
|
|
17
17
|
* @param params.chainId - The chain the transaction targets (used to resolve supported spenders and the per-token cap).
|
|
18
18
|
* @returns A deep-frozen `Transaction<ERC20ApprovalAction>` with the capped approval amount.
|
|
19
|
+
* @throws {UnsupportedChainIdError} when `chainId` is absent from the address registry.
|
|
19
20
|
* @throws {UnsupportedErc20ApprovalSpenderError} when `spender` is not a supported SDK spender for `chainId`.
|
|
20
21
|
* @example
|
|
21
22
|
* ```ts
|
|
@@ -42,9 +43,14 @@ export const encodeErc20Approval = (params) => {
|
|
|
42
43
|
"midnight",
|
|
43
44
|
"midnightBundles",
|
|
44
45
|
"vaultExitBundlesV1",
|
|
46
|
+
"vaultBundlesV1",
|
|
47
|
+
"blueBundlesV1",
|
|
45
48
|
],
|
|
46
49
|
});
|
|
47
|
-
const amountValue = MathLib.min(amount,
|
|
50
|
+
const amountValue = MathLib.min(amount,
|
|
51
|
+
// Checksum the lookup key so a differently-cased token still resolves its per-token cap
|
|
52
|
+
// instead of falling back to `maxUint256`, which UNI/ONDO/COMP/FLUID reject above `uint96`.
|
|
53
|
+
MAX_TOKEN_APPROVALS[chainId]?.[getAddress(token)] ?? maxUint256);
|
|
48
54
|
return deepFreeze({
|
|
49
55
|
to: token,
|
|
50
56
|
data: encodeFunctionData({
|