Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved tests coverage #7

Merged
merged 9 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl ClaimCallbacks for Contract {
#[private]
fn after_claim(&mut self, claimed_amount: U128, jars_before_transfer: Vec<Jar>, event: EventKind) -> U128 {
if is_promise_success() {
for jar_before_transfer in jars_before_transfer.iter() {
for jar_before_transfer in jars_before_transfer {
let jar = self.get_jar_internal(jar_before_transfer.index);

self.jars.replace(jar_before_transfer.index, jar.unlocked());
Expand All @@ -115,7 +115,7 @@ impl ClaimCallbacks for Contract {

claimed_amount
} else {
for jar_before_transfer in jars_before_transfer.iter() {
for jar_before_transfer in jars_before_transfer {
self.jars
.replace(jar_before_transfer.index, jar_before_transfer.unlocked());
}
Expand Down
20 changes: 17 additions & 3 deletions contract/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Mul;

use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
serde::{self, Deserialize, Deserializer, Serialize, Serializer},
Expand Down Expand Up @@ -33,12 +35,24 @@ pub struct UDecimal {
}

impl UDecimal {
pub(crate) fn mul(&self, value: u128) -> u128 {
pub(crate) fn to_f32(&self) -> f32 {
self.significand as f32 / 10u128.pow(self.exponent) as f32
}
}

impl Mul<u128> for UDecimal {
type Output = u128;

fn mul(self, value: u128) -> Self::Output {
value * self.significand / 10u128.pow(self.exponent)
}
}

pub(crate) fn to_f32(&self) -> f32 {
self.significand as f32 / 10u128.pow(self.exponent) as f32
impl Mul<u128> for &UDecimal {
type Output = u128;

fn mul(self, value: u128) -> Self::Output {
value * self.significand / 10u128.pow(self.exponent)
}
}

Expand Down
7 changes: 5 additions & 2 deletions contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ impl JarApi for Contract {
mod tests {
use near_sdk::AccountId;

use crate::{jar::model::Jar, product::tests::get_product};
use crate::{
jar::model::Jar,
product::tests::{get_product, YEAR_IN_MS},
};

#[test]
fn get_interest_before_maturity() {
Expand All @@ -220,7 +223,7 @@ mod tests {
0,
);

let interest = jar.get_interest(&product, 365 * 24 * 60 * 60 * 1000);
let interest = jar.get_interest(&product, YEAR_IN_MS);
assert_eq!(12_000_000, interest);
}

Expand Down
11 changes: 7 additions & 4 deletions contract/src/jar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ pub mod view;
#[cfg(test)]
mod helpers {
use near_sdk::AccountId;
use crate::common::{Timestamp, TokenAmount};
use crate::jar::model::{Jar, JarState};
use crate::product::model::ProductId;

use crate::{
common::{Timestamp, TokenAmount},
jar::model::{Jar, JarState},
product::model::ProductId,
};

impl Jar {
pub(crate) fn generate(index: u32, account_id: &AccountId, product_id: &ProductId) -> Jar {
Expand Down Expand Up @@ -35,4 +38,4 @@ mod helpers {
self
}
}
}
}
2 changes: 1 addition & 1 deletion contract/src/jar/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Jar {

let term_in_minutes = (effective_term / MS_IN_MINUTE) as u128;
let apy = self.get_apy(product);
let total_interest = apy.mul(self.principal);
let total_interest = apy * self.principal;
vasyafromrussia marked this conversation as resolved.
Show resolved Hide resolved

let interest = (term_in_minutes * total_interest) / MINUTES_IN_YEAR as u128;

Expand Down
2 changes: 1 addition & 1 deletion contract/src/migration/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use near_sdk::{

use crate::product::model::ProductId;

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug, PartialEq, Clone)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct CeFiJar {
pub id: String,
Expand Down
35 changes: 35 additions & 0 deletions contract/src/product/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ pub struct RegisterProductCommand {
pub is_enabled: bool,
}

#[cfg(test)]
impl Default for RegisterProductCommand {
fn default() -> Self {
Self {
id: "default_product".to_string(),
apy_default: (U128(12), 2),
apy_fallback: None,
cap_min: U128(100),
cap_max: U128(100_000_000_000),
terms: TermsDto::default(),
withdrawal_fee: None,
public_key: None,
is_enabled: true,
}
}
}

impl From<RegisterProductCommand> for Product {
fn from(value: RegisterProductCommand) -> Self {
let apy = if let Some(apy_fallback) = value.apy_fallback {
Expand Down Expand Up @@ -62,6 +79,13 @@ pub enum TermsDto {
Flexible,
}

#[cfg(test)]
impl Default for TermsDto {
fn default() -> Self {
Self::Fixed(Default::default())
}
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, PartialEq, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct FixedProductTermsDto {
Expand All @@ -70,6 +94,17 @@ pub struct FixedProductTermsDto {
pub allows_restaking: bool,
}

#[cfg(test)]
impl Default for FixedProductTermsDto {
fn default() -> Self {
Self {
lockup_term: U64(crate::product::tests::YEAR_IN_MS),
allows_restaking: false,
allows_top_up: false,
}
}
}

impl From<TermsDto> for Terms {
fn from(value: TermsDto) -> Self {
match value {
Expand Down
71 changes: 21 additions & 50 deletions contract/src/product/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ pub mod view;
#[cfg(test)]
pub(crate) mod tests {
use near_sdk::{
json_types::{Base64VecU8, U128, U64},
json_types::{Base64VecU8, U128},
test_utils::accounts,
};

use crate::{
common::{tests::Context, UDecimal},
common::{tests::Context, Duration, UDecimal},
product::{
api::ProductApi,
command::{FixedProductTermsDto, RegisterProductCommand, TermsDto, WithdrawalFeeDto},
Expand All @@ -26,6 +26,8 @@ pub(crate) mod tests {
]
}

pub(crate) const YEAR_IN_MS: Duration = 365 * 24 * 60 * 60 * 1000;

pub(crate) fn get_product() -> Product {
Product {
id: "product".to_string(),
Expand All @@ -35,7 +37,7 @@ pub(crate) mod tests {
max: 100_000_000_000,
},
terms: Terms::Fixed(FixedProductTerms {
lockup_term: 365 * 24 * 60 * 60 * 1000,
lockup_term: YEAR_IN_MS,
allows_top_up: false,
allows_restaking: false,
}),
Expand All @@ -45,71 +47,48 @@ pub(crate) mod tests {
}
}

pub(crate) fn get_fee_product_command(fee: WithdrawalFeeDto) -> RegisterProductCommand {
pub(crate) fn get_product_with_fee_command(fee: WithdrawalFeeDto) -> RegisterProductCommand {
RegisterProductCommand {
id: "product_with_fee".to_string(),
apy_default: (U128(12), 2),
apy_fallback: None,
cap_min: U128(100),
cap_max: U128(100_000_000_000),
terms: TermsDto::Fixed(FixedProductTermsDto {
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
allows_restaking: false,
allows_top_up: false,
}),
withdrawal_fee: Some(fee),
public_key: None,
is_enabled: true,
..Default::default()
}
}

pub(crate) fn get_register_product_command() -> RegisterProductCommand {
RegisterProductCommand {
id: "product".to_string(),
apy_default: (U128(12), 2),
apy_fallback: None,
cap_min: U128(100),
cap_max: U128(100_000_000_000),
..Default::default()
}
}

pub(crate) fn get_register_refillable_product_command() -> RegisterProductCommand {
RegisterProductCommand {
id: "product_refillable".to_string(),
terms: TermsDto::Fixed(FixedProductTermsDto {
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
allows_restaking: false,
allows_top_up: false,
allows_top_up: true,
..Default::default()
}),
withdrawal_fee: None,
public_key: None,
is_enabled: true,
..Default::default()
}
}

pub(crate) fn get_register_flexible_product_command() -> RegisterProductCommand {
RegisterProductCommand {
id: "product_flexible".to_string(),
apy_default: (U128(12), 2),
apy_fallback: None,
cap_min: U128(100),
cap_max: U128(100_000_000_000),
terms: TermsDto::Flexible,
withdrawal_fee: None,
public_key: None,
is_enabled: true,
..Default::default()
}
}

pub(crate) fn get_register_restakable_product_command() -> RegisterProductCommand {
RegisterProductCommand {
id: "product_restakable".to_string(),
apy_default: (U128(12), 2),
apy_fallback: None,
cap_min: U128(100),
cap_max: U128(100_000_000_000),
terms: TermsDto::Fixed(FixedProductTermsDto {
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
allows_restaking: true,
allows_top_up: false,
..Default::default()
}),
withdrawal_fee: None,
public_key: None,
is_enabled: true,
..Default::default()
}
}

Expand All @@ -118,16 +97,8 @@ pub(crate) mod tests {
id: "product_premium".to_string(),
apy_default: (U128(20), 2),
apy_fallback: Some((U128(10), 2)),
cap_min: U128(100),
cap_max: U128(100_000_000_000),
terms: TermsDto::Fixed(FixedProductTermsDto {
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
allows_top_up: false,
allows_restaking: false,
}),
withdrawal_fee: None,
public_key: public_key.or_else(|| Some(Base64VecU8(get_premium_product_public_key()))),
is_enabled: true,
..Default::default()
}
}

Expand Down
Loading