浏览代码

Build successful

Nitish-bot 11 月之前
父节点
当前提交
caf69bc3d2

+ 2 - 6
basics/pda-rent-payer/steel/api/src/instruction.rs

@@ -4,16 +4,12 @@ use steel::*;
 #[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
 pub enum PdaRentPayerInstruction {
     InitializeRentVault = 0,
-    CreateNewAccount = 1
+    CreateNewAccount = 1,
 }
 
 #[repr(C)]
 #[derive(Clone, Copy, Debug, Pod, Zeroable)]
-pub struct InitializeRentVault {}
-
-#[repr(C)]
-#[derive(Clone, Copy, Debug, Pod, Zeroable)]
-pub struct DepositRent {
+pub struct InitializeRentVault {
     pub amount: u64,
 }
 

+ 2 - 2
basics/pda-rent-payer/steel/api/src/lib.rs

@@ -9,10 +9,10 @@ pub mod prelude {
     pub use crate::error::*;
     pub use crate::instruction::*;
     pub use crate::sdk::*;
-    pub use crate::state::*; 
+    pub use crate::state::*;
 }
 
 use steel::*;
 
 // TODO Set program id
-declare_id!("H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg"); 
+declare_id!("H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg");

+ 7 - 5
basics/pda-rent-payer/steel/api/src/sdk.rs

@@ -2,15 +2,18 @@ use steel::*;
 
 use crate::prelude::*;
 
-pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey) -> Instruction {
+pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey, data: &[u8]) -> Instruction {
     Instruction {
         program_id: crate::ID,
         accounts: vec![
             AccountMeta::new(signer_info, true),
             AccountMeta::new(rent_vault_pda().0, false),
-            AccountMeta::new_readonly(system_program::ID, false),
+            AccountMeta::new_readonly(system_program, false),
         ],
-        data: InitializeRentVault {}.to_bytes(),
+        data: InitializeRentVault {
+            amount: u64::from_be_bytes(data[..8].try_into().unwrap()),
+        }
+        .to_bytes(),
     }
 }
 
@@ -22,7 +25,6 @@ pub fn create_new_account(rent_vault: Pubkey, new_account: Pubkey) -> Instructio
             AccountMeta::new(new_account, true),
             AccountMeta::new_readonly(system_program::ID, false),
         ],
-        data: CreateNewAccount {}.to_bytes()
+        data: CreateNewAccount {}.to_bytes(),
     }
 }
-

+ 1 - 1
basics/pda-rent-payer/steel/api/src/state/accounts.rs

@@ -1,5 +1,5 @@
-use steel::*;
 use super::PdaRentPayerAccount;
+use steel::*;
 
 /// This empty struct represents the payer vault account
 #[repr(C)]

+ 4 - 4
basics/pda-rent-payer/steel/api/src/state/mod.rs

@@ -1,10 +1,10 @@
 mod accounts;
 
+use crate::consts::*;
 pub use accounts::*;
 use steel::*;
-use crate::consts::*;
 
-/// This enum represents the discriminator for the 
+/// This enum represents the discriminator for the
 /// accounts this program can interact with
 #[repr(u8)]
 #[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
@@ -15,10 +15,10 @@ pub enum PdaRentPayerAccount {
 
 /// Fetch PDA of the rent_vault account.
 pub fn rent_vault_pda() -> (Pubkey, u8) {
-    Pubkey::find_program_address(&[RENT_VAULT], &crate::id())            
+    Pubkey::find_program_address(&[RENT_VAULT], &crate::id())
 }
 
 // Fetch PDA of the newly_created account.
 // pub fn new_account_pda() -> (Pubkey, u8) {
-//     Pubkey::find_program_address(&[NEW_ACCOUNT], &crate::id())            
+//     Pubkey::find_program_address(&[NEW_ACCOUNT], &crate::id())
 // }

+ 10 - 20
basics/pda-rent-payer/steel/program/src/create_new_account.rs

@@ -4,30 +4,20 @@ use steel::*;
 pub fn process_create_account(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
     // // Parse args.
     // let args = Add::try_from_bytes(data)?;
-	// let amount = u64::from_le_bytes(args.amount);
+    // let amount = u64::from_le_bytes(args.amount);
 
     // Load and validate accounts.
-    let [payer_info, new_account_info, system_program] = accounts else {
-        return Err(ProgramError::NotEnoughAccountKeys);        
+    let [payer_info, new_account_info] = accounts else {
+        return Err(ProgramError::NotEnoughAccountKeys);
     };
-	new_account_info
-        .is_signer()?
-        .is_empty()?
-        .is_writable()?;
-    payer_info.is_writable()?.has_seeds(
-        &[RENT_VAULT],
-        &pda_rent_payer_api::ID,
-    )?;
-    system_program.is_program(&system_program::ID)?;
+    new_account_info.is_signer()?.is_empty()?.is_writable()?;
+    payer_info
+        .is_writable()?
+        .has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
 
-    // Create new account.
-    create_account::<NewAccount>(
-        new_account_info,
-        system_program,
-        payer_info,
-        &pda_rent_payer_api::ID,
-        &[RENT_VAULT],
-    )?;
+    // Create new account by simply sending a
+    // token amount to the new account.
+    payer_info.send(100, new_account_info);
 
     Ok(())
 }

+ 14 - 10
basics/pda-rent-payer/steel/program/src/init_rent_vault.rs

@@ -2,20 +2,20 @@ use pda_rent_payer_api::prelude::*;
 use solana_program::msg;
 use steel::*;
 
-pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
-    // // Parse args
-    // let args = data[..8].try_into().expect("Ma chud gayi");
-    // let amount = u64::from_le_bytes(args);
+pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
+    // Parse args
+    let args = data[..8].try_into().expect("Error parsing args");
+    let amount = u64::from_le_bytes(args);
 
     // Load and validate accounts.
     let [payer_info, rent_vault_info, system_program] = accounts else {
-        return Err(ProgramError::NotEnoughAccountKeys);        
+        return Err(ProgramError::NotEnoughAccountKeys);
     };
-    payer_info.is_signer()?.is_writable()?;
-    rent_vault_info.is_empty()?.is_writable()?.has_seeds(
-        &[RENT_VAULT],
-        &pda_rent_payer_api::ID,
-    )?;
+    payer_info.is_signer()?;
+    rent_vault_info
+        .is_empty()?
+        .is_writable()?
+        .has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
     system_program.is_program(&system_program::ID)?;
 
     // Initialize vault.
@@ -27,6 +27,10 @@ pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], _data: &[u8]) -> P
         &[RENT_VAULT],
     )?;
 
+    payer_info.send(amount, rent_vault_info);
+
+    rent_vault_info.collect(amount, payer_info)?;
+
     let _ = rent_vault_info.as_account_mut::<RentVault>(&pda_rent_payer_api::ID)?;
 
     msg!("Initialized rent vault.");

+ 3 - 3
basics/pda-rent-payer/steel/program/src/lib.rs

@@ -1,8 +1,8 @@
-mod init_rent_vault;
 mod create_new_account;
+mod init_rent_vault;
 
-use init_rent_vault::*;
 use create_new_account::*;
+use init_rent_vault::*;
 
 use pda_rent_payer_api::prelude::*;
 use steel::*;
@@ -14,7 +14,7 @@ pub fn process_instruction(
 ) -> ProgramResult {
     /// Parse instruction automatically detects which instruction is being called
     /// based on the discriminator and returns the instruction and the data
-    let (ix ,data) = parse_instruction(&pda_rent_payer_api::ID, program_id, data)?;
+    let (ix, data) = parse_instruction(&pda_rent_payer_api::ID, program_id, data)?;
 
     match ix {
         PdaRentPayerInstruction::InitializeRentVault => process_initialize_vault(accounts, data)?,

+ 7 - 12
basics/pda-rent-payer/steel/tests/test.ts

@@ -24,13 +24,11 @@ const LOAD_LAMPORTS = LAMPORTS_PER_SOL; // 1 SOL
 
 const instructionDiscriminators = {
     InitializeRentVault: Buffer.from([0]),
-    DepositRent: Buffer.from([1]),
-    CreateNewAccount: Buffer.from([2]),
+    CreateNewAccount: Buffer.from([1]),
 }
 
 describe("Pay the rent for an account using a PDA", () => {
   let context: ProgramTestContext;
-  let lastBlock: Blockhash;
   let client: BanksClient;
   let payer: Keypair;
   
@@ -46,16 +44,18 @@ describe("Pay the rent for an account using a PDA", () => {
     );
     client = context.banksClient;
     payer = context.payer;
-    lastBlock = context.lastBlockhash;
     
   });
 
   it("should initialize rent vault PDA", async () => {
-    const data = Buffer.concat([instructionDiscriminators.InitializeRentVault]);
+    const amount = Buffer.alloc(8);
+    amount.writeBigInt64BE(BigInt(LOAD_LAMPORTS), 0);
+    const data = Buffer.concat([instructionDiscriminators.InitializeRentVault, amount]);
+    
     const ix = new TransactionInstruction({
       keys: [
         { pubkey: payer.publicKey, isSigner: true, isWritable: false },
-        { pubkey: vault_pda, isSigner: true, isWritable: true },
+        { pubkey: vault_pda, isSigner: false, isWritable: true },
         { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
       ],
       programId: PROGRAM_ID,
@@ -63,17 +63,13 @@ describe("Pay the rent for an account using a PDA", () => {
     });
 
     const tx = new Transaction();
-    tx.recentBlockhash = lastBlock;
+    tx.recentBlockhash = context.lastBlockhash;
     tx.add(ix).sign(payer);
 
     // Process Transaction with all the instructions
     await client.processTransaction(tx);
   });
 
-  it("should deposit rent into the vault", async () => {
-
-  });
-
   it("should create new account using rent vault", async () => {
     const new_account = Keypair.generate();
 
@@ -83,7 +79,6 @@ describe("Pay the rent for an account using a PDA", () => {
     keys: [
         { pubkey: vault_pda, isSigner: false, isWritable: true },
         { pubkey: new_account.publicKey, isSigner: true, isWritable: true },
-        { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
     ],
     programId: PROGRAM_ID,
     data,