Skip to content

Commit

Permalink
Rename vault (#104)
Browse files Browse the repository at this point in the history
* More vault to loan rename

* bump version
  • Loading branch information
xlc authored Feb 9, 2020
1 parent 26d3fd1 commit d46f977
Showing 4 changed files with 25 additions and 26 deletions.
12 changes: 6 additions & 6 deletions modules/honzon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ decl_module! {
<cdp_engine::Module<T>>::settle_cdp_has_debit(who, currency_id)?;
}

pub fn update_vault(
pub fn update_loan(
origin,
currency_id: CurrencyIdOf<T>,
collateral: AmountOf<T>,
@@ -87,7 +87,7 @@ decl_module! {
<cdp_engine::Module<T>>::update_position(&who, currency_id, collateral, T::DebitAmount::zero())?;
}

pub fn transfer_vault_from(
pub fn transfer_loan_from(
origin,
currency_id: CurrencyIdOf<T>,
from: T::AccountId,
@@ -101,7 +101,7 @@ decl_module! {
<loans::Module<T>>::transfer(from.clone(), to.clone(), currency_id)?;
}

/// `origin` allow `to` to manipulate the `currency_id` vault
/// `origin` allow `to` to manipulate the `currency_id` loan
pub fn authorize(
origin,
currency_id: CurrencyIdOf<T>,
@@ -115,7 +115,7 @@ decl_module! {
Self::deposit_event(RawEvent::Authorization(from, to, currency_id));
}

/// `origin` refuse `to` to manipulate the vault of `currency_id`
/// `origin` refuse `to` to manipulate the loan of `currency_id`
pub fn unauthorize(
origin,
currency_id: CurrencyIdOf<T>,
@@ -129,7 +129,7 @@ decl_module! {
Self::deposit_event(RawEvent::UnAuthorization(from, to, currency_id));
}

/// `origin` refuse anyone to manipulate its vault
/// `origin` refuse anyone to manipulate its loan
pub fn unauthorize_all(origin) {
let from = ensure_signed(origin)?;

@@ -142,7 +142,7 @@ decl_module! {
}

impl<T: Trait> Module<T> {
/// check if `from` allow `to` to manipulate its vault
/// check if `from` allow `to` to manipulate its loan
pub fn check_authorization(from: &T::AccountId, to: &T::AccountId, currency_id: CurrencyIdOf<T>) -> DispatchResult {
ensure!(
from == to || Self::authorization(from, (currency_id, to)),
16 changes: 8 additions & 8 deletions modules/honzon/src/tests.rs
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ fn unauthorize_all_should_work() {
}

#[test]
fn transfer_vault_from_should_work() {
fn transfer_loan_from_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(CdpEngineModule::set_collateral_params(
Origin::ROOT,
@@ -116,9 +116,9 @@ fn transfer_vault_from_should_work() {
Some(Some(Ratio::from_rational(9, 5))),
Some(10000),
));
assert_ok!(HonzonModule::update_vault(Origin::signed(ALICE), BTC, 100, 50));
assert_ok!(HonzonModule::update_loan(Origin::signed(ALICE), BTC, 100, 50));
assert_ok!(HonzonModule::authorize(Origin::signed(ALICE), BTC, BOB));
assert_ok!(HonzonModule::transfer_vault_from(Origin::signed(BOB), BTC, ALICE));
assert_ok!(HonzonModule::transfer_loan_from(Origin::signed(BOB), BTC, ALICE));
assert_eq!(LoansModule::collaterals(BOB, BTC), 100);
assert_eq!(LoansModule::debits(BOB, BTC), 50);
});
@@ -128,14 +128,14 @@ fn transfer_vault_from_should_work() {
fn transfer_unauthorization_loans_should_not_work() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
HonzonModule::transfer_vault_from(Origin::signed(ALICE), BTC, BOB),
HonzonModule::transfer_loan_from(Origin::signed(ALICE), BTC, BOB),
Error::<Runtime>::NoAuthorization,
);
});
}

#[test]
fn update_vault_should_work() {
fn update_loan_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(CdpEngineModule::set_collateral_params(
Origin::ROOT,
@@ -146,7 +146,7 @@ fn update_vault_should_work() {
Some(Some(Ratio::from_rational(9, 5))),
Some(10000),
));
assert_ok!(HonzonModule::update_vault(Origin::signed(ALICE), BTC, 100, 50));
assert_ok!(HonzonModule::update_loan(Origin::signed(ALICE), BTC, 100, 50));
assert_eq!(LoansModule::collaterals(ALICE, BTC), 100);
assert_eq!(LoansModule::debits(ALICE, BTC), 50);
});
@@ -163,11 +163,11 @@ fn emergency_shutdown_should_work() {
Error::<Runtime>::AlreadyShutdown,
);
assert_noop!(
HonzonModule::update_vault(Origin::signed(ALICE), BTC, 100, 50),
HonzonModule::update_loan(Origin::signed(ALICE), BTC, 100, 50),
Error::<Runtime>::AlreadyShutdown,
);
assert_noop!(
HonzonModule::transfer_vault_from(Origin::signed(ALICE), BTC, BOB),
HonzonModule::transfer_loan_from(Origin::signed(ALICE), BTC, BOB),
Error::<Runtime>::AlreadyShutdown,
);
});
21 changes: 10 additions & 11 deletions modules/loans/src/lib.rs
Original file line number Diff line number Diff line change
@@ -61,8 +61,8 @@ decl_event!(
UpdatePosition(AccountId, CurrencyId, Amount, DebitAmount),
/// Update collaterals and debits success (account, currency_id, collaterals, debits)
UpdateCollateralsAndDebits(AccountId, CurrencyId, Amount, DebitAmount),
/// Transfer vault (from, to)
TransferVault(AccountId, AccountId, CurrencyId),
/// Transfer loan (from, to)
TransferLoan(AccountId, AccountId, CurrencyId),
}
);

@@ -104,7 +104,7 @@ impl<T: Trait> Module<T> {
) -> DispatchResult {
// ensure mutate safe
Self::check_add_and_sub(&who, currency_id, collaterals, debits)?;
Self::update_vault(&who, currency_id, collaterals, debits)?;
Self::update_loan(&who, currency_id, collaterals, debits)?;
Self::deposit_event(RawEvent::UpdateCollateralsAndDebits(
who,
currency_id,
@@ -161,15 +161,14 @@ impl<T: Trait> Module<T> {
}

// mutate collaterals and debits
Self::update_vault(who, currency_id, collaterals, debits)
.expect("Will never fail ensured by check_add_and_sub");
Self::update_loan(who, currency_id, collaterals, debits).expect("Will never fail ensured by check_add_and_sub");

Self::deposit_event(RawEvent::UpdatePosition(who.clone(), currency_id, collaterals, debits));

Ok(())
}

// transfer vault
// transfer loan
pub fn transfer(from: T::AccountId, to: T::AccountId, currency_id: CurrencyIdOf<T>) -> DispatchResult {
// get `from` position data
let collateral: BalanceOf<T> = Self::collaterals(&from, currency_id);
@@ -192,11 +191,11 @@ impl<T: Trait> Module<T> {
.map_err(|_| Error::<T>::RiskCheckFailed)?;

// execute transfer
Self::update_vault(&from, currency_id, -collateral, -debit)
Self::update_loan(&from, currency_id, -collateral, -debit)
.expect("Will never fail ensured by check_add_and_sub");
Self::update_vault(&to, currency_id, collateral, debit).expect("Will never fail ensured by check_add_and_sub");
Self::update_loan(&to, currency_id, collateral, debit).expect("Will never fail ensured by check_add_and_sub");

Self::deposit_event(RawEvent::TransferVault(from, to, currency_id));
Self::deposit_event(RawEvent::TransferLoan(from, to, currency_id));

Ok(())
}
@@ -221,7 +220,7 @@ impl<T: Trait> Module<T> {
Ok(())
}

/// ensure sum and sub will success when updating vault collaterals and debits
/// ensure sum and sub will success when updating loan collaterals and debits
fn check_add_and_sub(
who: &T::AccountId,
currency_id: CurrencyIdOf<T>,
@@ -267,7 +266,7 @@ impl<T: Trait> Module<T> {
Ok(())
}

fn update_vault(
fn update_loan(
who: &T::AccountId,
currency_id: CurrencyIdOf<T>,
collaterals: AmountOf<T>,
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("acala"),
impl_name: create_runtime_str!("acala"),
authoring_version: 1,
spec_version: 25,
spec_version: 26,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

0 comments on commit d46f977

Please sign in to comment.