A fullstack template for vaults - includes a NextJS UI supported by a NextJS + Vercel backend.
- Vault Manager
- create & manage vaults
- update vault parameters
- Vault
- Deposit & Withdraw
- View vault history
- View vault depositor history
It is still highly recommended to familiarize yourself with the Drift Vaults SDK here: https://github.com/drift-labs/drift-vaults
- repo
- drift-common (git submodule)
- protocol
- sdk
- common-ts
- react
- icons
- ui
- If you didn't clone this repo with the
--recursive
flag, first rungit submodule update --init
. This will pull down the latest commit in thedrift-common
submodule. - Run
cd drift-common && git submodule update --init && cd ..
to pull the latest from theprotocol
submodule. - Run
./build_all_sm.sh
- this builds and symlinks all of the local libraries- In case this doesn't work.
build_all_sm.sh
is just building and symlinking all of the necessary packages in thedrift-common
submodule :drift-common/icons
,drift-common/react
, anddrift-common/common-ts
, so you may be able to do this yourself manually or debug the issue.
- In case this doesn't work.
- In the future if you need to rebuild modules, just run
./build_all_sm.sh
again. - Make sure to checkout
master
branch for the submodules.
- Add the project to Vercel.
- Add the custom install command:
bun run install-vercel
- Add the custom build command:
bun run build-vercel
- Deploy the project
The UI allows anyone to create their own vault, and manage it accordingly. You will be able to
- View vault on-chain account info
- Update vault parameters
The UI allows anyone to deposit and withdraw from any vault (added as hardcoded configs). You will be able to
- Deposit & Withdraw
- View vault history
- View vault depositor history
- View APY of vault
The entire backend is based on solutions from Vercel + NextJS. The backend mainly supports the following:
- Vault Performance History
- Vault Depositor Performance History (extrapolated from Vault Performance History)
- Vault semi-live APY calculation
Backend architecture overview showing the relationship between components
A cronjob is set to run every midnight (UTC) to take a snapshot of the vault's metrics - e.g. TVL, total shares, main spot market oracle price etc. These information can be used to construct a vault's performance history graph.
A cronjob is set to run every minute to port any vault depositor record event from the blockchain. Events include deposits, withdrawal requests, request cancellation and withdrawals. With this data, combined with the vault performance history, we can extrapolate the vault depositor's performance history.
A cronjob is set to run every hour to calculate the vault's APY. We only calculate and cache the APY every hour, since it requires reading the vault's snapshot history every time it calculates the APY.
All parameters of the cronjobs are subjective. Feel free to adjust according to your needs.
We use Vercel Postgres for our database, with Drizzle as our ORM.
- Create a new Vercel Postgres database.
- Connect the project to the database, and copy the Postgres secrets into your
.env
file. - Delete the
./ui/db-migrations
directory. - Run
bun run migrate-db
in./ui
to generate the new migrations for your new database.
We use Vercel KV for caching, mainly for the vault's calculated APY.
- Create a new Vercel KV database.
- Connect the project to the database, and copy the KV secrets into your
.env
file.
Cronjobs are only run on the production branch. Cronjobs are essentially NextJS API routes that are called on a schedule based on vercel.json
.
- Add the
CRON_SECRET
to your.env
file. This should be a random string of characters. This ensures that your cronjobs are protected from being called by anyone. Ensure that this is set in your Vercel environment variables. - Add
NEXT_PUBLIC_RPC_OVERRIDE
to your.env
file. This should be the RPC URL of the cluster you are using. - You may edit the cronjobs in
vercel.json
to run on a different schedule, suited to your needs. - Push any changes to the cronjob schedule/API routes to the production branch to ensure the changes are in effect.
Ensure all environment variables are reflected on your local machine and in Vercel.