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

(bug): Can't open position on localnet because of snapshot #992

Closed
matthiasmatt opened this issue Oct 10, 2022 · 0 comments · Fixed by #993
Closed

(bug): Can't open position on localnet because of snapshot #992

matthiasmatt opened this issue Oct 10, 2022 · 0 comments · Fixed by #993
Assignees

Comments

@matthiasmatt
Copy link
Contributor

When we check for fluctuation limit we use a snapshot which can come from a different pool than the one we are looking at.

Here's a log :

/*
*
Check's that a pool that we're about to save to state does not violate the fluctuation limit.
Always tries to check against a snapshot from a previous block. If one doesn't exist, then it just uses the current snapshot.
This should run prior to updating the snapshot, otherwise it will compare the currently updated vpool to itself.

args:
  - ctx: the cosmos-sdk context
  - pool: the updated vpool

ret:
  - err: error if any
*/
func (k Keeper) checkFluctuationLimitRatio(ctx sdk.Context, pool types.VPool) error {
	if pool.FluctuationLimitRatio.IsZero() {
		// early return to avoid expensive state operations
		return nil
	}

	it := k.ReserveSnapshots.Iterate(ctx, keys.NewRange[keys.Pair[common.AssetPair, keys.Uint64Key]]().Descending())
	defer it.Close()
	if !it.Valid() {
		return fmt.Errorf("error getting last snapshot number for pair %s", pool.Pair)
	}
	latestSnapshot := it.Value()

	fmt.Println("##########################################################")
	fmt.Println("##########################################################")
	fmt.Println("##########################################################")

	fmt.Println("markPrice", pool.GetMarkPrice())
	fmt.Println("snapshotUpperLimit", latestSnapshot.GetUpperMarkPriceFluctuationLimit(pool.FluctuationLimitRatio))
	fmt.Println("snapshotLowerLimit", latestSnapshot.GetLowerMarkPriceFluctuationLimit(pool.FluctuationLimitRatio))

	markPrice := pool.GetMarkPrice()
	snapshotUpperLimit := latestSnapshot.GetUpperMarkPriceFluctuationLimit(pool.FluctuationLimitRatio)
	snapshotLowerLimit := latestSnapshot.GetLowerMarkPriceFluctuationLimit(pool.FluctuationLimitRatio)
	fmt.Println(markPrice.GT(snapshotUpperLimit) || markPrice.LT(snapshotLowerLimit))

	fmt.Println("latestSnapshot", latestSnapshot)
	if pool.IsOverFluctuationLimitInRelationWithSnapshot(latestSnapshot) {
		return types.ErrOverFluctuationLimit
	}

	return nil
}
##########################################################
##########################################################
##########################################################
markPrice 20000.000000004000000000
snapshotUpperLimit 1650.001650001650001650
snapshotLowerLimit 1350.001350001350001350
markPrice.GT(snapshotUpperLimit) || markPrice.LT(snapshotLowerLimit) true
latestSnapshot {ueth:unusd 666666000000.000000000000000000 1000000000000000.000000000000000000 1665431259665}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants