{"componentChunkName":"component---src-pages-sips-sip-markdown-remark-frontmatter-sip-tsx","path":"/sips/sip-91/","result":{"data":{"markdownRemark":{"fileAbsolutePath":"/vercel/path0/content/sips/sip-91.md","frontmatter":{"sip":91,"sccp":null,"title":"Debt Cache Contract","network":"Ethereum","author":"Anton Jurisevic (@zyzek)","type":"Governance","proposal":null,"implementor":null,"release":null,"created":"2020-10-20T00:00:00.000Z","updated":null,"status":"Implemented"},"html":"<!--You can leave these HTML comments in your merged SIP and delete the visible duplicate text guides, they will not appear and may be helpful to refer to if you edit it again. This is the suggested template for new SIPs. Note that an SIP number will be assigned by an editor. When opening a pull request to submit your SIP, please use an abbreviated title in the filename, `sip-draft_title_abbrev.md`. The title should be 44 characters or less.-->\n<h2 id=\"simple-summary\" style=\"position:relative;\"><a href=\"#simple-summary\" aria-label=\"simple summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Simple Summary</h2>\n<!--\"If you can't explain it simply, you don't understand it well enough.\" Simply describe the outcome the proposed changes intends to achieve. This should be non-technical and accessible to a casual community member.-->\n<p>Separates the debt snapshot logic out of <code>Issuer</code>, and into a new <code>DebtCache</code> contract.</p>\n<h2 id=\"abstract\" style=\"position:relative;\"><a href=\"#abstract\" aria-label=\"abstract permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Abstract</h2>\n<!--A short (~200 word) description of the proposed change, the abstract should clearly describe the proposed change. This is what *will* be done if the SIP is implemented, not *why* it should be done or *how* it will be done. If the SIP proposes deploying a new contract, write, \"we propose to deploy a new contract that will do x\".-->\n<p>This SIP proposes adding a new <code>DebtCache</code> contract, along with a <code>RealtimeDebtCache</code> implementation\ninheriting <code>DebtCache</code>, which will be used on L2 where debt snapshots are not required.\nAll debt cache related functions that currently exist in <code>Issuer</code> will be moved into the new contracts.</p>\n<h2 id=\"motivation\" style=\"position:relative;\"><a href=\"#motivation\" aria-label=\"motivation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Motivation</h2>\n<!--This is the problem statement. This is the *why* of the SIP. It should clearly explain *why* the current state of the protocol is inadequate.  It is critical that you explain *why* the change is needed, if the SIP proposes changing how something is calculated, you must address *why* the current calculation is innaccurate or wrong. This is not the place to describe how the SIP will address the issue!-->\n<p>The modifications applied in <a href=\"/8d14e868f4a63ca5e3bc9a5ab9e83d4c/sip-83.md\">SIP-83</a> to cache the total system debt increased the size of the <code>Issuer</code>\ncontract to the point that its compiled bytecode was too large to be deployed on the L2 OVM. As the L2\ndeployment of Synthetix has no execution costs, debt snapshots are unnecessary in this context.\nBy separating out the snapshot logic, the <code>Issuer</code> and <code>DebtCache</code> contracts will individually fit into the\ncontract size limit on L2, and the <code>DebtCache</code> itself can be individually replaced with a <code>RealtimeDebtCache</code>\nversion which does not require cache synchronisation.</p>\n<p>This structure will also allow debt cache logic to be upgraded without modifying the <code>Issuer</code>, which when\nredeployed requires a time-consuming process of re-adding all Synths to the system.</p>\n<h2 id=\"specification\" style=\"position:relative;\"><a href=\"#specification\" aria-label=\"specification permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Specification</h2>\n<!--The specification should describe the syntax and semantics of any new feature, there are five sections\n1. Overview\n2. Rationale\n3. Technical Specification\n4. Test Cases\n5. Configurable Values\n-->\n<h3 id=\"rationale\" style=\"position:relative;\"><a href=\"#rationale\" aria-label=\"rationale permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Rationale</h3>\n<!--This is where you explain the reasoning behind how you propose to solve the problem. Why did you propose to implement the change in this way, what were the considerations and trade-offs. The rationale fleshes out what motivated the design and why particular design decisions were made. It should describe alternate designs that were considered and related work. The rationale may also provide evidence of consensus within the community, and should discuss important objections or concerns raised during discussion.-->\n<p>Although in principle one could simply replace the entire <code>Issuer</code> contract with a different version for L2,\nthis would require modifying the internal structure of that contract, and entail multiple versions of the\ncore debt snapshot code to exist. This would increase the overhead of making contract modifications in the\nfuture. By separating out the logic into a new contract, this overhead is reduced, and the Issuer contract's\nsize is brought down much further below the fundamental size limits imposed by the OVM, allowing more headroom\nto extend its functionality.</p>\n<p>Many functions will be renamed in this refactor; although in many cases no functionality will change, these\nmodifications to the interface are in service of making it clearer, more consistent, and more complete.</p>\n<h3 id=\"technical-specification\" style=\"position:relative;\"><a href=\"#technical-specification\" aria-label=\"technical specification permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Technical Specification</h3>\n<!--The technical specification should outline the public API of the changes proposed. That is, changes to any of the interfaces Synthetix currently exposes or the creations of new ones.-->\n<p><code>Issuer</code> will no longer be responsible for maintaining the debt cache, but will instead pass this responsibility on to\nthe new <code>DebtCache</code> contract.</p>\n<h4 id=\"interface-modifications\" style=\"position:relative;\"><a href=\"#interface-modifications\" aria-label=\"interface modifications permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Interface Modifications</h4>\n<p>The following functions, and a number of supporting internal functions will be moved from the <code>Issuer</code> and into a\nnew contract called <code>DebtCache</code>. These functions will be renamed to clarify the resulting interface. Several new functions\nwill added to round out its functionality and simplify the implementation.</p>\n<table>\n<thead>\n<tr>\n<th><code>Issuer</code> Function</th>\n<th><code>DebtCache</code> Function</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>debtSnapshotStaleTime</code></td>\n<td><code>debtSnapshotStaleTime</code></td>\n<td>Reports the current snapshot stale time.</td>\n</tr>\n<tr>\n<td><code>currentSNXIssuedDebtForCurrencies</code></td>\n<td><code>currentSynthDebts</code></td>\n<td>Reports the debt values for a set of synths at current prices and supply.</td>\n</tr>\n<tr>\n<td><code>cachedSNXIssuedDebtForCurrencies</code></td>\n<td><code>cachedSynthDebts</code></td>\n<td>Reports the cached debt values for a set of synths.</td>\n</tr>\n<tr>\n<td><code>currentSNXIssuedDebt</code></td>\n<td><code>currentDebt</code></td>\n<td>Reports the current total system debt value across all synths.</td>\n</tr>\n<tr>\n<td><code>cachedSNXIssuedDebtInfo</code></td>\n<td><code>cacheInfo</code></td>\n<td>Reports the cached system debt, when a snapshot was last taken, and the cache's invalidity and stale status</td>\n</tr>\n<tr>\n<td><code>cacheSNXIssuedDebt</code></td>\n<td><code>takeDebtSnapshot</code></td>\n<td>Takes completely fresh debt snapshot, updating the cache, timestamp, and validity status.</td>\n</tr>\n<tr>\n<td><code>updateSNXIssuedDebtForCurrencies</code></td>\n<td><code>updateCachedSynthDebts</code></td>\n<td>Modifies the cached debt value with the deltas from a specific set of synths.</td>\n</tr>\n<tr>\n<td><code>purgeDebtCacheForSynth</code></td>\n<td><code>purgeCachedSynthDebt</code></td>\n<td>Admin function to purge the cached value of a specific Synth if it was not added/removed from the system properly after an upgrade.</td>\n</tr>\n<tr>\n<td><code>updateSNXIssuedDebtForSynth</code></td>\n<td><code>updateCachedSynthDebtWithRate</code></td>\n<td>Allows the issuer and exchanger contracts to update a synth's cached debt without refetching its price</td>\n</tr>\n<tr>\n<td><code>updateSNXIssuedDebtOnExchange</code></td>\n<td>Deleted</td>\n<td>Exchange-specific logic will be moved into the <code>Exchanger</code> contract; the same functionality will be implemented with the new <code>updateCachedSynthDebtsWithRates</code> function.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>updateCachedSynthDebtsWithRates</code></td>\n<td>As <code>updateCachedSynthDebtWithRate</code>, but for a set of synths.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>updateDebtCacheValidity</code></td>\n<td>Allows the issuer to invalidate teh cache when adding or removing synths.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>cachedDebt</code></td>\n<td>Reports the cached system debt.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>cacheTimestamp</code></td>\n<td>The timestamp that the cache was last refreshed with a full snapshot.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>cacheInvalid</code></td>\n<td>True if the cache has been invalidated by, or since, the last full snapshot.</td>\n</tr>\n<tr>\n<td>None</td>\n<td><code>cacheStale</code></td>\n<td>True if the cache timestamp is older than the stale time.</td>\n</tr>\n</tbody>\n</table>\n<p>The <code>DebtCacheSynchronised</code> event will be renamed to <code>DebtCacheSnapshotTaken</code>.</p>\n<p>In addition, the issuer itself will also gain a new function, <code>Issuer.synthAddresses(bytes32[] memory currencyKeys) returns (ISynth[] memory)</code>,\nwhich will be used by the debt cache to obtain several synth addresses in a single function call in order to fetch the\ntotal supply of each.</p>\n<h4 id=\"flexible-storage-removal\" style=\"position:relative;\"><a href=\"#flexible-storage-removal\" aria-label=\"flexible storage removal permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Flexible Storage Removal</h4>\n<p>To simplify implementation, offset the gas cost of the additional function call, the cached debt values will be stored in\nthe debt cache contract itself rather than in flexible storage.\nThere is less of a necessity to persist this information since it no longer rides along with the issuer, but in addition\nstoring the cache in the contract will decrease gas consumption for any account calling\n<code>takeDebtSnapshot</code>, which will improve gas costs for the snapshot keeper, which must run regularly to keep the\ndebt snapshot from going stale.</p>\n<h4 id=\"l2-realtime-debt-cache\" style=\"position:relative;\"><a href=\"#l2-realtime-debt-cache\" aria-label=\"l2 realtime debt cache permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>L2 Realtime Debt Cache</h4>\n<p>On L2 <code>DebtCache</code> will be replaced by <code>RealtimeDebtCache</code>, which is a drop-in replacement that shares an identical\ninterface; but its semantics will be altered as follows:</p>\n<ul>\n<li><code>cacheInfo</code> will report realtime values from <code>currentDebt</code> for the debt and invalidity. The cache timestamp will always report as the current block timestamp, and the cache will never be stale.</li>\n<li><code>cachedSynthDebts</code> will report realtime values from <code>currentSynthDebts</code>.</li>\n<li>All mutative functions in the interface such as <code>takeDebtSnapshot</code> will become no-ops.</li>\n</ul>\n<h4 id=\"issuer-multiple-synth-additionremoval\" style=\"position:relative;\"><a href=\"#issuer-multiple-synth-additionremoval\" aria-label=\"issuer multiple synth additionremoval permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Issuer Multiple Synth Addition/Removal</h4>\n<p>While the <code>Issuer</code> is being modified, functions to add and remove multiple Synths at once will also be added,\nwhich will speed up redeployments of the <code>Issuer</code> (among other operations) by batching Synth migrations rather\nthan performing them one by one.</p>\n<h3 id=\"test-cases\" style=\"position:relative;\"><a href=\"#test-cases\" aria-label=\"test cases permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Test Cases</h3>\n<!--Test cases for an implementation are mandatory for SIPs but can be included with the implementation..-->\n<p>Test cases are included with the implementation in <a href=\"https://github.com/Synthetixio/synthetix/pull/811\">its pull request</a>.</p>\n<h2 id=\"copyright\" style=\"position:relative;\"><a href=\"#copyright\" aria-label=\"copyright permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Copyright</h2>\n<p>Copyright and related rights waived via <a href=\"https://creativecommons.org/publicdomain/zero/1.0/\">CC0</a>.</p>"}},"pageContext":{"id":"e88c459d-fb71-5cf7-b970-887506dc85c8","frontmatter__sip":91,"__params":{"frontmatter__sip":"91"}}},"staticQueryHashes":[]}