Skip to content

Commit

Permalink
Add tokens/token-swap/steel
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman labh authored and Aman labh committed Oct 27, 2024
1 parent 45e302a commit 2c1b70e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tokens/token-swap/steel/ tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions tokens/token-swap/steel/ tests/README.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions tokens/token-swap/steel/ tests/test_token_swap.rs
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions tokens/token-swap/steel/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -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<Self, ProgramError> {
SwapInstruction::try_from_slice(input).map_err(|_| ProgramError::InvalidInstructionData)
}
}
22 changes: 22 additions & 0 deletions tokens/token-swap/steel/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions tokens/token-swap/steel/src/processor.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
8 changes: 8 additions & 0 deletions tokens/token-swap/steel/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// src/state.rs

use solana_program::pubkey::Pubkey;

pub struct TokenAccount {
pub owner: Pubkey,
pub balance: u64,
}

0 comments on commit 2c1b70e

Please sign in to comment.