🕐 Time to complete: ±10 minutes 🧰 What you need:

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

1

Install Dependencies

Install the required packages.
npm install @compass-labs/api-sdk viem dotenv
2

Set Environment Variables

Create a .env file in your project root.
.env
PRIVATE_KEY="your_wallet_private_key"
RPC_URL="your_ethereum_rpc_url"
COMPASS_API_KEY="your_compass_api_key"
3

Import Libraries & Environment Variables

4

Instantiate Required Clients

The full, uninterrupted code is available at the end of the tutorial.

Query Pendle Markets

First, we need a Pendle market on Arbitrum to perform transactions with. Let’s query the list of active markets and select one we’d like to interact with.
1

Query Active Markets

Use the Pendle Markets function to query a list of active markets on Arbitrum.
2

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.
3

Destructure Variables

Destructure variables from the selected market that will be used multiple times throughout the rest of the tutorial.

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.
1

Check and Set Allowance

Check the current allowance given for the Pendle Router on the USDC contract. If that allowance is less than the amount of USDC we intend to use to purchase PT, then set the allowance to a sufficient level.
2

Buy PT

Buy PT with 100 USDC 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.
1

Check User Position

Check our current position in the Pendle market by including a user address with the Pendle Market function. This will allow us to obtain our current PT balance.
2

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.
3

Sell PT

Sell our current PT balance 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.
1

Check User Position

Check our current position in the Pendle market by including a user address with the Pendle Market function. This will allow us to obtain our current Underlying Asset balance.
2

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.
3

Buy YT

Buy YT with our current Underlying Asset balance 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.
1

Redeem Claimable Yield

Redeem any outstanding claimable yield on our YT. This will land in our wallet in the form of the market’s Underlying Asset.
2

Check User Position

Check our current position in the Pendle market using the position function.
3

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.
4

Sell YT

Sell YT for USDT this time 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.
1

Check USDT Balance

Check our current balance of USDT we obtained by selling our YT.
2

Check and Set Allowance

Check the current allowance given for the Pendle Router on the USDT contract. If that allowance is less than the amount of USDT we intend to use to purchase LP, then set the allowance to a sufficient level.
3

Add Liquidity

Add liquidity with our current USDT balance 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, 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!