Skip to content

Commit 45aab10

Browse files
Improved tests coverage (#7)
* simplified product commad creation a bit * prepare_jar method for withdrawal tests * withdrawal fee test * percent fee and failed withdraw tests * binary * binary * Rename year const --------- Co-authored-by: Vasily Styagov <[email protected]>
1 parent 5ce74bd commit 45aab10

File tree

11 files changed

+175
-231
lines changed

11 files changed

+175
-231
lines changed

contract/src/claim/api.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl ClaimCallbacks for Contract {
105105
#[private]
106106
fn after_claim(&mut self, claimed_amount: U128, jars_before_transfer: Vec<Jar>, event: EventKind) -> U128 {
107107
if is_promise_success() {
108-
for jar_before_transfer in jars_before_transfer.iter() {
108+
for jar_before_transfer in jars_before_transfer {
109109
let jar = self.get_jar_internal(jar_before_transfer.index);
110110

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

116116
claimed_amount
117117
} else {
118-
for jar_before_transfer in jars_before_transfer.iter() {
118+
for jar_before_transfer in jars_before_transfer {
119119
self.jars
120120
.replace(jar_before_transfer.index, jar_before_transfer.unlocked());
121121
}

contract/src/common.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Mul;
2+
13
use near_sdk::{
24
borsh::{self, BorshDeserialize, BorshSerialize},
35
serde::{self, Deserialize, Deserializer, Serialize, Serializer},
@@ -33,12 +35,24 @@ pub struct UDecimal {
3335
}
3436

3537
impl UDecimal {
36-
pub(crate) fn mul(&self, value: u128) -> u128 {
38+
pub(crate) fn to_f32(&self) -> f32 {
39+
self.significand as f32 / 10u128.pow(self.exponent) as f32
40+
}
41+
}
42+
43+
impl Mul<u128> for UDecimal {
44+
type Output = u128;
45+
46+
fn mul(self, value: u128) -> Self::Output {
3747
value * self.significand / 10u128.pow(self.exponent)
3848
}
49+
}
3950

40-
pub(crate) fn to_f32(&self) -> f32 {
41-
self.significand as f32 / 10u128.pow(self.exponent) as f32
51+
impl Mul<u128> for &UDecimal {
52+
type Output = u128;
53+
54+
fn mul(self, value: u128) -> Self::Output {
55+
value * self.significand / 10u128.pow(self.exponent)
4256
}
4357
}
4458

contract/src/jar/api.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ impl JarApi for Contract {
207207
mod tests {
208208
use near_sdk::AccountId;
209209

210-
use crate::{jar::model::Jar, product::tests::get_product};
210+
use crate::{
211+
jar::model::Jar,
212+
product::tests::{get_product, YEAR_IN_MS},
213+
};
211214

212215
#[test]
213216
fn get_interest_before_maturity() {
@@ -220,7 +223,7 @@ mod tests {
220223
0,
221224
);
222225

223-
let interest = jar.get_interest(&product, 365 * 24 * 60 * 60 * 1000);
226+
let interest = jar.get_interest(&product, YEAR_IN_MS);
224227
assert_eq!(12_000_000, interest);
225228
}
226229

contract/src/jar/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ pub mod view;
55
#[cfg(test)]
66
mod helpers {
77
use near_sdk::AccountId;
8-
use crate::common::{Timestamp, TokenAmount};
9-
use crate::jar::model::{Jar, JarState};
10-
use crate::product::model::ProductId;
8+
9+
use crate::{
10+
common::{Timestamp, TokenAmount},
11+
jar::model::{Jar, JarState},
12+
product::model::ProductId,
13+
};
1114

1215
impl Jar {
1316
pub(crate) fn generate(index: u32, account_id: &AccountId, product_id: &ProductId) -> Jar {
@@ -35,4 +38,4 @@ mod helpers {
3538
self
3639
}
3740
}
38-
}
41+
}

contract/src/jar/model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl Jar {
206206

207207
let term_in_minutes = (effective_term / MS_IN_MINUTE) as u128;
208208
let apy = self.get_apy(product);
209-
let total_interest = apy.mul(self.principal);
209+
let total_interest = apy * self.principal;
210210

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

contract/src/migration/model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use near_sdk::{
77

88
use crate::product::model::ProductId;
99

10-
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug, PartialEq, Clone)]
10+
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
1111
#[serde(crate = "near_sdk::serde")]
1212
pub struct CeFiJar {
1313
pub id: String,

contract/src/product/command.rs

+35
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ pub struct RegisterProductCommand {
2323
pub is_enabled: bool,
2424
}
2525

26+
#[cfg(test)]
27+
impl Default for RegisterProductCommand {
28+
fn default() -> Self {
29+
Self {
30+
id: "default_product".to_string(),
31+
apy_default: (U128(12), 2),
32+
apy_fallback: None,
33+
cap_min: U128(100),
34+
cap_max: U128(100_000_000_000),
35+
terms: TermsDto::default(),
36+
withdrawal_fee: None,
37+
public_key: None,
38+
is_enabled: true,
39+
}
40+
}
41+
}
42+
2643
impl From<RegisterProductCommand> for Product {
2744
fn from(value: RegisterProductCommand) -> Self {
2845
let apy = if let Some(apy_fallback) = value.apy_fallback {
@@ -62,6 +79,13 @@ pub enum TermsDto {
6279
Flexible,
6380
}
6481

82+
#[cfg(test)]
83+
impl Default for TermsDto {
84+
fn default() -> Self {
85+
Self::Fixed(Default::default())
86+
}
87+
}
88+
6589
#[derive(BorshDeserialize, BorshSerialize, Serialize, PartialEq, Deserialize, Clone, Debug)]
6690
#[serde(crate = "near_sdk::serde")]
6791
pub struct FixedProductTermsDto {
@@ -70,6 +94,17 @@ pub struct FixedProductTermsDto {
7094
pub allows_restaking: bool,
7195
}
7296

97+
#[cfg(test)]
98+
impl Default for FixedProductTermsDto {
99+
fn default() -> Self {
100+
Self {
101+
lockup_term: U64(crate::product::tests::YEAR_IN_MS),
102+
allows_restaking: false,
103+
allows_top_up: false,
104+
}
105+
}
106+
}
107+
73108
impl From<TermsDto> for Terms {
74109
fn from(value: TermsDto) -> Self {
75110
match value {

contract/src/product/mod.rs

+21-50
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ pub mod view;
66
#[cfg(test)]
77
pub(crate) mod tests {
88
use near_sdk::{
9-
json_types::{Base64VecU8, U128, U64},
9+
json_types::{Base64VecU8, U128},
1010
test_utils::accounts,
1111
};
1212

1313
use crate::{
14-
common::{tests::Context, UDecimal},
14+
common::{tests::Context, Duration, UDecimal},
1515
product::{
1616
api::ProductApi,
1717
command::{FixedProductTermsDto, RegisterProductCommand, TermsDto, WithdrawalFeeDto},
@@ -26,6 +26,8 @@ pub(crate) mod tests {
2626
]
2727
}
2828

29+
pub(crate) const YEAR_IN_MS: Duration = 365 * 24 * 60 * 60 * 1000;
30+
2931
pub(crate) fn get_product() -> Product {
3032
Product {
3133
id: "product".to_string(),
@@ -35,7 +37,7 @@ pub(crate) mod tests {
3537
max: 100_000_000_000,
3638
},
3739
terms: Terms::Fixed(FixedProductTerms {
38-
lockup_term: 365 * 24 * 60 * 60 * 1000,
40+
lockup_term: YEAR_IN_MS,
3941
allows_top_up: false,
4042
allows_restaking: false,
4143
}),
@@ -45,71 +47,48 @@ pub(crate) mod tests {
4547
}
4648
}
4749

48-
pub(crate) fn get_fee_product_command(fee: WithdrawalFeeDto) -> RegisterProductCommand {
50+
pub(crate) fn get_product_with_fee_command(fee: WithdrawalFeeDto) -> RegisterProductCommand {
4951
RegisterProductCommand {
5052
id: "product_with_fee".to_string(),
51-
apy_default: (U128(12), 2),
52-
apy_fallback: None,
53-
cap_min: U128(100),
54-
cap_max: U128(100_000_000_000),
55-
terms: TermsDto::Fixed(FixedProductTermsDto {
56-
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
57-
allows_restaking: false,
58-
allows_top_up: false,
59-
}),
6053
withdrawal_fee: Some(fee),
61-
public_key: None,
62-
is_enabled: true,
54+
..Default::default()
6355
}
6456
}
6557

6658
pub(crate) fn get_register_product_command() -> RegisterProductCommand {
6759
RegisterProductCommand {
6860
id: "product".to_string(),
69-
apy_default: (U128(12), 2),
70-
apy_fallback: None,
71-
cap_min: U128(100),
72-
cap_max: U128(100_000_000_000),
61+
..Default::default()
62+
}
63+
}
64+
65+
pub(crate) fn get_register_refillable_product_command() -> RegisterProductCommand {
66+
RegisterProductCommand {
67+
id: "product_refillable".to_string(),
7368
terms: TermsDto::Fixed(FixedProductTermsDto {
74-
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
75-
allows_restaking: false,
76-
allows_top_up: false,
69+
allows_top_up: true,
70+
..Default::default()
7771
}),
78-
withdrawal_fee: None,
79-
public_key: None,
80-
is_enabled: true,
72+
..Default::default()
8173
}
8274
}
8375

8476
pub(crate) fn get_register_flexible_product_command() -> RegisterProductCommand {
8577
RegisterProductCommand {
8678
id: "product_flexible".to_string(),
87-
apy_default: (U128(12), 2),
88-
apy_fallback: None,
89-
cap_min: U128(100),
90-
cap_max: U128(100_000_000_000),
9179
terms: TermsDto::Flexible,
92-
withdrawal_fee: None,
93-
public_key: None,
94-
is_enabled: true,
80+
..Default::default()
9581
}
9682
}
9783

9884
pub(crate) fn get_register_restakable_product_command() -> RegisterProductCommand {
9985
RegisterProductCommand {
10086
id: "product_restakable".to_string(),
101-
apy_default: (U128(12), 2),
102-
apy_fallback: None,
103-
cap_min: U128(100),
104-
cap_max: U128(100_000_000_000),
10587
terms: TermsDto::Fixed(FixedProductTermsDto {
106-
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
10788
allows_restaking: true,
108-
allows_top_up: false,
89+
..Default::default()
10990
}),
110-
withdrawal_fee: None,
111-
public_key: None,
112-
is_enabled: true,
91+
..Default::default()
11392
}
11493
}
11594

@@ -118,16 +97,8 @@ pub(crate) mod tests {
11897
id: "product_premium".to_string(),
11998
apy_default: (U128(20), 2),
12099
apy_fallback: Some((U128(10), 2)),
121-
cap_min: U128(100),
122-
cap_max: U128(100_000_000_000),
123-
terms: TermsDto::Fixed(FixedProductTermsDto {
124-
lockup_term: U64(365 * 24 * 60 * 60 * 1000),
125-
allows_top_up: false,
126-
allows_restaking: false,
127-
}),
128-
withdrawal_fee: None,
129100
public_key: public_key.or_else(|| Some(Base64VecU8(get_premium_product_public_key()))),
130-
is_enabled: true,
101+
..Default::default()
131102
}
132103
}
133104

0 commit comments

Comments
 (0)