Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
chore(accounts): accounts store adapted to upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Jun 29, 2023
1 parent eb5d444 commit 2f2eb74
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
14 changes: 7 additions & 7 deletions bucket/ddc_bucket/account/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl DdcBucket {

// todo: remove this method as we can not support iterable data structures of arbitrary data size
pub fn message_get_accounts(&self) -> Vec<AccountId> {
self.accounts.2.iter().cloned().collect()
self.accounts.accounts_keys.iter().cloned().collect()
}

pub fn message_account_deposit(&mut self) -> Result<()> {
Expand All @@ -36,7 +36,7 @@ impl DdcBucket {
let account_id = Self::env().caller();

if let Ok(mut account) = self.accounts.get(&account_id) {
let conv = &self.accounts.1;
let conv = &self.accounts.curr_converter;
account.bond(time_ms, conv, bond_amount)?;
self.accounts.save(&account_id, &account);
Ok(())
Expand Down Expand Up @@ -68,12 +68,12 @@ impl DdcBucket {
}

pub fn message_account_get_usd_per_cere(&self) -> Balance {
self.accounts.1.to_usd(1 * TOKEN)
self.accounts.curr_converter.to_usd(1 * TOKEN)
}

pub fn message_account_set_usd_per_cere(&mut self, usd_per_cere: Balance) {
self.only_with_permission(Permission::SetExchangeRate).unwrap();
self.accounts.1.set_usd_per_cere(usd_per_cere)
self.accounts.curr_converter.set_usd_per_cere(usd_per_cere)
}

pub fn receive_cash() -> Cash {
Expand All @@ -91,7 +91,7 @@ impl DdcBucket {
fn _account_withdraw(&mut self, from: AccountId, payable: Payable) -> Result<()> {
if let Ok(mut account) = self.accounts.get(&from) {
let time_ms = Self::env().block_timestamp();
let conv = &self.accounts.1;
let conv = &self.accounts.curr_converter;
account.withdraw(time_ms, conv, payable)?;
self.accounts.save(&from, &account);
Ok(())
Expand All @@ -111,11 +111,11 @@ impl DdcBucket {
}

fn _account_get_net(&self, from: AccountId) -> Balance {
match self.accounts.0.get(&from) {
match self.accounts.accounts.get(&from) {
None => 0,
Some(account) => {
let time_ms = Self::env().block_timestamp();
let conv = &self.accounts.1;
let conv = &self.accounts.curr_converter;
account.get_withdrawable(time_ms, conv)
}
}
Expand Down
26 changes: 13 additions & 13 deletions bucket/ddc_bucket/account/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ use super::entity::Account;

#[derive(Default, SpreadLayout, SpreadAllocate)]
#[cfg_attr(feature = "std", derive(ink_storage::traits::StorageLayout, Debug))]
pub struct AccountStore(
pub Mapping<AccountId, Account>,
pub CurrencyConverter,
pub struct AccountStore {
pub accounts: Mapping<AccountId, Account>,
// todo: remove this vector as it can store an arbitrary number of elements and easily exceed 16KB limit
pub Vec<AccountId>,
);
pub accounts_keys: Vec<AccountId>,
pub curr_converter: CurrencyConverter,
}

impl AccountStore {
/// Create a record for the given account if it does not exist yet.
/// Does not return extra contract storage used, due to blockchain changes.
pub fn create_if_not_exist(&mut self, account_id: AccountId) {
if !self.0.contains(account_id) {
if !self.accounts.contains(account_id) {
let acc = Account::new();
self.0.insert(account_id, &acc);
self.2.push(account_id);
self.accounts.insert(account_id, &acc);
self.accounts_keys.push(account_id);
};
}

pub fn balance(&self, account_id: &AccountId) -> Balance {
match self.0.get(account_id) {
match self.accounts.get(account_id) {
None => 0,
Some(account) => account.deposit.peek(),
}
}

pub fn get(&self, account_id: &AccountId) -> Result<Account> {
self.0.get(account_id).ok_or(AccountDoesNotExist)
self.accounts.get(account_id).ok_or(AccountDoesNotExist)
}

pub fn save(&mut self, account_id: &AccountId, account: &Account) {
self.0.insert(account_id, account)
self.accounts.insert(account_id, account)
}

/// Increase the rate of the given flow starting from the given time.
Expand All @@ -63,7 +63,7 @@ impl AccountStore {

pub fn settle_flow(&mut self, now_ms: u64, flow: &mut Flow) -> Result<Cash> {
let flowed_usd = flow.schedule.take_value_at_time(now_ms);
let flowed_cere = self.1.to_cere(flowed_usd);
let flowed_cere = self.curr_converter.to_cere(flowed_usd);
let (payable, cash) = Cash::borrow_payable_cash(flowed_cere);

let mut account = self.get(&flow.from)?;
Expand All @@ -76,7 +76,7 @@ impl AccountStore {
pub fn flow_covered_until(&self, flow: &Flow) -> Result<u64> {
let account = self.get(&flow.from)?;
let deposit_cere = account.deposit.peek();
let deposit_usd = self.1.to_usd(deposit_cere);
let deposit_usd = self.curr_converter.to_usd(deposit_cere);
Ok(account.schedule_covered_until(deposit_usd))
}
}
4 changes: 2 additions & 2 deletions bucket/ddc_bucket/cluster/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl DdcBucket {

let aggregate_payments_accounts;
{
let conv = &self.accounts.1;
let conv = &self.accounts.curr_converter;
aggregate_payments_accounts = aggregates_accounts.iter().map(|(client_id, resources_used)| {
let account_id = *client_id;
let cere_payment: Balance = conv.to_cere(*resources_used as Balance * cluster.cdn_usd_per_gb / KB_PER_GB );
Expand All @@ -513,7 +513,7 @@ impl DdcBucket {
}
};

let conv = &self.accounts.1;
let conv = &self.accounts.curr_converter;
let committer = &mut self.committer_store;

for &(cdn_node_key, resources_used) in aggregates_nodes.iter() {
Expand Down
2 changes: 1 addition & 1 deletion bucket/ddc_bucket/tests/test_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ fn cluster_distribute_cdn_revenue_ok() {
let usd_per_kb = rate / KB_PER_GB;
println!("The current rate per kb {}", usd_per_kb);

let cere_per_kb = ctx.contract.accounts.1.to_cere(usd_per_kb);
let cere_per_kb = ctx.contract.accounts.curr_converter.to_cere(usd_per_kb);
println!("The current cere rate per kb {}", cere_per_kb);

set_caller_value(ctx.provider_id0, 10 * TOKEN);
Expand Down
2 changes: 1 addition & 1 deletion bucket/ddc_bucket/tests/test_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ fn converter_ok() {
let usd_per_kb = rate / KB_PER_GB;
println!("The current rate per kb {}", usd_per_kb);

let cere_per_kb = ctx.contract.accounts.1.to_cere(usd_per_kb);
let cere_per_kb = ctx.contract.accounts.curr_converter.to_cere(usd_per_kb);
println!("The current cere rate per kb {}", cere_per_kb);
}

0 comments on commit 2f2eb74

Please sign in to comment.