The InterestBearingConfig
extension enables developers to set an interest rate
stored directly on the Mint Account. Interest is compounded continuously, based
on the network's timestamp.
In other words, the accrued interest is simply a visual UI conversion, but the underlying token quantity remains unchanged. This design eliminates the need for frequent rebase or update operations to adjust for accrued interest.
Note that the interest accrual is only a calculation, and does not involve minting new tokens.
In this guide, we'll walk through an example of using Solana Playground. Here is the final script.
Getting Started
Start by opening this Solana Playground link with the following starter code.
If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL.
If you do not have a Playground wallet, you may see a type error within the
editor on all declarations of pg.wallet.publicKey
. This type error will clear
after you create a Playground wallet.
To get devnet SOL, run the solana airdrop
command in the Playground's
terminal, or visit this devnet faucet.
Once you've created and funded the Playground wallet, click the "Run" button to run the starter code.
Add Dependencies
Let's start by setting up our script. We'll be using the @solana/web3.js
and
@solana/spl-token
libraries.
Replace the starter code with the following:
Mint Setup
First, let's define the properties of the Mint Account we'll be creating in the following step.
Next, let's determine the size of the new Mint Account and calculate the minimum lamports needed for rent exemption.
With Token Extensions, the size of the Mint Account will vary based on the extensions enabled.
Build Instructions
Next, let's build the set of instructions to:
- Create a new account
- Initialize the
InterestBearingConfig
extension - Initialize the remaining Mint Account data
First, build the instruction to invoke the System Program to create an account and assign ownership to the Token Extensions Program.
Next, build the instruction to initialize the InterestBearingConfig
extension
for the Mint Account.
Lastly, build the instruction to initialize the rest of the Mint Account data. This is the same as with the original Token Program.
Send Transaction
Next, let's add the instructions to a new transaction and send it to the
network. This will create a Mint Account with the InterestBearingConfig
extension enabled.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM.
Update Interest Rate
The designated Rate Authority can then update the interest rate on the Mint Account at any time.
Fetch Interest Config State
Next, let's check the updated interest rate by fetching the Mint Account data.
Calculate Accrued Interest
Lastly, let's calculate the interest accrued for a given amount. Note that this calculation can be performed independently for any amount, without the need for minting tokens.
Run the script by clicking the Run
button. You can then inspect the
transaction details on SolanaFM and view the data logged in the Playground
terminal.
Interest is continuously compounded based on the timestamp in the network. Due to drift that may occur in the network timestamp, the accumulated interest could be lower than the expected value. Thankfully, this is rare.
You should see an output similar to snippet below, where the decimal values indicate the interest that has accumulated:
Conclusion
The InterestBearingConfig
extension introduces a simple mechanism for tokens
to increase or decrease in value over time. By seamlessly integrating tools
commonly found in traditional finance, this innovation broadens Solana's
capabilities, bridging the gap between conventional financial instruments and
the world of blockchain.