interest-bearing.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { TOKEN_2022_PROGRAM_ID, amountToUiAmount } from '@solana/spl-token';
  4. import type { InterestBearing } from '../target/types/interest_bearing';
  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.updateRate(rate).accounts({ mintAccount: mintKeypair.publicKey }).rpc({ skipPreflight: true });
  25. console.log('Your transaction signature', transactionSignature);
  26. });
  27. it('Calculate accrued interest', async () => {
  28. await sleep(1);
  29. const amount = 1000;
  30. // Convert amount to UI amount with accrued interest
  31. // This helper is a simulated transaction
  32. const uiAmount = await amountToUiAmount(
  33. connection,
  34. wallet.payer,
  35. mintKeypair.publicKey, // Address of the Mint account
  36. amount, // Amount to be converted
  37. TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
  38. );
  39. console.log('\nAmount with Accrued Interest:', uiAmount);
  40. });
  41. });
  42. function sleep(s: number) {
  43. return new Promise((resolve) => setTimeout(resolve, s * 1000));
  44. }