Skip to content

Commit 7116bd1

Browse files
committed
Separate deposit from init vault
1 parent ecc3566 commit 7116bd1

File tree

11 files changed

+98
-74
lines changed

11 files changed

+98
-74
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/// Seed of the rent vault account PDA.
1+
// Seed of the rent vault account PDA.
22
pub const RENT_VAULT: &[u8] = b"rent_vault";
33

4-
/// Seed of the account PDA to be created.
5-
pub const NEW_ACCOUNT: &[u8] = b"new_account";
4+
// Seed of the account PDA to be created.
5+
// pub const NEW_ACCOUNT: &[u8] = b"new_account";

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ pub enum PdaRentPayerInstruction {
99

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

1620
#[repr(C)]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ pub mod prelude {
1515
use steel::*;
1616

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

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@ use steel::*;
22

33
use crate::prelude::*;
44

5-
pub fn initialize(signer: Pubkey) -> Instruction {
5+
pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey) -> Instruction {
66
Instruction {
77
program_id: crate::ID,
88
accounts: vec![
9-
AccountMeta::new(signer, true),
10-
AccountMeta::new(counter_pda().0, false),
9+
AccountMeta::new(signer_info, true),
10+
AccountMeta::new(rent_vault_pda().0, false),
1111
AccountMeta::new_readonly(system_program::ID, false),
1212
],
13-
data: Initialize {}.to_bytes()
13+
data: InitializeRentVault {}.to_bytes(),
1414
}
1515
}
1616

17-
pub fn add(signer: Pubkey, amount: u64) -> Instruction {
17+
pub fn create_new_account(rent_vault: Pubkey, new_account: Pubkey) -> Instruction {
1818
Instruction {
1919
program_id: crate::ID,
2020
accounts: vec![
21-
AccountMeta::new(signer, true),
22-
AccountMeta::new(counter_pda().0, false),
21+
AccountMeta::new(rent_vault, false),
22+
AccountMeta::new(new_account, true),
23+
AccountMeta::new_readonly(system_program::ID, false),
2324
],
24-
data: Add {
25-
amount: amount.to_le_bytes(),
26-
}
27-
.to_bytes(),
25+
data: CreateNewAccount {}.to_bytes()
2826
}
2927
}
28+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pub struct RentVault {}
1313
pub struct NewAccount {}
1414

1515
account!(PdaRentPayerAccount, RentVault);
16+
account!(PdaRentPayerAccount, NewAccount);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn rent_vault_pda() -> (Pubkey, u8) {
1818
Pubkey::find_program_address(&[RENT_VAULT], &crate::id())
1919
}
2020

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

basics/pda-rent-payer/steel/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"type": "module",
55
"description": "Use a PDA to pay the rent for the creation of a new account.",
66
"scripts": {
7-
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
7+
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts",
88
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
99
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
10-
"deploy": "solana program deploy ./program/target/so/create_account_program.so"
10+
"deploy": "solana program deploy ./program/target/so/pda_rent_payer_program.so"
1111
},
1212
"keywords": [
1313
"solana"
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
use steel_api::prelude::*;
1+
use pda_rent_payer_api::prelude::*;
22
use steel::*;
33

4-
pub fn process_add(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
5-
// Parse args.
6-
let args = Add::try_from_bytes(data)?;
7-
let amount = u64::from_le_bytes(args.amount);
4+
pub fn process_create_account(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
5+
// // Parse args.
6+
// let args = Add::try_from_bytes(data)?;
7+
// let amount = u64::from_le_bytes(args.amount);
88

9-
// Load accounts.
10-
let [signer_info, counter_info] = accounts else {
9+
// Load and validate accounts.
10+
let [payer_info, new_account_info, system_program] = accounts else {
1111
return Err(ProgramError::NotEnoughAccountKeys);
1212
};
13-
signer_info.is_signer()?;
14-
let counter = counter_info
15-
.as_account_mut::<Counter>(&steel_api::ID)?
16-
.assert_mut(|c| c.value < 100)?;
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)?;
1722

18-
// Update state
19-
counter.value += amount;
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+
)?;
2031

2132
Ok(())
2233
}
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
use steel_api::prelude::*;
1+
use pda_rent_payer_api::prelude::*;
2+
use solana_program::msg;
23
use steel::*;
34

45
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);
9+
510
// Load and validate accounts.
6-
let [signer_info, rent_vault_info, system_program] = accounts else {
11+
let [payer_info, rent_vault_info, system_program] = accounts else {
712
return Err(ProgramError::NotEnoughAccountKeys);
813
};
9-
signer_info.is_signer()?;
14+
payer_info.is_signer()?.is_writable()?;
1015
rent_vault_info.is_empty()?.is_writable()?.has_seeds(
1116
&[RENT_VAULT],
12-
&pda_rent_payer_api::ID
17+
&pda_rent_payer_api::ID,
1318
)?;
1419
system_program.is_program(&system_program::ID)?;
1520

16-
// Initialize counter.
17-
create_account::<Counter>(
21+
// Initialize vault.
22+
create_account::<RentVault>(
1823
rent_vault_info,
1924
system_program,
20-
signer_info,
25+
payer_info,
2126
&pda_rent_payer_api::ID,
2227
&[RENT_VAULT],
2328
)?;
24-
let rent_vault = rent_vault_info.as_account_mut::<RentVault>(&pda_rent_payer_api::ID)?;
29+
30+
let _ = rent_vault_info.as_account_mut::<RentVault>(&pda_rent_payer_api::ID)?;
31+
32+
msg!("Initialized rent vault.");
33+
msg!("PDA: {:?}", rent_vault_info.key);
2534

2635
Ok(())
2736
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub fn process_instruction(
1212
accounts: &[AccountInfo],
1313
data: &[u8],
1414
) -> ProgramResult {
15+
/// Parse instruction automatically detects which instruction is being called
16+
/// based on the discriminator and returns the instruction and the data
1517
let (ix ,data) = parse_instruction(&pda_rent_payer_api::ID, program_id, data)?;
1618

1719
match ix {

0 commit comments

Comments
 (0)