Skip to content

Commit

Permalink
Jar coverage (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ authored Sep 5, 2023
1 parent 4d610d0 commit 894cd05
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 40 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/checkout@v3

- name: Build
run: ./build-in-docker.sh
run: make build

- name: Upload binary
uses: actions/upload-artifact@v3
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@v3

- name: Unit tests
run: cargo test --all
run: make test

integration-tests:
needs: [ build ]
Expand All @@ -47,7 +47,7 @@ jobs:
path: res/

- name: Integration tests
run: cargo run -p integration-tests
run: make integration

push:
needs: [ unit-tests, integration-tests ]
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/checkout@v3

- name: Build
run: ./build-in-docker.sh
run: make build-in-docker

- name: Upload binary
uses: actions/upload-artifact@v3
Expand All @@ -31,7 +31,7 @@ jobs:
- uses: actions/checkout@v3

- name: Unit tests
run: cargo test --all
run: make test

integration-tests:
needs: [ build ]
Expand All @@ -47,4 +47,4 @@ jobs:
path: res/

- name: Integration tests
run: cargo run -p integration-tests
run: make integration
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
build:
./scripts/build.sh

build-in-docker:
./scripts/build-in-docker.sh

dock: build-in-docker

deploy:
./scripts/deploy.sh

cov:
./scripts/coverage.sh

test:
cargo test --all

integration:
cargo run -p integration-tests

int: integration

fmt:
cargo +nightly fmt --all
2 changes: 1 addition & 1 deletion contract/src/jar/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ mod signature_tests {
get_register_restakable_product_command,
},
},
withdraw::api::WithdrawApi,
};
use crate::withdraw::api::WithdrawApi;

// Signature for structure (value -> utf8 bytes):
// contract_id: "owner" -> [111, 119, 110, 101, 114]
Expand Down
5 changes: 1 addition & 4 deletions contract/src/jar/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ pub type JarIndex = u32;
/// and the ID of the last jar created for the recipient. The concatenation of this data
/// forms a message that is then hashed using the SHA-256 algorithm. This resulting hash is used
/// to verify the authenticity of the data against an Ed25519 signature provided in the ft_transfer_call data.
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct JarTicket {
/// The unique identifier of the product for which the jar is intended to be created.
/// This product_id links the request to the specific terms and conditions of the product that will govern the behavior of the jar.
Expand All @@ -45,7 +44,6 @@ pub struct JarTicket {
/// The `Jar` struct represents a deposit jar within the smart contract.
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde", rename_all = "snake_case")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct Jar {
/// The index of the jar in the `Contracts.jars` vector. Also serves as the unique identifier for the jar.
pub index: JarIndex,
Expand Down Expand Up @@ -85,7 +83,6 @@ pub struct Jar {
/// allowing for efficient interest calculations between state changes.
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct JarCache {
pub updated_at: Timestamp,
pub interest: TokenAmount,
Expand Down
8 changes: 3 additions & 5 deletions contract/src/jar/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Debug};

use near_sdk::{
json_types::{U128, U64},
Expand All @@ -10,9 +10,8 @@ use crate::{common::U32, product::model::ProductId, *};

pub type JarIndexView = U32;

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct JarView {
pub index: JarIndexView,
pub account_id: AccountId,
Expand All @@ -37,9 +36,8 @@ impl From<Jar> for JarView {
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct AggregatedTokenAmountView {
pub detailed: HashMap<JarIndexView, U128>,
pub total: U128,
Expand Down
47 changes: 38 additions & 9 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Contract {

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use common::tests::Context;
use near_sdk::{
json_types::{U128, U64},
Expand All @@ -90,7 +92,11 @@ mod tests {
use crate::{
claim::api::ClaimApi,
common::U32,
jar::{api::JarApi, model::JarTicket},
jar::{
api::JarApi,
model::JarTicket,
view::{AggregatedTokenAmountView, JarView},
},
penalty::api::PenaltyApi,
product::{
api::*,
Expand Down Expand Up @@ -218,7 +224,9 @@ mod tests {
let context = Context::new(admin);

let interest = context.contract.get_total_interest(alice);

assert_eq!(interest.total.0, 0);
assert_eq!(interest.detailed, HashMap::new());
}

#[test]
Expand All @@ -234,7 +242,7 @@ mod tests {
});

context.switch_account_to_owner();
context.contract.create_jar(
let jar = context.contract.create_jar(
alice.clone(),
JarTicket {
product_id: get_product().id,
Expand All @@ -244,10 +252,15 @@ mod tests {
None,
);

let contract_jar = JarView::from(context.contract.jars.get(0).unwrap().clone());
assert_eq!(jar, contract_jar);

context.set_block_timestamp_in_minutes(30);

let interest = context.contract.get_total_interest(alice).total.0;
assert_eq!(interest, 684);
let interest = context.contract.get_total_interest(alice);

assert_eq!(interest.total.0, 684);
assert_eq!(interest.detailed, HashMap::from([(U32(0), U128(684))]))
}

#[test]
Expand All @@ -263,7 +276,7 @@ mod tests {
});

context.switch_account_to_owner();
context.contract.create_jar(
let jar = context.contract.create_jar(
alice.clone(),
JarTicket {
product_id: get_product().id,
Expand All @@ -273,10 +286,20 @@ mod tests {
None,
);

let contract_jar = JarView::from(context.contract.jars.get(0).unwrap().clone());
assert_eq!(jar, contract_jar);

context.set_block_timestamp_in_days(365);

let interest = context.contract.get_total_interest(alice).total.0;
assert_eq!(interest, 12_000_000);
let interest = context.contract.get_total_interest(alice);

assert_eq!(
interest,
AggregatedTokenAmountView {
detailed: [(U32(0), U128(12_000_000))].into(),
total: U128(12_000_000)
}
)
}

#[test]
Expand All @@ -292,7 +315,7 @@ mod tests {
});

context.switch_account_to_owner();
context.contract.create_jar(
let jar = context.contract.create_jar(
alice.clone(),
JarTicket {
product_id: get_product().id,
Expand All @@ -302,6 +325,9 @@ mod tests {
None,
);

let contract_jar = JarView::from(context.contract.jars.get(0).unwrap().clone());
assert_eq!(jar, contract_jar);

context.set_block_timestamp_in_days(400);

let interest = context.contract.get_total_interest(alice).total.0;
Expand All @@ -321,7 +347,7 @@ mod tests {
});

context.switch_account_to_owner();
context.contract.create_jar(
let jar = context.contract.create_jar(
alice.clone(),
JarTicket {
product_id: get_product().id,
Expand All @@ -331,6 +357,9 @@ mod tests {
None,
);

let contract_jar = JarView::from(context.contract.jars.get(0).unwrap().clone());
assert_eq!(jar, contract_jar);

context.set_block_timestamp_in_days(182);

let mut interest = context.contract.get_total_interest(alice.clone()).total.0;
Expand Down
3 changes: 1 addition & 2 deletions contract/src/migration/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use near_sdk::{

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

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(Debug, PartialEq))]
pub struct CeFiJar {
pub id: String,
pub account_id: AccountId,
Expand Down
12 changes: 4 additions & 8 deletions contract/src/product/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ use crate::{
product::model::{Apy, Cap, DowngradableApy, FixedProductTerms, Product, ProductId, Terms, WithdrawalFee},
};

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct RegisterProductCommand {
pub id: ProductId,
pub apy_default: (U128, u32),
Expand Down Expand Up @@ -56,17 +55,15 @@ impl From<RegisterProductCommand> for Product {
}
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(crate = "near_sdk::serde", tag = "type", content = "data", rename_all = "snake_case")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub enum TermsDto {
Fixed(FixedProductTermsDto),
Flexible,
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, PartialEq, Deserialize, Clone, Debug)]
#[serde(crate = "near_sdk::serde")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub struct FixedProductTermsDto {
pub lockup_term: U64,
pub allows_top_up: bool,
Expand All @@ -86,9 +83,8 @@ impl From<TermsDto> for Terms {
}
}

#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, Debug)]
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone, PartialEq, Debug)]
#[serde(crate = "near_sdk::serde", tag = "type", content = "data", rename_all = "snake_case")]
#[cfg_attr(not(target_arch = "wasm32"), derive(PartialEq))]
pub enum WithdrawalFeeDto {
/// Fixed amount of tokens which a user will pay on tokens withdraw
Fix(U128),
Expand Down
20 changes: 19 additions & 1 deletion contract/src/product/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) mod tests {
common::{tests::Context, UDecimal},
product::{
api::ProductApi,
command::{FixedProductTermsDto, RegisterProductCommand, TermsDto},
command::{FixedProductTermsDto, RegisterProductCommand, TermsDto, WithdrawalFeeDto},
model::{Apy, Cap, DowngradableApy, FixedProductTerms, Product, Terms},
},
};
Expand Down Expand Up @@ -45,6 +45,24 @@ pub(crate) mod tests {
}
}

pub(crate) fn get_fee_product_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,
}
}

pub(crate) fn get_register_product_command() -> RegisterProductCommand {
RegisterProductCommand {
id: "product".to_string(),
Expand Down
27 changes: 26 additions & 1 deletion contract/src/withdraw/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,9 @@ mod tests {
jar::{api::JarApi, model::JarTicket},
product::{
api::ProductApi,
command::WithdrawalFeeDto,
model::Product,
tests::{get_register_flexible_product_command, get_register_product_command},
tests::{get_fee_product_command, get_register_flexible_product_command, get_register_product_command},
},
withdraw::api::WithdrawApi,
};
Expand Down Expand Up @@ -403,4 +404,28 @@ mod tests {

context.contract.withdraw(U32(0), Some(U128(2_000_000)));
}

#[test]
fn product_with_fee() {
// let alice = accounts(0);
// let admin = accounts(1);
// let mut context = Context::new(admin.clone());
//
// let product: Product = get_fee_product_command(WithdrawalFeeDto::Fix(U128(10))).into();
// context.switch_account(&admin);
// context.with_deposit_yocto(1, |context| {
// context
// .contract
// .register_product(get_register_flexible_product_command())
// });
//
// let ticket = JarTicket {
// product_id: product.id,
// valid_until: U64(0),
// };
//
// context
// .contract
// .create_jar(alice.clone(), ticket, U128(1_000_000), None);
}
}
Loading

0 comments on commit 894cd05

Please sign in to comment.