-
Notifications
You must be signed in to change notification settings - Fork 207
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
feat(run-protocol): interest charging O(1) for all vaults in a manager #4527
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
bdc0c19
types
turadg 5cae71a
runtime changes for type clarity (safe?)
turadg 5101d54
types
turadg 9d1b818
make accrued debt a function of prior debt and current compounded int…
turadg 68c894f
stub for orderedVaultStore
turadg 3757dc8
progress to hurdle: collectionManager cannot serialize Remotables wit…
turadg 07cd9f1
green test-OrderedVaultStore
turadg bf301e7
WIP
turadg fe1241d
style: remove extraneous key quotes
turadg 30f4089
comment
turadg ab1d602
getNormalizedDebt, toVaultKey, fromVaultKey
turadg cb26689
tests for orderedVaultStore
turadg e3b0be4
more isolation between modules
turadg 9164c53
cleanup
turadg c63819c
percent() helper for tests
turadg bac833f
done for now with test-prioritizedVaults (it's doing considerably les…
turadg c6eb3a7
test-vault passing
turadg 89a384b
test vaultFactory overdeposit passing
turadg a502eed
integration test passing (with an additional notification)
turadg 89a2af8
cleanup
turadg 0af7d8d
fix bug in key gen
turadg 2167398
more robust handling of uncollaterialized vaults
turadg b97ba88
cleanup
turadg c81bfdb
make test-orderedVaultStore deterministic
turadg 4259d64
forEachRatioGTE cb --> entriesPrioritizedGTE generator
turadg ab670eb
work around mixing the notifier streams
turadg 2146ec0
remove vault-interest unit test b/c interest requires a real vaultMan…
turadg ff10167
stop testing removal by notification that no longer happens
turadg 21d74d9
docs
turadg 0b1d5d2
object for debtSnapshot
turadg ce035f9
vault test for compound interest
turadg 9b36c94
clean up TODOs (remove or include ticket)
turadg c66af62
vaultFactory test for minimum debt
turadg ed35236
reduce debug/trace output
turadg 21bc9a1
docs
turadg 3e0d685
prevent liquidating a vault that is already liquidating
turadg d139de3
fix bug in getCollateralizationRatio
turadg b937187
more specific test for snapshot state when opening vault after launch
turadg 3410eb5
track debts as NatValue instead of Amount
turadg 754c085
factor out and test calculateCompoundedInterest()
turadg 5d43e74
arbitrary precision in compound interest state
turadg 5e4cc5f
refactor(zoe): move ratioGTE
turadg d3a746f
cleanup
turadg 9b3d4c2
docs: code review feedback
turadg 9c56712
Merge branch 'master' into ta/4341-durable-prioritizedVaults
turadg ceb95a2
doc: code review improvements
turadg d2560ba
correct initial latestInterestUpdate
turadg e71db04
store totalDebt as Amount
turadg d10e0e2
Merge branch 'master' into ta/4341-durable-prioritizedVaults
mergify[bot] 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
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,27 @@ | ||
# RUN protocol | ||
|
||
## Overview | ||
|
||
RUN is a stable token that enables the core of the Agoric economy. | ||
|
||
By convention there is one well-known **VaultFactory**. By governance it creates a **VaultManager** for each type of asset that can serve as collateral to mint RUN. | ||
|
||
Anyone can make a **Vault** by putting up collateral with the appropriate VaultManager. Then | ||
they can request RUN that is backed by that collateral. | ||
|
||
In any vault, when the ratio of the debt to the collateral exceeds a governed threshold, it is | ||
deemed undercollateralized. If the result of a price check shows that a vault is | ||
undercollateralized, the VaultManager liquidates it. | ||
## Persistence | ||
|
||
The above states are robust to system restarts and upgrades. This is accomplished using the Agoric (Endo?) Collections API. | ||
|
||
## Debts | ||
|
||
Debts are denominated in µRUN. (1 million µRUN = 1 RUN) | ||
|
||
Each interest charging period (say daily) the actual debts in all vaults are affected. Materializing that across all vaults would be O(n) writes. Instead, to make charging interest O(1) we virtualize the debt that a vault owes to be a function of stable vault attributes and values that change in the vault manager when it charges interest. Specifically, | ||
- a compoundedInterest value on the manager that keeps track of interest accrual since its launch | ||
- a debtSnapshot on the vault by which one can calculate the actual debt | ||
|
||
To maintain that the keys of vaults to liquidate are stable requires that its keys are also time-independent so they're recorded as a "normalized collateralization ratio", with the actual collateral divided by the normalized debt. |
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
71 changes: 71 additions & 0 deletions
71
packages/run-protocol/src/vaultFactory/orderedVaultStore.js
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,71 @@ | ||
// @ts-check | ||
// XXX avoid deep imports https://github.com/Agoric/agoric-sdk/issues/4255#issuecomment-1032117527 | ||
import { makeScalarBigMapStore } from '@agoric/swingset-vat/src/storeModule.js'; | ||
import { fromVaultKey, toVaultKey } from './storeUtils.js'; | ||
|
||
/** | ||
* Used by prioritizedVaults to wrap the Collections API for this use case. | ||
* | ||
* Designed to be replaceable by naked Collections API when composite keys are available. | ||
* | ||
* In this module debts are encoded as the inverse quotient (collateral over debt) so that | ||
* greater collaterization sorts after lower. (Higher debt-to-collateral come | ||
* first.) | ||
*/ | ||
|
||
/** @typedef {import('./vault').VaultKit} VaultKit */ | ||
/** @typedef {import('./storeUtils').CompositeKey} CompositeKey */ | ||
|
||
export const makeOrderedVaultStore = () => { | ||
// TODO make it work durably https://github.com/Agoric/agoric-sdk/issues/4550 | ||
/** @type {MapStore<string, VaultKit>} */ | ||
const store = makeScalarBigMapStore('orderedVaultStore', { durable: false }); | ||
|
||
/** | ||
* | ||
* @param {string} vaultId | ||
* @param {VaultKit} vaultKit | ||
*/ | ||
const addVaultKit = (vaultId, vaultKit) => { | ||
const { vault } = vaultKit; | ||
const debt = vault.getDebtAmount(); | ||
const collateral = vault.getCollateralAmount(); | ||
const key = toVaultKey(debt, collateral, vaultId); | ||
store.init(key, vaultKit); | ||
return key; | ||
}; | ||
|
||
/** | ||
* | ||
* @param {string} key | ||
* @returns {VaultKit} | ||
*/ | ||
const removeByKey = key => { | ||
try { | ||
const vaultKit = store.get(key); | ||
assert(vaultKit); | ||
store.delete(key); | ||
return vaultKit; | ||
} catch (e) { | ||
const keys = Array.from(store.keys()); | ||
console.error( | ||
'removeByKey failed to remove', | ||
key, | ||
'parts:', | ||
fromVaultKey(key), | ||
); | ||
console.error(' key literals:', keys); | ||
console.error(' key parts:', keys.map(fromVaultKey)); | ||
throw e; | ||
} | ||
}; | ||
|
||
return harden({ | ||
addVaultKit, | ||
removeByKey, | ||
keys: store.keys, | ||
entries: store.entries, | ||
getSize: store.getSize, | ||
values: store.values, | ||
}); | ||
}; |
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.
is
orderedVaultStore
going to support removing a range of keys as a bulk operation?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.
I think the underlying Collections API supports that but there's no need to use it yet since vaults are removed whenever their liquidation completes, not a single bulk operation.