diff --git a/tokens/token-swap/steel/ tests/Cargo.toml b/tokens/token-swap/steel/ tests/Cargo.toml new file mode 100644 index 000000000..5f73b409a --- /dev/null +++ b/tokens/token-swap/steel/ tests/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "token_swap" +version = "0.1.0" +edition = "2018" + +[dependencies] +solana-program = "1.8.0" +borsh = "0.9.1" # Serialization for instructions +steel = "version" # for Steel framework if using Steel +poseidon = "version" # for Poseidon framework if using Poseidon diff --git a/tokens/token-swap/steel/ tests/README.md b/tokens/token-swap/steel/ tests/README.md new file mode 100644 index 000000000..7415cf987 --- /dev/null +++ b/tokens/token-swap/steel/ tests/README.md @@ -0,0 +1,14 @@ +# Token Swap Program + +This program demonstrates a token swap between two accounts on Solana. It allows a user to transfer a specific amount from one account to another, enforcing balance checks and account ownership. + +## How to Use + +1. Deploy the program using Solana CLI. +2. Use the test script in `tests/test_token_swap.rs` to verify functionality. + +## Testing + +To test the program, run: +```bash +cargo test diff --git a/tokens/token-swap/steel/ tests/test_token_swap.rs b/tokens/token-swap/steel/ tests/test_token_swap.rs new file mode 100644 index 000000000..8608d74e8 --- /dev/null +++ b/tokens/token-swap/steel/ tests/test_token_swap.rs @@ -0,0 +1,20 @@ +// tests/test_token_swap.rs + +use solana_program_test::*; +use solana_sdk::{ + signature::Keypair, + transaction::Transaction, +}; +use token_swap::*; + +#[tokio::test] +async fn test_token_swap() { + let program = ProgramTest::new("token_swap", id(), processor!(process_instruction)); + let (mut banks_client, payer, recent_blockhash) = program.start().await; + + let source_account = Keypair::new(); + let destination_account = Keypair::new(); + + // Create a transaction for token swap and check balances + // Here, you would set initial balances, execute a swap, and verify final balances +} diff --git a/tokens/token-swap/steel/src/instruction.rs b/tokens/token-swap/steel/src/instruction.rs new file mode 100644 index 000000000..728b004ec --- /dev/null +++ b/tokens/token-swap/steel/src/instruction.rs @@ -0,0 +1,16 @@ +// src/instruction.rs + +use solana_program::program_error::ProgramError; +use borsh::{BorshDeserialize, BorshSerialize}; + +#[derive(BorshSerialize, BorshDeserialize)] +pub enum SwapInstruction { + /// Swaps tokens between two accounts. + Swap { amount: u64 }, +} + +impl SwapInstruction { + pub fn unpack(input: &[u8]) -> Result { + SwapInstruction::try_from_slice(input).map_err(|_| ProgramError::InvalidInstructionData) + } +} diff --git a/tokens/token-swap/steel/src/lib.rs b/tokens/token-swap/steel/src/lib.rs new file mode 100644 index 000000000..c7a81dd93 --- /dev/null +++ b/tokens/token-swap/steel/src/lib.rs @@ -0,0 +1,22 @@ +// src/lib.rs + +mod instruction; +mod processor; +mod state; + +use solana_program::{ + account_info::AccountInfo, + entrypoint, + entrypoint::ProgramResult, + pubkey::Pubkey, +}; + +entrypoint!(process_instruction); + +fn process_instruction( + program_id: &Pubkey, + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> ProgramResult { + processor::process(program_id, accounts, instruction_data) +} diff --git a/tokens/token-swap/steel/src/processor.rs b/tokens/token-swap/steel/src/processor.rs new file mode 100644 index 000000000..bcc850c51 --- /dev/null +++ b/tokens/token-swap/steel/src/processor.rs @@ -0,0 +1,37 @@ +// src/processor.rs + +use crate::instruction::SwapInstruction; +use solana_program::{ + account_info::{next_account_info, AccountInfo}, + entrypoint::ProgramResult, + pubkey::Pubkey, + program_error::ProgramError, +}; + +pub fn process( + _program_id: &Pubkey, + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> ProgramResult { + let instruction = SwapInstruction::unpack(instruction_data)?; + + let accounts_iter = &mut accounts.iter(); + let source_account = next_account_info(accounts_iter)?; + let destination_account = next_account_info(accounts_iter)?; + + match instruction { + SwapInstruction::Swap { amount } => { + let mut source_balance = source_account.lamports.borrow_mut(); + let mut destination_balance = destination_account.lamports.borrow_mut(); + + if *source_balance < amount { + return Err(ProgramError::InsufficientFunds); + } + + *source_balance -= amount; + *destination_balance += amount; + } + } + + Ok(()) +} diff --git a/tokens/token-swap/steel/src/state.rs b/tokens/token-swap/steel/src/state.rs new file mode 100644 index 000000000..3317ee16e --- /dev/null +++ b/tokens/token-swap/steel/src/state.rs @@ -0,0 +1,8 @@ +// src/state.rs + +use solana_program::pubkey::Pubkey; + +pub struct TokenAccount { + pub owner: Pubkey, + pub balance: u64, +}