Skip to main content

Overview

Morpho Vaults are smart contracts that allow users to deposit assets into yield-generating vaults built on top of Morpho’s lending protocol. We’ll walk through the steps of using Turnkey to sign some common transactions to Morpho’s Steakhouse USDC Vault on Base Mainnet. The flow is also going to show Turnkey’s policy engine in action by restricting the signing operations only to the specific Morpho contracts. The working example can be found here.

Getting started

The first step is to set up your Turnkey organization and account. By following the Quickstart guide, you should have:
  • A root user with a public/private API key pair within the Turnkey parent organization
  • An organization ID
The next step is to create another user within the organization with a different API key and remove it from the root quorum. You can do this from the Turnkey dashboard or API. Here’s a simple script that shows how to update the root quorum using @turnkey/sdk-server. Finally, make sure you have a wallet with an Ethereum account created within this organization and have it funded with some ETH and USDC on Base Mainnet.

Setting up the policy for the non-root user

Now, we want to use the non-root user for signing transactions to Morpho and restrict it to only be able to interact with the USDC and Morpho vault smart contracts. We’ll define a new API client that would use the organization root user to create the required policy:

Set up the Turnkey signer

We’ll be using @turnkey/viem to create a Turnkey custom signer which implements the signing APIs expected by the viem client. Notice that the Turnkey API client is going to use the non-root user API key now as using a root user will bypass the policy engine evaluation.

Approve the vault to spend USDC and deposit USDC into the vault

Check user share balance and vault data

In order to see how much the user can withdraw we can call the balanceOf function if the Vault contract.
You can also call the Morpho GraphQL API and fetch the vault live data:

Withdraw from the vault or redeem the whole amount

Call the withdraw function if you want a specific amount:
Or redeem for the full shares amount: