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

feat: unlock jars #91

Merged
merged 9 commits into from
Jun 27, 2024
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
11 changes: 10 additions & 1 deletion contract/src/claim/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ use sweat_jar_model::{
};

use crate::{
common::Timestamp,
common::{
gas_data::{GAS_FOR_AFTER_CLAIM, GAS_FOR_FT_TRANSFER},
Timestamp,
},
event::{emit, ClaimEventItem, EventKind},
internal::assert_gas,
jar::model::Jar,
Contract, ContractExt, JarsStorage,
};
Expand Down Expand Up @@ -135,6 +139,11 @@ impl Contract {
now: Timestamp,
) -> PromiseOrValue<ClaimedAmountView> {
use crate::ft_interface::FungibleTokenInterface;

assert_gas(GAS_FOR_FT_TRANSFER.as_gas() * 2 + GAS_FOR_AFTER_CLAIM.as_gas(), || {
format!("claim_interest: number of jars: {}", jars_before_transfer.len())
});

self.ft_contract()
.ft_transfer(account_id, claimed_amount.get_total().0, "claim", &None)
.then(after_claim_call(claimed_amount, jars_before_transfer, event, now))
Expand Down
13 changes: 13 additions & 0 deletions contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,17 @@ impl JarApi for Contract {

result
}

fn unlock_jars_for_account(&mut self, account_id: AccountId) {
self.assert_manager();

let jars = self
.account_jars
.get_mut(&account_id)
.expect("Account doesn't have jars");

for jar in &mut jars.jars {
jar.is_pending_withdraw = false;
}
}
}
2 changes: 2 additions & 0 deletions contract/src/jar/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl From<Jar> for JarView {
principal: U128(value.principal),
claimed_balance: U128(value.claimed_balance),
is_penalty_applied: value.is_penalty_applied,
is_pending_withdraw: value.is_pending_withdraw,
}
}
}
Expand All @@ -27,6 +28,7 @@ impl From<&Jar> for JarView {
principal: U128(value.principal),
claimed_balance: U128(value.claimed_balance),
is_penalty_applied: value.is_penalty_applied,
is_pending_withdraw: value.is_pending_withdraw,
}
}
}
52 changes: 52 additions & 0 deletions contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,58 @@ fn get_interest_after_withdraw() {
assert_eq!(12_000_000, interest.amount.total.0);
}

#[test]
#[should_panic(expected = "Can be performed only by admin")]
fn unlock_not_by_manager() {
let alice = alice();
let admin = admin();

let reference_product = generate_product();

let mut reference_jar = Jar::generate(0, &alice, &reference_product.id).principal(100);
reference_jar.is_pending_withdraw = true;
let jars = &[reference_jar];

let mut context = Context::new(admin).with_products(&[reference_product]).with_jars(jars);

context.switch_account(&alice);
context.contract().unlock_jars_for_account(alice);
}

#[test]
fn unlock_by_manager() {
let alice = alice();
let admin = admin();

let reference_product = generate_product();

let reference_jar_id = 0;
let mut reference_jar = Jar::generate(reference_jar_id, &alice, &reference_product.id).principal(100);
reference_jar.is_pending_withdraw = true;
let jars = &[reference_jar];

let mut context = Context::new(admin.clone())
.with_products(&[reference_product])
.with_jars(jars);

assert!(
context
.contract()
.get_jar(alice.clone(), reference_jar_id.into())
.is_pending_withdraw
);

context.switch_account(&admin);
context.contract().unlock_jars_for_account(alice.clone());

assert!(
!context
.contract()
.get_jar(alice.clone(), reference_jar_id.into())
.is_pending_withdraw
);
}

#[test]
fn test_u32() {
let n = U32(12345678);
Expand Down
2 changes: 2 additions & 0 deletions model/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ pub trait JarApi {
fn restake(&mut self, jar_id: JarIdView) -> JarView;

fn restake_all(&mut self) -> Vec<JarView>;

fn unlock_jars_for_account(&mut self, account_id: AccountId);
}

#[make_integration_version]
Expand Down
1 change: 1 addition & 0 deletions model/src/jar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct JarView {
pub principal: U128,
pub claimed_balance: U128,
pub is_penalty_applied: bool,
pub is_pending_withdraw: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down
Binary file modified res/sweat_jar.wasm
Binary file not shown.