Last active
November 11, 2023 13:36
-
-
Save vl-dev/b870976148cbfbc63ccf17f123df631c to your computer and use it in GitHub Desktop.
This is a typescript test for the Anchor program demonstrating a way how to return rent to the original payer on Solana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as anchor from "@coral-xyz/anchor"; | |
import { Program } from "@coral-xyz/anchor"; | |
import { RentReturn } from "../target/types/rent_return"; | |
import { expect } from "chai"; | |
const { SystemProgram, PublicKey } = anchor.web3; | |
describe("rent_return", () => { | |
// Configure the client to use the local cluster. | |
const provider = anchor.AnchorProvider.env() | |
anchor.setProvider(provider); | |
const program = anchor.workspace.RentReturn as Program<RentReturn>; | |
it("Rent is returned", async () => { | |
const myAccount = PublicKey.findProgramAddressSync([Buffer.from("acc")], program.programId)[0]; | |
const startBalance = await provider.connection.getBalance(provider.wallet.publicKey); | |
// Create account | |
const tx = await program.methods.initialize().accounts({ | |
acc: myAccount, | |
feePayer: provider.wallet.publicKey, | |
systemProgram: SystemProgram.programId, | |
}).rpc(); | |
console.log("Account creation transaction signature: ", tx); | |
const accountCreationFee = startBalance - await provider.connection.getBalance(provider.wallet.publicKey); | |
console.log("Paid for the account creation: ", accountCreationFee); | |
// Close account | |
const tx2 = await program.methods.close().accounts({ | |
acc: myAccount, | |
destination: provider.wallet.publicKey, | |
}).rpc(); | |
console.log("Account close transaction signature: ", tx2); | |
const totalPaid = startBalance - await provider.connection.getBalance(provider.wallet.publicKey) | |
console.log("Total fees paid:", totalPaid); | |
expect(totalPaid).to.be.lessThan(accountCreationFee); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment