Typescript
使用 Web3.js 的 Token2022

使用 Web3.js 的 Token2022

利息承载扩展

InterestBearing 扩展是一种 Mint 账户扩展,允许用户为其代币应用利率,并在任何时候检索包括利息在内的更新总额。

初始化铸币账户

要在 InterestBearing 扩展上初始化一个 Mint 账户,我们需要使用 interestBearingMint() 函数。

以下是如何使用利息承载扩展创建铸币的方法:

ts
import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import {
  createInitializeMintInstruction,
  createInitializeMintCloseAuthorityInstruction,
  getMintLen,
  ExtensionType,
  TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";

const mint = Keypair.generate();

// Calculate the size needed for a Mint account with Transfer Fee extension
const mintLen = getMintLen([ExtensionType.MintCloseAuthority]);

// 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`,
);
Expand
[40 more lines]

计算利息

InterestBearing 扩展不会生成新的代币;显示的金额仅通过 amount_to_ui_amount 函数包含了累计利息,使得变化仅为视觉上的。

因此,在 unpacking 我们想要计算利息的 Token 账户后,我们可以轻松获取执行计算所需的所有信息以获得利息金额。

幸运的是,我们甚至不需要这样做,因为我们可以像这样使用 amountToUiAmount 函数:

ts
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);

更新利息承载扩展

通过 InterestBearing 扩展,可以更改代币账户产生的利息,这得益于其数据结构的方式:

rust
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,
}

为此,我们可以像这样使用 updateRateInterestBearingMint 函数:

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

如果需要,我们还可以通过使用 setAuthority 函数并传入正确的 AuthorityType 来更改设置利息的权限,如下所示:

ts
const setAuthorityInstruction = await createSetAuthorityInstruction(
  mint.publicKey,
  keypair.publicKey,
  AuthorityType.InterestRate,
  newRateAuthority.publicKey,
  [],
  TOKEN_2022_PROGRAM_ID,
);
Blueshift © 2026Commit: 3c44267