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/refactoring #254

Merged
merged 6 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestTerraExport(t *testing.T) {
newTapp := NewTerraApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, 0)
_, _, err := newTapp.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")

_, _, err = newTapp.ExportAppStateAndValidators(true, []string{})
require.NoError(t, err, "ExportAppStateAndValidators for zero height should not have an error")
}

func setGenesis(tapp *TerraApp) error {
Expand Down
11 changes: 11 additions & 0 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

core "github.com/terra-project/core/types"
"github.com/terra-project/core/x/oracle"
"github.com/terra-project/core/x/slashing"
"github.com/terra-project/core/x/staking"
Expand Down Expand Up @@ -197,4 +198,14 @@ func (app *TerraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []s
app.treasuryKeeper.ClearHistoricalIssuance(ctx)
// clear all tax proceeds
app.treasuryKeeper.ClearTaxProceeds(ctx)

// left last tax rate
lastTaxRate := app.treasuryKeeper.GetTaxRate(ctx, core.GetEpoch(ctx))
app.treasuryKeeper.ClearTaxRates(ctx)
app.treasuryKeeper.SetTaxRate(ctx, 0, lastTaxRate)

// left last reward weight
lastRewardWeight := app.treasuryKeeper.GetRewardWeight(ctx, core.GetEpoch(ctx))
app.treasuryKeeper.ClearRewardWeights(ctx)
app.treasuryKeeper.SetRewardWeight(ctx, 0, lastRewardWeight)
}
67 changes: 55 additions & 12 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ paths:
description: Internal Server Error
/market/terra_pool_delta:
get:
summary: Get Terra pool delta, which is the gap between TerraPool and BasePool
summary: Get Terra pool delta, is usdr amount used for swap operation from the TerraPool.
tags:
- Market
produces:
Expand Down Expand Up @@ -2334,6 +2334,53 @@ paths:
description: Bad Request
500:
description: Internal Server Error
/oracle/voters/{validator}/feeder:
post:
summary: Generate oracle feeder right delegation message
tags:
- Oracle
produces:
- application/json
parameters:
- in: path
name: validator
description: Feeder right delegator
required: true
type: string
- in: body
name: feeder right delegation request body
schema:
$ref: "#/definitions/DelegateReq"
responses:
200:
description: OK
schema:
$ref: "#/definitions/StdTx"
400:
description: Bad request
500:
description: Internal Server Error
get:
summary: Get delegated oracle feeder of a validator
tags:
- Oracle
produces:
- application/json
parameters:
- in: path
name: validator
description: Feeder right delegator
required: true
type: string
responses:
200:
description: OK
schema:
$ref: "#/definitions/Address"
400:
description: Bad Request
500:
description: Internal Server Error
/oracle/parameters:
get:
summary: Get oracle params
Expand Down Expand Up @@ -3357,6 +3404,13 @@ definitions:
description: "proof salt was used to make prevote hash; initial prevote does not require this field"
validator:
$ref: "#/definitions/ValidatorAddress"
DelegateReq:
type: object
properties:
base_req:
$ref: "#/definitions/BaseReq"
feeder:
$ref: "#/definitions/Address"
PriceVote:
type: object
properties:
Expand All @@ -3382,17 +3436,6 @@ definitions:
submit_block:
type: number
example: "1"
VotingInfo:
type: object
properties:
address:
$ref: "#/definitions/ValidatorAddress"
start_height:
type: string
index_offset:
type: string
missed_votes_counter:
type: string
OracleParams:
type: object
properties:
Expand Down
41 changes: 41 additions & 0 deletions x/auth/client/rest/multisig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rest

import (
"encoding/hex"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/multisig"
"github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/terra-project/core/app"
)

func TestMultisigPubkey(t *testing.T) {
cdc := app.MakeCodec()

pubkey, err := cdc.MarshalBinaryBare(multisig.NewPubKeyMultisigThreshold(3,
[]crypto.PubKey{
secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(),
}))

require.NoError(t, err)

fmt.Println(hex.EncodeToString(pubkey))

pubkey, err = cdc.MarshalBinaryBare(multisig.NewPubKeyMultisigThreshold(2,
[]crypto.PubKey{
secp256k1.GenPrivKey().PubKey(),
secp256k1.GenPrivKey().PubKey(),
}))

require.NoError(t, err)

fmt.Println(hex.EncodeToString(pubkey))
}
1 change: 0 additions & 1 deletion x/market/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ var (
ErrNoEffectivePrice = types.ErrNoEffectivePrice
ErrInsufficientSwapCoins = types.ErrInsufficientSwapCoins
ErrRecursiveSwap = types.ErrRecursiveSwap
ErrInactive = types.ErrInactive
NewGenesisState = types.NewGenesisState
DefaultGenesisState = types.DefaultGenesisState
ValidateGenesis = types.ValidateGenesis
Expand Down
9 changes: 5 additions & 4 deletions x/market/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
return marketQueryCmd
}

// GetCmdQuerySwap implements the query swap amount command.
// GetCmdQuerySwap implements the query swap simulation result command.
func GetCmdQuerySwap(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "swap [offer-coin] [ask-denom]",
Expand Down Expand Up @@ -72,15 +72,16 @@ $ terracli query query swap 5000000uluna usdr
return cmd
}

// GetCmdQueryTerraPoolDelta implements the query params command.
// GetCmdQueryTerraPoolDelta implements the query terra pool delta command.
func GetCmdQueryTerraPoolDelta(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "terra-pool-delta",
Args: cobra.NoArgs,
Short: "Query terra pool delta",
Long: `Query terra pool delta, which is the gap between TerraPool and BasePool.
Long: `Query terra pool delta, which is usdr amount used for swap operation from the TerraPool.
It can be negative if the market wants more Terra than Luna, and vice versa if the market wants more Luna.

$ terracli query market terra-pool-delta
$ terracli query market terra-pool-delta
`,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
Expand Down
4 changes: 4 additions & 0 deletions x/market/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ func TestSwapMsg(t *testing.T) {
diff := beforeTerraPoolDelta.Sub(afterTerraPoolDelta)
price, _ := input.OracleKeeper.GetLunaPrice(input.Ctx, core.MicroSDRDenom)
require.Equal(t, price.MulInt(amt), diff.Abs())

swapMsg = NewMsgSwap(keeper.Addrs[0], offerCoin, core.MicroLunaDenom)
res = h(input.Ctx, swapMsg)
require.False(t, res.IsOK())
}
17 changes: 4 additions & 13 deletions x/market/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}

// GetTerraPoolDelta returns the gap between TerraPool and BasePool
// GetTerraPoolDelta returns the gap between the TerraPool and the BasePool
func (k Keeper) GetTerraPoolDelta(ctx sdk.Context) (delta sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.TerraPoolDeltaKey)
Expand All @@ -61,7 +61,7 @@ func (k Keeper) GetTerraPoolDelta(ctx sdk.Context) (delta sdk.Dec) {
return
}

// SetTerraPoolDelta updates TerraPoolDelta which is gap between TerraPool and BasePool
// SetTerraPoolDelta updates TerraPoolDelta which is gap between the TerraPool and the BasePool
func (k Keeper) SetTerraPoolDelta(ctx sdk.Context, delta sdk.Dec) {
store := ctx.KVStore(k.storeKey)

Expand All @@ -75,17 +75,8 @@ func (k Keeper) ReplenishPools(ctx sdk.Context) {
regressionAmt := delta.QuoInt64(k.PoolRecoveryPeriod(ctx))

// Replenish terra pool towards base pool
if delta.IsPositive() {
delta = delta.Sub(regressionAmt)
if delta.IsNegative() {
delta = sdk.ZeroDec()
}
} else if delta.IsNegative() {
delta = delta.Add(regressionAmt)
if delta.IsPositive() {
delta = sdk.ZeroDec()
}
}
// regressionAmt cannot make delta zero
delta = delta.Sub(regressionAmt)

k.SetTerraPoolDelta(ctx, delta)
}
12 changes: 12 additions & 0 deletions x/market/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestReplenishPools(t *testing.T) {
terraPoolDelta := input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
require.True(t, terraPoolDelta.IsZero())

// Positive delta
diff := basePool.QuoInt64(core.BlocksPerDay)
input.MarketKeeper.SetTerraPoolDelta(input.Ctx, diff)

Expand All @@ -41,4 +42,15 @@ func TestReplenishPools(t *testing.T) {
replenishAmt := diff.QuoInt64(input.MarketKeeper.PoolRecoveryPeriod(input.Ctx))
expectedDelta := diff.Sub(replenishAmt)
require.Equal(t, expectedDelta, terraPoolDelta)

// Negetive delta
diff = diff.Neg()
input.MarketKeeper.SetTerraPoolDelta(input.Ctx, diff)

input.MarketKeeper.ReplenishPools(input.Ctx)

terraPoolDelta = input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
replenishAmt = diff.QuoInt64(input.MarketKeeper.PoolRecoveryPeriod(input.Ctx))
expectedDelta = diff.Sub(replenishAmt)
require.Equal(t, expectedDelta, terraPoolDelta)
}
2 changes: 1 addition & 1 deletion x/market/internal/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) MinSpread(ctx sdk.Context) (res sdk.Dec) {
return
}

// PoolRecoveryPeriod is the period required to recover Terra&Luna Pool to BasePool
// PoolRecoveryPeriod is the period required to recover Terra&Luna Pools to the BasePool
func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res int64) {
k.paramSpace.Get(ctx, types.ParamStoreKeyPoolRecoveryPeriod, &res)
return
Expand Down
24 changes: 12 additions & 12 deletions x/market/internal/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk

terraPoolDelta := k.GetTerraPoolDelta(ctx)

offerBaseCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom)
if err != nil {
return err
}

askBaseCoin, err := k.ComputeInternalSwap(ctx, askCoin, core.MicroSDRDenom)
if err != nil {
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) must be increased and the luna swap pool(ask) must be decreased
if offerCoin.Denom != core.MicroLunaDenom && askCoin.Denom == core.MicroLunaDenom {
offerBaseCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom)
if err != nil {
return err
}

terraPoolDelta = terraPoolDelta.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) must be increased and the terra swap pool(ask) must be decreased
if offerCoin.Denom == core.MicroLunaDenom && askCoin.Denom != core.MicroLunaDenom {
askBaseCoin, err := k.ComputeInternalSwap(ctx, askCoin, core.MicroSDRDenom)
if err != nil {
return err
}

terraPoolDelta = terraPoolDelta.Sub(askBaseCoin.Amount)
}

Expand Down
22 changes: 18 additions & 4 deletions x/market/internal/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@ func TestApplySwapToPool(t *testing.T) {

offerCoin := sdk.NewCoin(core.MicroLunaDenom, sdk.NewInt(1000))
askCoin := sdk.NewDecCoin(core.MicroSDRDenom, sdk.NewInt(1700))

oldSDRPoolDelta := input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
input.MarketKeeper.ApplySwapToPool(input.Ctx, offerCoin, askCoin)
newSDRPoolDelta := input.MarketKeeper.GetTerraPoolDelta(input.Ctx)

sdrDiff := newSDRPoolDelta.Sub(oldSDRPoolDelta)

require.Equal(t, sdk.NewDec(-1700), sdrDiff)
}

// reverse swap
offerCoin = sdk.NewCoin(core.MicroSDRDenom, sdk.NewInt(1700))
askCoin = sdk.NewDecCoin(core.MicroLunaDenom, sdk.NewInt(1000))
oldSDRPoolDelta = input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
input.MarketKeeper.ApplySwapToPool(input.Ctx, offerCoin, askCoin)
newSDRPoolDelta = input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
sdrDiff = newSDRPoolDelta.Sub(oldSDRPoolDelta)
require.Equal(t, sdk.NewDec(1700), sdrDiff)

// TERRA <> TERRA, no pool changes are expected
offerCoin = sdk.NewCoin(core.MicroSDRDenom, sdk.NewInt(1700))
askCoin = sdk.NewDecCoin(core.MicroKRWDenom, sdk.NewInt(3400))
oldSDRPoolDelta = input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
input.MarketKeeper.ApplySwapToPool(input.Ctx, offerCoin, askCoin)
newSDRPoolDelta = input.MarketKeeper.GetTerraPoolDelta(input.Ctx)
sdrDiff = newSDRPoolDelta.Sub(oldSDRPoolDelta)
require.Equal(t, sdk.NewDec(0), sdrDiff)
}
func TestComputeSwap(t *testing.T) {
input := CreateTestInput(t)

Expand Down
5 changes: 0 additions & 5 deletions x/market/internal/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,3 @@ func ErrInsufficientSwapCoins(codespace sdk.CodespaceType, rval sdk.Int) sdk.Err
func ErrRecursiveSwap(codespace sdk.CodespaceType, denom string) sdk.Error {
return sdk.NewError(codespace, CodeRecursiveSwap, "Can't swap tokens with the same denomination: "+denom)
}

// ErrInactive called when the coin swap exceeds the daily swap limit for Luna
func ErrInactive(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeInactive, "Can't swap because the market is inactive.")
}
2 changes: 1 addition & 1 deletion x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
k.DeletePrice(ctx, activeDenom)
}

// Changes whitelist array to map for fast lookup
// Changes whitelist array to map for the fast lookup
whitelistMap := make(map[string]bool)
for _, denom := range k.Whitelist(ctx) {
whitelistMap[denom] = true
Expand Down
Loading