Typescript
Token2022 with Web3.js

Token2022 with Web3.js

The Interest Bearing Extension

The InterestBearing extension is a Mint account extension that lets users apply an interest rate to their tokens and retrieve the updated total, including interest, at any given moment.

Initializing the Mint Account

To initialize the InterestBearing extension on a Mint account we're going to need the interestBearingMint() function.

Here's how to create a mint with the Interest Bearing extension:

import {
    Keypair,
    SystemProgram,
    Transaction,
    sendAndConfirmTransaction,
} from '@solana/web3.js';
import {
    createInitializeMintInstruction,
    createInitializeInterestBearingMintInstruction,
    getMintLen,
    ExtensionType,
    TOKEN_2022_PROGRAM_ID,
} from '@solana/spl-token';
 
const mint = Keypair.generate();
 
// Calculate the size needed for a Mint account with Interest Bearing extension
const mintLen = getMintLen([ExtensionType.InterestBearingConfig]);
 
// Calculate minimum lamports required for rent exemption
const lamports = await connection.getMinimumBalanceForRentExemption(mintLen);
 
// Create the account with the correct size and owner
const createAccountInstruction = SystemProgram.createAccount({
    fromPubkey: keypair.publicKey,
    newAccountPubkey: mint.publicKey,
    space: mintLen,
    lamports,
    programId: TOKEN_2022_PROGRAM_ID,
});
 
// Initialize the Interest Bearing extension
const initializeMintInterestBearing = createInitializeInterestBearingMintInstruction(
    mint.publicKey,
    keypair.publicKey,
    500,
    TOKEN_2022_PROGRAM_ID,
);
 
// Initialize the mint itself
const initializeMintInstruction = createInitializeMintInstruction(
    mint.publicKey,
    6,
    keypair.publicKey,
    null,
    TOKEN_2022_PROGRAM_ID,
);
 
// Combine all instructions in the correct order
const transaction = new Transaction().add(
    createAccountInstruction,
    initializeMintNonTransferable,
    initializeMintInstruction,
);
 
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair, mint]);
 
console.log(`Mint created! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);

Calculating the Interest

The InterestBearing extension, does not generate new tokens; the displayed amount simply includes the accumulated interest through the amount_to_ui_amount function, making the change purely aesthetic.

So after unpacking the Token account that we want to calculate the interest from, it's easy to get all the information that we need to perform the calculation to get the amount of interest.

Luckily for us we don't even need to do so since we can use the amountToUiAmount function like so:

const tokenInfo = await getAccount(
    connection,
    tokenAccount,
    undefined,
    TOKEN_2022_PROGRAM_ID,
);
 
console.log("Token Amount: ", tokenInfo.amount);
    
const uiAmount = await amountToUiAmount(
    connection,
    keypair,
    mint.publicKey,
    tokenInfo.amount,
    TOKEN_2022_PROGRAM_ID,
);
    
console.log("UI Amount: ", uiAmount);

Updating the Interest Bearing Extension

With the InterestBearing extension is possible to change the interest produced by the token account thanks to the way its data is structured:

pub struct InterestBearing {
    pub rate_authority: Pubkey,
    pub initialization_timestamp: i64,
    pub pre_update_average_rate: u16,
    pub last_update_timestamp: i64,
    pub current_rate: u16,
}

To do so we can use the updateRateInterestBearingMint function like so:

const updateRateInstruction = await createUpdateRateInterestBearingMintInstruction(
    mint.publicKey,
    newRateAuthority.publicKey,
    1000, // updated rate
    undefined,
    TOKEN_2022_PROGRAM_ID,
);

And if we want, we can change the authority over setting the interest using the setAuthority function and passing in the right AuthorityType like so:

const setAuthorityInstruction = await createSetAuthorityInstruction(
    mint.publicKey,
    keypair.publicKey,
    AuthorityType.InterestRate,
    newRateAuthority.publicKey,
    [],
    TOKEN_2022_PROGRAM_ID,
);
Contents
View Source
Blueshift © 2025Commit: 538ee5b