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

Perps: use same check for theoretical and actual funding payment #10119

Merged
merged 4 commits into from Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .spelling
Expand Up @@ -86,6 +86,8 @@ mutexes
nodewallet
notarising
OpenRPC
perp
perps
phish
Prometheus
proto
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -38,6 +38,8 @@
- [10050](https://github.com/vegaprotocol/vega/issues/10050) - Cleanup `mempool` cache on commit.
- [10052](https://github.com/vegaprotocol/vega/issues/10052) - Some recent stats tables should have been `hypertables` with retention periods.
- [10103](https://github.com/vegaprotocol/vega/issues/10103) - List ledgers `API` returns bad error when filtering by transfer type only.
- [10120](https://github.com/vegaprotocol/vega/issues/10120) - Assure theoretical and actual funding payment calculations are consistent.
- [10121](https://github.com/vegaprotocol/vega/issues/10121) - Assure `EstimatePosition` API works correctly with sparse perps data

## 0.73.0

Expand Down
17 changes: 11 additions & 6 deletions core/products/perpetual.go
Expand Up @@ -676,7 +676,7 @@ func (p *Perpetual) handleSettlementCue(ctx context.Context, t int64) {
return
}

if !p.haveData(t) || t == p.startedAt {
if !p.haveDataBeforeGivenTime(t) || t == p.startedAt {
// we have no points, or the interval is zero length so we just start a new interval
p.broker.Send(events.NewFundingPeriodEvent(ctx, p.id, p.seq, p.startedAt, ptr.From(t), nil, nil, nil, nil))
p.startNewFundingPeriod(ctx, t)
Expand All @@ -701,7 +701,7 @@ func (p *Perpetual) handleSettlementCue(ctx context.Context, t int64) {
}

func (p *Perpetual) GetData(t int64) *types.ProductData {
if !p.readyForData() {
if !p.readyForData() || !p.haveData() {
return nil
}

Expand Down Expand Up @@ -777,13 +777,13 @@ func (p *Perpetual) readyForData() bool {
return p.startedAt > 0
}

// haveData returns whether we have any data points before the given time.
func (p *Perpetual) haveData(endAt int64) bool {
// haveDataBeforeGivenTime returns whether we have at least one data point from each of the internal and external price series before the given time.
func (p *Perpetual) haveDataBeforeGivenTime(endAt int64) bool {
if !p.readyForData() {
return false
}

if len(p.internalTWAP.points) == 0 || len(p.externalTWAP.points) == 0 {
if !p.haveData() {
return false
}

Expand All @@ -794,6 +794,11 @@ func (p *Perpetual) haveData(endAt int64) bool {
return true
}

// haveData returns whether we have at least one data point from each of the internal and external price series.
func (p *Perpetual) haveData() bool {
return len(p.internalTWAP.points) > 0 && len(p.externalTWAP.points) > 0
}

// calculateFundingPayment returns the funding payment and funding rate for the interval between when the current funding period
// started and the given time. Used on settlement-cues and for margin calculations.
func (p *Perpetual) calculateFundingPayment(t int64) *fundingData {
Expand Down Expand Up @@ -876,7 +881,7 @@ func (p *Perpetual) calculateInterestTerm(externalTWAP, internalTWAP *num.Uint,
// for a party with a position of +1.
func (p *Perpetual) GetMarginIncrease(t int64) num.Decimal {
// if we have no data, or the funding factor is zero, then the margin increase will always be zero
if !p.haveData(t) || p.p.MarginFundingFactor.IsZero() {
if !p.haveDataBeforeGivenTime(t) || p.p.MarginFundingFactor.IsZero() {
return num.DecimalZero()
}

Expand Down