0

Currently, it seems like to calculating the accrued interest for a mint account with the InterestBearingConfig extension requires sending a transaction with the AmountToUiAmount instruction.

/**
 * Construct a AmountToUiAmount instruction
 *
 * @param mint         Public key of the mint
 * @param amount       Amount of tokens to be converted to UiAmount
 * @param programId    SPL Token program account
 *
 * @return Instruction to add to a transaction
 */
export function createAmountToUiAmountInstruction(
    mint: PublicKey,
    amount: number | bigint,
    programId = TOKEN_PROGRAM_ID
): TransactionInstruction {
    const keys = [{ pubkey: mint, isSigner: false, isWritable: false }];

    const data = Buffer.alloc(amountToUiAmountInstructionData.span);
    amountToUiAmountInstructionData.encode(
        {
            instruction: TokenInstruction.AmountToUiAmount,
            amount: BigInt(amount),
        },
        data
    );

    return new TransactionInstruction({ keys, programId, data });
}

Is there a way to calculate the interest without sending a transaction? For example, to just display on a UI.

1 Answer 1

1

It looks like the amountToUiAmount helper function simulates the transaction instead of sending one.

export async function amountToUiAmount(
    connection: Connection,
    payer: Signer,
    mint: PublicKey,
    amount: number | bigint,
    programId = TOKEN_PROGRAM_ID
): Promise<string | TransactionError | null> {
    const transaction = new Transaction().add(createAmountToUiAmountInstruction(mint, amount, programId));
    const { returnData, err } = (await connection.simulateTransaction(transaction, [payer], false)).value;
    if (returnData?.data) {
        return Buffer.from(returnData.data[0], returnData.data[1]).toString('utf-8');
    }
    return err;
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.