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

Commit

Permalink
fix(accounts): currency converter is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Jun 29, 2023
1 parent ea3c23c commit 637c414
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions bucket/ddc_bucket/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,42 @@ use crate::ddc_bucket::{Balance, TOKEN};
pub type CERE = Balance;
pub type USD = Balance;

const PRECISION: Balance = 10_000_000;

#[derive(SpreadLayout)]
#[cfg_attr(feature = "std", derive(ink_storage::traits::StorageLayout, Debug))]
pub struct CurrencyConverter(Balance /* how many USD for PRECISION CERE */);
pub struct CurrencyConverter {
/* how many USD for PRECISION CERE */
rate: Balance
}

const PRECISION: Balance = 10_000_000;

impl SpreadAllocate for CurrencyConverter {
fn allocate_spread(_: &mut KeyPtr) -> Self {
Self(PRECISION)
Self {
rate: PRECISION
}
}
}

impl Default for CurrencyConverter {
fn default() -> Self {
Self(PRECISION)
Self {
rate: PRECISION
}
}
}

impl CurrencyConverter { // 10_000_000
pub fn set_usd_per_cere(&mut self, usd_per_cere: USD) {
self.0 = usd_per_cere * PRECISION / TOKEN;
self.rate = usd_per_cere * PRECISION / TOKEN;
}

pub fn to_cere(&self, usd: USD) -> CERE {
usd * PRECISION / self.0
usd * PRECISION / self.rate
}

pub fn to_usd(&self, cere: CERE) -> USD {
self.0 * cere / PRECISION
self.rate * cere / PRECISION
}
}

0 comments on commit 637c414

Please sign in to comment.