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

Store limit order quote to compute risk adjusted rewards later on #908

Merged
merged 7 commits into from
Dec 6, 2022

Conversation

MartinquaXD
Copy link
Contributor

@MartinquaXD MartinquaXD commented Dec 2, 2022

Fixes #891

In order to be able to calculate risk adjusted fees for limit orders we now store the quote generated while updating their surplus_fee in the database. Note that we have to adjust the expiration time to match the max time between 2 surplus_fee updates.

There is one thing still missing. To avoid allowing users to create market orders with quotes that are meant for limit orders and therefore are allowed to be older than market order quotes. A new quote kind needs to be added to the DB. Just wanted to implement what was suggested in the issue in case we might want to merge ASAP.

Test Plan

Ran a manual test on goerli with slight adjustments to the code so that it also computes the rewards on goerli and got an auction with a limit order that contained rewards:

auction
{
  "id": 943,
  "block": 8084631,
  "latestSettlementBlock": 13418189,
  "orders": [
    {
      "creationDate": "2022-12-06T09:16:01.130607Z",
      "owner": "0x41bfdacf886aa804978a180a28f95f52401ce72d",
      "uid": "0xcad61f4953d6a7b4735ef8c2721dff9ff0f602101329ddbf4d259f12f87a313141bfdacf886aa804978a180a28f95f52401ce72d638f0cc0",
      "availableBalance": "480000000000000000",
      "executedBuyAmount": "0",
      "executedSellAmount": "0",
      "executedSellAmountBeforeFees": "0",
      "executedFeeAmount": "0",
      "invalidated": false,
      "status": "open",
      "class": "limit",
      "surplusFee": "155797627524",
      "surplusFeeTimestamp": "2022-12-06T09:25:35.529513Z",
      "executedSurplusFee": null,
      "settlementContract": "0x9008d19f58aabd9ed0d60971565aa8510560ab41",
      "fullFeeAmount": "155797627524",
      "isLiquidityOrder": false,
      "sellToken": "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6",
      "buyToken": "0x3430d04e42a722c5ae52c5bffbf1f230c2677600",
      "receiver": "0x0000000000000000000000000000000000000000",
      "sellAmount": "10000000000000000",
      "buyAmount": "1000000000000000000000",
      "validTo": 1670319296,
      "appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
      "feeAmount": "0",
      "kind": "sell",
      "partiallyFillable": false,
      "sellTokenBalance": "erc20",
      "buyTokenBalance": "erc20",
      "signingScheme": "eip712",
      "signature": "0x4e89f1773f76ba1f38e5803bb011c9820d79e4b8c280ac1bf0da25a9657c20323abc8ef3c1db6ac39dcacdbb8f1e5b238f8b538c5d458dffe7bf7edb75fbeea31b",
      "interactions": {
        "pre": []
      }
    }
  ],
  "prices": {
    "0x3430d04e42a722c5ae52c5bffbf1f230c2677600": "88027419652712",
    "0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6": "1000000000000000000"
  },
  "rewards": {
    "0xcad61f4953d6a7b4735ef8c2721dff9ff0f602101329ddbf4d259f12f87a313141bfdacf886aa804978a180a28f95f52401ce72d638f0cc0": 37.77560120529544
  }
}

@MartinquaXD MartinquaXD requested a review from a team as a code owner December 2, 2022 18:33
Comment on lines 122 to 123
// Make quote last long enough to compute risk adjusted rewards for the order.
quote.data.expiration = Utc::now() + self.limit_order_age;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also comment here why we store the quote.

Comment on lines 126 to 127
quote.data.expiration = Utc::now() + self.limit_order_age;
self.quoter.store_quote(quote).await?;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Shouldn't we change update_limit_order_fees to store both the quote and the fees in an atomic transaction?

Copy link
Contributor

Choose a reason for hiding this comment

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

Specifically, I would change it to just store it in the order_quotes table and not in the quotes table.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, making that atomic definitely makes sense. 👍
Will double check the table where we store the quote.

Copy link
Contributor

@nlordell nlordell left a comment

Choose a reason for hiding this comment

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

LGTM, I think this fix is fine as a hotfix, but I think we should store both the quote and the fees atomically though.

@nlordell
Copy link
Contributor

nlordell commented Dec 5, 2022

Wait - I think this PR is storing the quote in the quotes table and not the order_quotes table (which is where an order's associated quote metadata is stored permanently).

@nlordell
Copy link
Contributor

nlordell commented Dec 5, 2022

To avoid allowing users to create market orders with quotes that are meant for limit orders and therefore are allowed to be older than market order quotes.

This shouldn't be a problem if we store in the order_quotes table which just associates quote data with an order (and not with anything in the quotes table).

)
.await?;

database::quotes::save(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we want to save the quote. I think this is the correct database method to use:

pub async fn insert_quote_and_update_on_conflict(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Damn wrong table again. I don't know the details around quote storing but I know that it's pretty confusing to have 2 tables with very similar names. :/

Copy link
Contributor

Choose a reason for hiding this comment

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

🙈

Do you think we should rename the table to make things clearer?

@@ -64,6 +80,19 @@ impl Postgres {
},
)
.await?;

database::orders::insert_quote_and_update_on_conflict(
&mut ex,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean it is already part of a single database transaction? If so - cool!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it doesn't. Sorry was all over the place today. 3fafc92 should finally implement a transaction which does both updates atomically.

Copy link
Contributor

@nlordell nlordell left a comment

Choose a reason for hiding this comment

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

Looks correct to me!

@MartinquaXD
Copy link
Contributor Author

Ran a manual test to verify that the rewards get computed (more details in PR description).

@MartinquaXD MartinquaXD merged commit de41803 into main Dec 6, 2022
@MartinquaXD MartinquaXD deleted the store-limit-order-quote branch December 6, 2022 09:35
@github-actions github-actions bot locked and limited conversation to collaborators Dec 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit Order Risk Adjusted Rewards Not Being Calculated
3 participants