jpcaulfi 3 years ago
parent
commit
11f71e4ed0

+ 1 - 1
basics/checking-accounts/anchor/Anchor.toml

@@ -1,7 +1,7 @@
 [features]
 seeds = false
 [programs.localnet]
-anchor_program_example = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
+anchor_program_example = "ECWPhR3rJbaPfyNFgphnjxSEexbTArc7vxD8fnW6tgKw"
 
 [registry]
 url = "https://anchor.projectserum.com"

+ 15 - 6
basics/checking-accounts/anchor/programs/anchor-program-example/src/lib.rs

@@ -1,18 +1,27 @@
 use anchor_lang::prelude::*;
 
-declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
+
+declare_id!("ECWPhR3rJbaPfyNFgphnjxSEexbTArc7vxD8fnW6tgKw");
+
 
 #[program]
 pub mod anchor_program_example {
     use super::*;
 
-    pub fn hello(ctx: Context<Hello>) -> Result<()> {
-        
-        msg!("Hello, Solana!");
-        
+    pub fn check_accounts(_ctx: Context<CheckingAccounts>) -> Result<()> {
+
         Ok(())
     }
 }
 
 #[derive(Accounts)]
-pub struct Hello {}
+pub struct CheckingAccounts<'info> {
+    payer: Signer<'info>,
+    #[account(mut)]
+    /// CHECK: This account's data is empty
+    account_to_create: AccountInfo<'info>,
+    #[account(mut)]
+    /// CHECK: This account's data is empty
+    account_to_change: AccountInfo<'info>,
+    system_program: Program<'info, System>,
+}

+ 35 - 10
basics/checking-accounts/anchor/tests/test.ts

@@ -2,21 +2,46 @@ import * as anchor from "@project-serum/anchor";
 import { AnchorProgramExample } from "../target/types/anchor_program_example";
 
 describe("Anchor example", () => {
-  
-  // Configure the Anchor provider & load the program IDL
-  // The IDL gives you a typescript module
-  //
+
   const provider = anchor.AnchorProvider.env();
   anchor.setProvider(provider);
   const program = anchor.workspace.AnchorProgramExample as anchor.Program<AnchorProgramExample>;
+  const payer = provider.wallet as anchor.Wallet;
+
+  // We'll create this ahead of time.
+  // Our program will try to modify it.
+  const accountToChange = anchor.web3.Keypair.generate();
+  // Our program will create this.
+  const accountToCreate = anchor.web3.Keypair.generate();
+
+  it("Create an account owned by our program", async () => {
+
+    let ix = anchor.web3.SystemProgram.createAccount({
+        fromPubkey: provider.wallet.publicKey,
+        newAccountPubkey: accountToChange.publicKey,
+        lamports: await provider.connection.getMinimumBalanceForRentExemption(0),
+        space: 0,
+        programId: program.programId, // Our program
+    });
+
+    await anchor.web3.sendAndConfirmTransaction(
+        provider.connection, 
+        new anchor.web3.Transaction().add(ix),
+        [payer.payer, accountToChange]
+    );
+  });
 
-  it("Test our example", async () => {
+  it("Check accounts", async () => {
     
-    // Just run Anchor's IDL method to build a transaction!
-    //
-    await program.methods.hello()
-    .accounts({})
-    .rpc();
+    await program.methods.checkAccounts()
+      .accounts({
+        payer: provider.wallet.publicKey,
+        accountToCreate: accountToCreate.publicKey,
+        accountToChange: accountToChange.publicKey,
+        systemProgram: anchor.web3.SystemProgram.programId,
+      })
+      .signers([payer.payer])
+      .rpc();
 
   });
 });

+ 1 - 1
basics/checking-accounts/native/tests/test.ts

@@ -22,7 +22,7 @@ describe("Checking accounts", async () => {
     const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
     
     const PROGRAM_ID: PublicKey = new PublicKey(
-        "AE653DEBtNWr2VcU3FhVtFPc7rUf4z2Km8s5TnSwiiaW"
+        ""
     );
 
     // We'll create this ahead of time.