diff --git a/cmd/crates/soroban-spec-tools/src/lib.rs b/cmd/crates/soroban-spec-tools/src/lib.rs index bc3c2a3866..e6d496437e 100644 --- a/cmd/crates/soroban-spec-tools/src/lib.rs +++ b/cmd/crates/soroban-spec-tools/src/lib.rs @@ -1429,8 +1429,7 @@ mod tests { 0xc7, 0x79, 0xe4, 0xfe, 0x66, 0xe5, 0x6a, 0x24, 0x70, 0xdc, 0x98, 0xc0, 0xec, 0x9c, 0x07, 0x3d, 0x05, 0xc7, 0xb1, 0x03, ] - .try_into() - .unwrap() + .into() )) ), Err(e) => panic!("Unexpected error: {e}"), @@ -1441,7 +1440,7 @@ mod tests { Ok(addr) => assert_eq!( addr, ScVal::Address(ScAddress::Account(AccountId( - PublicKey::PublicKeyTypeEd25519([0; 32].try_into().unwrap()) + PublicKey::PublicKeyTypeEd25519([0; 32].into()) ))) ), Err(e) => panic!("Unexpected error: {e}"), @@ -1458,8 +1457,7 @@ mod tests { 0xc7, 0x79, 0xe4, 0xfe, 0x66, 0xe5, 0x6a, 0x24, 0x70, 0xdc, 0x98, 0xc0, 0xec, 0x9c, 0x07, 0x3d, 0x05, 0xc7, 0xb1, 0x03, ] - .try_into() - .unwrap() + .into() ) ))) ), diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/lib.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/lib.rs index 528b84227f..6b899e5df3 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/lib.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/lib.rs @@ -14,6 +14,7 @@ impl AtomicSwapContract { // Swap token A for token B atomically. Settle for the minimum requested price // for each party (this is an arbitrary choice; both parties could have // received the full amount as well). + #[allow(clippy::too_many_arguments)] pub fn swap( env: Env, a: Address, @@ -26,12 +27,8 @@ impl AtomicSwapContract { min_a_for_b: i128, ) { // Verify preconditions on the minimum price for both parties. - if amount_b < min_b_for_a { - panic!("not enough token B for token A"); - } - if amount_a < min_a_for_b { - panic!("not enough token A for token B"); - } + assert!(amount_b >= min_b_for_a, "not enough token B for token A"); + assert!(amount_a >= min_a_for_b, "not enough token A for token B"); // Require authorization for a subset of arguments specific to a party. // Notice, that arguments are symmetric - there is no difference between // `a` and `b` in the call and hence their signatures can be used @@ -70,7 +67,7 @@ fn move_token( token.transfer( &contract_address, from, - &(&max_spend_amount - &transfer_amount), + &(max_spend_amount - transfer_amount), ); } diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/test.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/test.rs index cf7929ef3e..e32b45e0a7 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/test.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/swap/src/test.rs @@ -32,10 +32,10 @@ fn test_atomic_swap() { let token_admin = Address::generate(&env); - let (token_a, token_a_admin) = create_token_contract(&env, &token_admin); - let (token_b, token_b_admin) = create_token_contract(&env, &token_admin); - token_a_admin.mint(&a, &1000); - token_b_admin.mint(&b, &5000); + let (token_a, a_admin) = create_token_contract(&env, &token_admin); + let (token_b, b_admin) = create_token_contract(&env, &token_admin); + a_admin.mint(&a, &1000); + b_admin.mint(&b, &5000); let contract = create_atomic_swap_contract(&env); diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/admin.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/admin.rs index a820bf040b..c39b341b1f 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/admin.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/admin.rs @@ -2,7 +2,7 @@ use soroban_sdk::{Address, Env}; use crate::storage_types::DataKey; -pub fn has_administrator(e: &Env) -> bool { +pub fn has(e: &Env) -> bool { let key = DataKey::Admin; e.storage().instance().has(&key) } diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/allowance.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/allowance.rs index ad7468716e..a414fc2e39 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/allowance.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/allowance.rs @@ -1,7 +1,7 @@ use crate::storage_types::{AllowanceDataKey, AllowanceValue, DataKey}; use soroban_sdk::{Address, Env}; -pub fn read_allowance(e: &Env, from: Address, spender: Address) -> AllowanceValue { +pub fn read(e: &Env, from: Address, spender: Address) -> AllowanceValue { let key = DataKey::Allowance(AllowanceDataKey { from, spender }); if let Some(allowance) = e.storage().temporary().get::<_, AllowanceValue>(&key) { if allowance.expiration_ledger < e.ledger().sequence() { @@ -20,21 +20,16 @@ pub fn read_allowance(e: &Env, from: Address, spender: Address) -> AllowanceValu } } -pub fn write_allowance( - e: &Env, - from: Address, - spender: Address, - amount: i128, - expiration_ledger: u32, -) { +pub fn write(e: &Env, from: Address, spender: Address, amount: i128, expiration_ledger: u32) { let allowance = AllowanceValue { amount, expiration_ledger, }; - if amount > 0 && expiration_ledger < e.ledger().sequence() { - panic!("expiration_ledger is less than ledger seq when amount > 0") - } + assert!( + !(amount > 0 && expiration_ledger < e.ledger().sequence()), + "expiration_ledger is less than ledger seq when amount > 0" + ); let key = DataKey::Allowance(AllowanceDataKey { from, spender }); e.storage().temporary().set(&key.clone(), &allowance); @@ -44,16 +39,14 @@ pub fn write_allowance( .checked_sub(e.ledger().sequence()) .unwrap(); - e.storage().temporary().extend_ttl(&key, live_for, live_for) + e.storage().temporary().extend_ttl(&key, live_for, live_for); } } -pub fn spend_allowance(e: &Env, from: Address, spender: Address, amount: i128) { - let allowance = read_allowance(e, from.clone(), spender.clone()); - if allowance.amount < amount { - panic!("insufficient allowance"); - } - write_allowance( +pub fn spend(e: &Env, from: Address, spender: Address, amount: i128) { + let allowance = read(e, from.clone(), spender.clone()); + assert!(allowance.amount >= amount, "insufficient allowance"); + write( e, from, spender, diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/balance.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/balance.rs index 76134e8d82..56d824c45d 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/balance.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/balance.rs @@ -1,7 +1,7 @@ use crate::storage_types::{DataKey, BALANCE_BUMP_AMOUNT, BALANCE_LIFETIME_THRESHOLD}; use soroban_sdk::{Address, Env}; -pub fn read_balance(e: &Env, addr: Address) -> i128 { +pub fn read(e: &Env, addr: Address) -> i128 { let key = DataKey::Balance(addr); if let Some(balance) = e.storage().persistent().get::(&key) { e.storage() @@ -13,7 +13,7 @@ pub fn read_balance(e: &Env, addr: Address) -> i128 { } } -fn write_balance(e: &Env, addr: Address, amount: i128) { +fn write(e: &Env, addr: Address, amount: i128) { let key = DataKey::Balance(addr); e.storage().persistent().set(&key, &amount); e.storage() @@ -21,15 +21,13 @@ fn write_balance(e: &Env, addr: Address, amount: i128) { .extend_ttl(&key, BALANCE_LIFETIME_THRESHOLD, BALANCE_BUMP_AMOUNT); } -pub fn receive_balance(e: &Env, addr: Address, amount: i128) { - let balance = read_balance(e, addr.clone()); - write_balance(e, addr, balance + amount); +pub fn receive(e: &Env, addr: Address, amount: i128) { + let balance = read(e, addr.clone()); + write(e, addr, balance + amount); } -pub fn spend_balance(e: &Env, addr: Address, amount: i128) { - let balance = read_balance(e, addr.clone()); - if balance < amount { - panic!("insufficient balance"); - } - write_balance(e, addr, balance - amount); +pub fn spend(e: &Env, addr: Address, amount: i128) { + let balance = read(e, addr.clone()); + assert!(balance >= amount, "insufficient balance"); + write(e, addr, balance - amount); } diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/contract.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/contract.rs index cc5690c6cf..519a8fdea8 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/contract.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/contract.rs @@ -1,19 +1,14 @@ //! This contract demonstrates a sample implementation of the Soroban token //! interface. -use crate::admin::{has_administrator, read_administrator, write_administrator}; -use crate::allowance::{read_allowance, spend_allowance, write_allowance}; -use crate::balance::{read_balance, receive_balance, spend_balance}; -use crate::metadata::{read_decimal, read_name, read_symbol, write_metadata}; use crate::storage_types::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD}; +use crate::{admin, allowance, balance, metadata}; use soroban_sdk::token::{self, Interface as _}; use soroban_sdk::{contract, contractimpl, Address, Env, String}; use soroban_token_sdk::metadata::TokenMetadata; use soroban_token_sdk::TokenUtils; fn check_nonnegative_amount(amount: i128) { - if amount < 0 { - panic!("negative amount is not allowed: {}", amount) - } + assert!(amount >= 0, "negative amount is not allowed: {amount}"); } #[contract] @@ -22,46 +17,42 @@ pub struct Token; #[contractimpl] impl Token { pub fn initialize(e: Env, admin: Address, decimal: u32, name: String, symbol: String) { - if has_administrator(&e) { - panic!("already initialized") - } - write_administrator(&e, &admin); - if decimal > u8::MAX.into() { - panic!("Decimal must fit in a u8"); - } - - write_metadata( + assert!(!admin::has(&e), "already initialized"); + admin::write_administrator(&e, &admin); + assert!(decimal <= u8::MAX.into(), "Decimal must fit in a u8"); + + metadata::write( &e, TokenMetadata { decimal, name, symbol, }, - ) + ); } pub fn mint(e: Env, to: Address, amount: i128) { check_nonnegative_amount(amount); - let admin = read_administrator(&e); + let admin = admin::read_administrator(&e); admin.require_auth(); e.storage() .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - receive_balance(&e, to.clone(), amount); + balance::receive(&e, to.clone(), amount); TokenUtils::new(&e).events().mint(admin, to, amount); } pub fn set_admin(e: Env, new_admin: Address) { - let admin = read_administrator(&e); + let admin = admin::read_administrator(&e); admin.require_auth(); e.storage() .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - write_administrator(&e, &new_admin); + admin::write_administrator(&e, &new_admin); TokenUtils::new(&e).events().set_admin(admin, new_admin); } } @@ -72,7 +63,7 @@ impl token::Interface for Token { e.storage() .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - read_allowance(&e, from, spender).amount + allowance::read(&e, from, spender).amount } fn approve(e: Env, from: Address, spender: Address, amount: i128, expiration_ledger: u32) { @@ -84,7 +75,7 @@ impl token::Interface for Token { .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - write_allowance(&e, from.clone(), spender.clone(), amount, expiration_ledger); + allowance::write(&e, from.clone(), spender.clone(), amount, expiration_ledger); TokenUtils::new(&e) .events() .approve(from, spender, amount, expiration_ledger); @@ -94,7 +85,7 @@ impl token::Interface for Token { e.storage() .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - read_balance(&e, id) + balance::read(&e, id) } fn transfer(e: Env, from: Address, to: Address, amount: i128) { @@ -106,8 +97,8 @@ impl token::Interface for Token { .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - spend_balance(&e, from.clone(), amount); - receive_balance(&e, to.clone(), amount); + balance::spend(&e, from.clone(), amount); + balance::receive(&e, to.clone(), amount); TokenUtils::new(&e).events().transfer(from, to, amount); } @@ -120,10 +111,10 @@ impl token::Interface for Token { .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - spend_allowance(&e, from.clone(), spender, amount); - spend_balance(&e, from.clone(), amount); - receive_balance(&e, to.clone(), amount); - TokenUtils::new(&e).events().transfer(from, to, amount) + allowance::spend(&e, from.clone(), spender, amount); + balance::spend(&e, from.clone(), amount); + balance::receive(&e, to.clone(), amount); + TokenUtils::new(&e).events().transfer(from, to, amount); } fn burn(e: Env, from: Address, amount: i128) { @@ -135,7 +126,7 @@ impl token::Interface for Token { .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - spend_balance(&e, from.clone(), amount); + balance::spend(&e, from.clone(), amount); TokenUtils::new(&e).events().burn(from, amount); } @@ -148,20 +139,20 @@ impl token::Interface for Token { .instance() .extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT); - spend_allowance(&e, from.clone(), spender, amount); - spend_balance(&e, from.clone(), amount); - TokenUtils::new(&e).events().burn(from, amount) + allowance::spend(&e, from.clone(), spender, amount); + balance::spend(&e, from.clone(), amount); + TokenUtils::new(&e).events().burn(from, amount); } fn decimals(e: Env) -> u32 { - read_decimal(&e) + metadata::decimal(&e) } fn name(e: Env) -> String { - read_name(&e) + metadata::name(&e) } fn symbol(e: Env) -> String { - read_symbol(&e) + metadata::symbol(&e) } } diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/lib.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/lib.rs index b5f04e4dcf..5151c189fe 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/lib.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/lib.rs @@ -3,7 +3,7 @@ mod admin; mod allowance; mod balance; -mod contract; +pub mod contract; mod metadata; mod storage_types; mod test; diff --git a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/metadata.rs b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/metadata.rs index 715feeeaa1..0ae7c40761 100644 --- a/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/metadata.rs +++ b/cmd/crates/soroban-test/tests/fixtures/test-wasms/token/src/metadata.rs @@ -1,22 +1,22 @@ use soroban_sdk::{Env, String}; use soroban_token_sdk::{metadata::TokenMetadata, TokenUtils}; -pub fn read_decimal(e: &Env) -> u32 { +pub fn decimal(e: &Env) -> u32 { let util = TokenUtils::new(e); util.metadata().get_metadata().decimal } -pub fn read_name(e: &Env) -> String { +pub fn name(e: &Env) -> String { let util = TokenUtils::new(e); util.metadata().get_metadata().name } -pub fn read_symbol(e: &Env) -> String { +pub fn symbol(e: &Env) -> String { let util = TokenUtils::new(e); util.metadata().get_metadata().symbol } -pub fn write_metadata(e: &Env, metadata: TokenMetadata) { +pub fn write(e: &Env, metadata: TokenMetadata) { let util = TokenUtils::new(e); util.metadata().set_metadata(&metadata); } diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index b5a4ce2c04..db21beaae1 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -149,7 +149,7 @@ struct TemplateFiles; fn init( project_path: &Path, - frontend_template: &String, + frontend_template: &str, with_examples: &[String], ) -> Result<(), Error> { // create a project dir, and copy the contents of the base template (contract-init-template) into it @@ -453,7 +453,7 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = vec![]; - init(project_dir.as_path(), &String::new(), &with_examples).unwrap(); + init(project_dir.as_path(), "", &with_examples).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -472,7 +472,7 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = ["alloc".to_owned()]; - init(project_dir.as_path(), &String::new(), &with_examples).unwrap(); + init(project_dir.as_path(), "", &with_examples).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -495,7 +495,7 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join("project"); let with_examples = ["account".to_owned(), "atomic_swap".to_owned()]; - init(project_dir.as_path(), &String::new(), &with_examples).unwrap(); + init(project_dir.as_path(), "", &with_examples).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -519,7 +519,7 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join("project"); let with_examples = ["invalid_example".to_owned(), "atomic_swap".to_owned()]; - assert!(init(project_dir.as_path(), &String::new(), &with_examples,).is_err()); + assert!(init(project_dir.as_path(), "", &with_examples,).is_err()); temp_dir.close().unwrap(); }