Skip to content

Instantly share code, notes, and snippets.

@vl-dev
Last active November 11, 2023 13:36
Show Gist options
  • Save vl-dev/b870976148cbfbc63ccf17f123df631c to your computer and use it in GitHub Desktop.
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
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