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

[Feature][market] swap constant product #233

Merged
merged 13 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 2 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ ignore:
- "docs"
- "*.md"
- "*.rst"
- "**/cli"
- "**/rest"
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
app.stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks(), app.oracleKeeper.Hooks()))
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks(), app.oracleKeeper.StakingHooks()))

app.mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
Expand Down Expand Up @@ -198,8 +198,8 @@ func NewTerraApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
// initialized with tokens from genesis accounts.
app.mm.SetOrderInitGenesis(genaccounts.ModuleName, distr.ModuleName,
staking.ModuleName, auth.ModuleName, bank.ModuleName, slashing.ModuleName,
oracle.ModuleName, market.ModuleName, treasury.ModuleName,
supply.ModuleName, crisis.ModuleName, genutil.ModuleName)
supply.ModuleName, oracle.ModuleName, treasury.ModuleName,
market.ModuleName, crisis.ModuleName, genutil.ModuleName)

app.mm.RegisterInvariants(&app.crisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
Expand Down
67 changes: 61 additions & 6 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1497,9 +1497,9 @@ paths:
$ref: "#/definitions/Coin"
500:
description: Internal Server Error
/market/prev_day_issuance:
/market/terra_pool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terra_pool --> terra_swap_pool

get:
summary: Get prev day issuance
summary: Get TERRA pool amount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the current size of the Terra swap pool, denominated in usdr

tags:
- Market
produces:
Expand All @@ -1508,9 +1508,63 @@ paths:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Coin"
type: number
format: float
example: "10000000.00"
400:
description: Bad Request
500:
description: Internal Server Error
/market/luna_pool:
get:
summary: Get LUNA pool amount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the current size of the Luna swap pool, denominated in uluna

tags:
- Market
produces:
- application/json
responses:
200:
description: OK
schema:
type: number
format: float
example: "10000000.00"
400:
description: Bad Request
500:
description: Internal Server Error
/market/base_pool:
get:
summary: Get Base(equilibrium) pool amount
tags:
- Market
produces:
- application/json
responses:
200:
description: OK
schema:
type: number
format: float
example: "10000000.00"
400:
description: Bad Request
500:
description: Internal Server Error
/market/last_update_height:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last_cp_update_height

get:
summary: Get last pool update height
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the last swap pool reset height

tags:
- Market
produces:
- application/json
responses:
200:
description: OK
schema:
type: number
format: integer
example: "1123"
400:
description: Bad Request
500:
Expand Down Expand Up @@ -2958,4 +3012,5 @@ definitions:
gas:
type: number
format: integer
example: 10000
example: "10000"

2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ github.com/google/go-github/v27 v27.0.6/go.mod h1:/0Gr8pJ55COkmv+S/yPKCczSkUPIM/
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I=
Expand Down Expand Up @@ -214,6 +215,7 @@ github.com/tendermint/iavl v0.12.4/go.mod h1:8LHakzt8/0G3/I8FUU0ReNx98S/EP6eyPJk
github.com/tendermint/tendermint v0.32.1/go.mod h1:jmPDAKuNkev9793/ivn/fTBnfpA9mGBww8MPRNPNxnU=
github.com/tendermint/tendermint v0.32.2 h1:FvZWdksfDg/65vKKr5Lgo57keARFnmhrUEXHwyrV1QY=
github.com/tendermint/tendermint v0.32.2/go.mod h1:NwMyx58S8VJ7tEpFKqRVlVWKO9N9zjTHu+Dx96VsnOE=
github.com/tendermint/tendermint v0.32.3 h1:GEnWpGQ795h5oTFNbfBLsY0LW/CW2j6p6HtiYNfxsgg=
github.com/tendermint/tm-db v0.1.1 h1:G3Xezy3sOk9+ekhjZ/kjArYIs1SmwV+1OUgNkj7RgV0=
github.com/tendermint/tm-db v0.1.1/go.mod h1:0cPKWu2Mou3IlxecH+MEUSYc1Ch537alLe6CpFrKzgw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
1 change: 1 addition & 0 deletions x/auth/client/utils/feeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
}
)

// String implements fmt.Stringer interface
func (r EstimateFeeResp) String() string {
return fmt.Sprintf(`EstimateFeeResp
fees: %s,
Expand Down
5 changes: 3 additions & 2 deletions x/auth/internal/types/lazy_vesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s LazySchedule) GetRatio() sdk.Dec {
return s.Ratio
}

// String implements the fmt.Stringer interface
// String implements fmt.Stringer interface
func (s LazySchedule) String() string {
return fmt.Sprintf(`LazySchedule:
StartTime: %v,
Expand Down Expand Up @@ -121,7 +121,7 @@ func (vs VestingSchedule) IsValid() bool {
return sumRatio.Equal(sdk.OneDec())
}

// String implements the fmt.Stringer interface
// String implements fmt.Stringer interface
func (vs VestingSchedule) String() string {
return fmt.Sprintf(`VestingSchedule:
Denom: %v,
Expand Down Expand Up @@ -233,6 +233,7 @@ func (lgva BaseLazyGradedVestingAccount) GetEndTime() int64 {
return 0
}

// String implements fmt.Stringer interface
func (lgva BaseLazyGradedVestingAccount) String() string {
var pubkey string

Expand Down
22 changes: 17 additions & 5 deletions x/market/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package market

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

core "github.com/terra-project/core/types"
Expand All @@ -9,17 +11,27 @@ import (

// EndBlocker is called at the end of every block
func EndBlocker(ctx sdk.Context, k Keeper) {
if !core.IsPeriodLastBlock(ctx, core.BlocksPerDay) {

// Replenishes each pools towards equilibrium
k.ReplenishPools(ctx)

// Update pools at the last block of every interval
// Retry update when inactive state
if !core.IsPeriodLastBlock(ctx, k.PoolUpdateInterval(ctx)) && k.IsMarketActive(ctx) {
return
}

// update luna issuance at last block of a day
updatedIssuance := k.UpdatePrevDayIssuance(ctx)
basePool, err := k.UpdatePools(ctx)

if err != nil {
// TODO - check log level
k.Logger(ctx).Error(fmt.Sprintf("Failed to update BasePool: %s", err))
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventDaliyIssuanceUpdate,
sdk.NewAttribute(types.AttributeKeyIssuance, updatedIssuance.String()),
types.EventPoolUpdate,
sdk.NewAttribute(types.AttributeKeyBasePool, basePool.String()),
),
)
}
52 changes: 46 additions & 6 deletions x/market/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,56 @@ import (
"github.com/terra-project/core/x/market/internal/keeper"
)

func TestOracleThreshold(t *testing.T) {
func TestPoolsUpdate(t *testing.T) {
input := keeper.CreateTestInput(t)
input.OracleKeeper.SetLunaPrice(input.Ctx, core.MicroSDRDenom, sdk.OneDec())

targetIssuance := sdk.NewInt(1000000)
// Update Pools at the non-last block of interval
input.Ctx = input.Ctx.WithBlockHeight(1)
issuance := sdk.NewInt(12345)
supply := input.SupplyKeeper.GetSupply(input.Ctx)
supply = supply.SetTotal(sdk.NewCoins(sdk.NewCoin(core.MicroLunaDenom, targetIssuance)))
supply = supply.SetTotal(sdk.NewCoins(sdk.NewCoin(core.MicroLunaDenom, issuance)))
input.SupplyKeeper.SetSupply(input.Ctx, supply)

input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerDay - 1)
EndBlocker(input.Ctx, input.MarketKeeper)
issuance := input.MarketKeeper.GetPrevDayIssuance(input.Ctx).AmountOf(core.MicroLunaDenom)
require.Equal(t, targetIssuance, issuance)
basePool := input.MarketKeeper.GetBasePool(input.Ctx)
require.Equal(t, input.MarketKeeper.DailyTerraLiquidityRatio(input.Ctx).MulInt(issuance), basePool)

// Update Pools at the last block of interval
input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerEpoch - 1)
issuance = sdk.NewInt(1000000)
supply = input.SupplyKeeper.GetSupply(input.Ctx)
supply = supply.SetTotal(sdk.NewCoins(sdk.NewCoin(core.MicroLunaDenom, issuance)))
input.SupplyKeeper.SetSupply(input.Ctx, supply)

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

// Update Pools at the last block of the another interval
input.Ctx = input.Ctx.WithBlockHeight(core.BlocksPerEpoch*2 - 1)
issuance = sdk.NewInt(10000000000)
supply = input.SupplyKeeper.GetSupply(input.Ctx)
supply = supply.SetTotal(sdk.NewCoins(sdk.NewCoin(core.MicroLunaDenom, issuance)))
input.SupplyKeeper.SetSupply(input.Ctx, supply)

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

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

delta := sdk.NewDec(1000000)
regressionAmt := sdk.NewDec(1)
basePool := regressionAmt.MulInt64(core.BlocksPerDay)
input.MarketKeeper.SetBasePool(input.Ctx, basePool)
input.MarketKeeper.SetTerraPool(input.Ctx, basePool.Sub(delta))

EndBlocker(input.Ctx, input.MarketKeeper)

terraPool := input.MarketKeeper.GetTerraPool(input.Ctx)

require.Equal(t, basePool.Sub(delta).Add(regressionAmt), terraPool)
}
52 changes: 30 additions & 22 deletions x/market/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import (
)

const (
DefaultCodespace = types.DefaultCodespace
CodeInsufficientSwap = types.CodeInsufficientSwap
CodeNoEffectivePrice = types.CodeNoEffectivePrice
CodeRecursiveSwap = types.CodeRecursiveSwap
CodeExceedsSwapLimit = types.CodeExceedsSwapLimit
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
DefaultParamspace = types.DefaultParamspace
QuerySwap = types.QuerySwap
QueryPrevDayIssuance = types.QueryPrevDayIssuance
QueryParameters = types.QueryParameters
DefaultCodespace = types.DefaultCodespace
CodeInsufficientSwap = types.CodeInsufficientSwap
CodeNoEffectivePrice = types.CodeNoEffectivePrice
CodeRecursiveSwap = types.CodeRecursiveSwap
CodeInactive = types.CodeInactive
ModuleName = types.ModuleName
StoreKey = types.StoreKey
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
DefaultParamspace = types.DefaultParamspace
QuerySwap = types.QuerySwap
QueryTerraPool = types.QueryTerraPool
QueryLunaPool = types.QueryLunaPool
QueryBasePool = types.QueryBasePool
QueryLastUpdateHeight = types.QueryLastUpdateHeight
QueryParameters = types.QueryParameters
)

var (
Expand All @@ -32,7 +35,7 @@ var (
ErrNoEffectivePrice = types.ErrNoEffectivePrice
ErrInsufficientSwapCoins = types.ErrInsufficientSwapCoins
ErrRecursiveSwap = types.ErrRecursiveSwap
ErrExceedsDailySwapLimit = types.ErrExceedsDailySwapLimit
ErrInactive = types.ErrInactive
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
Expand All @@ -44,14 +47,19 @@ var (
NewQuerier = keeper.NewQuerier

// variable aliases
ModuleCdc = types.ModuleCdc
PrevDayIssuanceKey = types.PrevDayIssuanceKey
ParamStoreKeyDailyLunaDeltaCap = types.ParamStoreKeyDailyLunaDeltaCap
ParamStoreKeyMaxSwapSpread = types.ParamStoreKeyMaxSwapSpread
ParamStoreKeyMinSwapSpread = types.ParamStoreKeyMinSwapSpread
DefaultDailyLunaDeltaCap = types.DefaultDailyLunaDeltaCap
DefaultMaxSwapSpread = types.DefaultMaxSwapSpread
DefaultMinSwapSpread = types.DefaultMinSwapSpread
ModuleCdc = types.ModuleCdc
BasePoolKey = types.BasePoolKey
LunaPoolKey = types.LunaPoolKey
TerraPoolKey = types.TerraPoolKey
LastUpdateHeightKey = types.LastUpdateHeightKey
ParamStoreKeyPoolUpdateInterval = types.ParamStoreKeyPoolUpdateInterval
ParamStoreKeyDailyTerraLiquidityRatio = types.ParamStoreKeyDailyTerraLiquidityRatio
ParamStoreKeyMinSpread = types.ParamStoreKeyMinSpread
ParmamStoreKeyTobinTax = types.ParmamStoreKeyTobinTax
DefaultPoolUpdateInterval = types.DefaultPoolUpdateInterval
DefaultDailyTerraLiquidityRatio = types.DefaultDailyTerraLiquidityRatio
DefaultMinSpread = types.DefaultMinSpread
DefaultTobinTax = types.DefaultTobinTax
)

type (
Expand Down
Loading