Programmable Ethereum Transactions Without Smart Contracts.
Turn your manual transactions into programmable, composable, and reusable actions with Plug. No more tedious contract writing or one-off scripts. Just simple, powerful sentences at your fingertips powered by:
We've been building Aave strategies in the Plug app, leaning on the broad set of market, asset, and account data Aave hands every creator by default. But we've exhausted what we can reasonably do to hedge against volatile borrowing costs.
Aave V3's one pool per asset per chain structure means a given asset has exactly one borrow rate, so there's no cheaper version of the same loan to move into. The most we can do to navigate a spiking rate is use our borrow rate guard that exits us when costs climb and re-enters when they settle, which keeps us safe but leaves us sitting on the sidelines, waiting, every time.
Then Morpho Blue support lands in Plug, and its isolated market structure, where the same collateral and debt token pair lives across many markets at many different rates, changes the game. Now, what if instead of ducking to the sidelines when rates spike and waiting for them to drop back down, we refinance our loan into a cheaper market the moment the opportunity presents itself? No exiting, waiting, or hopping to another chain.
Turns out we're not the only ones after this. The same week we seed our strategy in Discovery, the Euler team ships their own guided refinance flow as a first-party feature:
While amazing, the reality is that it took Euler's development team to ship it, for Euler. We can bring the same strategy to Morpho, or any isolated market protocol, as plug creators, in a couple days, with a fleet of reusable plugs that doesn't require us to be anyone's protocol team.
One Mirror plug checks our Morpho market constraints and, if they pass, flash-moves our loan to a cheaper market. That's the whole thing, and it looks like almost nothing. So let's climb through all the logic one level at a time and see how we reason our way to a fleet of plugs with this yacht on top orchestrating the refinance. By the end, you'll have the full fleet to repurpose, and a blueprint for creating your own.
Creating our fleet starts with a curveball. The moment we start playing with all the Morpho actions in the app, we realize the protocol doesn't hand us the core reads we're used to getting from Aave. Most of the pieces our strategy needs feel like they're missing. So before we can assemble anything, we have to identify and build the foundational primitives ourselves.
Morpho feels like a blank canvas for a creator, but thankfully Mirror lets us work with focused concerns and piece everything we need together like LEGOs: a few small, single-purpose functions, and we can assemble a fleet of plugs that support our yacht.
So we work top-down, letting the yacht tell us what it needs, so we can start laying the foundation, knowing nothing higher up works without it. And right away, we identify five core reads and conversions everything will stand on, and each carries a Morpho-specific nuance we have to get right. Because if we get the math wrong, every number stacked on top inherits the error.
Our first two primitives are conversions:
Borrow Shares to Borrow Assets turns Morpho's internal virtual-share accounting into numbers we can reason about. Morpho tracks our debt in shares rather than tokens, so to know the actual debt we owe for our refinancing checks and the flash loan that moves our position, we need to convert those shares into assets.
Our collateral needs the same kind of conversion before it means anything to us. It's only useful once it's priced in the same loan-token terms as our debt, so we build Collateral Amount to Collateral Value to return that value.
Our next three primitives give us the numbers the decision-making plugs in our fleet will rely on:
Get Health Factor, built from our collateral value, debt, and the market's liquidation LTV, tells us how much room we have in a market before a liquidation event. And we notably round it down, so it never reports us safer than we are.
Get Borrow APR, annualized from the per-second rate Morpho reports for each market, lets us compare one market's cost against another's.
And Get Market Liquidity, the gap between a market's total supplied and total borrowed assets, tells us whether a market can absorb our position.
Notably, each of these generic primitives only returns a number, but they'll help us answer the important questions we're about to start asking.
There are two core questions our refinancing strategy needs to answer, which we'll handle one at a time: "should we move" and "how do we move." And our primitives will support each.
But before we go ahead and apply any opinions to solve the "should we move" side, we first reason about what even makes a move worth it, because Morpho's market list is full of traps: a ghost market at 0.84% with twenty dollars of liquidity where our debt has nowhere to land, or a slightly cheaper market that shifts our health factor enough to leave us a candle from liquidation.
With those traps in mind, our "should we move" decision has three constraints: is the target market cheaper enough, safe enough, and liquid enough to refinance into right now.
Thankfully, we don't have to work these constraints out from scratch. We've built similar guards on Aave before: the borrow rate entry and exit guard mentioned above, the liquidity drain guard, and the rebalance to target health factor logic, each carrying the same intentions we're after now. And underneath, we know that each one of the guards is a comparison, just like our three Morpho constraints.
We also know that a comparison holds no opinion of its own: it doesn't care how big of a rate gap counts as cheaper, what health factor counts as safe, or how much liquidity is ideal. So we build the comparison logic once, as three generic functions, and we'll reuse them to check each of these constraints later.
Like our initial primitives, none of these know anything about our strategy, which is what makes them worth breaking out on their own. Find Value Delta compares any two values and returns the difference and which is larger, Check Minimum Threshold tests any value against a minimum we set, and Check Required Amount sizes any amount against a requirement with a multiplier we set. And Check Minimum Threshold even does double duty, since both our "cheaper enough" and "safe enough" constraints come down to clearing a floor, we can build it once and reuse it for both.
Now the whole foundation is in place: the reads and conversions from before and these table stakes number comparisons. So from here on, the constraints that decide whether we should refinance and the precise flash move we make when they pass can reliably use them.
Now we build the yacht itself, the one plug that does exactly what we want. It has to hold up against everything we've flagged along the way: the data Morpho never handed us, the ghost markets with nowhere for our debt to land, and the cheaper markets that quietly erode our health factor. We built our way past the first. Our constraints will handle the rest.
This is where our opinions finally come in. Each constraint combines a data primitive, one of our number comparisons, and a threshold we choose.
"Is the target market cheaper enough" is the meaty check, since comparing rates takes two markets. So we bundle Get Borrow APR for each side with Find Value Delta into a small measure, Find Borrow Rate Delta, which hands back the gap and which market wins. Check Borrow Rate Delta Vs. Min Threshold then runs that gap through Check Minimum Threshold, passing only when the target rate is cheaper by more than we require.
"Is the target market safe enough" is simpler. We assemble Check Health Factor Vs. Min Threshold to run Get Health Factor through that same Check Minimum Threshold comparison, confirming our health factor in the target market is above our minimum safe level.
"Is the target market liquid enough" calls for a different kind of check. Here, Check Market Liquidity Vs. Required Amount runs Get Market Liquidity through Check Required Amount, confirming the target market covers our debt plus the liquidity buffer we set.
Next, we wrap all three constraints into a single call the strategy can make. Check Refinancing Constraints computes our debt, runs the three checks, and returns one go or no-go decision plus the amounts the move needs if it's a go. That settles our "should we move" question.
The last piece to address, "how do we move" is simply onchain steps, all in one atomic transaction that either lands whole or reverts: we flash loan the loan token to cover our full debt, repay our old loan to free our collateral, withdraw that collateral, supply it into the cheaper market, and borrow the loan token again in the new market. Morpho's flash loan settles itself as the transaction closes, pulling back the amount it lent, which is exactly what that final borrow leaves us holding. No swap to move a / position from Market A to Market B, and Morpho charges nothing for the flash loan.
Now we're standing on the yacht we opened with.
Within days of the Morpho integration landing in the Plug app, we have a powerful fourteen-plug fleet that safely refinances our loan into a cheaper market by default, built across four levels: the primitives, the measure and check boats, the wrapper boat, and the yacht on top. It works beautifully, scoped to the two same-pair markets that commonly exist today, and every piece under it is ours and every other plug creator's to reuse across any Morpho strategy.
Throughout this process, we learned that owning our fleet means owning its foundation too, including the parts Morpho never handed us. Aave's user account data read returns a health factor already computed, math the protocol stands behind. Morpho only exposes the raw ingredients: our collateral and borrow shares, the market's asset and share totals, the oracle price, the liquidation LTV, and a per-second rate.
So we derive the debt, the health factor, and the borrow APRs ourselves, plus the math Morpho leaves to us to get right: the virtual-share accounting behind the debt and the per-second-to-annual conversion behind the rates, so nothing stacked on top inherits an error.
But honestly, that's part of the fun of being a plug creator. A protocol doesn't owe us convenience. Aave spoiled us with reads handed over ready to use, and Morpho's rawer surface is where we let that expectation go and compose what we need from what's actually there. Once we accept that, a protocol that exposes less becomes an exciting blank canvas to create on rather than a wall.
That shift is what we really walk away with. We started only comfortable building on Aave, and today we're confident working with whatever any protocol exposes, composing the foundation ourselves instead of waiting for it to be handed over.
And the one wall we hit is already coming down. In this pass, we only weighed our current market against a single target at a time, because we had no clean way to manage a broader set of market preferences. But this is exactly what pushed our team to build maps. And guess what? Maps just landed in the app, so we're immediately empowered to rework the fleet to shop a set of target markets at once. That way, our loan always sits in the single cheapest, safe market Morpho offers, refinancing the moment a better one opens up.