Skip to content

Commit 7d38b44

Browse files
Merge remote-tracking branch 'refs/remotes/origin/dev' into feat/rejars
2 parents 41a352f + 7544401 commit 7d38b44

File tree

23 files changed

+505
-242
lines changed

23 files changed

+505
-242
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contract/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sweat_jar"
3-
version = "3.3.6"
3+
version = "3.3.10"
44
authors = ["Sweat Economy"]
55
edition = "2021"
66

contract/src/claim/api.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl ClaimApi for Contract {
7575
..AccountV1Companion::default()
7676
};
7777

78-
account.score.try_claim_score();
78+
account.score.try_reset_score();
7979

8080
if accumulator.get_total().0 > 0 {
8181
self.claim_interest(
@@ -155,7 +155,6 @@ impl Contract {
155155
let jar = account.get_jar_mut(&product_id);
156156
jar.unlock();
157157

158-
// TODO: check if should delete jar
159158
if jar.should_close() {
160159
account.jars.remove(&product_id);
161160
}

contract/src/common/tests.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use near_contract_standards::fungible_token::Balance;
1010
use near_sdk::{test_utils::VMContextBuilder, testing_env, AccountId, NearToken};
11-
use sweat_jar_model::{api::InitApi, ProductId, MS_IN_DAY, MS_IN_HOUR, MS_IN_MINUTE};
11+
use sweat_jar_model::{api::InitApi, ProductId, TokenAmount, MS_IN_DAY, MS_IN_HOUR, MS_IN_MINUTE};
1212

1313
use crate::{
1414
common::Timestamp,
@@ -137,3 +137,13 @@ impl AfterCatchUnwind for Context {
137137
self.contract.clear_poison();
138138
}
139139
}
140+
141+
pub trait TokenUtils {
142+
fn to_otto(&self) -> TokenAmount;
143+
}
144+
145+
impl TokenUtils for u128 {
146+
fn to_otto(&self) -> TokenAmount {
147+
self * 10u128.pow(18)
148+
}
149+
}

contract/src/event.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct SweatJarEvent {
7373
}
7474

7575
/// `JarId` and interest to claim
76-
pub type ClaimEventItem = (JarId, U128);
76+
pub type ClaimEventItem = (ProductId, U128);
7777

7878
/// (id, fee, amount)
7979
pub type WithdrawData = (ProductId, U128, U128);
@@ -188,7 +188,7 @@ mod test {
188188
fn test_contract_version() {
189189
let admin = admin();
190190
let context = Context::new(admin);
191-
assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.6");
191+
assert_eq!(context.contract().contract_version(), "sweat_jar-3.3.10");
192192
}
193193

194194
#[test]
@@ -201,7 +201,7 @@ mod test {
201201
.to_json_event_string(),
202202
r#"EVENT_JSON:{
203203
"standard": "sweat_jar",
204-
"version": "3.3.6",
204+
"version": "3.3.10",
205205
"event": "top_up",
206206
"data": {
207207
"id": 10,
@@ -229,7 +229,7 @@ mod test {
229229
.to_json_event_string(),
230230
r#"EVENT_JSON:{
231231
"standard": "sweat_jar",
232-
"version": "3.3.6",
232+
"version": "3.3.10",
233233
"event": "create_jar",
234234
"data": {
235235
"id": 555,
@@ -246,18 +246,22 @@ mod test {
246246
);
247247

248248
assert_eq!(
249-
SweatJarEvent::from(EventKind::Claim(vec![(1, 1.into()), (2, 2.into())])).to_json_event_string(),
249+
SweatJarEvent::from(EventKind::Claim(vec![
250+
("product_id".to_string(), 1.into()),
251+
("another_product_id".to_string(), 2.into())
252+
]))
253+
.to_json_event_string(),
250254
r#"EVENT_JSON:{
251255
"standard": "sweat_jar",
252-
"version": "3.3.6",
256+
"version": "3.3.10",
253257
"event": "claim",
254258
"data": [
255259
[
256-
1,
260+
"product_id",
257261
"1"
258262
],
259263
[
260-
2,
264+
"another_product_id",
261265
"2"
262266
]
263267
]
@@ -278,7 +282,7 @@ mod test {
278282
.to_json_event_string(),
279283
r#"EVENT_JSON:{
280284
"standard": "sweat_jar",
281-
"version": "3.3.6",
285+
"version": "3.3.10",
282286
"event": "record_score",
283287
"data": [
284288
{
@@ -307,7 +311,7 @@ mod test {
307311
SweatJarEvent::from(EventKind::OldScoreWarning((111, Local(5)))).to_json_event_string(),
308312
r#"EVENT_JSON:{
309313
"standard": "sweat_jar",
310-
"version": "3.3.6",
314+
"version": "3.3.10",
311315
"event": "old_score_warning",
312316
"data": [
313317
111,

contract/src/integration_test/integration_test.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use near_sdk::{env, near_bindgen, AccountId, Timestamp};
44
use sweat_jar_model::{api::IntegrationTestMethods, ProductId, TokenAmount};
55

66
use crate::{
7-
jar::{account::v1::AccountV1, model::Deposit},
7+
jar::account::v1::AccountV1,
88
Contract, ContractExt,
99
};
1010

@@ -21,14 +21,13 @@ impl IntegrationTestMethods for Contract {
2121

2222
let account = self.get_or_create_account_mut(&account_id);
2323
for i in 0..number_of_jars {
24-
account.deposit_for_test(&product_id, now + i as u64, principal);
24+
account.deposit(&product_id, principal, (now + i as u64).into());
2525
}
2626
}
2727
}
2828

2929
impl AccountV1 {
3030
fn deposit_for_test(&mut self, product_id: &ProductId, timestamp: Timestamp, principal: TokenAmount) {
31-
let deposit = Deposit::new(timestamp, principal);
32-
self.push(product_id, deposit);
31+
self.deposit(product_id, principal, timestamp);
3332
}
3433
}

contract/src/jar/account/v1.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{
2020
#[near]
2121
#[derive(Default, Debug, PartialEq, Clone)]
2222
pub struct AccountV1 {
23-
/// Is used as nonce in `get_ticket_hash` method.
2423
/// TODO: doc change for BE migration
2524
pub nonce: u32,
2625
pub jars: HashMap<ProductId, JarV2>,
@@ -76,13 +75,9 @@ impl AccountV1 {
7675
.unwrap_or_else(|| panic_str(format!("Jar for product {product_id} is not found").as_str()))
7776
}
7877

79-
pub(crate) fn deposit(&mut self, product_id: &ProductId, principal: TokenAmount) {
80-
let deposit = Deposit::new(env::block_timestamp_ms(), principal);
81-
self.push(product_id, deposit);
82-
}
78+
pub(crate) fn deposit(&mut self, product_id: &ProductId, principal: TokenAmount, time: Option<Timestamp>) {
79+
let deposit = Deposit::new(time.unwrap_or_else(env::block_timestamp_ms), principal);
8380

84-
// TODO: refactor, move to some container
85-
pub(crate) fn push(&mut self, product_id: &ProductId, deposit: Deposit) {
8681
if let Some(jar) = self.jars.get_mut(product_id) {
8782
jar.deposits.push(deposit);
8883
} else {

contract/src/jar/api.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ use sweat_jar_model::{
99

1010
use crate::{
1111
assert::assert_not_locked_legacy,
12-
jar::{
13-
account::v1::AccountV1,
14-
model::{AccountLegacyV2, Deposit},
15-
view::DetailedJarV2,
16-
},
12+
jar::{account::v1::AccountV1, model::AccountLegacyV2, view::DetailedJarV2},
1713
product::model::v1::{InterestCalculator, Product},
1814
score::AccountScore,
1915
Contract, ContractExt,
@@ -36,7 +32,7 @@ impl Contract {
3632
let account = self.get_account_mut(account_id);
3733
let jar = account.get_jar_mut(&product.id);
3834
jar.clean_up_deposits(partition_index);
39-
account.deposit(&product.id, amount);
35+
account.deposit(&product.id, amount, None);
4036

4137
Some(amount)
4238
}
@@ -85,7 +81,6 @@ impl JarApi for Contract {
8581
vec![]
8682
}
8783

88-
// TODO: check that claimed balance is subtracted from cached interest for legacy accounts
8984
fn get_total_interest(&self, account_id: AccountId) -> AggregatedInterestView {
9085
if let Some(account) = self.try_get_account(&account_id) {
9186
return self.get_total_interest_for_account(account);
@@ -130,9 +125,7 @@ impl From<&AccountLegacyV2> for AccountV1 {
130125
for jar in value.jars.iter() {
131126
assert_not_locked_legacy(jar);
132127

133-
let deposit = Deposit::new(jar.created_at, jar.principal);
134-
account.push(&jar.product_id, deposit);
135-
128+
account.deposit(&jar.product_id, jar.principal, jar.created_at.into());
136129
account.get_jar_mut(&jar.product_id).claimed_balance += jar.claimed_balance;
137130

138131
if !account.is_penalty_applied {

contract/src/jar/model/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ impl Contract {
6767
}
6868

6969
let account = self.get_or_create_account_mut(&account_id);
70-
account.deposit(product_id, amount);
70+
account.deposit(product_id, amount, None);
7171
}
7272
}

contract/src/jar/model/v2.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
pub struct JarV2 {
1515
pub deposits: Vec<Deposit>,
1616
pub cache: Option<JarCache>,
17+
// TODO: get rid of it
1718
pub claimed_balance: TokenAmount,
1819
pub is_pending_withdraw: bool,
1920
pub claim_remainder: u64,

contract/src/jar/tests/tests.rs

+1-56
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,11 @@
11
#![cfg(test)]
22

3-
use fake::Fake;
43
use near_sdk::{
54
json_types::{U128, U64},
65
test_utils::test_env::alice,
76
};
8-
use sweat_jar_model::{UDecimal, MS_IN_YEAR};
9-
10-
use crate::{
11-
common::{tests::Context, Timestamp},
12-
jar::{
13-
account::v1::AccountV1,
14-
model::{JarTicket, JarV2},
15-
},
16-
product::model::{Apy, FixedProductTerms, InterestCalculator, Product, Terms},
17-
test_utils::admin,
18-
};
19-
// TODO: move interest calculation tests to product module
20-
21-
#[test]
22-
fn get_interest_before_maturity() {
23-
let terms = Terms::Fixed(FixedProductTerms {
24-
apy: Apy::Constant(UDecimal::new(12, 2)),
25-
lockup_term: 2 * MS_IN_YEAR,
26-
});
27-
let jar = JarV2::new().with_deposit(0, 100_000_000);
28-
let account = AccountV1::default();
29-
30-
let (interest, _) = terms.get_interest(&account, &jar, MS_IN_YEAR);
31-
assert_eq!(12_000_000, interest);
32-
}
33-
34-
#[test]
35-
fn get_interest_after_maturity() {
36-
let terms = Terms::Fixed(FixedProductTerms {
37-
apy: Apy::Constant(UDecimal::new(12, 2)),
38-
lockup_term: MS_IN_YEAR,
39-
});
40-
let jar = JarV2::new().with_deposit(0, 100_000_000);
41-
let account = AccountV1::default();
42-
43-
let (interest, _) = terms.get_interest(&account, &jar, 400 * 24 * 60 * 60 * 1000);
44-
assert_eq!(12_000_000, interest);
45-
}
467

47-
#[test]
48-
fn interest_precision() {
49-
let terms = Terms::Fixed(FixedProductTerms {
50-
apy: Apy::Constant(UDecimal::new(1, 0)),
51-
lockup_term: MS_IN_YEAR,
52-
});
53-
let jar = JarV2::new().with_deposit(0, u128::from(MS_IN_YEAR));
54-
let account = AccountV1::default();
55-
56-
assert_eq!(terms.get_interest(&account, &jar, 10000000000).0, 10000000000);
57-
assert_eq!(terms.get_interest(&account, &jar, 10000000001).0, 10000000001);
58-
59-
for _ in 0..100 {
60-
let time: Timestamp = (10..MS_IN_YEAR).fake();
61-
assert_eq!(terms.get_interest(&account, &jar, time).0, time as u128);
62-
}
63-
}
8+
use crate::{common::tests::Context, jar::model::JarTicket, product::model::Product, test_utils::admin};
649

6510
#[test]
6611
#[should_panic(expected = "It's not possible to create new jars for this product")]

contract/src/migration/api.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use sweat_jar_model::{ProductId, TokenAmount};
33

44
use crate::{
55
jar::{
6-
account::{v1::AccountV1, versioned::AccountVersioned},
6+
account::{versioned::AccountVersioned, Account},
77
model::JarCache,
88
},
99
product::model::InterestCalculator,
@@ -22,7 +22,7 @@ impl Contract {
2222
require!(!self.accounts.contains_key(&account_id), "Account already exists");
2323

2424
let now = env::block_timestamp_ms();
25-
let mut account = AccountV1::from(&account);
25+
let mut account = Account::from(&account);
2626

2727
let interest: Vec<(ProductId, TokenAmount, u64)> = account
2828
.jars

contract/src/product/helpers.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use ed25519_dalek::{Signer, SigningKey};
66
use general_purpose::STANDARD;
77
use near_sdk::AccountId;
88
use rand::rngs::OsRng;
9-
use sweat_jar_model::{ProductId, TokenAmount, UDecimal, MS_IN_YEAR};
9+
use sweat_jar_model::{
10+
ProductId, TokenAmount, UDecimal, MS_IN_YEAR,
11+
};
1012

1113
use crate::{
1214
common::tests::Context,
@@ -130,7 +132,6 @@ impl Context {
130132
}
131133
}
132134

133-
// TODO: move to tests
134135
impl Apy {
135136
pub(crate) fn new_downgradable() -> Self {
136137
Apy::Downgradable(DowngradableApy {

0 commit comments

Comments
 (0)