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

feat(refactor): improve quality of vpool code #979

Merged
merged 27 commits into from
Oct 6, 2022
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bb02872
check if base amount is zero first
jgimeno Sep 30, 2022
9f66cfe
clean some code, and remove unused funcs
jgimeno Sep 30, 2022
3bc2392
simplify existing pool
jgimeno Sep 30, 2022
9357fec
reduce check limit function to one
jgimeno Sep 30, 2022
b1b2d68
use mark price from the pool
jgimeno Sep 30, 2022
20b559b
use the pool mark price
jgimeno Sep 30, 2022
af914c0
update keeper
jgimeno Sep 30, 2022
895fc59
move is over fluctuation limit to pool
jgimeno Sep 30, 2022
3f9f8df
add overfluctuation check into snapshot
jgimeno Sep 30, 2022
35d96bc
rename fluctuation limit ration
jgimeno Sep 30, 2022
5c642d7
add over spread ratio check
jgimeno Sep 30, 2022
dfb164b
clean more code
jgimeno Sep 30, 2022
7b309dc
move vpool from pointer to type
jgimeno Sep 30, 2022
172a291
Merge branch 'master' into feat/clean-vpool
jgimeno Oct 2, 2022
3e77319
Merge branch 'master' into feat/clean-vpool
jgimeno Oct 3, 2022
2f3c7de
move query keeper to preperty instead of wrapped
jgimeno Oct 3, 2022
e171dec
add keeper as type instead of wrap
jgimeno Oct 3, 2022
96acf9c
lint
jgimeno Oct 3, 2022
9521fa6
change variable
jgimeno Oct 5, 2022
b584ff5
use mark price in all vpool
jgimeno Oct 5, 2022
54fe49b
change SpotPrice to MarkPrice in PositionChangedEvent event
jgimeno Oct 5, 2022
462fb81
Merge remote-tracking branch 'origin/master' into feat/clean-vpool
jgimeno Oct 5, 2022
6ba8518
change GetSpotTWAP to GetMarkPriceTWAP
jgimeno Oct 5, 2022
b4bae2c
add changes based on PR
jgimeno Oct 5, 2022
ab4ad3f
linter
jgimeno Oct 5, 2022
9afd4f7
Merge branch 'master' into feat/clean-vpool
jgimeno Oct 5, 2022
aaab015
Merge remote-tracking branch 'origin/master' into feat/clean-vpool
jgimeno Oct 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move is over fluctuation limit to pool
jgimeno committed Sep 30, 2022
commit 895fc591a8da61a6636d7d17db942eed767b1bea
33 changes: 1 addition & 32 deletions x/vpool/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -263,44 +263,13 @@ func (k Keeper) checkFluctuationLimitRatio(ctx sdk.Context, pool *types.VPool) e
return fmt.Errorf("error getting last snapshot number for pair %s", pool.Pair)
}

if isOverFluctuationLimit(pool, latestSnapshot) {
if pool.IsOverFluctuationLimit(latestSnapshot) {
return types.ErrOverFluctuationLimit
}

return nil
}

/*
*
isOverFluctuationLimit compares the updated pool's spot price with the current spot price.

If the fluctuation limit ratio is zero, then the fluctuation limit check is skipped.

args:
- pool: the updated vpool
- snapshot: the snapshot to compare against

ret:
- bool: true if the fluctuation limit is violated. false otherwise
*/
func isOverFluctuationLimit(pool *types.VPool, snapshot types.ReserveSnapshot) bool {
if pool.FluctuationLimitRatio.IsZero() {
return false
}

price := pool.QuoteAssetReserve.Quo(pool.BaseAssetReserve)

lastPrice := snapshot.QuoteAssetReserve.Quo(snapshot.BaseAssetReserve)
upperLimit := lastPrice.Mul(sdk.OneDec().Add(pool.FluctuationLimitRatio))
lowerLimit := lastPrice.Mul(sdk.OneDec().Sub(pool.FluctuationLimitRatio))

if price.GT(upperLimit) || price.LT(lowerLimit) {
return true
}

return false
}

/*
IsOverSpreadLimit compares the current spot price of the vpool (given by pair) to the underlying's index price (given by an oracle).
It panics if you provide it with a pair that doesn't exist in the state.
94 changes: 0 additions & 94 deletions x/vpool/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -451,100 +451,6 @@ func TestGetVpools(t *testing.T) {
})
}

func TestIsOverFluctuationLimit(t *testing.T) {
tests := []struct {
name string
pool types.VPool

isOverLimit bool
}{
{
name: "zero fluctuation limit ratio",
pool: types.VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.OneDec(),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.ZeroDec(),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "lower limit of fluctuation limit",
pool: types.VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(999),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "upper limit of fluctuation limit",
pool: types.VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(1001),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "under fluctuation limit",
pool: types.VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(998),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: true,
},
{
name: "over fluctuation limit",
pool: types.VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(1002),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
snapshot := types.NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.OneDec(),
sdk.NewDec(1000),
time.Now(),
0,
)
assert.EqualValues(t, tc.isOverLimit, isOverFluctuationLimit(&tc.pool, snapshot))
})
}
}

func TestCheckFluctuationLimitRatio(t *testing.T) {
tests := []struct {
name string
30 changes: 30 additions & 0 deletions x/vpool/types/pool.go
Original file line number Diff line number Diff line change
@@ -180,3 +180,33 @@ func (p VPool) GetMarkPrice() sdk.Dec {

return p.QuoteAssetReserve.Quo(p.BaseAssetReserve)
}

/*
IsOverFluctuationLimit compares the updated pool's spot price with the current spot price.

If the fluctuation limit ratio is zero, then the fluctuation limit check is skipped.

args:
- pool: the updated vpool
- snapshot: the snapshot to compare against

ret:
- bool: true if the fluctuation limit is violated. false otherwise
*/
func (p VPool) IsOverFluctuationLimit(snapshot ReserveSnapshot) bool {
if p.FluctuationLimitRatio.IsZero() {
return false
}

markPrice := p.GetMarkPrice()

lastPrice := snapshot.QuoteAssetReserve.Quo(snapshot.BaseAssetReserve)
upperLimit := lastPrice.Mul(sdk.OneDec().Add(p.FluctuationLimitRatio))
lowerLimit := lastPrice.Mul(sdk.OneDec().Sub(p.FluctuationLimitRatio))

if markPrice.GT(upperLimit) || markPrice.LT(lowerLimit) {
return true
}

return false
}
96 changes: 96 additions & 0 deletions x/vpool/types/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package types

import (
"github.com/stretchr/testify/assert"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
@@ -467,3 +469,97 @@ func TestVPool_GetMarkPrice(t *testing.T) {
})
}
}

func TestVPool_IsOverFluctuationLimit(t *testing.T) {
tests := []struct {
name string
pool VPool

isOverLimit bool
}{
{
name: "zero fluctuation limit ratio",
pool: VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.OneDec(),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.ZeroDec(),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "lower limit of fluctuation limit",
pool: VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(999),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "upper limit of fluctuation limit",
pool: VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(1001),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: false,
},
{
name: "under fluctuation limit",
pool: VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(998),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: true,
},
{
name: "over fluctuation limit",
pool: VPool{
Pair: common.Pair_BTC_NUSD,
QuoteAssetReserve: sdk.NewDec(1002),
BaseAssetReserve: sdk.OneDec(),
FluctuationLimitRatio: sdk.MustNewDecFromStr("0.001"),
TradeLimitRatio: sdk.OneDec(),
MaxOracleSpreadRatio: sdk.OneDec(),
MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"),
MaxLeverage: sdk.MustNewDecFromStr("15"),
},
isOverLimit: true,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
snapshot := NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.OneDec(),
sdk.NewDec(1000),
time.Now(),
0,
)
assert.EqualValues(t, tc.isOverLimit, tc.pool.IsOverFluctuationLimit(snapshot))
})
}
}