Skip to content
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

Implement vault capacity model #790

Merged
merged 16 commits into from
Dec 15, 2022
Merged

Implement vault capacity model #790

merged 16 commits into from
Dec 15, 2022

Conversation

sander2
Copy link
Member

@sander2 sander2 commented Nov 25, 2022

Picked up from #783, rebased on #781

Compilation is working, integration tests are working (although some tests in test_fee_pool are commented out as they have not been updated yet. However, I am still pretty confident that the code is working correctly, since I updated the randomized test (test_fee_pool_matches_ideal_implementation) and it seems to be doing good.

All stake manipulation is done through the new file vault-registry/src/pool-manager.rs. I had to add a hook in Loans to update the exchange rate of lend tokens, since they change when the exchange rate of the underlying changes. We'll still need to call the hook in all other places where the exchange rate of lend tokens can change.

@gregdhill gregdhill force-pushed the feat/vault-capacity branch from 03ae1e4 to 8e58b5b Compare December 1, 2022 08:58
@gregdhill gregdhill changed the title Feat: vault capacity Implement vault capacity model Dec 1, 2022
@gregdhill gregdhill force-pushed the feat/vault-capacity branch from 8e58b5b to b0470fd Compare December 1, 2022 09:01
@gregdhill gregdhill force-pushed the feat/vault-capacity branch from 27cc92c to b24503d Compare December 8, 2022 11:59
nominator_id: &T::AccountId,
currency_id: CurrencyId<T>,
) -> Result<BalanceOf<T>, DispatchError> {
frame_support::storage::with_transaction::<_, DispatchError, _>(|| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this is safe to use from the RPC interface as long as we always rollback. @sander2 what do you think? I noticed you followed a similar approach in the integration tests. The alternative is to add a distribute_and_compute_reward method to the Rewards interface.

Copy link
Member Author

@sander2 sander2 Dec 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only used in rpcs, then no rollback should be required at all since rpcs shouldn't be able to make storage changes. If this is also called from with extrinsics, then it is necessary, and afaik it should be safe. The question either way is what happens if you try to (temporarily or permanently) modify chain state in rpcs. We'd need to test that it doesn't just panic, or that it's a no-op

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this using a local standalone node and it seemed to work as expected. As far as I could tell nothing was written to state and the node definitely did not panic.

@gregdhill gregdhill marked this pull request as ready for review December 12, 2022 16:17
Copy link
Member Author

@sander2 sander2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we talked about this before, but I don't quite remember how you answered this previously. Why exactly do we need the PoolId? For the capacity pool, it's (), and for the normal reward layer it's a CurrencyId. However, as far as I can see, the CurrencyId we pass is always the vault's collateral currency so it doesn't seem to be doing anything. Note that the reward pallet already supported multiple reward currencies, independently of the vault's collateral currency.

crates/vault-registry/src/pool_manager.rs Outdated Show resolved Hide resolved
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let prev_count = reward::migration::v0::Stake::<Runtime, VaultRewardsInstance>::iter().count();
log::info!(target: TARGET, "{} stake entries will be migrated", prev_count);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comparing the totals, can we compare the balances of each vault after using with_transaction to do a dry-run of withdraw_rewards?

"Previous rewards should be in staking"
);

Ok(())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have some general integrity checks, that check that the capacity, reward, staking and TotalUserVaultCollateral entries are consistent. This will be useful to check the correctness of the migration, but will also be useful to continuously check the integrity using the new try-state

crates/reward/src/lib.rs Show resolved Hide resolved
Copy link
Member Author

@sander2 sander2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

gregdhill and others added 15 commits December 14, 2022 17:33
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
@gregdhill gregdhill merged commit 0af17ef into master Dec 15, 2022
@gregdhill gregdhill deleted the feat/vault-capacity branch December 15, 2022 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants