Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia committed Nov 26, 2024
1 parent 7d38b44 commit 7a6e5c8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ClaimApi for Contract {

env::log_str(format!("Account {:?} has {} jars", account_id.clone(), account.jars.len()).as_str());

for (product_id, jar) in account.jars.iter() {
for (product_id, jar) in &account.jars {
if jar.is_pending_withdraw {
continue;
}
Expand Down
15 changes: 5 additions & 10 deletions contract/src/jar/account/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct AccountV1Companion {

impl Contract {
pub(crate) fn try_get_account(&self, account_id: &AccountId) -> Option<&Account> {
self.accounts.get(account_id).map(|account| account.deref())
self.accounts.get(account_id).map(Deref::deref)
}

pub(crate) fn get_account(&self, account_id: &AccountId) -> &Account {
Expand Down Expand Up @@ -129,12 +129,10 @@ impl AccountV1 {
}
}

type ProductFilter = dyn FnMut(&Product) -> bool;

impl Contract {
pub(crate) fn update_account_cache(
&mut self,
account_id: &AccountId,
filter: Option<Box<dyn FnMut(&Product) -> bool>>,
) {
pub(crate) fn update_account_cache(&mut self, account_id: &AccountId, filter: Option<Box<ProductFilter>>) {
let now = env::block_timestamp_ms();
let products = self.get_products(account_id, filter);
let account = self.get_account_mut(account_id);
Expand All @@ -150,10 +148,7 @@ impl Contract {
account.update_jar_cache(product, env::block_timestamp_ms());
}

fn get_products<P>(&self, account_id: &AccountId, filter: Option<P>) -> Vec<Product>
where
P: FnMut(&Product) -> bool,
{
fn get_products(&self, account_id: &AccountId, filter: Option<Box<ProductFilter>>) -> Vec<Product> {
let products = self
.get_account(account_id)
.jars
Expand Down
8 changes: 4 additions & 4 deletions contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Contract {
let mut detailed_amounts = HashMap::<ProductId, U128>::new();
let mut total_amount: TokenAmount = 0;

for (product_id, jar) in account.jars.iter() {
for (product_id, jar) in &account.jars {
let product = self.get_product(product_id);
let interest = product.terms.get_interest(account, jar, env::block_timestamp_ms()).0;

Expand Down Expand Up @@ -107,7 +107,7 @@ impl JarApi for Contract {
self.assert_migrated(&account_id);

let account = self.get_account_mut(&account_id);
for (_, jar) in account.jars.iter_mut() {
for jar in account.jars.values_mut() {
jar.is_pending_withdraw = false;
}
}
Expand All @@ -117,12 +117,12 @@ impl From<&AccountLegacyV2> for AccountV1 {
fn from(value: &AccountLegacyV2) -> Self {
let mut account = AccountV1 {
nonce: value.last_id,
jars: Default::default(),
jars: HashMap::default(),
score: AccountScore::default(),
is_penalty_applied: false,
};

for jar in value.jars.iter() {
for jar in &value.jars {
assert_not_locked_legacy(jar);

account.deposit(&jar.product_id, jar.principal, jar.created_at.into());
Expand Down
3 changes: 2 additions & 1 deletion contract/src/jar/model/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct JarV2 {
pub claim_remainder: u64,
}

#[allow(clippy::option_option)]
#[near(serializers=[json])]
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Default)]
pub struct JarV2Companion {
Expand Down Expand Up @@ -110,7 +111,7 @@ impl JarV2 {
}

if let Some(deposits) = &companion.deposits {
self.deposits = deposits.to_vec();
self.deposits.clone_from(deposits);
}

if let Some(is_pending_withdraw) = companion.is_pending_withdraw {
Expand Down
5 changes: 1 addition & 4 deletions contract/src/jar/verification/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ impl Contract {
valid_until: Timestamp,
nonce: u32,
) -> String {
format!(
"{},{},{},{},{},{}",
contract_account_id, receiver_account_id, product_id, amount, nonce, valid_until,
)
format!("{contract_account_id},{receiver_account_id},{product_id},{amount},{nonce},{valid_until}")
}

fn verify_signature(signature: &[u8], product_public_key: &[u8], ticket_hash: &[u8]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion contract/src/penalty/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl PenaltyApi for Contract {
fn batch_set_penalty(&mut self, account_ids: Vec<AccountId>, value: bool) {
self.assert_manager();

for account_id in account_ids.iter() {
for account_id in &account_ids {
self.assert_migrated(account_id);
self.update_account_cache(account_id, None);

Expand Down
9 changes: 5 additions & 4 deletions contract/src/restake/api/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl RestakeApi for Contract {
let mut total_mature_balance = 0;
let mut total_fee = 0;

for (product_id, jar) in self.get_account(&account_id).jars.iter() {
for (product_id, jar) in &self.get_account(&account_id).jars {
if jar.is_pending_withdraw {
continue;
}
Expand All @@ -63,7 +63,7 @@ impl RestakeApi for Contract {
}
}

for (product_id, _) in partition_indices.iter() {
for (product_id, _) in &partition_indices {
self.update_jar_cache(&account_id, product_id);
}

Expand Down Expand Up @@ -101,6 +101,7 @@ pub(super) trait RemainderTransfer {
fn transfer_remainder(&mut self, request: Request) -> PromiseOrValue<()>;
}

#[allow(dead_code)] // False positive since rust 1.78. It is used from `ext_contract` macro.
#[ext_contract(ext_self)]
pub(super) trait RemainderTransferCallback {
fn after_transfer_remainder(&mut self, request: Request) -> PromiseOrValue<()>;
Expand All @@ -110,7 +111,7 @@ pub(super) trait RemainderTransferCallback {
impl RemainderTransferCallback for Contract {
#[private]
fn after_transfer_remainder(&mut self, request: Request) -> PromiseOrValue<()> {
for (product_id, _) in request.partitions.iter() {
for (product_id, _) in &request.partitions {
self.get_account_mut(&request.account_id)
.get_jar_mut(product_id)
.unlock();
Expand All @@ -128,7 +129,7 @@ impl Contract {
pub(super) fn clean_up_and_deposit(&mut self, request: Request) {
let account = self.get_account_mut(&request.account_id);

for (product_id, partition_index) in request.partitions.iter() {
for (product_id, partition_index) in &request.partitions {
account.get_jar_mut(product_id).clean_up_deposits(*partition_index);
}

Expand Down
8 changes: 4 additions & 4 deletions contract/src/score/score_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl ScoreApi for Contract {
fn record_score(&mut self, batch: Vec<(AccountId, Vec<(Score, UTC)>)>) {
self.assert_manager();

for (account_id, _) in batch.iter() {
for (account_id, _) in &batch {
self.assert_migrated(account_id);
}

Expand All @@ -32,7 +32,7 @@ impl ScoreApi for Contract {

let account = self.get_account_mut(&account_id);
account.score.try_reset_score();
account.score.update(new_score.adjust(&account.score.timezone));
account.score.update(new_score.adjust(account.score.timezone));

event.push(ScoreData {
account_id,
Expand All @@ -58,12 +58,12 @@ impl ScoreApi for Contract {

trait ScoreConverter {
/// Convert Score to a User's timezone
fn adjust(&self, timezone: &Timezone) -> Chain;
fn adjust(&self, timezone: Timezone) -> Chain;
fn to_event(&self) -> Vec<(U32, UTC)>;
}

impl ScoreConverter for Vec<(Score, UTC)> {
fn adjust(&self, timezone: &Timezone) -> Chain {
fn adjust(&self, timezone: Timezone) -> Chain {
self.iter().map(|score| (score.0, timezone.adjust(score.1))).collect()
}

Expand Down
6 changes: 3 additions & 3 deletions contract/src/withdraw/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ impl WithdrawApi for Contract {
let fee = product.calculate_fee(amount);

let request = WithdrawalRequest {
amount,
partition_index,
product_id,
amount,
fee,
partition_index,
};

self.transfer_withdraw(&account_id, request)
Expand Down Expand Up @@ -102,7 +102,7 @@ impl WithdrawApi for Contract {
request.total_fee += fee;
}

for request in request.requests.iter() {
for request in &request.requests {
self.get_account_mut(&account_id)
.get_jar_mut(&request.product_id)
.lock();
Expand Down
Binary file modified res/sweat_jar.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ cargo clippy -p sweat_jar \
-A clippy::needless-pass-by-value \
-A clippy::must-use-candidate \
-A clippy::missing_panics_doc \
-A clippy::explicit_deref_methods \
\
-D warnings

0 comments on commit 7a6e5c8

Please sign in to comment.