Skip to content

Commit

Permalink
Initialize vault and load it with 1029$
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitish-bot committed Nov 1, 2024
1 parent caf69bc commit c5c56f1
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 175 deletions.
1 change: 1 addition & 0 deletions basics/pda-rent-payer/steel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords = ["solana"]

[workspace.dependencies]
pda-rent-payer-api = { path = "./api", version = "0.1.0" }
borsh = "1.5"
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
Expand Down
6 changes: 3 additions & 3 deletions basics/pda-rent-payer/steel/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Steel
# PDA Rent Payer

**Steel** is a ...
**PDA Rent Payer** is a program that uses a PDA to pay the rent
for the creation of a system program by simply transferring lamports to it

## API
- [`Consts`](api/src/consts.rs) – Program constants.
- [`Error`](api/src/error.rs) – Custom program errors.
- [`Event`](api/src/event.rs) – Custom program events.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions
Expand Down
1 change: 1 addition & 0 deletions basics/pda-rent-payer/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme.workspace = true
keywords.workspace = true

[dependencies]
borsh.workspace = true
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion basics/pda-rent-payer/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use borsh::{ BorshDeserialize, BorshSerialize };
use steel::*;

#[repr(u8)]
Expand All @@ -8,7 +9,7 @@ pub enum PdaRentPayerInstruction {
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Copy, Debug, Pod, Zeroable)]
pub struct InitializeRentVault {
pub amount: u64,
}
Expand Down
2 changes: 1 addition & 1 deletion basics/pda-rent-payer/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pub mod prelude {
use steel::*;

// TODO Set program id
declare_id!("H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg");
declare_id!("HK5TuboXztZv7anSa3GptyCZ5wMYiqbY8kNSVEtqWDuD");
8 changes: 2 additions & 6 deletions basics/pda-rent-payer/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ use steel::*;

use crate::prelude::*;

pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey, data: &[u8]) -> Instruction {
pub fn init_rent_vault(signer_info: Pubkey, system_program: Pubkey, amount: u64) -> 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, false),
],
data: InitializeRentVault {
amount: u64::from_be_bytes(data[..8].try_into().unwrap()),
}
.to_bytes(),
data: InitializeRentVault { amount }.to_bytes(),
}
}

Expand All @@ -23,7 +20,6 @@ pub fn create_new_account(rent_vault: Pubkey, new_account: Pubkey) -> Instructio
accounts: vec![
AccountMeta::new(rent_vault, false),
AccountMeta::new(new_account, true),
AccountMeta::new_readonly(system_program::ID, false),
],
data: CreateNewAccount {}.to_bytes(),
}
Expand Down
6 changes: 3 additions & 3 deletions basics/pda-rent-payer/steel/api/src/state/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::PdaRentPayerAccount;
use super::PdaRentPayerAccountDiscriminator;
use steel::*;

/// This empty struct represents the payer vault account
Expand All @@ -12,5 +12,5 @@ pub struct RentVault {}
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct NewAccount {}

account!(PdaRentPayerAccount, RentVault);
account!(PdaRentPayerAccount, NewAccount);
account!(PdaRentPayerAccountDiscriminator, RentVault);
account!(PdaRentPayerAccountDiscriminator, NewAccount);
7 changes: 1 addition & 6 deletions basics/pda-rent-payer/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use steel::*;
/// accounts this program can interact with
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum PdaRentPayerAccount {
pub enum PdaRentPayerAccountDiscriminator {
RentVault = 0,
NewAccount = 1,
}
Expand All @@ -17,8 +17,3 @@ pub enum PdaRentPayerAccount {
pub fn rent_vault_pda() -> (Pubkey, u8) {
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())
// }
26 changes: 26 additions & 0 deletions basics/pda-rent-payer/steel/cicd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# Buld and deploy this program with ease using a single command
# Run this script with "bash cicd.sh" or "./cicd.sh"
# Note: Try running "chmod +x cicd.sh" if you face any issues.

# Check if cargo is installed
if ! command -v cargo &> /dev/null
then
echo "Cargo could not be found. Please install Rust."
exit 1
fi

# Check if solana CLI is installed
if ! command -v solana &> /dev/null
then
echo "Solana CLI could not be found. Please install Solana."
exit 1
fi


# Build
cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so

# Deploy
solana program deploy ./program/target/so/pda_rent_payer_program.so
11 changes: 5 additions & 6 deletions basics/pda-rent-payer/steel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
"type": "module",
"description": "Use a PDA to pay the rent for the creation of a new account.",
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/test.ts",
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
"deploy": "solana program deploy ./program/target/so/pda_rent_payer_program.so"
},
"keywords": [
"solana"
],
"keywords": ["solana"],
"author": "",
"license": "MIT",
"dependencies": {
Expand All @@ -20,9 +18,10 @@
"devDependencies": {
"@types/chai": "^4.3.20",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.2",
"@types/node": "^22.8.5",
"borsh": "^2.0.0",
"chai": "^4.5.0",
"mocha": "^10.7.3",
"mocha": "^10.8.2",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3"
Expand Down
40 changes: 24 additions & 16 deletions basics/pda-rent-payer/steel/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions basics/pda-rent-payer/steel/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ keywords.workspace = true
crate-type = ["cdylib", "lib"]

[dependencies]
borsh.workspace = true
pda-rent-payer-api.workspace = true
solana-program.workspace = true
steel.workspace = true
Expand Down
35 changes: 25 additions & 10 deletions basics/pda-rent-payer/steel/program/src/create_new_account.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
use pda_rent_payer_api::prelude::*;
use solana_program::msg;
use steel::sysvar::rent::Rent;
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);

// Load and validate accounts.
let [payer_info, new_account_info] = accounts else {
pub fn process_create_account(accounts: &[AccountInfo<'_>]) -> ProgramResult {
// Load accounts
let [rent_vault_info, new_account_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

// Validate accounts
new_account_info.is_signer()?.is_empty()?.is_writable()?;
payer_info
rent_vault_info
.is_writable()?
.has_seeds(&[RENT_VAULT], &pda_rent_payer_api::ID)?;
system_program.is_program(&system_program::ID)?;

let vault_balance = rent_vault_info.lamports();

// Create new account by simply sending a
msg!("Vault balance: {}", vault_balance);
// First we get the lamports required for rent
// assuming this account has no inner data
let lamports_required_for_rent = (Rent::get()?).minimum_balance(0);

if vault_balance < lamports_required_for_rent {
return Err(ProgramError::InsufficientFunds);
}

// Then we create a new account by simply sending a
// token amount to the new account.
payer_info.send(100, new_account_info);
rent_vault_info.send(lamports_required_for_rent, new_account_info);

msg!("Created new account.");
msg!("New account: {:?}", new_account_info.key);

Ok(())
}
Loading

0 comments on commit c5c56f1

Please sign in to comment.