Skip to content

Commit

Permalink
add basics/pda-rent-payer/steel (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitish-bot authored Jan 2, 2025
1 parent 82a68b3 commit 123c289
Show file tree
Hide file tree
Showing 20 changed files with 1,800 additions and 0 deletions.
3 changes: 3 additions & 0 deletions basics/pda-rent-payer/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
test-ledger
node_modules
22 changes: 22 additions & 0 deletions basics/pda-rent-payer/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[workspace]
resolver = "2"
members = ["api", "program"]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
homepage = ""
documentation = ""
repository = ""
readme = "./README.md"
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"
steel = "2.1"
thiserror = "1.0"
28 changes: 28 additions & 0 deletions basics/pda-rent-payer/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# PDA Rent Payer

**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.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions
- [`Add`](program/src/add.rs) – Add ...
- [`Initialize`](program/src/initialize.rs) – Initialize ...

## State
- [`Counter`](api/src/state/counter.rs) – Counter ...

## Get started

Compile your program:
```sh
steel build
```

Run unit and integration tests:
```sh
steel test
```
19 changes: 19 additions & 0 deletions basics/pda-rent-payer/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "pda-rent-payer-api"
description = "API for interacting with the PDA rent payer program"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true

[dependencies]
borsh.workspace = true
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
steel.workspace = true
thiserror.workspace = true
5 changes: 5 additions & 0 deletions basics/pda-rent-payer/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Seed of the rent vault account PDA.
pub const RENT_VAULT: &[u8] = b"rent_vault";

// Seed of the account PDA to be created.
// pub const NEW_ACCOUNT: &[u8] = b"new_account";
10 changes: 10 additions & 0 deletions basics/pda-rent-payer/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use steel::*;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum PdaRentPayerError {
#[error("Rent vault account already initialized")]
RentVaultInitialized = 0,
}

error!(PdaRentPayerError);
22 changes: 22 additions & 0 deletions basics/pda-rent-payer/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use borsh::{BorshDeserialize, BorshSerialize};
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum PdaRentPayerInstruction {
InitializeRentVault = 0,
CreateNewAccount = 1,
}

#[repr(C)]
#[derive(BorshSerialize, BorshDeserialize, Clone, Copy, Debug, Pod, Zeroable)]
pub struct InitializeRentVault {
pub amount: u64,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CreateNewAccount {}

instruction!(PdaRentPayerInstruction, InitializeRentVault);
instruction!(PdaRentPayerInstruction, CreateNewAccount);
18 changes: 18 additions & 0 deletions basics/pda-rent-payer/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod consts;
pub mod error;
pub mod instruction;
pub mod sdk;
pub mod state;

pub mod prelude {
pub use crate::consts::*;
pub use crate::error::*;
pub use crate::instruction::*;
pub use crate::sdk::*;
pub use crate::state::*;
}

use steel::*;

// TODO Set program id
declare_id!("HK5TuboXztZv7anSa3GptyCZ5wMYiqbY8kNSVEtqWDuD");
26 changes: 26 additions & 0 deletions basics/pda-rent-payer/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use steel::*;

use crate::prelude::*;

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 }.to_bytes(),
}
}

pub fn create_new_account(rent_vault: Pubkey, new_account: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(rent_vault, false),
AccountMeta::new(new_account, true),
],
data: CreateNewAccount {}.to_bytes(),
}
}
16 changes: 16 additions & 0 deletions basics/pda-rent-payer/steel/api/src/state/accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::PdaRentPayerAccountDiscriminator;
use steel::*;

/// This empty struct represents the payer vault account
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct RentVault {}

/// This empty struct represents the account
/// that the vault will pay for
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct NewAccount {}

account!(PdaRentPayerAccountDiscriminator, RentVault);
account!(PdaRentPayerAccountDiscriminator, NewAccount);
19 changes: 19 additions & 0 deletions basics/pda-rent-payer/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod accounts;

use crate::consts::*;
pub use accounts::*;
use steel::*;

/// This enum represents the discriminator for the
/// accounts this program can interact with
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum PdaRentPayerAccountDiscriminator {
RentVault = 0,
NewAccount = 1,
}

/// Fetch PDA of the rent_vault account.
pub fn rent_vault_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[RENT_VAULT], &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
29 changes: 29 additions & 0 deletions basics/pda-rent-payer/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "pda-rent-payer-program",
"version": "1.0.0",
"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",
"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"],
"author": "",
"license": "MIT",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/chai": "^4.3.20",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.5",
"borsh": "^2.0.0",
"chai": "^4.5.0",
"mocha": "^10.8.2",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3"
}
}
Loading

0 comments on commit 123c289

Please sign in to comment.