|
| 1 | +--- |
| 2 | +sidebar_position: 6 |
| 3 | +slug: morpho-setup-ts |
| 4 | +title: Morpho Setup |
| 5 | +authors: [nikerzetic] |
| 6 | +description: Fund a smart account and authorize Morpho Blue lending via the MorphoMarketShim on Coston2. |
| 7 | +tags: [intermediate, ethereum, flare-smart-accounts] |
| 8 | +keywords: |
| 9 | + [ |
| 10 | + ethereum, |
| 11 | + flare-smart-accounts, |
| 12 | + evm, |
| 13 | + flare-network, |
| 14 | + account-abstraction, |
| 15 | + morpho, |
| 16 | + defi, |
| 17 | + ] |
| 18 | +unlisted: false |
| 19 | +--- |
| 20 | + |
| 21 | +import CodeBlock from "@theme/CodeBlock"; |
| 22 | +import MorphoSetupScript from "!!raw-loader!/examples/developer-hub-javascript/smart-accounts/morpho/setup.ts"; |
| 23 | + |
| 24 | +This guide walks through the one-shot setup script for [Morpho Blue](https://docs.morpho.org/), leveraging a Flare smart account. |
| 25 | +The smart account is the borrower's end-to-end: it holds tokens, supplies collateral, borrows, and later repays via the XRPL memo field [custom instructions](/smart-accounts/custom-instruction). |
| 26 | + |
| 27 | +The setup script does two kinds of work: |
| 28 | + |
| 29 | +1. **Flare EOA transactions** — mint mock ERC-20 collateral and loan tokens to the personal account via permissionless `setBalance`. |
| 30 | +2. **XRPL memo instructions** — up to three payments that approve the `MorphoMarketShim` for both tokens and call `setAuthorization` on Morpho Blue. |
| 31 | + |
| 32 | +:::warning |
| 33 | +Run this script once before the [Morpho Borrow](/smart-accounts/guides/typescript-viem/morpho-borrow-ts) and [Morpho Repay](/smart-accounts/guides/typescript-viem/morpho-repay-ts) guides. |
| 34 | +Each step is idempotent, and skips work that has already been done on the chain. |
| 35 | +::: |
| 36 | + |
| 37 | +The full Morpho cycle lives in the [flare-viem-starter](https://github.com/flare-foundation/flare-viem-starter) repository under `src/morpho/`. |
| 38 | + |
| 39 | +## Prerequisites |
| 40 | + |
| 41 | +- An XRPL testnet wallet (`XRPL_SEED`) registered with the Flare smart-accounts operator and funded with testnet XRP. |
| 42 | +- A Flare-side EOA (`PRIVATE_KEY`) used only to call permissionless mock-token `setBalance` — not to sign Morpho operations on behalf of the smart account. |
| 43 | +- `XRPL_TESTNET_RPC_URL` pointing at an XRPL testnet node. |
| 44 | +- Familiarity with [State Lookup](/smart-accounts/guides/typescript-viem/state-lookup-ts) and [Custom Instruction](/smart-accounts/guides/typescript-viem/custom-instruction-ts). |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +Morpho Blue markets are identified by a five-field [`MarketParams`](https://docs.morpho.org/get-started/resources/contracts/morpho#market-parameters) tuple (loan token, collateral token, oracle, IRM, LLTV). |
| 49 | +Opening a position normally requires multiple Morpho calls (`supplyCollateral`, `borrow`, and so on). |
| 50 | +Each call encoded in an XRPL memo must stay under the roughly 1024-byte memo cap. |
| 51 | + |
| 52 | +The `MorphoMarketShim` contract pins the market parameters in immutable storage at deploy time. |
| 53 | +That shrinks each memo payload so a bundled operation fits in one payment: |
| 54 | + |
| 55 | +- `supplyAndBorrow` — used in the [Morpho Borrow](/smart-accounts/guides/typescript-viem/morpho-borrow-ts) guide. |
| 56 | +- `repayAndWithdrawCollateral` — used in the [Morpho Repay](/smart-accounts/guides/typescript-viem/morpho-repay-ts) guide. |
| 57 | + |
| 58 | +The smart account remains `onBehalf` for the position; borrowed assets are returned to the smart account, so the position remains self-contained. |
| 59 | + |
| 60 | +### Coston2 Deployment Addresses |
| 61 | + |
| 62 | +| Contract | Address | |
| 63 | +| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | |
| 64 | +| Morpho Blue | [`0x8aE0b3CE90F16E88063516f2d88C8ac2ab552d95`](https://coston2-explorer.flare.network/address/0x8aE0b3CE90F16E88063516f2d88C8ac2ab552d95) | |
| 65 | +| Loan token (mock) | [`0x4984B127c3065f4348858fAFdBa020f2c8633905`](https://coston2-explorer.flare.network/address/0x4984B127c3065f4348858fAFdBa020f2c8633905) | |
| 66 | +| Collateral token (mock) | [`0x98bf2F2fF322d5eb61D6aE04Df50856525a85D16`](https://coston2-explorer.flare.network/address/0x98bf2F2fF322d5eb61D6aE04Df50856525a85D16) | |
| 67 | +| Oracle (mock) | [`0x1e80830e9903c839Db803442c976DD2360D47FE0`](https://coston2-explorer.flare.network/address/0x1e80830e9903c839Db803442c976DD2360D47FE0) | |
| 68 | +| IRM (mock) | [`0xDC275701300865D882D44ffe7cb1153535636d1a`](https://coston2-explorer.flare.network/address/0xDC275701300865D882D44ffe7cb1153535636d1a) | |
| 69 | +| MorphoMarketShim | [`0x33d81a1d7986bB3AbAB4F67Ad6117233ADd6F87A`](https://coston2-explorer.flare.network/address/0x33d81a1d7986bB3AbAB4F67Ad6117233ADd6F87A) | |
| 70 | + |
| 71 | +The market uses an LLTV of `86%` (`860000000000000000` in WAD). |
| 72 | +The market ID is `keccak256(abi.encode(marketParams))`, matching Morpho Blue's `MarketParamsLib.id()`. |
| 73 | + |
| 74 | +## Full Script |
| 75 | + |
| 76 | +The script below is sets up the Morpho Blue position by funding the smart account with mock tokens, approving the shim smart contract, and authorizing the shim smart contract on Morpho Blue. |
| 77 | + |
| 78 | +<CodeBlock language="typescript" title="src/morpho/setup.ts"> |
| 79 | + {MorphoSetupScript} |
| 80 | +</CodeBlock> |
| 81 | + |
| 82 | +## Code Breakdown |
| 83 | + |
| 84 | +The numbered comments in the script above map to these steps. |
| 85 | + |
| 86 | +**1., 2. Configure and connect.** |
| 87 | +Set funding sizes in whole token units (`100` collateral, `1000` loan token), then open an XRPL client and wallet. |
| 88 | +The XRPL wallet controls the personal account; the Flare EOA is only used for mock minting in step 6. |
| 89 | + |
| 90 | +| Token | Units | Purpose | |
| 91 | +| ---------- | ------ | ---------------------------------------------- | |
| 92 | +| Collateral | `100` | Matches the supply size in the borrow script | |
| 93 | +| Loan | `1000` | Buffer for interest across borrow/repay cycles | |
| 94 | + |
| 95 | +**3., 4. Resolve addresses and log them.** |
| 96 | +In parallel, the script looks up the personal account, the memo-only XRP fee, and market decimals. |
| 97 | +It then prints the personal account, operator EOA, market ID, and shim address. |
| 98 | + |
| 99 | +**5. Snapshot before setup.** |
| 100 | +It logs the Morpho position and wallet balances. |
| 101 | +On a first run, borrow shares and position collateral should be zero. |
| 102 | + |
| 103 | +**6. Fund mock tokens on Flare.** |
| 104 | +The `mintMock` function calls permissionless `setBalance` on the Coston2 mock ERC-20s, crediting the personal account. |
| 105 | +This is a normal Flare transaction from the operator EOA without interacting with the smart account. |
| 106 | + |
| 107 | +**7. Approve and authorize the shim.** |
| 108 | +It sends up to 3 XRPL memos: unlimited `approve` for collateral and loan tokens, then `setAuthorization` for Morpho Blue. |
| 109 | +Each action uses its own memo because two calls do not fit under the 1024-byte cap. |
| 110 | + |
| 111 | +**8. Snapshot after setup.** |
| 112 | +Wallet balances should reflect the mint; Morpho position fields stay zero until [Morpho Borrow](/smart-accounts/guides/typescript-viem/morpho-borrow-ts). |
| 113 | + |
| 114 | +## Expected Output |
| 115 | + |
| 116 | +When you run the script, you should see the following output. |
| 117 | + |
| 118 | +```bash |
| 119 | +Personal account: 0x... |
| 120 | +Operator EOA: 0x... |
| 121 | +Morpho market id: 0x... |
| 122 | +Shim address: 0x33d81a1d7986bB3AbAB4F67Ad6117233ADd6F87A |
| 123 | + |
| 124 | +=== Before setup === |
| 125 | + position supply (≈loan tokens): 0 |
| 126 | + position borrow (≈loan tokens): 0 |
| 127 | + position collateral: 0 |
| 128 | + ... |
| 129 | + |
| 130 | +Funded smart account with collateral and loan tokens. |
| 131 | + |
| 132 | +[approve-collateral] XRPL transaction hash: ... |
| 133 | +[approve-loan] XRPL transaction hash: ... |
| 134 | +[set-authorization] XRPL transaction hash: ... |
| 135 | + |
| 136 | +=== After setup === |
| 137 | + smart-account collateral balance: 100 |
| 138 | + smart-account loan-token balance: 1000 |
| 139 | + ... |
| 140 | +``` |
| 141 | + |
| 142 | +You can repeat borrow and repay without re-running the setup, as the loan-token buffer covers interest. |
| 143 | +Re-run setup if allowances are revoked or the loan-token balance is exhausted. |
| 144 | + |
| 145 | +:::tip[What's next] |
| 146 | + |
| 147 | +After you have completed the setup, you can: |
| 148 | + |
| 149 | +- Open a position by running the [Morpho Borrow](/smart-accounts/guides/typescript-viem/morpho-borrow-ts) guide. |
| 150 | +- Close a position by running the [Morpho Repay](/smart-accounts/guides/typescript-viem/morpho-repay-ts) guide. |
| 151 | + |
| 152 | +::: |
0 commit comments