Skip to content

Commit

Permalink
Merge pull request #11455 from vegaprotocol/scale-funding-payment
Browse files Browse the repository at this point in the history
Scale funding payment
  • Loading branch information
jeremyletang authored Jul 15, 2024
2 parents e8844bd + d1a53e1 commit d6314b9
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 225 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- [11400](https://github.com/vegaprotocol/vega/issues/11400) - Add support for long block auction.
- [11026](https://github.com/vegaprotocol/vega/issues/11026) - Add API flag to get paid liquidity fees for a `vAMM` using the parent key.
- [11027](https://github.com/vegaprotocol/vega/issues/11027) - Add API filters to get fees and rewards by market, across epochs.
- [10360](https://github.com/vegaprotocol/vega/issues/10360) - Scale funding payment by fraction of period spent outside of auction.

### 🐛 Fixes

Expand Down
76 changes: 54 additions & 22 deletions core/integration/features/settlement/0019-MCAL_018.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Feature: check when settlement data precision is different/equal to the settleme

Background:

And the following assets are registered:
Given the following assets are registered:
| id | decimal places |
| USD | 2 |

Expand All @@ -26,15 +26,15 @@ Feature: check when settlement data precision is different/equal to the settleme

@Perpetual
Scenario: 001 oracle data decimal > asset decimal 0070-MKTD-018, 0019-MCAL-091
# start at this timestamp
Given time is updated to "2021-02-11T16:35:21Z"
And the markets:
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | position decimal places | market type | sla params |
| ETH/DEC19 | ETH | USD | default-simple-risk-model-3 | default-margin-calculator | 1 | default-none | default-none | perp-oracle-1 | 0.25 | 0 | -1 | perp | default-futures |
And the following network parameters are set:
| name | value |
| market.auction.minimumDuration | 1 |
| limits.markets.maxPeggedOrders | 2 |
Given the following network parameters are set:
| name | value |
| market.auction.minimumDuration | 1 |
| limits.markets.maxPeggedOrders | 2 |
| network.markPriceUpdateMaximumFrequency | 5s |
And the parties deposit on asset's general account the following amount:
| party | asset | amount |
Expand Down Expand Up @@ -62,11 +62,13 @@ Feature: check when settlement data precision is different/equal to the settleme
| aux2 | ETH/DEC19 | buy | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GTC |
| aux | ETH/DEC19 | sell | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GTC |

Then the opening auction period ends for market "ETH/DEC19"
# Time moves forwards 2s
Then the network moves ahead "2" blocks
And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19"

# Another +1s making it 2021-02-11T16:35:24
When the network moves ahead "1" blocks
When the parties place the following orders:
And the parties place the following orders:
| party | market id | side | volume | price | resulting trades | type | tif |
| party1 | ETH/DEC19 | sell | 1 | 1000 | 0 | TYPE_LIMIT | TIF_GTC |
| party2 | ETH/DEC19 | buy | 1 | 1000 | 1 | TYPE_LIMIT | TIF_GTC |
Expand All @@ -88,9 +90,18 @@ Feature: check when settlement data precision is different/equal to the settleme
| party3 | USD | ETH/DEC19 | 132000 | 99866000 |
| lp1 | USD | ETH/DEC19 | 6600000 | 492200000 |

When the oracles broadcast data with block time signed with "0xCAFECAFE1":

And the oracles broadcast data with block time signed with "0xCAFECAFE1":
| name | value | time offset |
| perp.funding.cue | 1613061324 | 0s |
# 4320h is 6 * 30 * 24 (180days, or 6 months) with a block duration of 10 days (so 18 ticks)
# making it 2021-08-10T16:35:24
Then the network moves ahead "4320h" with block duration of "240h"
# now move ahead the remaining time to get to 2021-08-11T11:04:07
# We're moving by 66,583s (18 * 3600 + 29 * 60 + 43), divisible by 11 (6053)
# So set block duration to 6053 for 11 ticks
And the network moves ahead "18h29m43s" with block duration of "6053s"
#When time is updated to "2021-08-12T11:04:07Z"

When the network moves ahead "5" blocks
When the oracles broadcast data with block time signed with "0xCAFECAFE1":
Expand All @@ -109,6 +120,7 @@ Feature: check when settlement data precision is different/equal to the settleme

@Perpetual
Scenario: 002 oracle data decimal < asset decimal 0070-MKTD-019
Given time is updated to "2021-02-11T16:35:21Z"
And the markets:
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | position decimal places | decimal places | market type | sla params |
| ETH/DEC19 | ETH | USD | default-simple-risk-model-3 | default-margin-calculator | 1 | default-none | default-none | perp-oracle-3 | 0.25 | 0 | 1 | 2 | perp | default-futures |
Expand Down Expand Up @@ -145,11 +157,12 @@ Feature: check when settlement data precision is different/equal to the settleme
| aux2 | ETH/DEC19 | buy | 100 | 100000 | 0 | TYPE_LIMIT | TIF_GTC |
| aux | ETH/DEC19 | sell | 100 | 100000 | 0 | TYPE_LIMIT | TIF_GTC |

Then the opening auction period ends for market "ETH/DEC19"
Then the network moves ahead "2" blocks
And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19"

# Time at start of the scenario is now +3s
When the network moves ahead "1" blocks
When the parties place the following orders:
And the parties place the following orders:
| party | market id | side | volume | price | resulting trades | type | tif |
| party1 | ETH/DEC19 | sell | 100 | 100000 | 0 | TYPE_LIMIT | TIF_GTC |
| party2 | ETH/DEC19 | buy | 100 | 100000 | 1 | TYPE_LIMIT | TIF_GTC |
Expand All @@ -171,17 +184,29 @@ Feature: check when settlement data precision is different/equal to the settleme
| party3 | USD | ETH/DEC19 | 132000 | 99866000 |
| lp1 | USD | ETH/DEC19 | 6600000 | 492200000 |


When the oracles broadcast data with block time signed with "0xCAFECAFE3":
| name | value | time offset |
| perp.funding.cue | 1613061324 | 0s |

When the network moves ahead "5" blocks
When the oracles broadcast data with block time signed with "0xCAFECAFE3":
# 4320h is 6 * 30 * 24 (180days, or 6 months) with a block duration of 10 days (so 18 ticks)
# making it 2021-08-10T16:35:24
Then the network moves ahead "4320h" with block duration of "240h"
# now move ahead the remaining time to get to 2021-08-11T11:04:07
# We're moving by 66,583s (18 * 3600 + 29 * 60 + 43), divisible by 11 (6053)
# So set block duration to 6053 for 11 ticks
And the network moves ahead "18h29m43s" with block duration of "6053s"

# When time is updated to "2021-08-12T11:04:07Z"

And the network moves ahead "5" blocks
Then the oracles broadcast data with block time signed with "0xCAFECAFE3":
| name | value | time offset |
| perp.ETH.value | 30000 | 0s |
| perp.funding.cue | 1628766252 | 0s |
#1628766252 is half year after the first oracel time

#Then debug funding period events
And the following transfers should happen:
| from | to | from account | to account | market id | amount | asset |
| aux2 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 74700 | USD |
Expand All @@ -190,15 +215,17 @@ Feature: check when settlement data precision is different/equal to the settleme
| market | aux | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 74700 | USD |
| market | party1 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 149400 | USD |

@PBlock
Scenario: 003 oracle data decimal = asset decimal, while market decimal is 1, 0070-MKTD-020
Given time is updated to "2021-02-11T16:35:21Z"
And the markets:
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | decimal places | position decimal places | market type | sla params |
| ETH/DEC19 | ETH | USD | default-simple-risk-model-3 | default-margin-calculator | 1 | default-none | default-none | perp-oracle-2 | 0.25 | 0 | 1 | -1 | perp | default-futures |
And the following network parameters are set:
| name | value |
| market.auction.minimumDuration | 1 |
| limits.markets.maxPeggedOrders | 2 |
Given the following network parameters are set:
And the following network parameters are set:
| name | value |
| network.markPriceUpdateMaximumFrequency | 5s |
And the parties deposit on asset's general account the following amount:
Expand Down Expand Up @@ -227,7 +254,7 @@ Feature: check when settlement data precision is different/equal to the settleme
| aux2 | ETH/DEC19 | buy | 1 | 10000 | 0 | TYPE_LIMIT | TIF_GTC |
| aux | ETH/DEC19 | sell | 1 | 10000 | 0 | TYPE_LIMIT | TIF_GTC |

Then the opening auction period ends for market "ETH/DEC19"
Then the network moves ahead "2" blocks
And the trading mode should be "TRADING_MODE_CONTINUOUS" for the market "ETH/DEC19"

When the network moves ahead "1" blocks
Expand All @@ -253,25 +280,30 @@ Feature: check when settlement data precision is different/equal to the settleme
| party3 | USD | ETH/DEC19 | 132000 | 99866000 |
| lp1 | USD | ETH/DEC19 | 6600000 | 492200000 |


When the oracles broadcast data with block time signed with "0xCAFECAFE2":
| name | value | time offset |
| perp.funding.cue | 1613061324 | 0s |

When the network moves ahead "5" blocks
When the oracles broadcast data with block time signed with "0xCAFECAFE2":

# 4320h is 6 * 30 * 24 (180days, or 6 months) with a block duration of 10 days (so 18 ticks)
# making it 2021-08-10T16:35:24
Then the network moves ahead "4320h" with block duration of "240h"
# now move ahead the remaining time to get to 2021-08-11T11:04:07
# We're moving by 66,583s (18 * 3600 + 29 * 60 + 43), divisible by 11 (6053)
# So set block duration to 6053 for 11 ticks
And the network moves ahead "18h29m43s" with block duration of "6053s"

And the network moves ahead "5" blocks
And the oracles broadcast data with block time signed with "0xCAFECAFE2":
| name | value | time offset |
| perp.ETH.value | 300000 | 0s |
| perp.funding.cue | 1628766252 | 0s |
#1628766252 is half year after the first oracel time

And the following transfers should happen:
Then the following transfers should happen:
| from | to | from account | to account | market id | amount | asset |
| aux2 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 74700 | USD |
| party2 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 74700 | USD |
| party3 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 74700 | USD |
| market | aux | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 74700 | USD |
| market | party1 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 149400 | USD |




Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,22 @@ Feature: check when settlement data precision is different/equal to the settleme
| party1 | ETH/DEC19 | sell | 1 | 2000 | 0 | TYPE_LIMIT | TIF_GTC |
| party3 | ETH/DEC19 | buy | 1 | 2000 | 1 | TYPE_LIMIT | TIF_GTC |

When time is updated to "2021-02-11T16:35:24Z"
Then system unix time is "1613061324"

When the oracles broadcast data with block time signed with "0xCAFECAFE1":
| name | value | time offset |
| perp.funding.cue | 1613061324 | 0s |
And the network moves ahead "5" blocks

When time is updated to "2021-08-12T11:04:12Z"
Then system unix time is "1628766252"

Then the oracles broadcast data with block time signed with "0xCAFECAFE1":
| name | value | time offset |
| perp.ETH.value | 300000000 | 0s |
| perp.funding.cue | 1628766252 | 0s |
# #1628766252 is half year after the first oracel time
#1628766252 is half year after the first oracle time

And the following transfers should happen:
| from | to | from account | to account | market id | amount | asset |
Expand All @@ -104,27 +110,35 @@ Feature: check when settlement data precision is different/equal to the settleme
| id | data source config | linear slippage factor | quadratic slippage factor |
| ETH/DEC19 | perp-oracle-2 | 0.25 | 0 |

When time is updated to "2021-08-12T11:04:30Z"
Then system unix time is "1628766270"

And the oracles broadcast data with block time signed with "0xCAFECAFE2":
| name | value | time offset |
| perp.funding.cue | 1628766270 | 0s |
| perp.ETH.value | 350000000 | 0s |

When the network moves ahead "5" blocks
When time is updated to "2024-10-12T20:51:10Z"
Then system unix time is "1728766270"

Then the oracles broadcast data with block time signed with "0xCAFECAFE2":
| name | value | time offset |
| perp.ETH.value | 350000000 | 0s |
| perp.funding.cue | 1728766270 | 0s |

And the following funding period events should be emitted:
| start | end | internal twap | external twap | funding payment |
| 1628766270 | 1728766270 | 200000 | 350000000 | -174800000 |

# funding loss, win, margin excess transfers:
And the following transfers should happen:
| from | to | from account | to account | market id | amount | asset |
| aux | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1080000 | USD |
| aux | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 297994700 | USD |
| party1 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1680000 | USD |
| party1 | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 997469400 | USD |
| market | party2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741366 | USD |
| market | party3 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741368 | USD |
| market | aux2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741366 | USD |
| aux2 | aux2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432666666 | USD |
| party2 | party2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432666666 | USD |
| party3 | party3 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432666668 | USD |
| from | to | from account | to account | market id | amount | asset | type |
| aux | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1080000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| aux | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 297994700 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| party1 | market | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 1680000 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| party1 | market | ACCOUNT_TYPE_GENERAL | ACCOUNT_TYPE_SETTLEMENT | ETH/DEC19 | 997469400 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS |
| market | party2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741366 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
| market | party3 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741368 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
| market | aux2 | ACCOUNT_TYPE_SETTLEMENT | ACCOUNT_TYPE_MARGIN | ETH/DEC19 | 432741366 | USD | TRANSFER_TYPE_PERPETUALS_FUNDING_WIN |
| aux2 | aux2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432711486 | USD | TRANSFER_TYPE_MARGIN_HIGH |
| party2 | party2 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432711486 | USD | TRANSFER_TYPE_MARGIN_HIGH |
| party3 | party3 | ACCOUNT_TYPE_MARGIN | ACCOUNT_TYPE_GENERAL | ETH/DEC19 | 432711488 | USD | TRANSFER_TYPE_MARGIN_HIGH |
Loading

0 comments on commit d6314b9

Please sign in to comment.