-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aman labh
authored and
Aman labh
committed
Oct 27, 2024
1 parent
45e302a
commit 2c1b70e
Showing
7 changed files
with
127 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,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 |
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,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 |
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,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 | ||
} |
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 @@ | ||
// 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) | ||
} | ||
} |
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 @@ | ||
// 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) | ||
} |
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,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(()) | ||
} |
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,8 @@ | ||
// src/state.rs | ||
|
||
use solana_program::pubkey::Pubkey; | ||
|
||
pub struct TokenAccount { | ||
pub owner: Pubkey, | ||
pub balance: u64, | ||
} |