|
| 1 | +import { |
| 2 | + ConfirmOptions, |
| 3 | + Connection, |
| 4 | + Keypair, |
| 5 | + PublicKey, |
| 6 | + sendAndConfirmTransaction, |
| 7 | + Signer, |
| 8 | + SystemProgram, |
| 9 | + Transaction, |
| 10 | +} from '@solana/web3.js'; |
| 11 | +import { getSigners } from '../../actions/internal'; |
| 12 | +import { TOKEN_2022_PROGRAM_ID } from '../../constants'; |
| 13 | +import { createInitializeMintInstruction } from '../../instructions'; |
| 14 | +import { ExtensionType, getMintLen } from '../extensionType'; |
| 15 | +import { |
| 16 | + createInitializeInterestBearingMintInstruction, |
| 17 | + createUpdateRateInterestBearingMintInstruction, |
| 18 | +} from './instructions'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Initialize an interest bearing account on a mint |
| 22 | + * |
| 23 | + * @param connection Connection to use |
| 24 | + * @param payer Payer of the transaction fees |
| 25 | + * @param mintAuthority Account or multisig that will control minting |
| 26 | + * @param freezeAuthority Optional account or multisig that can freeze token accounts |
| 27 | + * @param rateAuthority The public key for the account that can update the rate |
| 28 | + * @param rate The initial interest rate |
| 29 | + * @param decimals Location of the decimal place |
| 30 | + * @param keypair Optional keypair, defaulting to a new random one |
| 31 | + * @param confirmOptions Options for confirming the transaction |
| 32 | + * @param programId SPL Token program account |
| 33 | + * |
| 34 | + * @return Public key of the mint |
| 35 | + */ |
| 36 | +export async function createInterestBearingMint( |
| 37 | + connection: Connection, |
| 38 | + payer: Signer, |
| 39 | + mintAuthority: PublicKey, |
| 40 | + freezeAuthority: PublicKey, |
| 41 | + rateAuthority: PublicKey, |
| 42 | + rate: number, |
| 43 | + decimals: number, |
| 44 | + keypair = Keypair.generate(), |
| 45 | + confirmOptions?: ConfirmOptions, |
| 46 | + programId = TOKEN_2022_PROGRAM_ID |
| 47 | +): Promise<PublicKey> { |
| 48 | + const mintLen = getMintLen([ExtensionType.InterestBearingMint]); |
| 49 | + const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); |
| 50 | + const transaction = new Transaction().add( |
| 51 | + SystemProgram.createAccount({ |
| 52 | + fromPubkey: payer.publicKey, |
| 53 | + newAccountPubkey: keypair.publicKey, |
| 54 | + space: mintLen, |
| 55 | + lamports, |
| 56 | + programId, |
| 57 | + }), |
| 58 | + createInitializeInterestBearingMintInstruction(keypair.publicKey, rateAuthority, rate, programId), |
| 59 | + createInitializeMintInstruction(keypair.publicKey, decimals, mintAuthority, freezeAuthority, programId) |
| 60 | + ); |
| 61 | + await sendAndConfirmTransaction(connection, transaction, [payer, keypair], confirmOptions); |
| 62 | + return keypair.publicKey; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Update the interest rate of an interest bearing account |
| 67 | + * |
| 68 | + * @param connection Connection to use |
| 69 | + * @param payer Payer of the transaction fees |
| 70 | + * @param mint Public key of the mint |
| 71 | + * @param rateAuthority The public key for the account that can update the rate |
| 72 | + * @param rate The initial interest rate |
| 73 | + * @param multiSigners Signing accounts if `owner` is a multisig |
| 74 | + * @param confirmOptions Options for confirming the transaction |
| 75 | + * @param programId SPL Token program account |
| 76 | + * |
| 77 | + * @return Signature of the confirmed transaction |
| 78 | + */ |
| 79 | +export async function updateRateInterestBearingMint( |
| 80 | + connection: Connection, |
| 81 | + payer: Signer, |
| 82 | + mint: PublicKey, |
| 83 | + rateAuthority: Signer, |
| 84 | + rate: number, |
| 85 | + multiSigners: Signer[] = [], |
| 86 | + confirmOptions?: ConfirmOptions, |
| 87 | + programId = TOKEN_2022_PROGRAM_ID |
| 88 | +): Promise<string> { |
| 89 | + const [rateAuthorityPublicKey, signers] = getSigners(rateAuthority, multiSigners); |
| 90 | + const transaction = new Transaction().add( |
| 91 | + createUpdateRateInterestBearingMintInstruction(mint, rateAuthorityPublicKey, rate, signers, programId) |
| 92 | + ); |
| 93 | + |
| 94 | + return await sendAndConfirmTransaction(connection, transaction, [payer, rateAuthority, ...signers], confirmOptions); |
| 95 | +} |
0 commit comments