Skip to content

Commit 7a16a2a

Browse files
Add products generating and context preparing (#9)
1 parent 45aab10 commit 7a16a2a

File tree

8 files changed

+352
-474
lines changed

8 files changed

+352
-474
lines changed

contract/src/claim/mod.rs

+26-46
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,28 @@ pub mod api;
22

33
#[cfg(test)]
44
mod tests {
5-
use near_sdk::{
6-
json_types::{U128, U64},
7-
test_utils::accounts,
8-
PromiseOrValue,
9-
};
5+
use near_sdk::{json_types::U128, test_utils::accounts, PromiseOrValue};
106

117
use crate::{
128
claim::api::ClaimApi,
13-
common::{tests::Context, U32},
14-
jar::{api::JarApi, model::JarTicket},
15-
product::{api::ProductApi, tests::get_register_product_command},
9+
common::{tests::Context, UDecimal, U32},
10+
jar::{api::JarApi, model::Jar},
11+
product::{
12+
model::{Apy, Product},
13+
tests::YEAR_IN_MS,
14+
},
1615
};
1716

1817
#[test]
1918
fn claim_total_when_nothing_to_claim() {
2019
let alice = accounts(0);
2120
let admin = accounts(1);
2221

23-
let mut context = Context::new(admin.clone());
24-
25-
context.switch_account(&admin);
26-
context.with_deposit_yocto(1, |context| {
27-
context.contract.register_product(get_register_product_command())
28-
});
22+
let product = generate_product();
23+
let jar = Jar::generate(0, &alice, &product.id).principal(100_000_000);
24+
let mut context = Context::new(admin).with_products(&[product]).with_jars(&[jar]);
2925

30-
context.switch_account_to_owner();
31-
context.contract.create_jar(
32-
alice.clone(),
33-
JarTicket {
34-
product_id: get_register_product_command().id,
35-
valid_until: U64(0),
36-
},
37-
U128(100_000_000),
38-
None,
39-
);
40-
41-
context.switch_account(&alice.clone());
26+
context.switch_account(&alice);
4227
let result = context.contract.claim_total();
4328

4429
if let PromiseOrValue::Value(value) = result {
@@ -53,30 +38,25 @@ mod tests {
5338
let alice = accounts(0);
5439
let admin = accounts(1);
5540

56-
let mut context = Context::new(admin.clone());
57-
58-
context.switch_account(&admin);
59-
context.with_deposit_yocto(1, |context| {
60-
context.contract.register_product(get_register_product_command())
61-
});
62-
63-
context.switch_account_to_owner();
64-
context.contract.create_jar(
65-
alice.clone(),
66-
JarTicket {
67-
product_id: get_register_product_command().id,
68-
valid_until: U64(0),
69-
},
70-
U128(100_000_000_000),
71-
None,
72-
);
41+
let product = generate_product();
42+
let reference_jar = Jar::generate(0, &alice, &product.id).principal(100_000_000_000);
43+
let mut context = Context::new(admin)
44+
.with_products(&[product])
45+
.with_jars(&[reference_jar.clone()]);
7346

7447
context.set_block_timestamp_in_days(365);
7548

76-
context.switch_account(&alice.clone());
77-
context.contract.claim_jars(vec![0], Some(U128(100)));
49+
context.switch_account(&alice);
50+
context.contract.claim_jars(vec![reference_jar.index], Some(U128(100)));
7851

79-
let jar = context.contract.get_jar(U32(0));
52+
let jar = context.contract.get_jar(U32(reference_jar.index));
8053
assert_eq!(100, jar.claimed_balance.0);
8154
}
55+
56+
fn generate_product() -> Product {
57+
Product::generate("product")
58+
.enabled(true)
59+
.lockup_term(YEAR_IN_MS)
60+
.apy(Apy::Constant(UDecimal::new(12, 2)))
61+
}
8262
}

contract/src/common.rs

-4
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ pub(crate) mod tests {
189189
testing_env!(self.builder.build());
190190
}
191191

192-
pub(crate) fn switch_account_to_owner(&mut self) {
193-
self.switch_account(&self.owner.clone());
194-
}
195-
196192
pub(crate) fn switch_account_to_ft_contract_account(&mut self) {
197193
self.switch_account(&self.ft_contract_id.clone());
198194
}

contract/src/ft_receiver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ mod tests {
113113

114114
let ticket_amount = 1_000_000u128;
115115
let ticket_valid_until = 100_000_000u64;
116-
let signature = signer.sign(
116+
let signature = signer.sign_base64(
117117
Contract::get_signature_material(
118118
&context.owner,
119119
&alice,

0 commit comments

Comments
 (0)