### Local Development Setup Source: https://github.com/morpho-org/sdks/blob/main/packages/wdk-protocol-lending-morpho-evm/README.md Install dependencies and run tests for local development from a checkout of the morpho-org/sdks repository. ```bash pnpm install pnpm --filter @morpho-org/wdk-protocol-lending-morpho-evm test ``` -------------------------------- ### Install @morpho-org/blue-sdk-viem Source: https://github.com/morpho-org/sdks/blob/main/packages/blue-sdk-viem/README.md Install the package using npm or yarn. ```bash npm install @morpho-org/blue-sdk-viem ``` ```bash yarn add @morpho-org/blue-sdk-viem ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/morpho-org/sdks/blob/main/CONTRIBUTING.md Clones the Morpho SDKs repository and installs project dependencies using pnpm. Ensures the lockfile is frozen for consistent installations. ```bash git clone https://github.com/morpho-org/sdks.git cd sdks pnpm install --frozen-lockfile ``` -------------------------------- ### Install @morpho-org/blue-sdk with yarn Source: https://github.com/morpho-org/sdks/blob/main/packages/blue-sdk/README.md Install the blue-sdk package using yarn. ```bash yarn add @morpho-org/blue-sdk ``` -------------------------------- ### Install @morpho-org/blue-sdk with npm Source: https://github.com/morpho-org/sdks/blob/main/packages/blue-sdk/README.md Install the blue-sdk package using npm. ```bash npm install @morpho-org/blue-sdk ``` -------------------------------- ### Install @morpho-org/liquidity-sdk-viem with npm Source: https://github.com/morpho-org/sdks/blob/main/packages/liquidity-sdk-viem/README.md Install the package using npm. This command adds the library to your project's dependencies. ```bash npm install @morpho-org/liquidity-sdk-viem ``` -------------------------------- ### LiquidityLoader Fetch Example Source: https://github.com/morpho-org/sdks/blob/main/packages/liquidity-sdk-viem/README.md Demonstrates how to use the LiquidityLoader to fetch liquidity data from both API and RPC sources using a Viem client. ```APIDOC ## Fetch from API or RPC ### Description This example shows how to initialize `LiquidityLoader` with a Viem client and fetch liquidity data from both the API and RPC endpoints for specified market IDs. ### Method `loader.fetch(marketId: MarketId, source: 'api' | 'rpc')` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { LiquidityLoader } from "@morpho-org/liquidity-sdk-viem"; const loader = new LiquidityLoader( client // viem client. ); const [withdrawals1, withdrawals2] = await Promise.all([ loader.fetch( "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc" as MarketId, "api" ), loader.fetch( "0xe475337d11be1db07f7c5a156e511f05d1844308e66e17d2ba5da0839d3b34d9" as MarketId, "rpc" ), ]); ``` ### Response #### Success Response (200) Returns an array containing liquidity data fetched from the specified sources. ``` -------------------------------- ### Install @morpho-org/liquidity-sdk-viem with yarn Source: https://github.com/morpho-org/sdks/blob/main/packages/liquidity-sdk-viem/README.md Install the package using yarn. This command adds the library to your project's dependencies. ```bash yarn add @morpho-org/liquidity-sdk-viem ``` -------------------------------- ### Install WDK Lending Module for Morpho EVM Source: https://github.com/morpho-org/sdks/blob/main/packages/wdk-protocol-lending-morpho-evm/README.md Install the package and its peer dependency `viem` using pnpm. ```bash pnpm add @morpho-org/wdk-protocol-lending-morpho-evm viem ``` -------------------------------- ### Install @morpho-org/test with yarn Source: https://github.com/morpho-org/sdks/blob/main/packages/test/README.md Use this command to install the package using yarn. ```bash yarn add @morpho-org/test ``` -------------------------------- ### Install morpho-ts with yarn Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-ts/README.md Install the @morpho-org/morpho-ts package using yarn. ```bash yarn add @morpho-org/morpho-ts ``` -------------------------------- ### Install morpho-ts with npm Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-ts/README.md Install the @morpho-org/morpho-ts package using npm. ```bash npm install @morpho-org/morpho-ts ``` -------------------------------- ### Install @morpho-org/morpho-test with npm Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-test/README.md Use this command to install the package using npm. ```bash npm install @morpho-org/morpho-test ``` -------------------------------- ### Install @morpho-org/morpho-test with yarn Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-test/README.md Use this command to install the package using yarn. ```bash yarn add @morpho-org/morpho-test ``` -------------------------------- ### Install Morpho SDK Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Install the Morpho SDK using pnpm. This command adds the necessary package to your project dependencies. ```bash pnpm add @morpho-org/morpho-sdk ``` -------------------------------- ### Install @morpho-org/test with npm Source: https://github.com/morpho-org/sdks/blob/main/packages/test/README.md Use this command to install the package using npm. ```bash npm install @morpho-org/test ``` -------------------------------- ### Providing Runnable Examples with JSDoc Source: https://github.com/morpho-org/sdks/blob/main/docs/jsdoc-style.md Include a single, runnable `@example` block per exported function. Use named constants, placeholder transports, and show the return type inline. Keep examples concise. ```typescript /** * @example * ```ts * import { createWalletClient, http } from "viem"; * import { mainnet } from "viem/chains"; * import { morphoViemExtension } from "@morpho-org/morpho-sdk"; * * const client = createWalletClient({ * chain: mainnet, * transport: http(), * account: borrower, * }).extend(morphoViemExtension()); * * const market = client.morpho.blue(marketParams, 1); * const positionData = await market.getPositionData(borrower); * const { buildTx } = market.borrow({ * userAddress: borrower, * amount: 1_000_000n, * positionData, * }); * const tx = buildTx(); * // tx satisfies Readonly> * ``` */ ``` -------------------------------- ### LiquidityLoader Fetch Only From API Example Source: https://github.com/morpho-org/sdks/blob/main/packages/liquidity-sdk-viem/README.md Illustrates initializing LiquidityLoader with chain ID and fetching liquidity data exclusively from the API. ```APIDOC ## Fetch only from API ### Description This example demonstrates initializing `LiquidityLoader` with configuration including the chain ID and then fetching liquidity data solely from the API for the given market IDs. ### Method `loader.fetch(marketId: MarketId)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```typescript import { ChainId } from "@morpho-org/blue-sdk"; import { LiquidityLoader } from "@morpho-org/liquidity-sdk-viem"; const loader = new LiquidityLoader({ chainId: ChainId.EthMainnet }); const [withdrawals1, withdrawals2] = await Promise.all([ loader.fetch( "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc" as MarketId ), loader.fetch( "0xe475337d11be1db07f7c5a156e511f05d1844308e66e17d2ba5da0839d3b34d9" as MarketId ), ]); ``` ### Response #### Success Response (200) Returns an array containing liquidity data fetched from the API. ``` -------------------------------- ### Initialize and Supply to Morpho Protocol Source: https://github.com/morpho-org/sdks/blob/main/packages/wdk-protocol-lending-morpho-evm/README.md Initialize the Morpho WDK lending module with a WDK-compatible EVM account and presets, then get supply requirements before supplying assets. ```javascript import MorphoProtocolEvm from '@morpho-org/wdk-protocol-lending-morpho-evm' // Use any WDK-compatible EVM wallet account instance. const morpho = new MorphoProtocolEvm(account, { presets: { earn: 'sky-money-usdt-savings', borrow: 'wsteth' } }) // Send approval requirements first when returned by the Morpho SDK. const requirements = await morpho.getSupplyRequirements({ token: '0xdAC17F958D2ee523a2206206994597C13D831ec7', amount: 1000000n }) // Then send the vault deposit transaction. await morpho.supply({ token: '0xdAC17F958D2ee523a2206206994597C13D831ec7', amount: 1000000n }) ``` -------------------------------- ### Install EVM Simulation Package Source: https://github.com/morpho-org/sdks/blob/main/packages/evm-simulation/README.md Add the @morpho-org/evm-simulation package to your project's dependencies in package.json. ```json { "dependencies": { "@morpho-org/evm-simulation": "workspace:*" } } ``` -------------------------------- ### Start Watch Build for Linked Package Source: https://github.com/morpho-org/sdks/blob/main/README.md Run this command in a separate process to continuously build the linked package as you make changes. This ensures your debugging reflects live modifications. ```bash pnpm --dir packages/blue-sdk build --watch ``` -------------------------------- ### Good Function JSDoc Example Source: https://github.com/morpho-org/sdks/blob/main/docs/jsdoc-style.md Demonstrates comprehensive JSDoc for a function, including imperative descriptions, detailed parameter explanations, error handling, and a usage example. This format is preferred for clarity and maintainability. ```typescript /** * Prepares a borrow transaction for a Morpho Blue market. * * Routed through bundler3 via `morphoBorrow`. The bundler uses the transaction initiator as * `onBehalf`. Uses `minSharePrice` to protect against share price manipulation between * transaction construction and execution. * * When `reallocations` are provided, `reallocateTo` actions are prepended to the bundle, moving * liquidity from other markets via the PublicAllocator before borrowing. Reallocation fees * accumulate in `tx.value`. * * @param params.market.chainId - The chain the market lives on. * @param params.market.marketParams - Market params (loanToken, collateralToken, oracle, irm, lltv). * @param params.args.amount - Loan asset amount to borrow, in the loan token's smallest unit. * @param params.args.receiver - Address that receives the borrowed assets. * @param params.args.minSharePrice - Minimum borrow share price (in ray). Slippage protection. * @param params.args.reallocations - Optional vault reallocations to execute before borrowing. * @param params.metadata - Optional analytics metadata attached to the bundle. * @returns A deep-frozen `Transaction` with `to`, `value`, `data`, and the * typed `action` discriminator. * @throws {NonPositiveBorrowAmountError} when `amount <= 0n`. * @throws {NonPositiveMinBorrowSharePriceError} when `minSharePrice < 0n`. * @example * ```ts * import { blueBorrow } from "@morpho-org/morpho-sdk"; * * const tx = blueBorrow({ * market: { chainId: 1, marketParams }, * args: { * amount: 1_000_000n, * receiver: borrower, * minSharePrice: 0n, * }, * }); * // tx satisfies Readonly> * ``` */ ``` -------------------------------- ### Vitest Test Structure Example Source: https://github.com/morpho-org/sdks/blob/main/CLAUDE.md Illustrates the recommended structure for Vitest tests, including happy paths, specific behaviors, and error assertions. ```typescript describe("functionName", () => { test("default", () => { /* primary happy path */ }); test("behavior: ", () => { /* edge case or variation */ }); test("error: ", async () => { await expect(fn(badInput)).rejects.toBeInstanceOf(ErrorClass); }); }); ``` -------------------------------- ### Initialize Morpho Protocol EVM SDK Source: https://github.com/morpho-org/sdks/blob/main/packages/wdk-protocol-lending-morpho-evm/README.md Instantiate the SDK with account and configuration options. Required when using explicit Morpho targets to guard against wallet chain switches. Options like `earnVaultAddress`, `borrowMarketParams`, or `borrowMarketId` necessitate passing `chainId`. ```javascript new MorphoProtocolEvm(account, options) ``` -------------------------------- ### TypeScript: Using `getRequirements` and `buildTx` Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Demonstrates the typical usage pattern for interacting with the Morpho SDK. First, call `getRequirements` to fetch necessary on-chain pre-requisites, then satisfy each requirement (e.g., sending an approval transaction or signing a permit). Finally, call `buildTx` with any collected signatures to construct the final transaction. ```typescript const { buildTx, getRequirements } = await vault.deposit({ /* ... */ }); const requirements = await getRequirements(); // → [{ type: "approval", tx: {...} }, { type: "permit", sign: async () => {...} }] // Consumer satisfies each requirement (send tx / sign permit), collects the signature, // then calls buildTx to get the final transaction: const tx = buildTx(permitSignature); ``` -------------------------------- ### Build and Test Coverage Commands Source: https://github.com/morpho-org/sdks/blob/main/CONTRIBUTING.md Provides additional useful commands for building the project and generating test coverage reports. ```bash pnpm build pnpm test:coverage pnpm coverage:report ``` -------------------------------- ### Deposit into VaultV2 Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Prepare a transaction to deposit assets into a VaultV2. This function returns a transaction builder and a function to get requirements for the transaction. ```typescript const { buildTx, getRequirements } = await vault.deposit({ amount: 1000000000000000000n, userAddress: "0xUser...", }); const requirements = await getRequirements(); const tx = buildTx(requirementSignature); ``` -------------------------------- ### Get Chain Addresses for Bundler3 Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/BUNDLER3.md Retrieves the addresses for Bundler3 and its associated GeneralAdapter1 on a specific chain. This is used to resolve the adapter contract address. ```typescript getChainAddresses(chainId).bundler3.generalAdapter1 ``` -------------------------------- ### Bad Function JSDoc Example Source: https://github.com/morpho-org/sdks/blob/main/docs/jsdoc-style.md Illustrates common mistakes in JSDoc for functions, such as sentence fragments, redundant inline types, and vague return types. ```typescript /** * Borrow. * @param {Object} params - The borrow parameters. * @returns {any} The transaction. */ export const blueBorrow = (params: BlueBorrowParams) => { … }; ``` -------------------------------- ### Borrow Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Allows a user to borrow assets from the market. It requires existing position data and returns functions to build the transaction and get necessary requirements. ```APIDOC ## Borrow ### Description Allows a user to borrow assets from the market. It requires existing position data and returns functions to build the transaction and get necessary requirements. ### Method `market.borrow(params)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **amount** (bigint) - Required - The amount of assets to borrow. - **userAddress** (string) - Required - The address of the user initiating the borrow. - **positionData** (object) - Required - Data pertaining to the user's current position. ### Request Example ```typescript const positionData = await market.getPositionData("0xUser..."); const { buildTx, getRequirements } = market.borrow({ amount: 500000000000000000n, userAddress: "0xUser...", positionData, }); const requirements = await getRequirements(); const tx = buildTx(); ``` ### Response Returns an object with `buildTx` and `getRequirements` functions. #### Success Response (200) An object containing: - **buildTx**: A function to construct the transaction. - **getRequirements**: A function to retrieve necessary authorizations or approvals. ``` -------------------------------- ### Initialize Morpho Blue Market Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Instantiate a Morpho Blue market interface using its MarketParams and the Viem client. This is the entry point for interacting with a specific Morpho Blue market. ```typescript const market = client.morpho.blue( { loanToken: "0xLoan...", collateralToken: "0xCollateral...", oracle: "0xOracle...", irm: "0xIrm...", lltv: 860000000000000000n, }, 1 ); ``` -------------------------------- ### Initialize Viem Client with Morpho Extension Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Initialize a Viem public client and extend it with the Morpho SDK functionalities. This client is used to interact with Morpho's smart contracts. ```typescript import { morphoViemExtension } from "@morpho-org/morpho-sdk"; import { createPublicClient, http } from "viem"; import { mainnet } from "viem/chains"; const client = createPublicClient({ chain: mainnet, transport: http() }).extend( morphoViemExtension(), ); ``` -------------------------------- ### Get Current Unix Timestamp with Time.timestamp Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-ts/README.md Use `Time.timestamp()` to retrieve the current Unix timestamp in seconds as a bigint. Ensure the `@morpho-org/morpho-ts` package is imported. ```typescript import { Time } from "@morpho-org/morpho-ts"; Time.timestamp(); // 1692671241n ``` -------------------------------- ### Link Local Package for Debugging Source: https://github.com/morpho-org/sdks/blob/main/README.md Use this command to link a local SDK package for runtime debugging. Ensure the path is correct relative to your project. ```bash pnpm link ../../sdks/packages/blue-sdk ``` -------------------------------- ### Fetch Liquidity Data from API or RPC Source: https://github.com/morpho-org/sdks/blob/main/packages/liquidity-sdk-viem/README.md Instantiate LiquidityLoader with a viem client and fetch withdrawal data from both API and RPC endpoints concurrently. Requires a viem client instance. ```typescript import { LiquidityLoader } from "@morpho-org/liquidity-sdk-viem"; const loader = new LiquidityLoader( client // viem client. ); const [withdrawals1, withdrawals2] = await Promise.all([ loader.fetch( "0xb323495f7e4148be5643a4ea4a8221eef163e4bccfdedc2a6f4696baacbc86cc" as MarketId, "api" ), loader.fetch( "0xe475337d11be1db07f7c5a156e511f05d1844308e66e17d2ba5da0839d3b34d9" as MarketId, "rpc" ), ]); ``` -------------------------------- ### Supply Collateral to Morpho Blue Market Source: https://github.com/morpho-org/sdks/blob/main/packages/morpho-sdk/README.md Prepare a transaction to supply collateral to a Morpho Blue market. This function returns a transaction builder and a function to get requirements for the transaction. ```typescript const { buildTx, getRequirements } = market.supplyCollateral({ amount: 1000000000000000000n, userAddress: "0xUser...", }); const requirements = await getRequirements(); const tx = buildTx(requirementSignature); ``` -------------------------------- ### Mock Viem Client for Unit Tests Source: https://github.com/morpho-org/sdks/blob/main/CLAUDE.md Demonstrates how to use a mocked Viem client for unit tests that require Viem actions. This mock uses a vi.fn-backed transport and should not be used for integration or fork tests. ```typescript import { createMockClient } from "@morpho-org/test/mock"; // ... inside a unit test const mockClient = createMockClient(); // Use mockClient where a viem Client is expected ``` -------------------------------- ### Typed Errors as Public API Example Source: https://github.com/morpho-org/sdks/blob/main/CLAUDE.md Define typed errors with specific failure modes. Preserve the 'cause' when wrapping errors and ensure error messages are instructional. This format is verbatim from BorrowExceedsSafeLtvError. ```text Borrow amount ${borrowAmount} exceeds safe maximum ${maxSafeBorrow} (LLTV minus buffer). Reduce borrow or increase collateral. ```