Skip to content

Commit

Permalink
adding changes for burn functionality on spread fee
Browse files Browse the repository at this point in the history
  • Loading branch information
muku314115 committed Nov 29, 2023
1 parent a0a38c3 commit 531dc31
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 65 deletions.
5 changes: 5 additions & 0 deletions proto/terra/market/v1beta1/market.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ message Params {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
bytes fee_burn_ratio = 4 [
(gogoproto.moretags) = "yaml:\"fee_burn_ratio\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
23 changes: 18 additions & 5 deletions x/market/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"

Check failure on line 4 in x/market/keeper/msg_server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
treasury "github.com/classic-terra/core/v2/x/treasury/types"

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

Expand Down Expand Up @@ -121,12 +122,24 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context,
return nil, err
}

// Send swap fee to oracle account
// Send swap fee to oracle account based on percentage, burn the rest
if feeCoin.IsPositive() {
feeCoins := sdk.NewCoins(feeCoin)
err = k.BankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, oracletypes.ModuleName, feeCoins)
if err != nil {
return nil, err
var sendCoins sdk.Coins
burnCoins := sdk.NewCoins(sdk.NewCoin(feeCoin.GetDenom(), sdk.NewDec(feeCoin.Amount.Int64()).Mul(k.FeeBurnRatio(ctx)).TruncateInt()))
if !burnCoins.IsZero() {
sendCoins = sdk.NewCoins(feeCoin.Sub(burnCoins[0]))
err = k.BankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, treasury.BurnModuleName, burnCoins)
if err != nil {
return nil, err
}
} else {
sendCoins = sdk.NewCoins(feeCoin)
}
if !sendCoins.IsZero() {
err = k.BankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, oracletypes.ModuleName, sendCoins)
if err != nil {
return nil, err
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions x/market/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func (k Keeper) MinStabilitySpread(ctx sdk.Context) (res sdk.Dec) {
return
}

// FeeBurnRatio represents the percentage of spread fee burnt during swaps to / from Luna.
func (k Keeper) FeeBurnRatio(ctx sdk.Context) (res sdk.Dec) {
k.paramSpace.Get(ctx, types.KeyFeeBurnRatio, &res)
return
}

// PoolRecoveryPeriod is the period required to recover Terra&Luna Pools to the MintBasePool & BurnBasePool
func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res uint64) {
k.paramSpace.Get(ctx, types.KeyPoolRecoveryPeriod, &res)
Expand Down
66 changes: 53 additions & 13 deletions x/market/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,63 @@ package types
import (
"testing"

"github.com/stretchr/testify/require"

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

func TestGenesisValidation(t *testing.T) {
genState := DefaultGenesisState()
require.NoError(t, ValidateGenesis(genState))

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

Check failure on line 10 in x/market/types/genesis_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
genState = DefaultGenesisState()
genState.Params.PoolRecoveryPeriod = 0
require.Error(t, ValidateGenesis(genState))
tests := []struct {
name string
GenState func() *GenesisState
wantErr bool
}{
{
name: "valid state",
GenState: DefaultGenesisState,
wantErr: false,
},
{
name: "invalid BasePool state",
GenState: func() *GenesisState {
genState := DefaultGenesisState()
genState.Params.BasePool = sdk.NewDec(-1)
return genState
},
wantErr: true,
},
{
name: "invalid PoolRecoveryPeriod state",
GenState: func() *GenesisState {
genState := DefaultGenesisState()
genState.Params.PoolRecoveryPeriod = 0
return genState
},
wantErr: true,
},
{
name: "invalid MinStabilitySpread state",
GenState: func() *GenesisState {
genState := DefaultGenesisState()
genState.Params.MinStabilitySpread = sdk.NewDec(-1)
return genState
},
wantErr: true,
},
{
name: "invalid FeeBurnRatio state",
GenState: func() *GenesisState {
genState := DefaultGenesisState()
genState.Params.FeeBurnRatio = sdk.NewDec(-1)
return genState
},
wantErr: true,
},
}

genState = DefaultGenesisState()
genState.Params.MinStabilitySpread = sdk.NewDec(-1)
require.Error(t, ValidateGenesis(genState))
for _, tt := range tests {
genState := tt.GenState()
if err := ValidateGenesis(genState); (err != nil) != tt.wantErr {
t.Errorf("expected wantErr = %v got %v", tt.wantErr, err != nil)
}
}
}
121 changes: 80 additions & 41 deletions x/market/types/market.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 531dc31

Please sign in to comment.