Note: This example integrates with Aave v3 directly. To offer yield in your product, Turnkey Earn supports Morpho vaults and Aave v3 reserves, with deposits, withdrawals, and position tracking handled by Turnkey. See
with-earn.
This example shows how to use a Turnkey wallet to interact with Aave v3 on Base mainnet, supplying and withdrawing USDC. It provides the following scripts:
deposit.tsapproves the Aave Pool contract to spend USDC and deposits USDC into Aavebalance.tsreads the aUSDC balance (eg. the deposit receipt)withdraw.tswithdraws an USDC amount from the pool
On top of it, we showcase the power of the Turnkey policy engine by allowing a non-root Turnkey user to only sign the specific transactions to the USDC and Aave Pool contract addresses:
createPolicy.tsuses an organization root user (RootQuorum) to create a specific policies for a non-root user (NONROOT_USER_ID) that will be used to sign the transactions to Aave and USDC contracts.
Key contracts addresses used on Base Mainnet come from the Aave Address Book
- USDC: AaveV3Base.ASSETS.USDC.UNDERLYING
- aUSDC: AaveV3Base.ASSETS.USDC.A_TOKEN
- Pool: AaveV3Base.POOL
Make sure you have Node.js installed locally; we recommend using Node v20+.
$ git clone https://github.com/tkhq/sdk
$ cd sdk/
$ corepack enable # Install `pnpm`
$ pnpm install -r # Install dependencies
$ pnpm run build-all # Compile source code
$ cd examples/defi/with-aave/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 wallet account created within this organization and have it funded with some ETH and USDC on Base Mainnet.
Once you've gathered these values, add them to a new .env.local file. Notice that your private key should be securely managed and never be committed to git.
cp .env.local.example .env.localNow open .env.local and add the missing environment variables:
TURNKEY_API_PUBLIC_KEYTURNKEY_API_PRIVATE_KEYTURNKEY_BASE_URLTURNKEY_ORGANIZATION_IDSIGN_WITHNONROOT_USER_IDNONROOT_API_PUBLIC_KEYNONROOT_API_PRIVATE_KEYINFURA_API_KEY
pnpm createPolicypnpm depositpnpm balancepnpm withdraw