{"componentChunkName":"component---src-pages-sips-sip-markdown-remark-frontmatter-sip-tsx","path":"/sips/sip-281/","result":{"data":{"markdownRemark":{"fileAbsolutePath":"/vercel/path0/content/sips/sip-281.md","frontmatter":{"sip":281,"sccp":null,"title":"PerpsV2 (Trading fixes & improvements)","network":"Optimism","author":"David Vuong (@davidvuong)","type":"Governance","proposal":"https://snapshot.org/#/snxgov.eth/proposal/0x53e5909ebefade60ad3c790a32ad90bd01dbe2d554838c3791d537e733085558","implementor":"David Vuong (@davidvuong), MEB (@barrasso)","release":null,"created":"2022-09-30T00:00:00.000Z","updated":null,"status":"Implemented"},"html":"<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<p>Improves the underlying mechanism of next price orders to be time oriented and takes further consideration into maker/taker fee calculations when a trade flips the market skew.</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<p>SIP proposes two changes, (1) delayed orders and (2) cross side maker/taker fees. Delayed orders updates the current next-price orders to allow execution based primarily on time rather than purely on a semi-arbitrary price feed update from CL.</p>\n<p>Cross side maker/taker fees is a fix on the current order fee calculation. Currently, if an order flips the market skew, the size in its entirety is charged with just maker fees. This is incorrect. The change involves updating the calculation such that the proportion of size that reduces the skew will be charged a maker fee but the remaining which increases the skew is charged with a taker fee.</p>\n<p>Note this SIP depends on SIP-280 as these changes extend on top of PerpsV2.</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<h3 id=\"delayed-orders\" style=\"position:relative;\"><a href=\"#delayed-orders\" aria-label=\"delayed orders 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>Delayed Orders</h3>\n<p>Current next price orders are non-deterministic and gives way to suboptimal user experience for traders. An order is only executable when a new price update occurs. An order operating under the rules of next price will only be executable if a feed's deviation threshold is reached, sometimes during low volatility, the threshold never kicks in and traders are at the mercy of the heartbeat.</p>\n<p>If the intent is to minimise frontrunning, the same can be achieved through a time based delay. This provides a win-win-win scenario in that traders receive a better user experience as they have a higher level of confidence their trade will be fulfilled/fulfilled faster, while capitalising on lower fees, and the protocol sees the same benefit to frontrunning as in next price.</p>\n<p>Note that next-price is not removed, but incorporated into delayed orders. That is, although time is preferential, if a price update occurs before the delay is reached, the order will still be executable.</p>\n<h3 id=\"cross-side-makertaker-fees\" style=\"position:relative;\"><a href=\"#cross-side-makertaker-fees\" aria-label=\"cross side makertaker fees 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>Cross Side Maker/Taker Fees</h3>\n<p>Order fee calculations in the current state do not behave correctly in the scenario where a trade is both a maker and a taker. This can happen when the size of the trade pushes the market skew past equilibrium and into the opposing direction. As a result, fees are miscalculated because the proportion of size in the opposite direction is calculated against a maker fee when it should in fact be a taker fee.</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<h3 id=\"overview\" style=\"position:relative;\"><a href=\"#overview\" aria-label=\"overview 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>Overview</h3>\n<p>Both features are implemented on top of the existing <code>PerpsV2</code> duplicated smart contracts and do not change any existing contracts deployed and used in real environments. Aside from renames and the addition of new values configurable by SCCP, all delayed order public API changes are isolated to <code>PerpsV2MarketDelayedOrders.sol</code>. All existing functionality around submission, cancellation, and execution of orders will be updated to incorporate time.</p>\n<p>Order fee calculations are even simpler in that changes are isolated to <code>PerpsV2MarketBase.sol._orderFee</code>, utilising existing helper functions for calculations.</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<h4 id=\"delayed-orders-1\" style=\"position:relative;\"><a href=\"#delayed-orders-1\" aria-label=\"delayed orders 1 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>Delayed Orders</h4>\n<p>The are two primary changes introduced as part of delayed orders. First, the rename of <code>nextPrice</code> to <code>delayedOrder</code> and second, the addition of a new parameter upon submission.</p>\n<p>Usage is as follows:</p>\n<pre><code class=\"language-solidity\">interface IPerpsV2MarketDelayedOrders {\n    function submitDelayedOrder(int sizeDelta, uint desiredTimeDelta) external;\n\n    function submitDelayedOrderWithTracking(\n        int sizeDelta,\n        uint desiredTimeDelta,\n        bytes32 trackingCode\n    ) external;\n\n    function cancelDelayedOrder(address account) external;\n\n    function executeDelayedOrder(address account) external;\n}\n</code></pre>\n<p><code>desiredTimeDelta</code> is the additional parameter. It behaves as follows:</p>\n<ol>\n<li>A trader must specify the desired amount of time (in seconds) to wait before this trade is executable by a keeper or otherwise</li>\n<li><code>desiredTimeDelta</code> must be between two bounds (<code>minTimeDelta</code> and <code>maxTimeDelta</code>, inclusive)</li>\n<li>When <code>desiredTimeDelta=0</code> then <code>minTimeDelta</code> will be used</li>\n<li>A revert will occur if <code>desiredTimeDelta</code> is greater than or less than the min/max delta respectively</li>\n</ol>\n<p>Note <code>submitDelayedOrderWithTracking</code> is subsequently also updated to include a <code>desiredTimeDelta</code> parameter.</p>\n<p>To incorporate time, delayed orders also track <em>when</em> they can be executable. This can be seen by the addition of the new <code>executableAtTime</code> attribute:</p>\n<pre><code class=\"language-solidity\">struct DelayedOrder {\n    int128 sizeDelta;\n    uint128 targetRoundId;\n    uint128 targetRoundId;\n    uint128 commitDeposit;\n    uint128 keeperDeposit;\n    uint256 executableAtTime; // The timestamp at which this order is executable at\n    bytes32 trackingCode;\n}\n</code></pre>\n<p>In addition to the public API, internal references to <code>nextPrice</code> are also replaced. This includes events. The only exception is <code>nextPriceConfirmWindow</code> as this is still used.</p>\n<p>Finally, a <code>delayedOrderConfirmWindow</code> to also be included. It behaves similar to <code>nextPriceConfirmWindow</code> but with respect to time. Currently, an order's confirmation window is considered 'over' if the price feed is updated more than <code>n</code> times where <code>n = numberOfRounds</code> since the specified <code>targetRoundId</code>. To incorporate time, the confirmation predicate will consider, <code>n'</code> where <code>n' = numberOfSecondsSinceOrderWasExecutable</code> together with <code>n</code>. In short a confirm window is also considered 'over' if,</p>\n<pre><code>(block.timestamp - executableAtTime) > n'\n</code></pre>\n<h3 id=\"cross-side-makertaker-fees-1\" style=\"position:relative;\"><a href=\"#cross-side-makertaker-fees-1\" aria-label=\"cross side makertaker fees 1 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>Cross Side Maker/Taker Fees</h3>\n<p>There are no changes to the public API. Instead, an internal <code>_orderFee</code> function is updated to incorporate taker fees for the proportion that increases the skew after it is flipped.</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<p>Relevant tests will be developed during implementation.</p>\n<h3 id=\"configurable-values-via-sccp\" style=\"position:relative;\"><a href=\"#configurable-values-via-sccp\" aria-label=\"configurable values via sccp 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>Configurable Values (Via SCCP)</h3>\n<p><code>delayedOrdersMinTimeDelta</code> and <code>delayedOrdersMaxTimeDelta</code> values to be added to bound the <code>desiredTimeDelta</code> delay. <code>delayedOrderConfirmWindow</code> as described above to be added. Additionally, <code>takerFeeNextPrice</code> and <code>makerFeeNextPrice</code>, two existing values are renamed to <code>takerFeeDelayedOrder</code> and <code>makerFeeDelayedOrder</code>.</p>\n<p>In total 3 new values and 2 values to be renamed. All values are configurable at the market level.</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":"d682dac8-fe8b-5caa-a614-1f4df38e4e10","frontmatter__sip":281,"__params":{"frontmatter__sip":"281"}}},"staticQueryHashes":[]}