Updating the Vault
After you have created a Vault, you can update its state by doing the following operations: Increasing the debt (method pay_debt
), paying the debt, and increasing the collateral.
Finding the prev_key
and the new_prev_key
The three methods you can use to update your vault will provide two VaultKey
that the contract will use to find and position your Vault.
The first one will be used to find your Vault in the linked list, this Vault will be removed from the linked list, and then using the second provided key the contract will position the Vault in the new position.
This process is similar to the one we used to create the vault, with the only difference being that instead of just one key, we need to find two.
The Vault is literally removed from the linked list during the update process so it is important that when you're finding the
new_prev_key
you assume your Vault is not in the linked list at that point.
Updating our Vault
Let's use the same example when we created our Vault and assume the current linked list is this one:
Let's assume we increased our collateral in such a way that now our Vault will have the biggest index, and thus it's now the safest Vault. So we call the method increase_collateral
with the following values:
let prev_key: OptionalVaultKey = OptionalVaultKey::Some(VaultKey {
index: 1000_0000000u128,
account: OWNER_OF_SECOND_VAULT,
denomination: symbol_short!("USD"),
});
let our_vault_key: VaultKey = VaultKey {
index: 1100_0000000u128,
account: OUR_ACCOUNT,
denomination: symbol_short!("USD"),
};
let new_prev_key: OptionalVaultKey = OptionalVaultKey::Some(VaultKey {
index: 1200_0000000u128,
account: OWNER_OF_LAST_VAULT,
denomination: symbol_short!("USD")
});
contract_client.new_vault(
&prev_key,
&our_vault_key,
&new_prev_key,
&5000_0000000u128,
);
After this method is called, the linked list will now look like this:
Increasing collateral, paying the debt, or increasing the debt
All three methods follow the same logic from above, with the only difference that in some cases like when you're paying your debt or increasing it, you need to consider the min_col_rate
and min_debt_creation
values we mentioned before.
Every time you pay your debt (even if not fully paid), the coins are burned by the protocol... But you only get your collateral back after you paid the full debt amount, at that point the Vault is also removed from the protocol.