Pendle: Trade Fixed & Variable Yield
Buy fixed yield (PT), go long variable yield (YT) and provide liquidity on Pendle protocol using the Compass SDK. No Solidity needed.
🕐 Time to complete: ±10 minutes
🧰 What you need:
- Free Compass API Key
- Wallet (EOA or smart account guide)
Introduction
Pendle lets you tokenize and trade future yield. With Compass Labs, you can interact with Pendle markets - including buying/selling PT, YT, and LP positions - in just a few lines of code.
Normally this would require complex smart contract logic and allowance juggling. With Compass SDK, you abstract all of that away. This tutorial walks you through a full strategy lifecycle across fixed and variable yield.
Setup
Install Dependencies
Install the required packages.
Set Environment Variables
Create a .env file in your project root.
Import Libraries & Environment Variables
Instantiate Required Clients
The full, uninterrupted code is available at the end of the tutorial.
Query Pendle Markets
First, we need a Pendle market to perform transactions with. Let’s query the list of active markets and select one we’d like to interact with.
Query Active Markets
Use the markets
function to query a list of active market on Arbitrum.
Select a Market
For simplicity, we are selecting the first market returned of the list of
active markets with markets[0]
. Consider things like implied yield,
maturity date and the underlying token when deciding which Pendle market to
transact with.
Destructure Variables
Destructure variables from the selected market that will be used ultiple times throughout the rest of the tutorial.
Here you can see we are also checking our current position in the Pendle market using the position
function. We will use this function a number of times throughout the tutorial to keep track of our position balances in the market.
Fixed Yield
Buy Principal Token (PT) to Earn Fixed Yield
Now that we have decided on a Pendle market to transact with, let us take our first position in the market by buying fixed yield. Fixed yield positions are represented by PT tokens. The price difference between 1 PT and 1 Underlying Asset represents the fixed yield your entitled to claim after the maturity date of the market when PT tokens can be redeemed 1:1 for the Underlying Asset.
Check User Position
Check our current position in the Pendle market using the position
function.
Check and Set Allowance
Check the current allowance given for the Pendle Router on the Underlying Asset contract. If that allowance is less than the amount of Underlying Asset we intend to use to purchase PT, then set the allowance to a sufficient level.
Buy PT
Buy PT with the market’s Underlying Asset and await confirmation that the transaction has been included on a block.
Sell Principal Token (PT) Before Maturity Date
Let’s say some time after we purchsed our PT, we notice the market offering a more favorable signal for going long on yield. This likely means the implied yield rate has come down since we first took the PT position. Before going long on yield, we may want to sell our PT position beforehand and use the proceeds to fund our YT position.
Check User Position
Check our current position in the Pendle market using the position
function.
Check and Set Allowance
Check the current allowance given for the Pendle Router on the PT contract. If that allowance is less than the amount of PT we intend to sell, then set the allowance to a sufficient level.
Sell PT
Sell PT for the market’s Underlying Asset and await confirmation that the transaction has been included on a block.
Both Principal Tokens (PT) and Yield Tokens (YT) are ERC-20 compatible and, just like the Underlying Asset, require sufficient spending allowances to be set on them before a given contract can spend a user’s balance.
Variable Yield
Buy Yield Token (YT) to Earn Variable Yield
We have decided a long yield position is now favorable. This is likely because the implied yield rate has lowered. We now think that the yield generated from the Underlying Asset will be greater than the current implied yield from now until the market’s maturity date.
Check User Position
Check our current position in the Pendle market using the position
function.
Check and Set Allowance
Check the current allowance given for the Pendle Router on the Underlying Asset contract. If that allowance is less than the amount of Underlying Asset we intend to use to purchase YT, then set the allowance to a sufficient level.
Buy YT
Buy YT with the market’s Underlying Asset and await confirmation that the transaction has been included on a block.
Redeem Claimable Yield and Sell YT Position
Perhaps now the implied yield has increased again and we’d like to sell our Yield Tokens (YT) for a profit before the maturity date of the market.
Redeem Claimable Yield
Redeem any unstanding claimable yield on our YT.
Check User Position
Check our current position in the Pendle market using the position
function.
Check and Set Allowance
Check the current allowance given for the Pendle Router on the YT contract. If that allowance is less than the amount of YT we intend to sell, then set the allowance to a sufficient level.
Sell YT
Sell YT for the market’s Underlying Asset and await confirmation that the transaction has been included on a block.
It’s best practice to always redeem claimable yield before selling any of your YT balance. Once the YT is sold, you relinquish ownership and thus lose the ability to claim any yield, as the yield rights are tied to the token itself, not your past ownership.
Provide Liquidity
Add Liquidity (LP)
Let’s say we are uncertain whether to take fixed yield or go long yield in a particular Pendle market but we’d still like to earn yield. Another option is to provide liquidity to the market and earn yield in the form of fees accrued by the market from other users taking positions. The liquidity you provide to a given Pendle market is represented by LP tokens.
Check User Position
Check our current position in the Pendle market using the position
function.
Check and Set Allowance
Check the current allowance given for the Pendle Router on the Underlying Asset contract. If that allowance is less than the amount of Underlying Asset we intend to use to purchase LP, then set the allowance to a sufficient level.
Add Liquidity
Add liquidity with the market’s Underlying Asset in exchange for LP and await confirmation that the transaction has been included on a block.
For further practice, try removing liquidity from the market using the
removeLiquidity
function, test with different Pendle markets, or explore Compass Bundler to bundle transactions.
Full Code
Here is the full script from the tutorial. Copy and paste into your code editor and play around!