-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basics/pda-rent-payer/steel (#293)
- Loading branch information
1 parent
82a68b3
commit 123c289
Showing
20 changed files
with
1,800 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
test-ledger | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.