Skip to content

Commit

Permalink
fix: handle no previous payment updates (#534)
Browse files Browse the repository at this point in the history
* fix: handle no previous payment updates

* easier to read
  • Loading branch information
adamewozniak authored Oct 14, 2024
1 parent 602c1c6 commit 87fa35f
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions x/gmp/types/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TriggerUpdate checks if the payment should be updated based on the current rate and the last update block.
func (p Payment) TriggerUpdate(rate math.LegacyDec, ctx sdk.Context) bool {
// Calculate the percentage difference
if p.LastBlock == 0 || p.LastPrice.IsZero() {
return true
}
priceDiff := p.LastPrice.Sub(rate).Abs()
percentageDiff := priceDiff.Quo(p.LastPrice).MulInt64(100)
deviationExceeded := priceDiff.Quo(p.LastPrice).MulInt64(100).GT(p.Deviation)

return percentageDiff.GT(p.Deviation) ||
p.LastBlock < ctx.BlockHeight()-p.Heartbeat
return deviationExceeded || p.LastBlock < ctx.BlockHeight()-p.Heartbeat
}

0 comments on commit 87fa35f

Please sign in to comment.