From 0658936d0eb163bcff1d6526f892ae7ea8765191 Mon Sep 17 00:00:00 2001 From: adpthegreat Date: Wed, 30 Oct 2024 22:14:04 +0100 Subject: [PATCH] ran pnpm fix --- .../programs/close_account_program/Cargo.toml | 20 --- .../programs/close_account_program/Xargo.toml | 2 - .../programs/close_account_program/src/lib.rs | 44 ------- .../tests/bankrun.test.ts | 32 ++--- .../tests/close_account_program.ts | 115 ++++++++---------- .../ts-programs/src/close_account_program.ts | 14 +-- 6 files changed, 66 insertions(+), 161 deletions(-) delete mode 100644 basics/close-account/poseidon/close_account_program/programs/close_account_program/Cargo.toml delete mode 100644 basics/close-account/poseidon/close_account_program/programs/close_account_program/Xargo.toml delete mode 100644 basics/close-account/poseidon/close_account_program/programs/close_account_program/src/lib.rs diff --git a/basics/close-account/poseidon/close_account_program/programs/close_account_program/Cargo.toml b/basics/close-account/poseidon/close_account_program/programs/close_account_program/Cargo.toml deleted file mode 100644 index d8c72322..00000000 --- a/basics/close-account/poseidon/close_account_program/programs/close_account_program/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "close_account_program" -version = "0.1.0" -description = "Created with Anchor" -edition = "2021" - -[lib] -crate-type = ["cdylib", "lib"] -name = "close_account_program" - -[features] -default = [] -cpi = ["no-entrypoint"] -no-entrypoint = [] -no-idl = [] -no-log-ix-name = [] -idl-build = ["anchor-lang/idl-build"] - -[dependencies] -anchor-lang = "0.30.1" diff --git a/basics/close-account/poseidon/close_account_program/programs/close_account_program/Xargo.toml b/basics/close-account/poseidon/close_account_program/programs/close_account_program/Xargo.toml deleted file mode 100644 index 475fb71e..00000000 --- a/basics/close-account/poseidon/close_account_program/programs/close_account_program/Xargo.toml +++ /dev/null @@ -1,2 +0,0 @@ -[target.bpfel-unknown-unknown.dependencies.std] -features = [] diff --git a/basics/close-account/poseidon/close_account_program/programs/close_account_program/src/lib.rs b/basics/close-account/poseidon/close_account_program/programs/close_account_program/src/lib.rs deleted file mode 100644 index 811df489..00000000 --- a/basics/close-account/poseidon/close_account_program/programs/close_account_program/src/lib.rs +++ /dev/null @@ -1,44 +0,0 @@ -use anchor_lang::prelude::*; -declare_id!("AtUc6zMfozxrQoK4PbDUnd5daS86XCPuT2og1293P5XX"); -#[program] -pub mod close_account { - use super::*; - pub fn create_user(ctx: Context, name: String) -> Result<()> { - ctx.accounts.user_state.user_bump = ctx.bumps.user_state; - ctx.accounts.user_state.user = ctx.accounts.user.key(); - ctx.accounts.user_state.name = name; - Ok(()) - } - pub fn close_user(ctx: Context) -> Result<()> { - Ok(()) - } -} -#[derive(Accounts)] -pub struct CreateUserContext<'info> { - #[account(mut)] - pub user: Signer<'info>, - #[account( - init, - payer = user, - space = 95, - seeds = [b"USER", - user.key().as_ref()], - bump, - )] - pub user_state: Account<'info, UserState>, - pub system_program: Program<'info, System>, -} -#[derive(Accounts)] -pub struct CloseUserContext<'info> { - #[account(mut, seeds = [b"USER", user.key().as_ref()], bump, close = user)] - pub user_account: Account<'info, UserState>, - #[account(mut)] - pub user: Signer<'info>, - pub system_program: Program<'info, System>, -} -#[account] -pub struct UserState { - pub user_bump: u8, - pub name: String, - pub user: Pubkey, -} diff --git a/basics/close-account/poseidon/close_account_program/tests/bankrun.test.ts b/basics/close-account/poseidon/close_account_program/tests/bankrun.test.ts index 91f42b14..9400c31e 100644 --- a/basics/close-account/poseidon/close_account_program/tests/bankrun.test.ts +++ b/basics/close-account/poseidon/close_account_program/tests/bankrun.test.ts @@ -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, @@ -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; @@ -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, }) @@ -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({ @@ -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' diff --git a/basics/close-account/poseidon/close_account_program/tests/close_account_program.ts b/basics/close-account/poseidon/close_account_program/tests/close_account_program.ts index 48e56c8f..94d37abd 100644 --- a/basics/close-account/poseidon/close_account_program/tests/close_account_program.ts +++ b/basics/close-account/poseidon/close_account_program/tests/close_account_program.ts @@ -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, @@ -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 { +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 { + 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); @@ -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, }) @@ -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({ @@ -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' diff --git a/basics/close-account/poseidon/close_account_program/ts-programs/src/close_account_program.ts b/basics/close-account/poseidon/close_account_program/ts-programs/src/close_account_program.ts index e2d71635..ac15b929 100644 --- a/basics/close-account/poseidon/close_account_program/ts-programs/src/close_account_program.ts +++ b/basics/close-account/poseidon/close_account_program/ts-programs/src/close_account_program.ts @@ -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(); @@ -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); } }