Skip to content

Commit

Permalink
remove 'claimed_balance' property from jar
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia committed Dec 2, 2024
1 parent d8e3e10 commit 133ba30
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
3 changes: 1 addition & 2 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ClaimApi for Contract {
let account = self.get_account_mut(&account_id);
for (product_id, (interest, remainder)) in interest_per_jar {
let jar = account.get_jar_mut(&product_id);
jar.claim(interest, remainder, now).lock();
jar.claim(remainder, now).lock();

event_data.items.push((product_id.clone(), interest.into()));
}
Expand Down Expand Up @@ -208,7 +208,6 @@ impl Jar {
fn to_rollback(&self) -> JarCompanion {
JarCompanion {
is_pending_withdraw: Some(false),
claimed_balance: Some(self.claimed_balance),
claim_remainder: Some(self.claim_remainder),
cache: Some(self.cache),
..JarCompanion::default()
Expand Down
4 changes: 0 additions & 4 deletions contract/src/claim/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ fn dont_delete_jar_on_all_interest_claim() {
context.contract().claim_total(None);

let jar = context.contract().get_account(&alice).get_jar(&product.id).clone();
assert_eq!(200_000, jar.claimed_balance);

let Some(ref cache) = jar.cache else { panic!() };

assert_eq!(cache.interest, 0);
Expand Down Expand Up @@ -143,8 +141,6 @@ fn claim_all_withdraw_all_and_delete_jar() {
assert_eq!(300_000, claimed.get_total().0);

let jar = context.contract().get_account(&alice).get_jar(&product.id).clone();
assert_eq!(300_000, jar.claimed_balance);

let Some(ref cache) = jar.cache else { panic!() };

assert_eq!(cache.interest, 0);
Expand Down
14 changes: 10 additions & 4 deletions contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl JarApi for Contract {
}

if let Some(account) = self.archive.get_account(&account_id) {
return self.get_total_interest_for_account(&Account::from(&account));
let account = self.map_legacy_account(&account);
return self.get_total_interest_for_account(&account);
}

AggregatedInterestView::default()
Expand Down Expand Up @@ -117,24 +118,29 @@ impl JarApi for Contract {
}
}

impl From<&AccountLegacyV2> for Account {
pub(crate) type MigratingAccount = (Account, HashMap<ProductId, TokenAmount>);

impl From<&AccountLegacyV2> for MigratingAccount {
fn from(value: &AccountLegacyV2) -> Self {
let mut account = Account {
nonce: value.last_id,
..Account::default()
};

let mut claimed_balances = HashMap::<ProductId, TokenAmount>::new();

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

account.deposit(&jar.product_id, jar.principal, jar.created_at.into());
account.get_jar_mut(&jar.product_id).claimed_balance += jar.claimed_balance;

*claimed_balances.entry(jar.product_id.clone()).or_insert(0) += jar.claimed_balance;

if !account.is_penalty_applied {
account.is_penalty_applied = jar.is_penalty_applied;
}
}

account
(account, claimed_balances)
}
}
10 changes: 1 addition & 9 deletions contract/src/jar/model/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use crate::{
pub struct Jar {
pub deposits: Vec<Deposit>,
pub cache: Option<JarCache>,
// TODO: get rid of it
pub claimed_balance: TokenAmount,
pub is_pending_withdraw: bool,
pub claim_remainder: u64,
}
Expand All @@ -26,7 +24,6 @@ pub struct Jar {
pub struct JarCompanion {
pub deposits: Option<Vec<Deposit>>,
pub cache: Option<Option<JarCache>>,
pub claimed_balance: Option<TokenAmount>,
pub is_pending_withdraw: Option<bool>,
pub claim_remainder: Option<u64>,
}
Expand Down Expand Up @@ -88,8 +85,7 @@ impl Jar {
self
}

pub(crate) fn claim(&mut self, claimed_amount: TokenAmount, remainder: u64, now: Timestamp) -> &mut Self {
self.claimed_balance += claimed_amount;
pub(crate) fn claim(&mut self, remainder: u64, now: Timestamp) -> &mut Self {
self.claim_remainder = remainder;
self.cache = Some(JarCache {
updated_at: now,
Expand Down Expand Up @@ -120,10 +116,6 @@ impl Jar {
self.claim_remainder = claim_remainder;
}

if let Some(claimed_balance) = companion.claimed_balance {
self.claimed_balance = claimed_balance;
}

if let Some(cache) = companion.cache {
self.cache = cache;
}
Expand Down
17 changes: 13 additions & 4 deletions contract/src/migration/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use sweat_jar_model::{ProductId, TokenAmount};
use crate::{
jar::{
account::{versioned::AccountVersioned, Account},
model::JarCache,
api::MigratingAccount,
model::{AccountLegacyV2, JarCache},
},
product::model::InterestCalculator,
Contract, ContractExt,
Expand All @@ -21,17 +22,25 @@ impl Contract {

require!(!self.accounts.contains_key(&account_id), "Account already exists");

let account = self.map_legacy_account(&account);
self.accounts.insert(account_id.clone(), AccountVersioned::new(account));
}
}

impl Contract {
pub(crate) fn map_legacy_account(&self, legacy_account: &AccountLegacyV2) -> Account {
let now = env::block_timestamp_ms();
let mut account = Account::from(&account);
let (mut account, claimed_balances) = MigratingAccount::from(legacy_account);

let interest: Vec<(ProductId, TokenAmount, u64)> = account
.jars
.iter()
.map(|(product_id, jar)| {
let product = self.get_product(product_id);
let (interest, remainder) = product.terms.get_interest(&account, jar, now);
let claimed_balance = claimed_balances.get(product_id).copied().unwrap_or_default();

(product_id.clone(), interest - jar.claimed_balance, remainder)
(product_id.clone(), interest - claimed_balance, remainder)
})
.collect();

Expand All @@ -44,7 +53,7 @@ impl Contract {
jar.claim_remainder = remainder;
}

self.accounts.insert(account_id.clone(), AccountVersioned::new(account));
account
}
}

Expand Down
1 change: 0 additions & 1 deletion contract/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl Jar {
Jar {
deposits: vec![],
cache: None,
claimed_balance: 0,
is_pending_withdraw: false,
claim_remainder: 0,
}
Expand Down
Binary file modified res/sweat_jar.wasm
Binary file not shown.

0 comments on commit 133ba30

Please sign in to comment.