interest-bearing.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { InterestBearing } from "../target/types/interest_bearing";
  4. import { TOKEN_2022_PROGRAM_ID, amountToUiAmount } from "@solana/spl-token";
  5. describe("interest-bearing", () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env();
  8. const connection = provider.connection;
  9. const wallet = provider.wallet as anchor.Wallet;
  10. anchor.setProvider(provider);
  11. const program = anchor.workspace.InterestBearing as Program<InterestBearing>;
  12. const mintKeypair = new anchor.web3.Keypair();
  13. it("Create Mint with InterestBearingConfig extension", async () => {
  14. const rate = 0;
  15. const transactionSignature = await program.methods
  16. .initialize(rate)
  17. .accounts({ mintAccount: mintKeypair.publicKey })
  18. .signers([mintKeypair])
  19. .rpc({ skipPreflight: true });
  20. console.log("Your transaction signature", transactionSignature);
  21. });
  22. it("Update Mint with Interest Rate", async () => {
  23. const rate = 100;
  24. const transactionSignature = await program.methods
  25. .updateRate(rate)
  26. .accounts({ mintAccount: mintKeypair.publicKey })
  27. .rpc({ skipPreflight: true });
  28. console.log("Your transaction signature", transactionSignature);
  29. });
  30. it("Calculate accrued interest", async () => {
  31. await sleep(1);
  32. const amount = 1000;
  33. // Convert amount to UI amount with accrued interest
  34. // This helper is a simulated transaction
  35. const uiAmount = await amountToUiAmount(
  36. connection,
  37. wallet.payer,
  38. mintKeypair.publicKey, // Address of the Mint account
  39. amount, // Amount to be converted
  40. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  41. );
  42. console.log("\nAmount with Accrued Interest:", uiAmount);
  43. });
  44. });
  45. function sleep(s: number) {
  46. return new Promise((resolve) => setTimeout(resolve, s * 1000));
  47. }