Skip to content

Commit

Permalink
some more small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Nov 3, 2023
1 parent 12ae185 commit 8938caa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion contract/src/burn/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl BurnApi for Contract {
self.is_service_call_running = true;

let mut total_to_burn = 0;
let mut keys_to_remove: Vec<UnixTimestamp> = vec![];
let mut keys_to_remove = vec![];
let now = now_seconds();

for (datetime, (_, total)) in self.accruals.iter() {
Expand Down
76 changes: 40 additions & 36 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,43 @@ use crate::{common::now_seconds, Contract, ContractExt, StorageKey::AccrualsEntr
#[near_bindgen]
impl ClaimApi for Contract {
fn get_claimable_balance_for_account(&self, account_id: AccountId) -> U128 {
if let Some(account_data) = self.accounts.get(&account_id) {
let mut total_accrual: TokensAmount = 0;
let now = now_seconds();
let Some(account_data) = self.accounts.get(&account_id) else {
return U128(0);
};

for (datetime, index) in &account_data.accruals {
if now - datetime > self.burn_period {
continue;
}
let mut total_accrual = 0;
let now = now_seconds();

if let Some((accruals, _)) = self.accruals.get(datetime) {
if let Some(amount) = accruals.get(*index) {
total_accrual += *amount;
}
}
for (datetime, index) in &account_data.accruals {
if now - datetime > self.burn_period {
continue;
}

U128(total_accrual)
} else {
U128(0)
let Some((accruals, _)) = self.accruals.get(datetime) else {
continue;
};

if let Some(amount) = accruals.get(*index) {
total_accrual += *amount;
}
}

U128(total_accrual)
}

fn is_claim_available(&self, account_id: AccountId) -> ClaimAvailabilityView {
if let Some(account_data) = self.accounts.get(&account_id) {
let Some(last_claim_at) = account_data.last_claim_at else {
return ClaimAvailabilityView::Available;
};
let Some(account_data) = self.accounts.get(&account_id) else {
return ClaimAvailabilityView::Unregistered;
};

let now_seconds = now_seconds();
let Some(last_claim_at) = account_data.last_claim_at else {
return ClaimAvailabilityView::Available;
};

if now_seconds - last_claim_at > self.claim_period {
ClaimAvailabilityView::Available
} else {
ClaimAvailabilityView::Unavailable((last_claim_at, self.claim_period))
}
if now_seconds() - last_claim_at > self.claim_period {
ClaimAvailabilityView::Available
} else {
ClaimAvailabilityView::Unregistered
ClaimAvailabilityView::Unavailable((last_claim_at, self.claim_period))
}
}

Expand All @@ -57,23 +57,27 @@ impl ClaimApi for Contract {
let account_data = self.accounts.get_mut(&account_id).expect("Account data is not found");

let now = now_seconds();
let mut total_accrual: TokensAmount = 0;
let mut details: Vec<(UnixTimestamp, TokensAmount)> = vec![];
let mut total_accrual = 0;
let mut details = vec![];

for (datetime, index) in &account_data.accruals {
if now - datetime > self.burn_period {
continue;
}

if let Some((accruals, total)) = self.accruals.get_mut(datetime) {
if let Some(amount) = accruals.get_mut(*index) {
details.push((*datetime, *amount));
let Some((accruals, total)) = self.accruals.get_mut(datetime) else {
continue;
};

let Some(amount) = accruals.get_mut(*index) else {
continue;
};

details.push((*datetime, *amount));

total_accrual += *amount;
*total -= *amount;
*amount = 0;
}
}
total_accrual += *amount;
*total -= *amount;
*amount = 0;
}

account_data.accruals.clear();
Expand Down
17 changes: 11 additions & 6 deletions contract/src/record/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use model::{account_record::AccountRecord, api::RecordApi, TokensAmount};
use near_sdk::{env::log_str, json_types::U128, near_bindgen, store::Vector, AccountId};
use model::{account_record::AccountRecord, api::RecordApi};
use near_sdk::{env::log_str, json_types::U128, near_bindgen, require, store::Vector, AccountId};

use crate::{common::now_seconds, Contract, ContractExt, StorageKey::AccrualsEntry};

Expand All @@ -11,17 +11,17 @@ impl RecordApi for Contract {
self.assert_oracle();

let now_seconds = now_seconds();
let mut balances: Vector<TokensAmount> = Vector::new(AccrualsEntry(now_seconds));
let mut total_balance: TokensAmount = 0;
let mut balances = Vector::new(AccrualsEntry(now_seconds));
let mut total_balance = 0;

for (account_id, amount) in amounts {
log_str(&format!("Record {amount:?} for {account_id}"));

let amount = amount.0;
let index = balances.len();

total_balance += amount;
balances.push(amount);
let index = balances.len() - 1;

if let Some(record) = self.accounts.get_mut(&account_id) {
record.accruals.push((now_seconds, index));
Expand All @@ -36,6 +36,11 @@ impl RecordApi for Contract {
}
}

self.accruals.insert(now_seconds, (balances, total_balance));
let existing = self.accruals.insert(now_seconds, (balances, total_balance));

require!(
existing.is_none(),
"Record for this timestamp: {now_seconds} already existed. It was owerwritten."
);
}
}

0 comments on commit 8938caa

Please sign in to comment.