Skip to content

Commit

Permalink
Set new parameter 'PoolRecoveryPeriod'. Blockly replenish Terra&Luna …
Browse files Browse the repository at this point in the history
…Pool by the amount of delta/PoolRecoveryPerio
  • Loading branch information
Yun committed Oct 14, 2019
1 parent f8e1843 commit 1a13bf8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 41 deletions.
8 changes: 4 additions & 4 deletions x/market/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPoolsUpdate(t *testing.T) {

EndBlocker(input.Ctx, input.MarketKeeper)
basePool := input.MarketKeeper.GetBasePool(input.Ctx)
require.Equal(t, input.MarketKeeper.DailyTerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)
require.Equal(t, input.MarketKeeper.TerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)

// Update Pools at the last block of interval
input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerEpoch - 1)
Expand All @@ -34,7 +34,7 @@ func TestPoolsUpdate(t *testing.T) {

EndBlocker(input.Ctx, input.MarketKeeper)
basePool = input.MarketKeeper.GetBasePool(input.Ctx)
require.Equal(t, input.MarketKeeper.DailyTerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)
require.Equal(t, input.MarketKeeper.TerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)

// Update Pools at the last block of the another interval
input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerEpoch*2 - 1)
Expand All @@ -45,14 +45,14 @@ func TestPoolsUpdate(t *testing.T) {

EndBlocker(input.Ctx, input.MarketKeeper)
basePool = input.MarketKeeper.GetBasePool(input.Ctx)
require.Equal(t, input.MarketKeeper.DailyTerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)
require.Equal(t, input.MarketKeeper.TerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)
}

func TestReplenishPools(t *testing.T) {
input := keeper.CreateTestInput(t)

delta := sdk.NewDec(1000000)
regressionAmt := sdk.NewDec(1)
regressionAmt := delta.QuoInt64(input.MarketKeeper.PoolRecoveryPeriod(input.Ctx))
basePool := regressionAmt.MulInt64(core.BlocksPerDay)
input.MarketKeeper.SetBasePool(input.Ctx, basePool)
input.MarketKeeper.SetTerraPool(input.Ctx, basePool.Sub(delta))
Expand Down
4 changes: 2 additions & 2 deletions x/market/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ var (
TerraPoolKey = types.TerraPoolKey
LastUpdateHeightKey = types.LastUpdateHeightKey
ParamStoreKeyPoolUpdateInterval = types.ParamStoreKeyPoolUpdateInterval
ParamStoreKeyDailyTerraLiquidityRatio = types.ParamStoreKeyDailyTerraLiquidityRatio
ParamStoreKeyTerraLiquidityRatio = types.ParamStoreKeyTerraLiquidityRatio
ParamStoreKeyMinSpread = types.ParamStoreKeyMinSpread
ParmamStoreKeyTobinTax = types.ParmamStoreKeyTobinTax
DefaultPoolUpdateInterval = types.DefaultPoolUpdateInterval
DefaultDailyTerraLiquidityRatio = types.DefaultDailyTerraLiquidityRatio
DefaultTerraLiquidityRatio = types.DefaultTerraLiquidityRatio
DefaultMinSpread = types.DefaultMinSpread
DefaultTobinTax = types.DefaultTobinTax
)
Expand Down
5 changes: 3 additions & 2 deletions x/market/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (k Keeper) ReplenishPools(ctx sdk.Context) {
basePool := k.GetBasePool(ctx)
terraPool := k.GetTerraPool(ctx)

regressionAmt := basePool.QuoInt64(core.BlocksPerDay)
delta := terraPool.Sub(basePool).Abs()
regressionAmt := delta.QuoInt64(k.PoolRecoveryPeriod(ctx))

// Replenish terra pool towards base pool
if terraPool.GT(basePool) {
Expand Down Expand Up @@ -144,7 +145,7 @@ func (k Keeper) UpdatePools(ctx sdk.Context) (sdk.Dec, sdk.Error) {
return sdk.ZeroDec(), err
}

basePool := k.DailyTerraLiquidityRatio(ctx).Mul(baseSupply.Amount)
basePool := k.TerraLiquidityRatio(ctx).Mul(baseSupply.Amount)
k.SetBasePool(ctx, basePool)
k.SetLastUpdateHeight(ctx, ctx.BlockHeight())

Expand Down
7 changes: 5 additions & 2 deletions x/market/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestUpdatePools(t *testing.T) {
input.SupplyKeeper.SetSupply(input.Ctx, supply)

input.MarketKeeper.UpdatePools(input.Ctx)
expectedBasePool := input.MarketKeeper.DailyTerraLiquidityRatio(input.Ctx).MulInt(total.AmountOf(core.MicroLunaDenom))
expectedBasePool := input.MarketKeeper.TerraLiquidityRatio(input.Ctx).MulInt(total.AmountOf(core.MicroLunaDenom))

require.Equal(t, expectedBasePool, input.MarketKeeper.GetBasePool(input.Ctx))
}
Expand All @@ -65,6 +65,9 @@ func TestReplenishPools(t *testing.T) {
input.MarketKeeper.SetTerraPool(input.Ctx, terraPool.Add(diff))

input.MarketKeeper.ReplenishPools(input.Ctx)

terraPool = input.MarketKeeper.GetTerraPool(input.Ctx)
require.Equal(t, basePool, terraPool)
replenishAmt := diff.QuoInt64(input.MarketKeeper.PoolRecoveryPeriod(input.Ctx))
expectedDelta := diff.Sub(replenishAmt)
require.Equal(t, basePool.Add(expectedDelta), terraPool)
}
14 changes: 10 additions & 4 deletions x/market/internal/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func ParamKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&types.Params{})
}

// DailyTerraLiquidityRatio is the ratio of Terra's market cap which will be made available per day
func (k Keeper) DailyTerraLiquidityRatio(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.ParamStoreKeyDailyTerraLiquidityRatio, &res)
// TerraLiquidityRatio is the ratio of Terra's market cap which will be made available per PoolRecoveryPeriod
func (k Keeper) TerraLiquidityRatio(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.ParamStoreKeyTerraLiquidityRatio, &res)
return
}

Expand All @@ -29,7 +29,13 @@ func (k Keeper) PoolUpdateInterval(ctx sdk.Context) (res int64) {
return
}

// TobinTax is a tax on all spot conversions of one TERRA into another TERRA
// PoolRecoveryPeriod is the period required to recover Terra&Luna Pool to BasePool
func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res int64) {
k.paramSpace.Get(ctx, types.ParamStoreKeyPoolRecoveryPeriod, &res)
return
}

// TobinTax is a tax on all spot conversions of one Terra into another Terra
func (k Keeper) TobinTax(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.ParmamStoreKeyTobinTax, &res)
return
Expand Down
12 changes: 6 additions & 6 deletions x/market/internal/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// OfferPool = OfferPool + offerAmt (Fills the swap pool with offerAmt)
// AskPool = AskPool - askAmt (Uses askAmt from the swap pool)
func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk.DecCoin) sdk.Error {
// No delta update in case TERRA to TERRA swap
// No delta update in case Terra to Terra swap
if offerCoin.Denom != core.MicroLunaDenom && askCoin.Denom != core.MicroLunaDenom {
return nil
}
Expand All @@ -28,12 +28,12 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk
return err
}

// In case swapping TERRA to LUNA, the terra swap pool(offer) is increased and the luna swap pool(ask) is decreased
// In case swapping Terra to Luna, the terra swap pool(offer) is increased and the luna swap pool(ask) is decreased
if offerCoin.Denom != core.MicroLunaDenom && askCoin.Denom == core.MicroLunaDenom {
terraPool = terraPool.Add(offerBaseCoin.Amount)
}

// In case swapping LUNA to TERRA, the luna swap pool(offer) is increased and the terra swap pool(ask) is decreased
// In case swapping Luna to Terra, the luna swap pool(offer) is increased and the terra swap pool(ask) is decreased
if offerCoin.Denom == core.MicroLunaDenom && askCoin.Denom != core.MicroLunaDenom {
terraPool = terraPool.Sub(askBaseCoin.Amount)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string
return sdk.DecCoin{}, sdk.Dec{}, err
}

// TERRA->TERRA swap
// Terra->Terra swap
// Apply only tobin tax without constant product spread
if offerCoin.Denom != core.MicroLunaDenom && askDenom != core.MicroLunaDenom {
spread = k.TobinTax(ctx)
Expand All @@ -89,11 +89,11 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string
var offerPool sdk.Dec // base denom(usdr) unit
var askPool sdk.Dec // base denom(usdr) unit
if offerCoin.Denom != core.MicroLunaDenom {
// TERRA->LUNA swap
// Terra->Luna swap
offerPool = terraPool
askPool = lunaPool
} else {
// LUNA->TERRA swap
// Luna->Terra swap
offerPool = lunaPool
askPool = terraPool
}
Expand Down
2 changes: 1 addition & 1 deletion x/market/internal/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestGenesisValidation(t *testing.T) {
genState.Params.PoolUpdateInterval = -1
require.Error(t, ValidateGenesis(genState))

genState.Params.DailyTerraLiquidityRatio = sdk.NewDec(-1)
genState.Params.TerraLiquidityRatio = sdk.NewDec(-1)
require.Error(t, ValidateGenesis(genState))

genState.Params.MinSpread = sdk.NewDec(-1)
Expand Down
50 changes: 30 additions & 20 deletions x/market/internal/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const DefaultParamspace = ModuleName
// Parameter keys
var (
ParamStoreKeyPoolUpdateInterval = []byte("poolupdateinterval")
// Terra's market cap made available per day
ParamStoreKeyDailyTerraLiquidityRatio = []byte("dailyterraliquidityratio")
// The period required to recover Terra&Luna Pool to BasePool
ParamStoreKeyPoolRecoveryPeriod = []byte("poolrecoveryperiod")
// Terra's market cap made available per ${poolrecoveryperiod}
ParamStoreKeyTerraLiquidityRatio = []byte("dailyterraliquidityratio")
// Min spread
ParamStoreKeyMinSpread = []byte("minspread")
// Tobin tax
Expand All @@ -25,29 +27,32 @@ var (

// Default parameter values
var (
DefaultPoolUpdateInterval = core.BlocksPerEpoch // 14,400
DefaultDailyTerraLiquidityRatio = sdk.NewDecWithPrec(1, 2) // 1%
DefaultMinSpread = sdk.NewDecWithPrec(2, 2) // 2%
DefaultTobinTax = sdk.NewDecWithPrec(30, 4) // 0.3%
DefaultPoolUpdateInterval = core.BlocksPerEpoch // 100,400
DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400
DefaultTerraLiquidityRatio = sdk.NewDecWithPrec(1, 2) // 1%
DefaultMinSpread = sdk.NewDecWithPrec(2, 2) // 2%
DefaultTobinTax = sdk.NewDecWithPrec(30, 4) // 0.3%
)

var _ subspace.ParamSet = &Params{}

// Params market parameters
type Params struct {
PoolUpdateInterval int64 `json:"pool_update_interval" yaml:"pool_update_interval"`
DailyTerraLiquidityRatio sdk.Dec `json:"daily_terra_liquidity_ratio" yaml:"daily_terra_liquidity_ratio"`
MinSpread sdk.Dec `json:"min_spread" yaml:"min_spread"`
TobinTax sdk.Dec `json:"tobin_tax" yaml:"tobin_tax"`
PoolUpdateInterval int64 `json:"pool_update_interval" yaml:"pool_update_interval"`
PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"`
TerraLiquidityRatio sdk.Dec `json:"terra_liquidity_ratio" yaml:"terra_liquidity_ratio"`
MinSpread sdk.Dec `json:"min_spread" yaml:"min_spread"`
TobinTax sdk.Dec `json:"tobin_tax" yaml:"tobin_tax"`
}

// DefaultParams creates default market module parameters
func DefaultParams() Params {
return Params{
PoolUpdateInterval: DefaultPoolUpdateInterval,
DailyTerraLiquidityRatio: DefaultDailyTerraLiquidityRatio,
MinSpread: DefaultMinSpread,
TobinTax: DefaultTobinTax,
PoolUpdateInterval: DefaultPoolUpdateInterval,
PoolRecoveryPeriod: DefaultPoolRecoveryPeriod,
TerraLiquidityRatio: DefaultTerraLiquidityRatio,
MinSpread: DefaultMinSpread,
TobinTax: DefaultTobinTax,
}
}

Expand All @@ -56,8 +61,11 @@ func (params Params) Validate() error {
if params.PoolUpdateInterval <= 0 {
return fmt.Errorf("pool update interval should be positive, is %d", params.PoolUpdateInterval)
}
if params.DailyTerraLiquidityRatio.LT(sdk.ZeroDec()) || params.DailyTerraLiquidityRatio.GT(sdk.OneDec()) {
return fmt.Errorf("daily terra liquidity ratio should be a value between [0,1], is %s", params.DailyTerraLiquidityRatio.String())
if params.PoolRecoveryPeriod <= 0 {
return fmt.Errorf("pool recovery period should be positive, is %d", params.PoolRecoveryPeriod)
}
if params.TerraLiquidityRatio.LT(sdk.ZeroDec()) || params.TerraLiquidityRatio.GT(sdk.OneDec()) {
return fmt.Errorf("daily terra liquidity ratio should be a value between [0,1], is %s", params.TerraLiquidityRatio.String())
}
if params.MinSpread.IsNegative() || params.MinSpread.GT(sdk.OneDec()) {
return fmt.Errorf("market minimum spead should be a value between [0,1], is %s", params.MinSpread.String())
Expand All @@ -75,7 +83,8 @@ func (params Params) Validate() error {
func (params *Params) ParamSetPairs() subspace.ParamSetPairs {
return subspace.ParamSetPairs{
{Key: ParamStoreKeyPoolUpdateInterval, Value: &params.PoolUpdateInterval},
{Key: ParamStoreKeyDailyTerraLiquidityRatio, Value: &params.DailyTerraLiquidityRatio},
{Key: ParamStoreKeyPoolRecoveryPeriod, Value: &params.PoolRecoveryPeriod},
{Key: ParamStoreKeyTerraLiquidityRatio, Value: &params.TerraLiquidityRatio},
{Key: ParamStoreKeyMinSpread, Value: &params.MinSpread},
{Key: ParmamStoreKeyTobinTax, Value: &params.TobinTax},
}
Expand All @@ -84,9 +93,10 @@ func (params *Params) ParamSetPairs() subspace.ParamSetPairs {
// String implements fmt.Stringer interface
func (params Params) String() string {
return fmt.Sprintf(`Treasury Params:
PoolUpdateInterval: %d
DailyTerraLiquidityRatio: %s
PoolUpdateInterval: %d
PoolRecoveryPeriod: %d
TerraLiquidityRatio: %s
MinSpread: %s
TobinTax: %s
`, params.PoolUpdateInterval, params.DailyTerraLiquidityRatio, params.MinSpread, params.TobinTax)
`, params.PoolUpdateInterval, params.PoolRecoveryPeriod, params.TerraLiquidityRatio, params.MinSpread, params.TobinTax)
}

0 comments on commit 1a13bf8

Please sign in to comment.