Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clippy #1233

Merged
merged 1 commit into from
Mar 1, 2024
Merged
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
8 changes: 3 additions & 5 deletions cmd/crates/soroban-spec-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
Expand All @@ -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}"),
Expand All @@ -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()
)
)))
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -70,7 +67,7 @@ fn move_token(
token.transfer(
&contract_address,
from,
&(&max_spend_amount - &transfer_amount),
&(max_spend_amount - transfer_amount),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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);
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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::<DataKey, i128>(&key) {
e.storage()
Expand All @@ -13,23 +13,21 @@ 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()
.persistent()
.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);
}
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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);
}
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod admin;
mod allowance;
mod balance;
mod contract;
pub mod contract;
mod metadata;
mod storage_types;
mod test;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Loading
Loading