-
Notifications
You must be signed in to change notification settings - Fork 85
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
Integration tests: fudge support for the generic environment #1588
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
b6eb261
Add handle and base env structure
lemunozm ad491e2
evolving and state support for fudge env
lemunozm 6c5f848
Support Client APIs
lemunozm fa48bea
simplify test cases
lemunozm 5f17c11
Add spetial fudge methos
lemunozm c45a04f
Add T to FudgeHandle
lemunozm 12beb07
sumbmit extrinsic works
lemunozm 170b4a2
use correct nonce
lemunozm b6c8075
minor Env trait simplification
lemunozm 3032d09
minor example reorder
lemunozm 084d148
fudge support for all runtimes
lemunozm c94c1ad
fix nonce and divide api between submit now and later
lemunozm 2fb97d5
minor organization
lemunozm bf6269a
init tests for loans
lemunozm 21577d0
fix ongoing loans test
lemunozm f998636
fix compilation
lemunozm 848339d
Merge remote-tracking branch 'origin/main' into integration-tests/fud…
lemunozm 4b159c5
minor renames
lemunozm 5d056ab
restore expect
lemunozm ef4d158
loan testing in progress
lemunozm 774c241
Merge remote-tracking branch 'origin/main' into integration-tests/fud…
lemunozm ab7138b
fixed issue with asset_registry
lemunozm 5e79525
add CurrencyInfo genesis utility
lemunozm 93a0dda
extend loan tests, fix block by seconds
lemunozm 6b29b2b
Api from Runtime into an associated type for expliciteness
lemunozm 83bd44c
minor changes
lemunozm ed92aab
fix and extend loan testing
lemunozm a496e35
fix and extend loan testing
lemunozm 744f59f
remove unused pallet sudo addition
lemunozm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
use cfg_primitives::{Balance, CollectionId, ItemId, PoolId, SECONDS_PER_YEAR}; | ||
use cfg_traits::{ | ||
interest::{CompoundingSchedule, InterestRate}, | ||
Seconds, TimeAsSecs, | ||
}; | ||
use cfg_types::permissions::PoolRole; | ||
use frame_support::traits::Get; | ||
use pallet_loans::{ | ||
entities::{ | ||
input::PrincipalInput, | ||
loans::LoanInfo, | ||
pricing::{ | ||
internal::{InternalPricing, MaxBorrowAmount as IntMaxBorrowAmount}, | ||
Pricing, | ||
}, | ||
}, | ||
types::{ | ||
valuation::ValuationMethod, BorrowRestrictions, InterestPayments, LoanRestrictions, | ||
Maturity, PayDownSchedule, RepayRestrictions, RepaymentSchedule, | ||
}, | ||
}; | ||
use runtime_common::apis::runtime_decl_for_PoolsApi::PoolsApiV1; | ||
|
||
use crate::{ | ||
generic::{ | ||
environment::{Blocks, Env}, | ||
envs::runtime_env::RuntimeEnv, | ||
runtime::Runtime, | ||
utils::{ | ||
self, | ||
genesis::{ | ||
self, | ||
currency::{cfg, usd6, CurrencyInfo, Usd6}, | ||
Genesis, | ||
}, | ||
POOL_MIN_EPOCH_TIME, | ||
}, | ||
}, | ||
utils::{accounts::Keyring, tokens::rate_from_percent}, | ||
}; | ||
|
||
const POOL_ADMIN: Keyring = Keyring::Admin; | ||
const INVESTOR: Keyring = Keyring::Alice; | ||
const BORROWER: Keyring = Keyring::Bob; | ||
|
||
const POOL_A: PoolId = 23; | ||
const NFT_A: (CollectionId, ItemId) = (1, ItemId(10)); | ||
|
||
const FOR_FEES: Balance = cfg(1); | ||
const EXPECTED_POOL_BALANCE: Balance = usd6(1_000_000); | ||
const COLLATERAL_VALUE: Balance = usd6(100_000); | ||
|
||
fn initialize_state_for_loans<Environment: Env<T>, T: Runtime>() -> Environment { | ||
let mut env = Environment::from_storage( | ||
Genesis::<T>::default() | ||
.add(genesis::balances(T::ExistentialDeposit::get() + FOR_FEES)) | ||
.add(genesis::assets(vec![Usd6::ID])) | ||
.add(genesis::tokens(vec![(Usd6::ID, Usd6::ED)])) | ||
.storage(), | ||
); | ||
|
||
env.state_mut(|| { | ||
// Creating a pool | ||
utils::give_balance::<T>(POOL_ADMIN.id(), T::PoolDeposit::get()); | ||
utils::create_empty_pool::<T>(POOL_ADMIN.id(), POOL_A, Usd6::ID); | ||
|
||
// Funding a pool | ||
let tranche_id = T::Api::tranche_id(POOL_A, 0).unwrap(); | ||
let tranche_investor = PoolRole::TrancheInvestor(tranche_id, Seconds::MAX); | ||
utils::give_pool_role::<T>(INVESTOR.id(), POOL_A, tranche_investor); | ||
utils::give_tokens::<T>(INVESTOR.id(), Usd6::ID, EXPECTED_POOL_BALANCE); | ||
utils::invest::<T>(INVESTOR.id(), POOL_A, tranche_id, EXPECTED_POOL_BALANCE); | ||
}); | ||
|
||
env.pass(Blocks::BySeconds(POOL_MIN_EPOCH_TIME)); | ||
|
||
env.state_mut(|| { | ||
// New epoch with the investor funds available | ||
utils::close_pool_epoch::<T>(POOL_ADMIN.id(), POOL_A); | ||
|
||
// Preparing borrower | ||
utils::give_pool_role::<T>(BORROWER.id(), POOL_A, PoolRole::Borrower); | ||
utils::give_nft::<T>(BORROWER.id(), NFT_A); | ||
}); | ||
|
||
env | ||
} | ||
|
||
fn internal_priced_loan<T: Runtime>(now: Seconds) -> LoanInfo<T> { | ||
LoanInfo { | ||
schedule: RepaymentSchedule { | ||
maturity: Maturity::Fixed { | ||
date: now + SECONDS_PER_YEAR, | ||
extension: SECONDS_PER_YEAR / 2, | ||
}, | ||
interest_payments: InterestPayments::None, | ||
pay_down_schedule: PayDownSchedule::None, | ||
}, | ||
interest_rate: InterestRate::Fixed { | ||
rate_per_year: rate_from_percent(20), | ||
compounding: CompoundingSchedule::Secondly, | ||
}, | ||
collateral: NFT_A, | ||
pricing: Pricing::Internal(InternalPricing { | ||
collateral_value: COLLATERAL_VALUE, | ||
max_borrow_amount: IntMaxBorrowAmount::UpToTotalBorrowed { | ||
advance_rate: rate_from_percent(100), | ||
}, | ||
valuation_method: ValuationMethod::OutstandingDebt, | ||
}), | ||
restrictions: LoanRestrictions { | ||
borrows: BorrowRestrictions::NotWrittenOff, | ||
repayments: RepayRestrictions::None, | ||
}, | ||
} | ||
} | ||
|
||
fn borrow<T: Runtime>() { | ||
let mut env = initialize_state_for_loans::<RuntimeEnv<T>, T>(); | ||
|
||
let info = env.state(|| { | ||
let now = <pallet_timestamp::Pallet<T> as TimeAsSecs>::now(); | ||
internal_priced_loan::<T>(now) | ||
}); | ||
|
||
env.submit_now( | ||
BORROWER, | ||
pallet_loans::Call::create { | ||
pool_id: POOL_A, | ||
info, | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
let loan_id = env | ||
.find_event(|e| match e { | ||
pallet_loans::Event::<T>::Created { loan_id, .. } => Some(loan_id), | ||
_ => None, | ||
}) | ||
.unwrap(); | ||
|
||
env.submit_now( | ||
BORROWER, | ||
pallet_loans::Call::borrow { | ||
pool_id: POOL_A, | ||
loan_id, | ||
amount: PrincipalInput::Internal(COLLATERAL_VALUE / 2), | ||
}, | ||
) | ||
.unwrap(); | ||
} | ||
|
||
crate::test_for_runtimes!(all, borrow); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above initializes a currency in
orml_asset_registry
calledUsd6
(6 decimals) with a metadata whose ED correctly matches theorml_tokens
. No more balance thanED
is given to all users (ALICE, BOB...) for that currency.