Skip to content

Commit

Permalink
make min spread to 100% to disable swap (#760)
Browse files Browse the repository at this point in the history
* make min spread to 100% to disable swap

* move fork code to app BeginBlocker

* add channel freeze (#761)

* add channel freeze

* save channels

Co-authored-by: Sunny Aggarwal <[email protected]>
  • Loading branch information
yun-yeo and sunnya97 authored May 13, 2022
1 parent 25d6113 commit 1302858
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 27 deletions.
27 changes: 27 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"fmt"
"io"
stdlog "log"
"net/http"
Expand All @@ -24,6 +25,7 @@ import (
ibcclient "github.com/cosmos/ibc-go/modules/core/02-client"
ibcclientclient "github.com/cosmos/ibc-go/modules/core/02-client/client"
ibcclienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types"
ibcchanneltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
ibchost "github.com/cosmos/ibc-go/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/modules/core/keeper"
Expand Down Expand Up @@ -112,6 +114,7 @@ import (
customslashing "github.com/terra-money/core/custom/slashing"
customstaking "github.com/terra-money/core/custom/staking"
customupgrade "github.com/terra-money/core/custom/upgrade"
core "github.com/terra-money/core/types"

"github.com/terra-money/core/x/market"
marketkeeper "github.com/terra-money/core/x/market/keeper"
Expand Down Expand Up @@ -584,6 +587,30 @@ func (app *TerraApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *TerraApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {

if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() == core.SwapDisableForkHeight {
// Make min spread to one to disable swap
params := app.MarketKeeper.GetParams(ctx)
params.MinStabilitySpread = sdk.OneDec()
app.MarketKeeper.SetParams(ctx, params)

// Disable IBC Channels
channelIDs := []string{
"channel-1", // Osmosis
"channel-49", // Crescent
"channel-20", // Juno
}
for _, channelID := range channelIDs {
channel, found := app.IBCKeeper.ChannelKeeper.GetChannel(ctx, ibctransfertypes.PortID, channelID)
if !found {
panic(fmt.Sprintf("%s not found", channelID))
}

channel.State = ibcchanneltypes.CLOSED
app.IBCKeeper.ChannelKeeper.SetChannel(ctx, ibctransfertypes.PortID, channelID, channel)
}
}

return app.mm.BeginBlock(ctx, req)
}

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk=
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
github.com/tendermint/btcd v0.1.1 h1:0VcxPfflS2zZ3RiOAHkBiFUcPvbtRj5O7zHmcJWHV7s=
Expand All @@ -795,6 +796,7 @@ github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RM
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tendermint/tm-db v0.5.1/go.mod h1:g92zWjHpCYlEvQXvy9M168Su8V1IBEeawpXVVBaK4f4=
github.com/tendermint/tm-db v0.6.4/go.mod h1:dptYhIpJ2M5kUuenLr+Yyf3zQOv1SgBZcl8/BmWlMBw=
github.com/tendermint/tm-db v0.6.6 h1:EzhaOfR0bdKyATqcd5PNeyeq8r+V4bRPHBfyFdD9kGM=
github.com/tendermint/tm-db v0.6.6/go.mod h1:wP8d49A85B7/erz/r4YbKssKw6ylsO/hKtFk7E1aWZI=
Expand Down
54 changes: 28 additions & 26 deletions types/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,38 @@ package types

import (
"github.com/terra-money/core/types/assets"
"github.com/terra-money/core/types/fork"
"github.com/terra-money/core/types/util"
)

const (
MicroLunaDenom = assets.MicroLunaDenom
MicroUSDDenom = assets.MicroUSDDenom
MicroKRWDenom = assets.MicroKRWDenom
MicroSDRDenom = assets.MicroSDRDenom
MicroCNYDenom = assets.MicroCNYDenom
MicroJPYDenom = assets.MicroJPYDenom
MicroEURDenom = assets.MicroEURDenom
MicroGBPDenom = assets.MicroGBPDenom
MicroMNTDenom = assets.MicroMNTDenom
MicroUnit = assets.MicroUnit
BlocksPerMinute = util.BlocksPerMinute
BlocksPerHour = util.BlocksPerHour
BlocksPerDay = util.BlocksPerDay
BlocksPerWeek = util.BlocksPerWeek
BlocksPerMonth = util.BlocksPerMonth
BlocksPerYear = util.BlocksPerYear
CoinType = util.CoinType
FullFundraiserPath = util.FullFundraiserPath
Bech32PrefixAccAddr = util.Bech32PrefixAccAddr
Bech32PrefixAccPub = util.Bech32PrefixAccPub
Bech32PrefixValAddr = util.Bech32PrefixValAddr
Bech32PrefixValPub = util.Bech32PrefixValPub
Bech32PrefixConsAddr = util.Bech32PrefixConsAddr
Bech32PrefixConsPub = util.Bech32PrefixConsPub
ColumbusChainID = "columbus-5"
BombayChainID = "bombay-12"
MicroLunaDenom = assets.MicroLunaDenom
MicroUSDDenom = assets.MicroUSDDenom
MicroKRWDenom = assets.MicroKRWDenom
MicroSDRDenom = assets.MicroSDRDenom
MicroCNYDenom = assets.MicroCNYDenom
MicroJPYDenom = assets.MicroJPYDenom
MicroEURDenom = assets.MicroEURDenom
MicroGBPDenom = assets.MicroGBPDenom
MicroMNTDenom = assets.MicroMNTDenom
MicroUnit = assets.MicroUnit
BlocksPerMinute = util.BlocksPerMinute
BlocksPerHour = util.BlocksPerHour
BlocksPerDay = util.BlocksPerDay
BlocksPerWeek = util.BlocksPerWeek
BlocksPerMonth = util.BlocksPerMonth
BlocksPerYear = util.BlocksPerYear
CoinType = util.CoinType
FullFundraiserPath = util.FullFundraiserPath
Bech32PrefixAccAddr = util.Bech32PrefixAccAddr
Bech32PrefixAccPub = util.Bech32PrefixAccPub
Bech32PrefixValAddr = util.Bech32PrefixValAddr
Bech32PrefixValPub = util.Bech32PrefixValPub
Bech32PrefixConsAddr = util.Bech32PrefixConsAddr
Bech32PrefixConsPub = util.Bech32PrefixConsPub
ColumbusChainID = "columbus-5"
BombayChainID = "bombay-12"
SwapDisableForkHeight = fork.SwapDisableForkHeight
)

// functions aliases
Expand Down
4 changes: 4 additions & 0 deletions types/fork/height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package fork

// SwapDisableForkHeight - make min spread to 100% to disable swap
const SwapDisableForkHeight = 7607790
16 changes: 16 additions & 0 deletions x/market/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ func TestMarketFilters(t *testing.T) {
require.NoError(t, err)
}

func TestSwapMsg_FailZeroReturn(t *testing.T) {
input, h := setup(t)

params := input.MarketKeeper.GetParams(input.Ctx)
params.MinStabilitySpread = sdk.OneDec()
input.MarketKeeper.SetParams(input.Ctx, params)

input.Ctx = input.Ctx.WithChainID(core.ColumbusChainID).WithBlockHeight(core.SwapDisableForkHeight)

amt := sdk.NewInt(10)
offerCoin := sdk.NewCoin(core.MicroLunaDenom, amt)
swapMsg := types.NewMsgSwap(keeper.Addrs[0], offerCoin, core.MicroSDRDenom)
_, err := h(input.Ctx, swapMsg)
require.Error(t, err)
}

func TestSwapMsg(t *testing.T) {
input, h := setup(t)

Expand Down
9 changes: 9 additions & 0 deletions x/market/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

core "github.com/terra-money/core/types"
"github.com/terra-money/core/x/market/types"
oracletypes "github.com/terra-money/core/x/oracle/types"
)
Expand Down Expand Up @@ -99,6 +100,14 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context,

// Mint asked coins and credit Trader's account
swapCoin, decimalCoin := swapDecCoin.TruncateDecimal()

// Ensure to fail the swap tx when zero swap coin
if ctx.ChainID() == core.ColumbusChainID && ctx.BlockHeight() >= core.SwapDisableForkHeight {
if !swapCoin.IsPositive() {
return nil, types.ErrZeroSwapCoin
}
}

feeDecCoin = feeDecCoin.Add(decimalCoin) // add truncated decimalCoin to swapFee
feeCoin, _ := feeDecCoin.TruncateDecimal()

Expand Down
2 changes: 1 addition & 1 deletion x/market/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (AppModule) ConsensusVersion() uint64 { return 1 }

// BeginBlock returns the begin blocker for the market module.
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {}

// EndBlock returns the end blocker for the market module.
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
Expand Down
1 change: 1 addition & 0 deletions x/market/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (
var (
ErrRecursiveSwap = sdkerrors.Register(ModuleName, 2, "recursive swap")
ErrNoEffectivePrice = sdkerrors.Register(ModuleName, 3, "no price registered with oracle")
ErrZeroSwapCoin = sdkerrors.Register(ModuleName, 4, "zero swap coin")
)

0 comments on commit 1302858

Please sign in to comment.