Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/ex01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,31 @@ pub const LAMPORTS_PER_SOL: u64 = 1_000_000_000;
///
/// `sol_to_lamports(2)` should return `2_000_000_000`.
pub fn sol_to_lamports(sol: u64) -> u64 {
todo!()
sol * LAMPORTS_PER_SOL
}

/// Convert lamports into whole SOL, dropping any remainder.
///
/// `lamports_to_sol(2_500_000_000)` should return `2`.
pub fn lamports_to_sol(lamports: u64) -> u64 {
todo!()
lamports / LAMPORTS_PER_SOL
}

/// Add a bonus to a balance and return the new balance.
pub fn apply_bonus(balance: u64, bonus: u64) -> u64 {
todo!()
balance + bonus
}

/// Double a value.
///
/// Use shadowing in your implementation: declare a new `value` instead of
/// mutating the parameter.
pub fn double(value: u64) -> u64 {
todo!()
let value = value * 2;
value
}

/// Return `true` when the balance is zero.
pub fn is_empty(balance: u64) -> bool {
todo!()
balance == 0
}
8 changes: 4 additions & 4 deletions src/ex02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
/// Solana charges 5_000 lamports per signature. This is only the base fee;
/// priority fees are added separately.
pub fn base_fee(signatures: u64) -> u64 {
todo!()
signatures * 5_000
}

/// Sum every amount in the slice.
///
/// An empty slice sums to zero.
pub fn total(amounts: &[u64]) -> u64 {
todo!()
amounts.iter().sum()
}

/// Count how many amounts are strictly greater than `threshold`.
pub fn count_above(amounts: &[u64], threshold: u64) -> usize {
todo!()
amounts.iter().filter(|&&amount| amount > threshold).count()
}

/// Never transfer more than the balance.
///
/// Return the amount that can actually be transferred.
pub fn clamp_transfer(amount: u64, balance: u64) -> u64 {
todo!()
amount.min(balance)
}
9 changes: 5 additions & 4 deletions src/ex03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
/// `make_label(String::from("alice"))` should return `"account:alice"`.
/// The function takes ownership of the name.
pub fn make_label(owner: String) -> String {
todo!()
format!("account:{}", owner)
}

/// Return an owned copy of a borrowed string.
///
/// The input is borrowed with `&str`, so the caller keeps ownership.
pub fn duplicate(text: &str) -> String {
todo!()
text.to_string()
}

/// Take ownership of an account name and return it together with its length.
///
/// The caller cannot use the original `String` after this call.
pub fn take_and_return(account: String) -> (String, usize) {
todo!()
let length = account.len();
(account, length)
}

/// Consume a vector of account names and return how many there were.
pub fn consume_all(accounts: Vec<String>) -> usize {
todo!()
accounts.len()
}
8 changes: 4 additions & 4 deletions src/ex04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
///
/// The slice is borrowed, so the caller keeps ownership.
pub fn total(amounts: &[u64]) -> u64 {
todo!()
amounts.iter().sum()
}

/// Add a fee to a balance through a mutable reference.
///
/// The function returns nothing. The caller's value must change.
pub fn add_fee(amount: &mut u64, fee: u64) {
todo!()
*amount += fee;
}

/// Count how many accounts are writable.
pub fn count_writable(flags: &[bool]) -> usize {
todo!()
flags.iter().filter(|&&flag| flag).count()
}

/// Append a note to a history through a mutable reference.
pub fn append_note(history: &mut Vec<String>, note: &str) {
todo!()
history.push(note.to_string());
}
17 changes: 13 additions & 4 deletions src/ex05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,35 @@ impl Account {
///
/// The owner of a new account is the `"system"` program.
pub fn new(address: String) -> Self {
todo!()
Self {
address,
lamports: 0,
owner: "system".to_string(),
}
}

/// Add lamports to the account.
pub fn deposit(&mut self, amount: u64) {
todo!()
self.lamports += amount;
}

/// Remove lamports from the account.
///
/// Return `true` when the withdrawal succeeded. Return `false` and leave
/// the balance unchanged when there are not enough lamports.
pub fn withdraw(&mut self, amount: u64) -> bool {
todo!()
if self.lamports >= amount {
self.lamports -= amount;
true
} else {
false
}
}

/// Return the balance in whole SOL, dropping any remainder.
///
/// This is for display only. Never use a floating-point number for money.
pub fn balance_in_sol(&self) -> u64 {
todo!()
self.lamports / 1_000_000_000
}
}
17 changes: 14 additions & 3 deletions src/ex06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@ pub enum Instruction {

/// Return the instruction kind: `"transfer"`, `"create"`, or `"close"`.
pub fn kind(ix: &Instruction) -> &'static str {
todo!()
match ix {
Instruction::Transfer { .. } => "transfer",
Instruction::CreateAccount { .. } => "create",
Instruction::Close(_) => "close",
}
}

/// Return how many lamports the instruction moves.
///
/// Only `Transfer` moves lamports. Every other variant returns `0`.
pub fn lamports_moved(ix: &Instruction) -> u64 {
todo!()
match ix {
Instruction::Transfer { lamports, .. } => *lamports,
_ => 0,
}
}

/// Return the account that is the main subject of the instruction.
///
/// For `Transfer` return the sender. For `CreateAccount` return the new
/// address. For `Close` return the address being closed.
pub fn account_touched(ix: &Instruction) -> &str {
todo!()
match ix {
Instruction::Transfer { from, .. } => from,
Instruction::CreateAccount { address } => address,
Instruction::Close(address) => address,
}
}
20 changes: 17 additions & 3 deletions src/ex07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ pub enum TransferError {
/// 2. `SameAccount` when `from == to`
/// 3. `Ok(())` otherwise
pub fn validate(from: &str, to: &str, amount: u64) -> Result<(), TransferError> {
todo!()
if amount == 0 {
Err(TransferError::ZeroAmount)
} else if from == to {
Err(TransferError::SameAccount)
} else {
Ok(())
}
}

/// Validate a transfer and return the sender's remaining balance.
Expand All @@ -31,12 +37,20 @@ pub fn transfer(
to: &str,
amount: u64,
) -> Result<u64, TransferError> {
todo!()
validate(from, to, amount)?;
if amount > balance {
Err(TransferError::InsufficientFunds {
available: balance,
required: amount,
})
} else {
Ok(balance - amount)
}
}

/// Pay through `transfer` and propagate any error with `?`.
///
/// This function exists to show the shape of a function that uses `?`.
pub fn pay(balance: u64, from: &str, to: &str, amount: u64) -> Result<u64, TransferError> {
todo!()
transfer(balance, from, to , amount)
}
29 changes: 25 additions & 4 deletions src/ex08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ pub struct BankAccount {
impl BankAccount {
/// Create an account with zero lamports and an empty history.
pub fn new(owner: &str) -> Self {
todo!()
Self {
owner: owner.to_string(),
lamports: 0,
history: vec ![],
}
}

/// Deposit lamports and record it in the history.
///
/// Return `BankError::ZeroAmount` for a zero deposit. A successful deposit
/// appends `"deposit <amount>"` to the history.
pub fn deposit(&mut self, amount: u64) -> Result<(), BankError> {
todo!()
if amount == 0 {
Err(BankError::ZeroAmount)
} else {
self.lamports += amount;
self.history.push(format!("deposit {}", amount));
Ok(())
}
}

/// Withdraw lamports and record it in the history.
Expand All @@ -39,11 +49,22 @@ impl BankAccount {
/// `BankError::InsufficientFunds` when the balance is too low. A successful
/// withdrawal appends `"withdraw <amount>"` to the history.
pub fn withdraw(&mut self, amount: u64) -> Result<(), BankError> {
todo!()
if amount == 0 {
Err(BankError::ZeroAmount)
} else if amount > self.lamports {
Err(BankError::InsufficientFunds {
available: self.lamports,
required: amount,
})
} else {
self.lamports -= amount;
self.history.push(format!("withdraw {}", amount));
Ok(())
}
}

/// Return the balance in whole SOL, dropping any remainder.
pub fn balance_in_sol(&self) -> u64 {
todo!()
self.lamports / 1_000_000_000
}
}