Skip to content

Commit

Permalink
Merge pull request #145 from Supercolony-net/feature/lending-integrat…
Browse files Browse the repository at this point in the history
…ion-tests

Integration tests for example project structure
  • Loading branch information
o-tsaruk authored Aug 9, 2022
2 parents f4bdbe5 + e269d7b commit 07fcc1c
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 82 deletions.
54 changes: 29 additions & 25 deletions docs/docs/smart-contracts/example/impls.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ pub trait Lending {
#[ink(message)]
fn total_shares(&self, asset_address: AccountId) -> Result<Balance, LendingError>;

/// This function will return the address of shares
/// which is bound to `asset_address` asset token
#[ink(message)]
fn get_asset_shares(&self, asset_address: AccountId) -> Result<AccountId, LendingError>;

/// This function will return true if the asset is accepted by the contract
#[ink(message)]
fn is_accepted_lending(&self, asset_address: AccountId) -> bool;

/// This function will return true if the asset is accepted by the contract
/// This function will return true if the asset is accepted for using as collateral
#[ink(message)]
fn is_accepted_collateral(&self, asset_address: AccountId) -> bool;

Expand Down Expand Up @@ -183,8 +188,9 @@ In this example we will not be using price oracles, we will do
our own simulated oracle. Since oracles are not the point of this example,
it will be enough for us. We will store prices info in our data struct.

```rust// Importing everything publicly from traits allows you to import every stuff related to lending
// by one import
```rust
// Importing everything publicly from traits allows you to import
// every stuff related to lending by one import
use crate::traits::lending::*;
use openbrush::{
storage::{
Expand All @@ -193,10 +199,8 @@ use openbrush::{
},
traits::{
AccountId,
AccountIdExt,
Balance,
Hash,
ZERO_ADDRESS,
},
};

Expand Down Expand Up @@ -263,36 +267,28 @@ where

/// Internal function which will return the address of the shares token
/// which are minted when `asset_address` is borrowed
pub fn get_reserve_asset<T>(instance: &T, asset_address: &AccountId) -> Result<AccountId, LendingError>
where
pub fn get_reserve_asset<T>(instance: &T, asset_address: &AccountId) -> Result<AccountId, LendingError>
where
T: Storage<Data>,
{
let reserve_asset = instance
instance
.data()
.asset_shares
.get(&asset_address)
.unwrap_or(ZERO_ADDRESS.into());
if reserve_asset.is_zero() {
return Err(LendingError::AssetNotSupported)
}
Ok(reserve_asset)
.ok_or(LendingError::AssetNotSupported)
}

/// internal function which will return the address of asset
/// which is bound to `shares_address` shares token
pub fn get_asset_from_shares<T>(instance: &T, shares_address: &AccountId) -> Result<AccountId, LendingError>
pub fn get_asset_from_shares<T>(instance: &T, shares_address: &AccountId) -> Result<AccountId, LendingError>
where
T: Storage<Data>,
{
let token = instance
{
instance
.data()
.shares_asset
.get(shares_address)
.unwrap_or(ZERO_ADDRESS.into());
if token.is_zero() {
return Err(LendingError::AssetNotSupported)
}
Ok(token)
.ok_or(LendingError::AssetNotSupported)
}

```
Expand Down Expand Up @@ -476,8 +472,8 @@ The `Storage<pausable::Data>` restriction is required to use `when_paused`, `whe
from [pausable](https://github.com/Supercolony-net/openbrush-contracts/blob/main/contracts/src/security/pausable/mod.rs#L24).

```rust
// Importing everything publicly from traits allows you to import every stuff related to lending
// by one import
// Importing everything publicly from traits allows you to import
// every stuff related to lending by one import
pub use crate::{
impls::lending::{
data,
Expand Down Expand Up @@ -552,7 +548,15 @@ impl<T: Storage<data::Data> + Storage<pausable::Data>> Lending for T {
}
Ok(PSP22Ref::total_supply(&mapped_asset))
}


default fn get_asset_shares(&self, asset_address: AccountId) -> Result<AccountId, LendingError> {
self
.data::<data::Data>()
.asset_shares
.get(&asset_address)
.ok_or(LendingError::AssetNotSupported)
}

default fn is_accepted_lending(&self, asset_address: AccountId) -> bool {
!self
.data::<data::Data>()
Expand Down Expand Up @@ -724,7 +728,7 @@ impl<T: Storage<data::Data> + Storage<pausable::Data>> Lending for T {
SharesRef::mint(
&reserve_asset,
contract,
to_repay - repay_amount - loan_info.borrow_amount,
to_repay - repay_amount,
)?;
LoanRef::update_loan(
&loan_account,
Expand Down
1 change: 1 addition & 0 deletions docs/docs/smart-contracts/example/loan.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl LoanContract {
String::from("LoanContract NFT").into_bytes(),
String::from("L-NFT").into_bytes(),
);
instance._init_with_owner(instance.env().caller());
})
}

Expand Down
9 changes: 7 additions & 2 deletions docs/docs/smart-contracts/example/psp22.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ we will implement that trait to be sure that all super traits are also implement
```rust
use openbrush::contracts::traits::psp22::{
extensions::metadata::*,
extensions::mintable::*,
*,
};

#[openbrush::wrapper]
pub type StableCoinRef = dyn PSP22 + PSP22Metadata;
pub type StableCoinRef = dyn PSP22 + PSP22Metadata + PSP22Mintable;

#[openbrush::trait_definition]
pub trait StableCoin: PSP22 + PSP22Metadata {}
pub trait StableCoin: PSP22 + PSP22Metadata + PSP22Mintable {}
```

## Add dependencies
Expand Down Expand Up @@ -58,6 +59,7 @@ pub mod token {
use lending_project::traits::stable_coin::*;
use openbrush::{
contracts::psp22::extensions::metadata::*,
contracts::psp22::extensions::mintable::*,
traits::Storage,
};
```
Expand Down Expand Up @@ -92,6 +94,9 @@ impl PSP22 for StableCoinContract {}
/// Implement PSP22Metadata Trait for our coin
impl PSP22Metadata for StableCoinContract {}

/// implement PSP22Mintable Trait for our coin
impl PSP22Mintable for StableCoinContract {}

// It forces the compiler to check that you implemented all super traits
impl StableCoin for StableCoinContract {}

Expand Down
27 changes: 8 additions & 19 deletions docs/docs/smart-contracts/example/shares.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,17 @@ We will be using these extensions in our token, so we will implement them for
our storage.

```rust
/// Implement PSP22 Trait for our coin
impl PSP22 for StableCoinContract {}
// Implement PSP22 Trait for our share
impl PSP22 for SharesContract {}

/// Implement PSP22Metadata Trait for our coin
impl PSP22Metadata for StableCoinContract {}
// Implement Ownable Trait for our share
impl Ownable for SharesContract {}

// It forces the compiler to check that you implemented all super traits
impl StableCoin for StableCoinContract {}
// Implement Metadata Trait for our share
impl PSP22Metadata for SharesContract {}

impl StableCoinContract {
/// Constructor with name and symbol
#[ink(constructor)]
pub fn new(name: Option<String>, symbol: Option<String>) -> Self {
ink_lang::codegen::initialize_contract(|instance: &mut StableCoinContract| {
instance.metadata.name = name;
instance.metadata.symbol = symbol;
instance.metadata.decimals = 18;
let total_supply = 1_000_000 * 10_u128.pow(18);
assert!(instance._mint(instance.env().caller(), total_supply).is_ok());
})
}
}
// It forces the compiler to check that you implemented all super traits
impl Shares for SharesContract {}
```

## Implement the Burnable and Mintable traits
Expand Down
1 change: 1 addition & 0 deletions example_project_structure/contracts/loan/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub mod loan {
String::from("LoanContract NFT").into_bytes(),
String::from("L-NFT").into_bytes(),
);
instance._init_with_owner(instance.env().caller());
})
}

Expand Down
4 changes: 4 additions & 0 deletions example_project_structure/contracts/stable_coin/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod token {
use lending_project::traits::stable_coin::*;
use openbrush::{
contracts::psp22::extensions::metadata::*,
contracts::psp22::extensions::mintable::*,
traits::Storage,
};

Expand All @@ -28,6 +29,9 @@ pub mod token {
/// Implement PSP22Metadata Trait for our coin
impl PSP22Metadata for StableCoinContract {}

/// implement PSP22Mintable Trait for our coin
impl PSP22Mintable for StableCoinContract {}

// It forces the compiler to check that you implemented all super traits
impl StableCoin for StableCoinContract {}

Expand Down
18 changes: 4 additions & 14 deletions example_project_structure/impls/lending/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use openbrush::{
},
traits::{
AccountId,
AccountIdExt,
Balance,
Hash,
ZERO_ADDRESS,
},
};

Expand Down Expand Up @@ -82,15 +80,11 @@ pub fn get_reserve_asset<T>(instance: &T, asset_address: &AccountId) -> Result<A
where
T: Storage<Data>,
{
let reserve_asset = instance
instance
.data()
.asset_shares
.get(&asset_address)
.unwrap_or(ZERO_ADDRESS.into());
if reserve_asset.is_zero() {
return Err(LendingError::AssetNotSupported)
}
Ok(reserve_asset)
.ok_or(LendingError::AssetNotSupported)
}

/// internal function which will return the address of asset
Expand All @@ -99,13 +93,9 @@ pub fn get_asset_from_shares<T>(instance: &T, shares_address: &AccountId) -> Res
where
T: Storage<Data>,
{
let token = instance
instance
.data()
.shares_asset
.get(shares_address)
.unwrap_or(ZERO_ADDRESS.into());
if token.is_zero() {
return Err(LendingError::AssetNotSupported)
}
Ok(token)
.ok_or(LendingError::AssetNotSupported)
}
10 changes: 9 additions & 1 deletion example_project_structure/impls/lending/lending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ impl<T: Storage<data::Data> + Storage<pausable::Data>> Lending for T {
Ok(PSP22Ref::total_supply(&mapped_asset))
}

default fn get_asset_shares(&self, asset_address: AccountId) -> Result<AccountId, LendingError> {
self
.data::<data::Data>()
.asset_shares
.get(&asset_address)
.ok_or(LendingError::AssetNotSupported)
}

default fn is_accepted_lending(&self, asset_address: AccountId) -> bool {
!self
.data::<data::Data>()
Expand Down Expand Up @@ -246,7 +254,7 @@ impl<T: Storage<data::Data> + Storage<pausable::Data>> Lending for T {
SharesRef::mint(
&reserve_asset,
contract,
to_repay - repay_amount - loan_info.borrow_amount,
to_repay - repay_amount,
)?;
LoanRef::update_loan(
&loan_account,
Expand Down
7 changes: 6 additions & 1 deletion example_project_structure/traits/lending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ pub trait Lending {
#[ink(message)]
fn total_shares(&self, asset_address: AccountId) -> Result<Balance, LendingError>;

/// This function will return the address of shares
/// which is bound to `asset_address` asset token
#[ink(message)]
fn get_asset_shares(&self, asset_address: AccountId) -> Result<AccountId, LendingError>;

/// This function will return true if the asset is accepted by the contract
#[ink(message)]
fn is_accepted_lending(&self, asset_address: AccountId) -> bool;

/// This function will return true if the asset is accepted by the contract
/// This function will return true if the asset is accepted for using as collateral
#[ink(message)]
fn is_accepted_collateral(&self, asset_address: AccountId) -> bool;

Expand Down
5 changes: 3 additions & 2 deletions example_project_structure/traits/stable_coin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use openbrush::contracts::traits::psp22::{
extensions::metadata::*,
extensions::mintable::*,
*,
};

#[openbrush::wrapper]
pub type StableCoinRef = dyn PSP22 + PSP22Metadata;
pub type StableCoinRef = dyn PSP22 + PSP22Metadata + PSP22Mintable;

#[openbrush::trait_definition]
pub trait StableCoin: PSP22 + PSP22Metadata {}
pub trait StableCoin: PSP22 + PSP22Metadata + PSP22Mintable {}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"prettier": "^2.7.1",
"redspot": "^0.13.5",
"ts-node": "^10.8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion redspot.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export default {
mocha: {
timeout: 60000
}
} as RedspotUserConfig
} as RedspotUserConfig
3 changes: 2 additions & 1 deletion tests/e2e/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export const consts = {

export const Roles = {
DefaultAdminRole: 0,
Minter: 0xfd9ab216
Minter: 0xfd9ab216,
Manager: 0x73a5ca6d,
}
Loading

0 comments on commit 07fcc1c

Please sign in to comment.