Skip to content

Commit caf69bc

Browse files
committed
Build successful
1 parent 7116bd1 commit caf69bc

File tree

9 files changed

+50
-63
lines changed

9 files changed

+50
-63
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ use steel::*;
44
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
55
pub enum PdaRentPayerInstruction {
66
InitializeRentVault = 0,
7-
CreateNewAccount = 1
7+
CreateNewAccount = 1,
88
}
99

1010
#[repr(C)]
1111
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
12-
pub struct InitializeRentVault {}
13-
14-
#[repr(C)]
15-
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
16-
pub struct DepositRent {
12+
pub struct InitializeRentVault {
1713
pub amount: u64,
1814
}
1915

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ pub mod prelude {
99
pub use crate::error::*;
1010
pub use crate::instruction::*;
1111
pub use crate::sdk::*;
12-
pub use crate::state::*;
12+
pub use crate::state::*;
1313
}
1414

1515
use steel::*;
1616

1717
// TODO Set program id
18-
declare_id!("H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg");
18+
declare_id!("H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg");

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ use steel::*;
22

33
use crate::prelude::*;
44

5-
pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey) -> Instruction {
5+
pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey, data: &[u8]) -> Instruction {
66
Instruction {
77
program_id: crate::ID,
88
accounts: vec![
99
AccountMeta::new(signer_info, true),
1010
AccountMeta::new(rent_vault_pda().0, false),
11-
AccountMeta::new_readonly(system_program::ID, false),
11+
AccountMeta::new_readonly(system_program, false),
1212
],
13-
data: InitializeRentVault {}.to_bytes(),
13+
data: InitializeRentVault {
14+
amount: u64::from_be_bytes(data[..8].try_into().unwrap()),
15+
}
16+
.to_bytes(),
1417
}
1518
}
1619

@@ -22,7 +25,6 @@ pub fn create_new_account(rent_vault: Pubkey, new_account: Pubkey) -> Instructio
2225
AccountMeta::new(new_account, true),
2326
AccountMeta::new_readonly(system_program::ID, false),
2427
],
25-
data: CreateNewAccount {}.to_bytes()
28+
data: CreateNewAccount {}.to_bytes(),
2629
}
2730
}
28-

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use steel::*;
21
use super::PdaRentPayerAccount;
2+
use steel::*;
33

44
/// This empty struct represents the payer vault account
55
#[repr(C)]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod accounts;
22

3+
use crate::consts::*;
34
pub use accounts::*;
45
use steel::*;
5-
use crate::consts::*;
66

7-
/// This enum represents the discriminator for the
7+
/// This enum represents the discriminator for the
88
/// accounts this program can interact with
99
#[repr(u8)]
1010
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
@@ -15,10 +15,10 @@ pub enum PdaRentPayerAccount {
1515

1616
/// Fetch PDA of the rent_vault account.
1717
pub fn rent_vault_pda() -> (Pubkey, u8) {
18-
Pubkey::find_program_address(&[RENT_VAULT], &crate::id())
18+
Pubkey::find_program_address(&[RENT_VAULT], &crate::id())
1919
}
2020

2121
// Fetch PDA of the newly_created account.
2222
// pub fn new_account_pda() -> (Pubkey, u8) {
23-
// Pubkey::find_program_address(&[NEW_ACCOUNT], &crate::id())
23+
// Pubkey::find_program_address(&[NEW_ACCOUNT], &crate::id())
2424
// }

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,20 @@ use steel::*;
44
pub fn process_create_account(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
55
// // Parse args.
66
// let args = Add::try_from_bytes(data)?;
7-
// let amount = u64::from_le_bytes(args.amount);
7+
// let amount = u64::from_le_bytes(args.amount);
88

99
// Load and validate accounts.
10-
let [payer_info, new_account_info, system_program] = accounts else {
11-
return Err(ProgramError::NotEnoughAccountKeys);
10+
let [payer_info, new_account_info] = accounts else {
11+
return Err(ProgramError::NotEnoughAccountKeys);
1212
};
13-
new_account_info
14-
.is_signer()?
15-
.is_empty()?
16-
.is_writable()?;
17-
payer_info.is_writable()?.has_seeds(
18-
&[RENT_VAULT],
19-
&pda_rent_payer_api::ID,
20-
)?;
21-
system_program.is_program(&system_program::ID)?;
13+
new_account_info.is_signer()?.is_empty()?.is_writable()?;
14+
payer_info
15+
.is_writable()?
16+
.has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
2217

23-
// Create new account.
24-
create_account::<NewAccount>(
25-
new_account_info,
26-
system_program,
27-
payer_info,
28-
&pda_rent_payer_api::ID,
29-
&[RENT_VAULT],
30-
)?;
18+
// Create new account by simply sending a
19+
// token amount to the new account.
20+
payer_info.send(100, new_account_info);
3121

3222
Ok(())
3323
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ use pda_rent_payer_api::prelude::*;
22
use solana_program::msg;
33
use steel::*;
44

5-
pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
6-
// // Parse args
7-
// let args = data[..8].try_into().expect("Ma chud gayi");
8-
// let amount = u64::from_le_bytes(args);
5+
pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
6+
// Parse args
7+
let args = data[..8].try_into().expect("Error parsing args");
8+
let amount = u64::from_le_bytes(args);
99

1010
// Load and validate accounts.
1111
let [payer_info, rent_vault_info, system_program] = accounts else {
12-
return Err(ProgramError::NotEnoughAccountKeys);
12+
return Err(ProgramError::NotEnoughAccountKeys);
1313
};
14-
payer_info.is_signer()?.is_writable()?;
15-
rent_vault_info.is_empty()?.is_writable()?.has_seeds(
16-
&[RENT_VAULT],
17-
&pda_rent_payer_api::ID,
18-
)?;
14+
payer_info.is_signer()?;
15+
rent_vault_info
16+
.is_empty()?
17+
.is_writable()?
18+
.has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
1919
system_program.is_program(&system_program::ID)?;
2020

2121
// Initialize vault.
@@ -27,6 +27,10 @@ pub fn process_initialize_vault(accounts: &[AccountInfo<'_>], _data: &[u8]) -> P
2727
&[RENT_VAULT],
2828
)?;
2929

30+
payer_info.send(amount, rent_vault_info);
31+
32+
rent_vault_info.collect(amount, payer_info)?;
33+
3034
let _ = rent_vault_info.as_account_mut::<RentVault>(&pda_rent_payer_api::ID)?;
3135

3236
msg!("Initialized rent vault.");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
mod init_rent_vault;
21
mod create_new_account;
2+
mod init_rent_vault;
33

4-
use init_rent_vault::*;
54
use create_new_account::*;
5+
use init_rent_vault::*;
66

77
use pda_rent_payer_api::prelude::*;
88
use steel::*;
@@ -14,7 +14,7 @@ pub fn process_instruction(
1414
) -> ProgramResult {
1515
/// Parse instruction automatically detects which instruction is being called
1616
/// based on the discriminator and returns the instruction and the data
17-
let (ix ,data) = parse_instruction(&pda_rent_payer_api::ID, program_id, data)?;
17+
let (ix, data) = parse_instruction(&pda_rent_payer_api::ID, program_id, data)?;
1818

1919
match ix {
2020
PdaRentPayerInstruction::InitializeRentVault => process_initialize_vault(accounts, data)?,

basics/pda-rent-payer/steel/tests/test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ const LOAD_LAMPORTS = LAMPORTS_PER_SOL; // 1 SOL
2424

2525
const instructionDiscriminators = {
2626
InitializeRentVault: Buffer.from([0]),
27-
DepositRent: Buffer.from([1]),
28-
CreateNewAccount: Buffer.from([2]),
27+
CreateNewAccount: Buffer.from([1]),
2928
}
3029

3130
describe("Pay the rent for an account using a PDA", () => {
3231
let context: ProgramTestContext;
33-
let lastBlock: Blockhash;
3432
let client: BanksClient;
3533
let payer: Keypair;
3634

@@ -46,34 +44,32 @@ describe("Pay the rent for an account using a PDA", () => {
4644
);
4745
client = context.banksClient;
4846
payer = context.payer;
49-
lastBlock = context.lastBlockhash;
5047

5148
});
5249

5350
it("should initialize rent vault PDA", async () => {
54-
const data = Buffer.concat([instructionDiscriminators.InitializeRentVault]);
51+
const amount = Buffer.alloc(8);
52+
amount.writeBigInt64BE(BigInt(LOAD_LAMPORTS), 0);
53+
const data = Buffer.concat([instructionDiscriminators.InitializeRentVault, amount]);
54+
5555
const ix = new TransactionInstruction({
5656
keys: [
5757
{ pubkey: payer.publicKey, isSigner: true, isWritable: false },
58-
{ pubkey: vault_pda, isSigner: true, isWritable: true },
58+
{ pubkey: vault_pda, isSigner: false, isWritable: true },
5959
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
6060
],
6161
programId: PROGRAM_ID,
6262
data,
6363
});
6464

6565
const tx = new Transaction();
66-
tx.recentBlockhash = lastBlock;
66+
tx.recentBlockhash = context.lastBlockhash;
6767
tx.add(ix).sign(payer);
6868

6969
// Process Transaction with all the instructions
7070
await client.processTransaction(tx);
7171
});
7272

73-
it("should deposit rent into the vault", async () => {
74-
75-
});
76-
7773
it("should create new account using rent vault", async () => {
7874
const new_account = Keypair.generate();
7975

@@ -83,7 +79,6 @@ describe("Pay the rent for an account using a PDA", () => {
8379
keys: [
8480
{ pubkey: vault_pda, isSigner: false, isWritable: true },
8581
{ pubkey: new_account.publicKey, isSigner: true, isWritable: true },
86-
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
8782
],
8883
programId: PROGRAM_ID,
8984
data,

0 commit comments

Comments
 (0)