Skip to content

Commit

Permalink
ran pnpm fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adpthegreat committed Oct 30, 2024
1 parent 7b81cf6 commit 0658936
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 161 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert';
import {before, describe, it} from "node:test"
import { before, describe, it } from 'node:test';
import * as anchor from '@coral-xyz/anchor';
import {
Keypair,
Expand Down Expand Up @@ -41,11 +41,7 @@ async function createAndProcessTransaction(

describe('Close an account', async () => {
// Configure the client to use the local cluster.
const context = await startAnchor(
"",
[{ name: "close_account_program", programId: PROGRAM_ID }],
[]
);
const context = await startAnchor('', [{ name: 'close_account_program', programId: PROGRAM_ID }], []);
const provider = new BankrunProvider(context);

const payer = provider.wallet as anchor.Wallet;
Expand All @@ -61,23 +57,15 @@ describe('Close an account', async () => {
lamports: 2 * LAMPORTS_PER_SOL,
});

await createAndProcessTransaction(
context.banksClient,
payer.payer,
transferInstruction,
[payer.payer]
);
await createAndProcessTransaction(context.banksClient, payer.payer, transferInstruction, [payer.payer]);
});

// Derive the PDA for the user's account.
const [userAccount, userAccountBump] = PublicKey.findProgramAddressSync(
[Buffer.from("USER"), user.publicKey.toBuffer()],
program.programId
);
const [userAccount, userAccountBump] = PublicKey.findProgramAddressSync([Buffer.from('USER'), user.publicKey.toBuffer()], program.programId);

it("Can create an account", async () => {
it('Can create an account', async () => {
await program.methods
.createUser("Jacob")
.createUser('Jacob')
.accounts({
user: user.publicKey,
})
Expand All @@ -86,12 +74,12 @@ describe('Close an account', async () => {

// Fetch the account data
const userAccountData = await program.account.userState.fetch(userAccount);
assert.equal(userAccountData.name, "Jacob");
assert.equal(userAccountData.name, 'Jacob');
assert.equal(userAccountData.user.toBase58(), user.publicKey.toBase58());
assert.notEqual(userAccountData, null);
});

it("Can close an Account", async () => {
it('Can close an Account', async () => {
await program.methods
.closeUser()
.accounts({
Expand All @@ -102,9 +90,7 @@ describe('Close an account', async () => {

// The account should no longer exist, returning null.
try {
const userAccountData = await program.account.userState.fetchNullable(
userAccount
);
const userAccountData = await program.account.userState.fetchNullable(userAccount);
assert.equal(userAccountData, null);
} catch (err) {
// Won't return null and will throw an error in anchor-bankrun'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert";
import * as anchor from "@coral-xyz/anchor";
import type { Program } from "@coral-xyz/anchor";
import assert from 'node:assert';
import * as anchor from '@coral-xyz/anchor';
import type { Program } from '@coral-xyz/anchor';
import {
Connection,
Keypair,
Expand All @@ -11,59 +11,57 @@ import {
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
} from "@solana/web3.js";
import type { CloseAccount } from "../target/types/close_account";

const connection = new Connection("https://api.devnet.solana.com");

async function createAndProcessTransaction(
payer: Keypair,
instruction: TransactionInstruction,
additionalSigners: Keypair[] = []
): Promise<{ transaction: VersionedTransaction; signature: string | null }> {
// Get the latest blockhash
const { blockhash, lastValidBlockHeight } = await getLatestBlockhash();
const message = new TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash: blockhash,
instructions: [instruction],
}).compileToV0Message();

const tx = new VersionedTransaction(message);

try {
const signature = await sendAndConfirmTransaction(tx);
return { transaction: tx, signature };
} catch (err) {
return { transaction: tx, signature: null };
}
}

async function getLatestBlockhash(): Promise<{
blockhash: string;
lastValidBlockHeight: number;
}> {
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash("finalized");
return { blockhash, lastValidBlockHeight };
} from '@solana/web3.js';
import type { CloseAccount } from '../target/types/close_account';

const connection = new Connection('https://api.devnet.solana.com');

async function createAndProcessTransaction(
payer: Keypair,
instruction: TransactionInstruction,
additionalSigners: Keypair[] = [],
): Promise<{ transaction: VersionedTransaction; signature: string | null }> {
// Get the latest blockhash
const { blockhash, lastValidBlockHeight } = await getLatestBlockhash();
const message = new TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash: blockhash,
instructions: [instruction],
}).compileToV0Message();

const tx = new VersionedTransaction(message);

try {
const signature = await sendAndConfirmTransaction(tx);
return { transaction: tx, signature };
} catch (err) {
return { transaction: tx, signature: null };
}
}

async function sendAndConfirmTransaction(tx: VersionedTransaction): Promise<string> {
async function getLatestBlockhash(): Promise<{
blockhash: string;
lastValidBlockHeight: number;
}> {
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
return { blockhash, lastValidBlockHeight };
}

const signature = await connection.sendTransaction(tx);
async function sendAndConfirmTransaction(tx: VersionedTransaction): Promise<string> {
const signature = await connection.sendTransaction(tx);

const { blockhash, lastValidBlockHeight } = await getLatestBlockhash();
const { blockhash, lastValidBlockHeight } = await getLatestBlockhash();

await connection.confirmTransaction({
blockhash: blockhash,
lastValidBlockHeight: lastValidBlockHeight,
signature: signature,
});
await connection.confirmTransaction({
blockhash: blockhash,
lastValidBlockHeight: lastValidBlockHeight,
signature: signature,
});

return signature;
return signature;
}

describe("Close an account", () => {
describe('Close an account', () => {
// Configure the client to use the local cluster.
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
Expand All @@ -81,20 +79,15 @@ describe("Close an account", () => {
lamports: 2 * LAMPORTS_PER_SOL,
});

await createAndProcessTransaction(payer.payer, transferInstruction, [
payer.payer,
]);
await createAndProcessTransaction(payer.payer, transferInstruction, [payer.payer]);
});

// Derive the PDA for the user's account.
const [userAccount, userAccountBump] = PublicKey.findProgramAddressSync(
[Buffer.from("USER"), payer.publicKey.toBuffer()],
program.programId
);
const [userAccount, userAccountBump] = PublicKey.findProgramAddressSync([Buffer.from('USER'), payer.publicKey.toBuffer()], program.programId);

it("Can create an account", async () => {
it('Can create an account', async () => {
await program.methods
.createUser("Jacob")
.createUser('Jacob')
.accounts({
user: user.publicKey,
})
Expand All @@ -103,12 +96,12 @@ describe("Close an account", () => {

// Fetch the account data
const userAccountData = await program.account.userState.fetch(userAccount);
assert.equal(userAccountData.name, "Jacob");
assert.equal(userAccountData.name, 'Jacob');
assert.equal(userAccountData.user.toBase58(), user.publicKey.toBase58());
assert.notEqual(userAccountData, null);
});

it("Can close an Account", async () => {
it('Can close an Account', async () => {
await program.methods
.closeUser()
.accounts({
Expand All @@ -119,9 +112,7 @@ describe("Close an account", () => {

// The account should no longer exist, returning null.
try {
const userAccountData = await program.account.userState.fetchNullable(
userAccount
);
const userAccountData = await program.account.userState.fetchNullable(userAccount);
assert.equal(userAccountData, null);
} catch (err) {
// Won't return null and will throw an error in anchor-bankrun'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Account, String as PoseidonString, Pubkey, Result, Signer, u8 } from '@solanaturbine/poseidon';

export default class CloseAccount {
static PROGRAM_ID = new Pubkey(
"AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XXo"
);
static PROGRAM_ID = new Pubkey('AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XXo');

createUser(
user: Signer,
userState: UserState,
name: PoseidonString<50>
): Result {
userState.derive(["USER", user.key]).init();
createUser(user: Signer, userState: UserState, name: PoseidonString<50>): Result {
userState.derive(['USER', user.key]).init();

userState.userBump = userState.getBump();

Expand All @@ -19,7 +13,7 @@ export default class CloseAccount {
userState.name = name;
}
closeUser(userAccount: UserState, user: Signer): Result {
userAccount.derive(["USER", user.key]).close(user);
userAccount.derive(['USER', user.key]).close(user);
}
}

Expand Down

0 comments on commit 0658936

Please sign in to comment.