From 666ba80f213ff6446cb27f063d51d43a1a39fa7e Mon Sep 17 00:00:00 2001 From: igor veras Date: Mon, 10 Jul 2023 16:34:02 -0300 Subject: [PATCH 01/16] max supply coin --- client/docs/swagger-ui/swagger.yaml | 3 + proto/terra/market/v1beta1/market.proto | 5 + x/market/keeper/params.go | 9 ++ x/market/keeper/swap.go | 59 ++++++++++-- x/market/keeper/swap_test.go | 4 +- x/market/legacy/v04/types.go | 7 +- x/market/legacy/v05/migrate.go | 2 + x/market/legacy/v05/migrate_test.go | 120 ++++++++++++++++++++++-- x/market/types/expected_keepers.go | 2 +- x/market/types/market.pb.go | 19 +++- x/market/types/params.go | 44 ++++++++- x/treasury/keeper/indicator.go | 2 +- x/treasury/keeper/policy.go | 2 +- x/treasury/types/exptected_keepers.go | 2 +- 14 files changed, 255 insertions(+), 25 deletions(-) diff --git a/client/docs/swagger-ui/swagger.yaml b/client/docs/swagger-ui/swagger.yaml index a09fe0492..862bc9e6e 100644 --- a/client/docs/swagger-ui/swagger.yaml +++ b/client/docs/swagger-ui/swagger.yaml @@ -55119,6 +55119,9 @@ definitions: min_stability_spread: type: string format: byte + max_supply_coin: + type: string + format: byte description: Params defines the parameters for the market module. terra.market.v1beta1.QueryParamsResponse: type: object diff --git a/proto/terra/market/v1beta1/market.proto b/proto/terra/market/v1beta1/market.proto index 756b89197..7994a7088 100644 --- a/proto/terra/market/v1beta1/market.proto +++ b/proto/terra/market/v1beta1/market.proto @@ -21,4 +21,9 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; + bytes max_supply_coin = 3 [ + (gogoproto.moretags) = "yaml:\"max_supply_coin\"", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin", + (gogoproto.nullable) = false + ]; } diff --git a/x/market/keeper/params.go b/x/market/keeper/params.go index ccfe60025..88f2c8063 100644 --- a/x/market/keeper/params.go +++ b/x/market/keeper/params.go @@ -25,6 +25,15 @@ func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res uint64) { return } +func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res []sdk.Coin) { + k.paramSpace.Get(ctx, types.KeyMaxSupplyCoin, &res) + return +} + +func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin []sdk.Coin) { + k.paramSpace.Set(ctx, types.KeyMaxSupplyCoin, maxSupplyCoin) +} + // GetParams returns the total set of market parameters. func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { k.paramSpace.GetParamSet(ctx, ¶ms) diff --git a/x/market/keeper/swap.go b/x/market/keeper/swap.go index 82c2c78c8..5066cf3fc 100644 --- a/x/market/keeper/swap.go +++ b/x/market/keeper/swap.go @@ -22,7 +22,7 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk // 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) + offerBaseCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom, true) if err != nil { return err } @@ -32,7 +32,7 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk // 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) + askBaseCoin, err := k.ComputeInternalSwap(ctx, askCoin, core.MicroSDRDenom, true) if err != nil { return err } @@ -54,15 +54,15 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string if offerCoin.Denom == askDenom { return sdk.DecCoin{}, sdk.ZeroDec(), sdkerrors.Wrap(types.ErrRecursiveSwap, askDenom) } - + var okVSupply = (offerCoin.Denom == core.MicroLunaDenom) // Swap offer coin to base denom for simplicity of swap process - baseOfferDecCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom) + baseOfferDecCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom, okVSupply) if err != nil { return sdk.DecCoin{}, sdk.Dec{}, err } // Get swap amount based on the oracle price - retDecCoin, err = k.ComputeInternalSwap(ctx, baseOfferDecCoin, askDenom) + retDecCoin, err = k.ComputeInternalSwap(ctx, baseOfferDecCoin, askDenom, false) if err != nil { return sdk.DecCoin{}, sdk.Dec{}, err } @@ -127,13 +127,19 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string spread = minSpread } + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(retDecCoin.Denom, retDecCoin.Amount.TruncateInt())) + + if errSup != nil { + return sdk.DecCoin{}, sdk.ZeroDec(), errSup + } + return retDecCoin, spread, nil } // ComputeInternalSwap returns the amount of asked DecCoin should be returned for a given offerCoin at the effective // exchange rate registered with the oracle. // Different from ComputeSwap, ComputeInternalSwap does not charge a spread as its use is system internal. -func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string) (sdk.DecCoin, error) { +func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string, notCheckSupply bool) (sdk.DecCoin, error) { if offerCoin.Denom == askDenom { return offerCoin, nil } @@ -152,6 +158,16 @@ func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askD if retAmount.LTE(sdk.ZeroDec()) { return sdk.DecCoin{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, offerCoin.String()) } + var ok = "nao" + if notCheckSupply { + ok = "sim" + } + if !notCheckSupply { + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(askDenom, retAmount.TruncateInt())) + if errSup != nil { + return sdk.DecCoin{}, sdkerrors.Wrap(errSup, " --- de onde "+ok+" - "+offerCoin.Denom) + } + } return sdk.NewDecCoinFromDec(askDenom, retAmount), nil } @@ -180,5 +196,36 @@ func (k Keeper) simulateSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom strin } retCoin, _ := swapCoin.TruncateDecimal() + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(retCoin.Denom, retCoin.Amount)) + if errSup != nil { + return sdk.Coin{}, errSup + } return retCoin, nil } +func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { + + var ok, amount = isExists(coin.Denom, k.GetMaxSupplyCoin(ctx)) + var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) + if ok { + if (totalSupply.Amount.Int64() + coin.Amount.TruncateInt().Int64()) > amount.Int64() { + //var decoin = sdk.NewDecCoin(retDecCoin.Denom, amount) + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) + } + } else { + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) + } + return nil +} +func isExists(demom string, coins []sdk.Coin) (result bool, amount sdk.Int) { + result = false + + for _, coin := range coins { + if coin.Denom == demom { + amount = coin.Amount + result = true + break + } + } + + return result, amount +} diff --git a/x/market/keeper/swap_test.go b/x/market/keeper/swap_test.go index 82b885555..7b101d6d3 100644 --- a/x/market/keeper/swap_test.go +++ b/x/market/keeper/swap_test.go @@ -75,13 +75,13 @@ func TestComputeInternalSwap(t *testing.T) { for i := 0; i < 100; i++ { offerCoin := sdk.NewDecCoin(core.MicroSDRDenom, lunaPriceInSDR.MulInt64(rand.Int63()+1).TruncateInt()) - retCoin, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom) + retCoin, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom, false) require.NoError(t, err) require.Equal(t, offerCoin.Amount.Quo(lunaPriceInSDR), retCoin.Amount) } offerCoin := sdk.NewDecCoin(core.MicroSDRDenom, lunaPriceInSDR.QuoInt64(2).TruncateInt()) - _, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom) + _, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom, false) require.Error(t, err) } diff --git a/x/market/legacy/v04/types.go b/x/market/legacy/v04/types.go index 571a40dff..5d1e6f7d7 100644 --- a/x/market/legacy/v04/types.go +++ b/x/market/legacy/v04/types.go @@ -13,9 +13,10 @@ const ( type ( // Params market parameters Params struct { - BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` - PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` - MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` + PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` + MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + MaxSupplyCoin []sdk.Coin `json:"max_supply_coin" yaml:"max_supply_coin"` } // GenesisState is the struct representation of the export genesis diff --git a/x/market/legacy/v05/migrate.go b/x/market/legacy/v05/migrate.go index 60957c28f..6327af8eb 100644 --- a/x/market/legacy/v05/migrate.go +++ b/x/market/legacy/v05/migrate.go @@ -15,12 +15,14 @@ import ( func Migrate( marketGenState v04market.GenesisState, ) *v05market.GenesisState { + return &v05market.GenesisState{ TerraPoolDelta: sdk.ZeroDec(), Params: v05market.Params{ BasePool: marketGenState.Params.BasePool, PoolRecoveryPeriod: uint64(marketGenState.Params.PoolRecoveryPeriod), MinStabilitySpread: marketGenState.Params.MinStabilitySpread, + MaxSupplyCoin: marketGenState.Params.MaxSupplyCoin, }, } } diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index e85a09649..cd0f69ce4 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -17,6 +17,7 @@ import ( ) func TestMigrate(t *testing.T) { + sdk.GetConfig().SetBech32PrefixForAccount(core.Bech32PrefixAccAddr, core.Bech32PrefixAccPub) encodingConfig := app.MakeEncodingConfig() @@ -32,6 +33,29 @@ func TestMigrate(t *testing.T) { BasePool: sdk.NewDec(1000000), PoolRecoveryPeriod: int64(10000), MinStabilitySpread: sdk.NewDecWithPrec(2, 2), + MaxSupplyCoin: []sdk.Coin{ + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000)}, + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000)}, + //{Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + }, }, } @@ -50,13 +74,95 @@ func TestMigrate(t *testing.T) { // Make sure about: // - BasePool to Mint & Burn pool expected := `{ - "params": { - "base_pool": "1000000.000000000000000000", - "min_stability_spread": "0.020000000000000000", - "pool_recovery_period": "10000" - }, - "terra_pool_delta": "0.000000000000000000" -}` + "params": { + "base_pool": "1000000.000000000000000000", + "min_stability_spread": "0.020000000000000000", + "pool_recovery_period": "10000", + "max_supply_coin": [ + { + "Denom": "uluna", + "Amount": "10000000000" + }, + { + "Denom": "usdr", + "Amount": "500000000" + }, + { + "Denom": "uusd", + "Amount": "500000000" + }, + { + "Denom": "ukrw", + "Amount": "500000000" + }, + { + "Denom": "umnt", + "Amount": "500000000" + }, + { + "Denom": "ueur", + "Amount": "500000000" + }, + { + "Denom": "ucny", + "Amount": "500000000" + }, + { + "Denom": "ujpy", + "Amount": "500000000" + }, + { + "Denom": "ugbp", + "Amount": "500000000" + }, + { + "Denom": "uinr", + "Amount": "500000000" + }, + { + "Denom": "ucad", + "Amount": "500000000" + }, + { + "Denom": "uchf", + "Amount": "500000000" + }, + { + "Denom": "uaud", + "Amount": "500000000" + }, + { + "Denom": "usgd", + "Amount": "500000000" + }, + { + "Denom": "uthb", + "Amount": "500000000" + }, + { + "Denom": "usek", + "Amount": "500000000" + }, + { + "Denom": "unok", + "Amount": "500000000" + }, + { + "Denom": "udkk", + "Amount": "500000000" + }, + { + "Denom": "uidr", + "Amount": "500000000" + }, + { + "Denom": "uphp", + "Amount": "500000000" + } + ] + }, + "terra_pool_delta": "0.000000000000000000" + }` assert.JSONEq(t, expected, string(indentedBz)) } diff --git a/x/market/types/expected_keepers.go b/x/market/types/expected_keepers.go index 2ef431d6b..96a7cf4d3 100644 --- a/x/market/types/expected_keepers.go +++ b/x/market/types/expected_keepers.go @@ -17,7 +17,7 @@ type BankKeeper interface { SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - + GetSupply(ctx sdk.Context, denom string) sdk.Coin BurnCoins(ctx sdk.Context, name string, amt sdk.Coins) error MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error diff --git a/x/market/types/market.pb.go b/x/market/types/market.pb.go index 53f7a0481..4ef0fbc08 100644 --- a/x/market/types/market.pb.go +++ b/x/market/types/market.pb.go @@ -12,6 +12,7 @@ import ( github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/gogo/protobuf/proto" + ) // Reference imports to suppress errors if they are not otherwise used. @@ -32,8 +33,10 @@ type Params struct { BasePool github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=base_pool,json=basePool,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"base_pool" yaml:"base_pool"` PoolRecoveryPeriod uint64 `protobuf:"varint,2,opt,name=pool_recovery_period,json=poolRecoveryPeriod,proto3" json:"pool_recovery_period,omitempty" yaml:"pool_recovery_period"` MinStabilitySpread github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=min_stability_spread,json=minStabilitySpread,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"min_stability_spread" yaml:"min_stability_spread"` + MaxSupplyCoin []github_com_cosmos_cosmos_sdk_types.Coin `protobuf:"bytes,4,opt,name=max_supply_coin,json=maxSupplyCoin,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Coin" json:"max_supply_coin" yaml:"max_supply_coin"` } + func (m *Params) Reset() { *m = Params{} } func (*Params) ProtoMessage() {} func (*Params) Descriptor() ([]byte, []int) { @@ -137,9 +140,22 @@ func (this *Params) Equal(that interface{}) bool { if !this.MinStabilitySpread.Equal(that1.MinStabilitySpread) { return false } + // if !this.MaxSupplyCoin.Equal(that1.MaxSupplyCoin) { + // return false + // } + // result := isExists(id, products) return true } - +// func isExists(id github_com_cosmos_cosmos_sdk_types.Coin, coins []github_com_cosmos_cosmos_sdk_types.Coin) (result bool) { +// result = false +// for _, coin := range coins { +// if coin.Id == id { +// result = true +// break +// } +// } +// return result +// } func (m *Params) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -170,6 +186,7 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x1a + if m.PoolRecoveryPeriod != 0 { i = encodeVarintMarket(dAtA, i, uint64(m.PoolRecoveryPeriod)) i-- diff --git a/x/market/types/params.go b/x/market/types/params.go index 5f3ddbe12..8c5f3806e 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -19,13 +19,37 @@ var ( KeyPoolRecoveryPeriod = []byte("PoolRecoveryPeriod") // Min spread KeyMinStabilitySpread = []byte("MinStabilitySpread") + KeyMaxSupplyCoin = []byte("MaxSupplyCoin") ) // Default parameter values var ( DefaultBasePool = sdk.NewDec(1000000 * core.MicroUnit) // 1000,000sdr = 1000,000,000,000usdr DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400 - DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) // 2% + DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) + DefaultMaxSupplyCoin = []sdk.Coin{ + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000)}, + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000)}, + //{Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + } ) var _ paramstypes.ParamSet = &Params{} @@ -36,6 +60,7 @@ func DefaultParams() Params { BasePool: DefaultBasePool, PoolRecoveryPeriod: DefaultPoolRecoveryPeriod, MinStabilitySpread: DefaultMinStabilitySpread, + MaxSupplyCoin: DefaultMaxSupplyCoin, } } @@ -57,6 +82,7 @@ func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs { paramstypes.NewParamSetPair(KeyBasePool, &p.BasePool, validateBasePool), paramstypes.NewParamSetPair(KeyPoolRecoveryPeriod, &p.PoolRecoveryPeriod, validatePoolRecoveryPeriod), paramstypes.NewParamSetPair(KeyMinStabilitySpread, &p.MinStabilitySpread, validateMinStabilitySpread), + paramstypes.NewParamSetPair(KeyMaxSupplyCoin, &p.MaxSupplyCoin, validateMaxSupplyCoin), } } @@ -71,7 +97,9 @@ func (p Params) Validate() error { if p.MinStabilitySpread.IsNegative() || p.MinStabilitySpread.GT(sdk.OneDec()) { return fmt.Errorf("market minimum stability spead should be a value between [0,1], is %s", p.MinStabilitySpread) } - + if len(p.MaxSupplyCoin) == 0 { + return fmt.Errorf("max supplay cannot be empty %s", p.MaxSupplyCoin) + } return nil } @@ -117,3 +145,15 @@ func validateMinStabilitySpread(i interface{}) error { return nil } +func validateMaxSupplyCoin(i interface{}) error { + _, ok := i.([]sdk.Coin) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + // if v[0].IsNegative() { + // return fmt.Errorf("min spread must be positive or zero: %s", v) + // } + + return nil +} diff --git a/x/treasury/keeper/indicator.go b/x/treasury/keeper/indicator.go index 037d81a94..d2a8d7de8 100644 --- a/x/treasury/keeper/indicator.go +++ b/x/treasury/keeper/indicator.go @@ -26,7 +26,7 @@ func (k Keeper) alignCoins(ctx sdk.Context, coins sdk.DecCoins, denom string) (a alignedAmt = sdk.ZeroDec() for _, coinReward := range coins { if coinReward.Denom != denom { - swappedReward, err := k.marketKeeper.ComputeInternalSwap(ctx, coinReward, denom) + swappedReward, err := k.marketKeeper.ComputeInternalSwap(ctx, coinReward, denom, false) if err != nil { continue } diff --git a/x/treasury/keeper/policy.go b/x/treasury/keeper/policy.go index a1190395b..5959b4be6 100644 --- a/x/treasury/keeper/policy.go +++ b/x/treasury/keeper/policy.go @@ -16,7 +16,7 @@ func (k Keeper) UpdateTaxCap(ctx sdk.Context) sdk.Coins { continue } - newDecCap, err := k.marketKeeper.ComputeInternalSwap(ctx, taxPolicyCap, denom.Name) + newDecCap, err := k.marketKeeper.ComputeInternalSwap(ctx, taxPolicyCap, denom.Name, false) if err == nil { newCap, _ := newDecCap.TruncateDecimal() newCaps = append(newCaps, newCap) diff --git a/x/treasury/types/exptected_keepers.go b/x/treasury/types/exptected_keepers.go index 86d27fc75..07895d993 100644 --- a/x/treasury/types/exptected_keepers.go +++ b/x/treasury/types/exptected_keepers.go @@ -25,7 +25,7 @@ type BankKeeper interface { // MarketKeeper expected market keeper type MarketKeeper interface { - ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string) (sdk.DecCoin, error) + ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string, notCheckSupply bool) (sdk.DecCoin, error) } // StakingKeeper expected keeper for staking module From 0419c01ac8d673e411cc17514d8a0c8efbcaf969 Mon Sep 17 00:00:00 2001 From: igor veras Date: Wed, 12 Jul 2023 12:57:25 -0300 Subject: [PATCH 02/16] Feito todos os ajustes confome especificado --- x/market/keeper/msg_server.go | 39 +++++++++++++ x/market/keeper/params.go | 4 +- x/market/keeper/swap.go | 59 ++------------------ x/market/keeper/swap_test.go | 4 +- x/market/legacy/v04/types.go | 8 +-- x/market/legacy/v05/migrate_test.go | 79 ++++++++++++++------------- x/market/types/market.pb.go | 18 +----- x/market/types/params.go | 44 +++++++-------- x/treasury/keeper/indicator.go | 2 +- x/treasury/keeper/policy.go | 2 +- x/treasury/types/exptected_keepers.go | 2 +- 11 files changed, 123 insertions(+), 138 deletions(-) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 550c8c7b8..5a302d197 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -7,6 +7,7 @@ import ( "github.com/classic-terra/core/v2/x/market/types" oracletypes "github.com/classic-terra/core/v2/x/oracle/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) type msgServer struct { @@ -67,6 +68,11 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, return nil, err } + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) + + if errSup != nil { + return nil, errSup + } // Charge a spread if applicable; the spread is burned var feeDecCoin sdk.DecCoin if spread.IsPositive() { @@ -150,3 +156,36 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, SwapFee: feeCoin, }, nil } +func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { + + var ok, amount = isExists(coin.Denom, k.GetMaxSupplyCoin(ctx)) + var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) + if ok { + + if totalSupply.Amount.Add(coin.Amount.TruncateInt()).GT(amount) { + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) + + } + // if (totalSupply.Amount.Int64() + coin.Amount.TruncateInt().Int64()) > amount.Int64() { + // //var decoin = sdk.NewDecCoin(retDecCoin.Denom, amount) + // return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) + + // } + } else { + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) + } + return nil +} +func isExists(demom string, coins sdk.Coins) (result bool, amount sdk.Int) { + result = false + + for _, coin := range coins { + if coin.Denom == demom { + amount = coin.Amount + result = true + break + } + } + + return result, amount +} diff --git a/x/market/keeper/params.go b/x/market/keeper/params.go index 88f2c8063..d14772064 100644 --- a/x/market/keeper/params.go +++ b/x/market/keeper/params.go @@ -25,12 +25,12 @@ func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res uint64) { return } -func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res []sdk.Coin) { +func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res sdk.Coins) { k.paramSpace.Get(ctx, types.KeyMaxSupplyCoin, &res) return } -func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin []sdk.Coin) { +func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin sdk.Coins) { k.paramSpace.Set(ctx, types.KeyMaxSupplyCoin, maxSupplyCoin) } diff --git a/x/market/keeper/swap.go b/x/market/keeper/swap.go index 5066cf3fc..72b85f4a9 100644 --- a/x/market/keeper/swap.go +++ b/x/market/keeper/swap.go @@ -22,7 +22,7 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk // 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, true) + offerBaseCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom) if err != nil { return err } @@ -32,7 +32,7 @@ func (k Keeper) ApplySwapToPool(ctx sdk.Context, offerCoin sdk.Coin, askCoin sdk // 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, true) + askBaseCoin, err := k.ComputeInternalSwap(ctx, askCoin, core.MicroSDRDenom) if err != nil { return err } @@ -54,15 +54,14 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string if offerCoin.Denom == askDenom { return sdk.DecCoin{}, sdk.ZeroDec(), sdkerrors.Wrap(types.ErrRecursiveSwap, askDenom) } - var okVSupply = (offerCoin.Denom == core.MicroLunaDenom) // Swap offer coin to base denom for simplicity of swap process - baseOfferDecCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom, okVSupply) + baseOfferDecCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom) if err != nil { return sdk.DecCoin{}, sdk.Dec{}, err } // Get swap amount based on the oracle price - retDecCoin, err = k.ComputeInternalSwap(ctx, baseOfferDecCoin, askDenom, false) + retDecCoin, err = k.ComputeInternalSwap(ctx, baseOfferDecCoin, askDenom) if err != nil { return sdk.DecCoin{}, sdk.Dec{}, err } @@ -127,19 +126,13 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string spread = minSpread } - var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(retDecCoin.Denom, retDecCoin.Amount.TruncateInt())) - - if errSup != nil { - return sdk.DecCoin{}, sdk.ZeroDec(), errSup - } - return retDecCoin, spread, nil } // ComputeInternalSwap returns the amount of asked DecCoin should be returned for a given offerCoin at the effective // exchange rate registered with the oracle. // Different from ComputeSwap, ComputeInternalSwap does not charge a spread as its use is system internal. -func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string, notCheckSupply bool) (sdk.DecCoin, error) { +func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string) (sdk.DecCoin, error) { if offerCoin.Denom == askDenom { return offerCoin, nil } @@ -158,16 +151,6 @@ func (k Keeper) ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askD if retAmount.LTE(sdk.ZeroDec()) { return sdk.DecCoin{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, offerCoin.String()) } - var ok = "nao" - if notCheckSupply { - ok = "sim" - } - if !notCheckSupply { - var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(askDenom, retAmount.TruncateInt())) - if errSup != nil { - return sdk.DecCoin{}, sdkerrors.Wrap(errSup, " --- de onde "+ok+" - "+offerCoin.Denom) - } - } return sdk.NewDecCoinFromDec(askDenom, retAmount), nil } @@ -196,36 +179,6 @@ func (k Keeper) simulateSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom strin } retCoin, _ := swapCoin.TruncateDecimal() - var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(retCoin.Denom, retCoin.Amount)) - if errSup != nil { - return sdk.Coin{}, errSup - } - return retCoin, nil -} -func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { - - var ok, amount = isExists(coin.Denom, k.GetMaxSupplyCoin(ctx)) - var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) - if ok { - if (totalSupply.Amount.Int64() + coin.Amount.TruncateInt().Int64()) > amount.Int64() { - //var decoin = sdk.NewDecCoin(retDecCoin.Denom, amount) - return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) - } - } else { - return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) - } - return nil -} -func isExists(demom string, coins []sdk.Coin) (result bool, amount sdk.Int) { - result = false - - for _, coin := range coins { - if coin.Denom == demom { - amount = coin.Amount - result = true - break - } - } - return result, amount + return retCoin, nil } diff --git a/x/market/keeper/swap_test.go b/x/market/keeper/swap_test.go index 7b101d6d3..82b885555 100644 --- a/x/market/keeper/swap_test.go +++ b/x/market/keeper/swap_test.go @@ -75,13 +75,13 @@ func TestComputeInternalSwap(t *testing.T) { for i := 0; i < 100; i++ { offerCoin := sdk.NewDecCoin(core.MicroSDRDenom, lunaPriceInSDR.MulInt64(rand.Int63()+1).TruncateInt()) - retCoin, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom, false) + retCoin, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom) require.NoError(t, err) require.Equal(t, offerCoin.Amount.Quo(lunaPriceInSDR), retCoin.Amount) } offerCoin := sdk.NewDecCoin(core.MicroSDRDenom, lunaPriceInSDR.QuoInt64(2).TruncateInt()) - _, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom, false) + _, err := input.MarketKeeper.ComputeInternalSwap(input.Ctx, offerCoin, core.MicroLunaDenom) require.Error(t, err) } diff --git a/x/market/legacy/v04/types.go b/x/market/legacy/v04/types.go index 5d1e6f7d7..7e5f27d53 100644 --- a/x/market/legacy/v04/types.go +++ b/x/market/legacy/v04/types.go @@ -13,10 +13,10 @@ const ( type ( // Params market parameters Params struct { - BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` - PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` - MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` - MaxSupplyCoin []sdk.Coin `json:"max_supply_coin" yaml:"max_supply_coin"` + BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` + PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` + MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + MaxSupplyCoin sdk.Coins `json:"max_supply_coin" yaml:"max_supply_coin"` } // GenesisState is the struct representation of the export genesis diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index cd0f69ce4..77ca24946 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -33,28 +33,29 @@ func TestMigrate(t *testing.T) { BasePool: sdk.NewDec(1000000), PoolRecoveryPeriod: int64(10000), MinStabilitySpread: sdk.NewDecWithPrec(2, 2), - MaxSupplyCoin: []sdk.Coin{ - {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, - {Denom: "usdr", Amount: sdk.NewInt(500000000)}, - {Denom: "uusd", Amount: sdk.NewInt(500000000)}, - {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, - {Denom: "umnt", Amount: sdk.NewInt(500000000)}, - {Denom: "ueur", Amount: sdk.NewInt(500000000)}, - {Denom: "ucny", Amount: sdk.NewInt(500000000)}, - {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, - {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, - {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + MaxSupplyCoin: sdk.Coins{ + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, {Denom: "ucad", Amount: sdk.NewInt(500000000)}, {Denom: "uchf", Amount: sdk.NewInt(500000000)}, - {Denom: "uaud", Amount: sdk.NewInt(500000000)}, - {Denom: "usgd", Amount: sdk.NewInt(500000000)}, - {Denom: "uthb", Amount: sdk.NewInt(500000000)}, - {Denom: "usek", Amount: sdk.NewInt(500000000)}, - {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, {Denom: "uphp", Amount: sdk.NewInt(500000000)}, - //{Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, }, }, } @@ -80,23 +81,23 @@ func TestMigrate(t *testing.T) { "pool_recovery_period": "10000", "max_supply_coin": [ { - "Denom": "uluna", - "Amount": "10000000000" + "Denom": "uaud", + "Amount": "500000000" }, { - "Denom": "usdr", + "Denom": "ucad", "Amount": "500000000" }, { - "Denom": "uusd", + "Denom": "uchf", "Amount": "500000000" }, { - "Denom": "ukrw", + "Denom": "ucny", "Amount": "500000000" }, { - "Denom": "umnt", + "Denom": "udkk", "Amount": "500000000" }, { @@ -104,15 +105,15 @@ func TestMigrate(t *testing.T) { "Amount": "500000000" }, { - "Denom": "ucny", + "Denom": "ugbp", "Amount": "500000000" }, { - "Denom": "ujpy", - "Amount": "500000000" + "Denom": "uhkd", + "Amount": "500000000" }, { - "Denom": "ugbp", + "Denom": "uidr", "Amount": "500000000" }, { @@ -120,43 +121,47 @@ func TestMigrate(t *testing.T) { "Amount": "500000000" }, { - "Denom": "ucad", + "Denom": "ujpy", "Amount": "500000000" }, { - "Denom": "uchf", + "Denom": "ukrw", "Amount": "500000000" }, { - "Denom": "uaud", + "Denom": "uluna", + "Amount": "10000000000" + }, + { + "Denom": "umnt", "Amount": "500000000" }, { - "Denom": "usgd", + "Denom": "unok", "Amount": "500000000" }, { - "Denom": "uthb", + "Denom": "uphp", "Amount": "500000000" }, { - "Denom": "usek", + "Denom": "usdr", "Amount": "500000000" }, { - "Denom": "unok", + "Denom": "usek", "Amount": "500000000" }, { - "Denom": "udkk", + "Denom": "usgd", "Amount": "500000000" }, { - "Denom": "uidr", + "Denom": "uthb", "Amount": "500000000" }, { - "Denom": "uphp", + "Denom": "uusd", "Amount": "500000000" } ] diff --git a/x/market/types/market.pb.go b/x/market/types/market.pb.go index 4ef0fbc08..30502517f 100644 --- a/x/market/types/market.pb.go +++ b/x/market/types/market.pb.go @@ -33,7 +33,7 @@ type Params struct { BasePool github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=base_pool,json=basePool,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"base_pool" yaml:"base_pool"` PoolRecoveryPeriod uint64 `protobuf:"varint,2,opt,name=pool_recovery_period,json=poolRecoveryPeriod,proto3" json:"pool_recovery_period,omitempty" yaml:"pool_recovery_period"` MinStabilitySpread github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=min_stability_spread,json=minStabilitySpread,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"min_stability_spread" yaml:"min_stability_spread"` - MaxSupplyCoin []github_com_cosmos_cosmos_sdk_types.Coin `protobuf:"bytes,4,opt,name=max_supply_coin,json=maxSupplyCoin,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Coin" json:"max_supply_coin" yaml:"max_supply_coin"` + MaxSupplyCoin github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,opt,name=max_supply_coin,json=maxSupplyCoin,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Coin" json:"max_supply_coin" yaml:"max_supply_coin"` } @@ -140,22 +140,10 @@ func (this *Params) Equal(that interface{}) bool { if !this.MinStabilitySpread.Equal(that1.MinStabilitySpread) { return false } - // if !this.MaxSupplyCoin.Equal(that1.MaxSupplyCoin) { - // return false - // } - // result := isExists(id, products) + return true } -// func isExists(id github_com_cosmos_cosmos_sdk_types.Coin, coins []github_com_cosmos_cosmos_sdk_types.Coin) (result bool) { -// result = false -// for _, coin := range coins { -// if coin.Id == id { -// result = true -// break -// } -// } -// return result -// } + func (m *Params) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) diff --git a/x/market/types/params.go b/x/market/types/params.go index 8c5f3806e..e98a643d2 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -27,28 +27,29 @@ var ( DefaultBasePool = sdk.NewDec(1000000 * core.MicroUnit) // 1000,000sdr = 1000,000,000,000usdr DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400 DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) - DefaultMaxSupplyCoin = []sdk.Coin{ - {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, - {Denom: "usdr", Amount: sdk.NewInt(500000000)}, - {Denom: "uusd", Amount: sdk.NewInt(500000000)}, - {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, - {Denom: "umnt", Amount: sdk.NewInt(500000000)}, - {Denom: "ueur", Amount: sdk.NewInt(500000000)}, - {Denom: "ucny", Amount: sdk.NewInt(500000000)}, - {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, - {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, - {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + DefaultMaxSupplyCoin = sdk.Coins{ + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, {Denom: "ucad", Amount: sdk.NewInt(500000000)}, {Denom: "uchf", Amount: sdk.NewInt(500000000)}, - {Denom: "uaud", Amount: sdk.NewInt(500000000)}, - {Denom: "usgd", Amount: sdk.NewInt(500000000)}, - {Denom: "uthb", Amount: sdk.NewInt(500000000)}, - {Denom: "usek", Amount: sdk.NewInt(500000000)}, - {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, {Denom: "uphp", Amount: sdk.NewInt(500000000)}, - //{Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, } ) @@ -146,14 +147,13 @@ func validateMinStabilitySpread(i interface{}) error { return nil } func validateMaxSupplyCoin(i interface{}) error { - _, ok := i.([]sdk.Coin) + v, ok := i.(sdk.Coins) if !ok { return fmt.Errorf("invalid parameter type: %T", i) } - - // if v[0].IsNegative() { - // return fmt.Errorf("min spread must be positive or zero: %s", v) - // } + if !v.IsValid() { + return fmt.Errorf("invalid max supply: %s", v) + } return nil } diff --git a/x/treasury/keeper/indicator.go b/x/treasury/keeper/indicator.go index d2a8d7de8..037d81a94 100644 --- a/x/treasury/keeper/indicator.go +++ b/x/treasury/keeper/indicator.go @@ -26,7 +26,7 @@ func (k Keeper) alignCoins(ctx sdk.Context, coins sdk.DecCoins, denom string) (a alignedAmt = sdk.ZeroDec() for _, coinReward := range coins { if coinReward.Denom != denom { - swappedReward, err := k.marketKeeper.ComputeInternalSwap(ctx, coinReward, denom, false) + swappedReward, err := k.marketKeeper.ComputeInternalSwap(ctx, coinReward, denom) if err != nil { continue } diff --git a/x/treasury/keeper/policy.go b/x/treasury/keeper/policy.go index 5959b4be6..a1190395b 100644 --- a/x/treasury/keeper/policy.go +++ b/x/treasury/keeper/policy.go @@ -16,7 +16,7 @@ func (k Keeper) UpdateTaxCap(ctx sdk.Context) sdk.Coins { continue } - newDecCap, err := k.marketKeeper.ComputeInternalSwap(ctx, taxPolicyCap, denom.Name, false) + newDecCap, err := k.marketKeeper.ComputeInternalSwap(ctx, taxPolicyCap, denom.Name) if err == nil { newCap, _ := newDecCap.TruncateDecimal() newCaps = append(newCaps, newCap) diff --git a/x/treasury/types/exptected_keepers.go b/x/treasury/types/exptected_keepers.go index 07895d993..86d27fc75 100644 --- a/x/treasury/types/exptected_keepers.go +++ b/x/treasury/types/exptected_keepers.go @@ -25,7 +25,7 @@ type BankKeeper interface { // MarketKeeper expected market keeper type MarketKeeper interface { - ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string, notCheckSupply bool) (sdk.DecCoin, error) + ComputeInternalSwap(ctx sdk.Context, offerCoin sdk.DecCoin, askDenom string) (sdk.DecCoin, error) } // StakingKeeper expected keeper for staking module From d2c0b51e7ed89c474581775afc889cd17e9d0251 Mon Sep 17 00:00:00 2001 From: igorv43 <139166460+igorv43@users.noreply.github.com> Date: Thu, 13 Jul 2023 09:22:15 -0300 Subject: [PATCH 03/16] feat: supply cap for market swaps (#305) Authored-by: igor veras --- client/docs/swagger-ui/swagger.yaml | 3 + proto/terra/market/v1beta1/market.proto | 5 + x/market/keeper/msg_server.go | 39 ++++++++ x/market/keeper/params.go | 9 ++ x/market/keeper/swap.go | 2 +- x/market/legacy/v04/types.go | 7 +- x/market/legacy/v05/migrate.go | 2 + x/market/legacy/v05/migrate_test.go | 125 ++++++++++++++++++++++-- x/market/types/expected_keepers.go | 2 +- x/market/types/market.pb.go | 5 + x/market/types/params.go | 44 ++++++++- 11 files changed, 229 insertions(+), 14 deletions(-) diff --git a/client/docs/swagger-ui/swagger.yaml b/client/docs/swagger-ui/swagger.yaml index a09fe0492..862bc9e6e 100644 --- a/client/docs/swagger-ui/swagger.yaml +++ b/client/docs/swagger-ui/swagger.yaml @@ -55119,6 +55119,9 @@ definitions: min_stability_spread: type: string format: byte + max_supply_coin: + type: string + format: byte description: Params defines the parameters for the market module. terra.market.v1beta1.QueryParamsResponse: type: object diff --git a/proto/terra/market/v1beta1/market.proto b/proto/terra/market/v1beta1/market.proto index 756b89197..7994a7088 100644 --- a/proto/terra/market/v1beta1/market.proto +++ b/proto/terra/market/v1beta1/market.proto @@ -21,4 +21,9 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; + bytes max_supply_coin = 3 [ + (gogoproto.moretags) = "yaml:\"max_supply_coin\"", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin", + (gogoproto.nullable) = false + ]; } diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 550c8c7b8..5a302d197 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -7,6 +7,7 @@ import ( "github.com/classic-terra/core/v2/x/market/types" oracletypes "github.com/classic-terra/core/v2/x/oracle/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) type msgServer struct { @@ -67,6 +68,11 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, return nil, err } + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) + + if errSup != nil { + return nil, errSup + } // Charge a spread if applicable; the spread is burned var feeDecCoin sdk.DecCoin if spread.IsPositive() { @@ -150,3 +156,36 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, SwapFee: feeCoin, }, nil } +func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { + + var ok, amount = isExists(coin.Denom, k.GetMaxSupplyCoin(ctx)) + var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) + if ok { + + if totalSupply.Amount.Add(coin.Amount.TruncateInt()).GT(amount) { + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) + + } + // if (totalSupply.Amount.Int64() + coin.Amount.TruncateInt().Int64()) > amount.Int64() { + // //var decoin = sdk.NewDecCoin(retDecCoin.Denom, amount) + // return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) + + // } + } else { + return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) + } + return nil +} +func isExists(demom string, coins sdk.Coins) (result bool, amount sdk.Int) { + result = false + + for _, coin := range coins { + if coin.Denom == demom { + amount = coin.Amount + result = true + break + } + } + + return result, amount +} diff --git a/x/market/keeper/params.go b/x/market/keeper/params.go index ccfe60025..d14772064 100644 --- a/x/market/keeper/params.go +++ b/x/market/keeper/params.go @@ -25,6 +25,15 @@ func (k Keeper) PoolRecoveryPeriod(ctx sdk.Context) (res uint64) { return } +func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res sdk.Coins) { + k.paramSpace.Get(ctx, types.KeyMaxSupplyCoin, &res) + return +} + +func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin sdk.Coins) { + k.paramSpace.Set(ctx, types.KeyMaxSupplyCoin, maxSupplyCoin) +} + // GetParams returns the total set of market parameters. func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { k.paramSpace.GetParamSet(ctx, ¶ms) diff --git a/x/market/keeper/swap.go b/x/market/keeper/swap.go index 82c2c78c8..72b85f4a9 100644 --- a/x/market/keeper/swap.go +++ b/x/market/keeper/swap.go @@ -54,7 +54,6 @@ func (k Keeper) ComputeSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom string if offerCoin.Denom == askDenom { return sdk.DecCoin{}, sdk.ZeroDec(), sdkerrors.Wrap(types.ErrRecursiveSwap, askDenom) } - // Swap offer coin to base denom for simplicity of swap process baseOfferDecCoin, err := k.ComputeInternalSwap(ctx, sdk.NewDecCoinFromCoin(offerCoin), core.MicroSDRDenom) if err != nil { @@ -180,5 +179,6 @@ func (k Keeper) simulateSwap(ctx sdk.Context, offerCoin sdk.Coin, askDenom strin } retCoin, _ := swapCoin.TruncateDecimal() + return retCoin, nil } diff --git a/x/market/legacy/v04/types.go b/x/market/legacy/v04/types.go index 571a40dff..7e5f27d53 100644 --- a/x/market/legacy/v04/types.go +++ b/x/market/legacy/v04/types.go @@ -13,9 +13,10 @@ const ( type ( // Params market parameters Params struct { - BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` - PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` - MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` + PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` + MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + MaxSupplyCoin sdk.Coins `json:"max_supply_coin" yaml:"max_supply_coin"` } // GenesisState is the struct representation of the export genesis diff --git a/x/market/legacy/v05/migrate.go b/x/market/legacy/v05/migrate.go index 60957c28f..6327af8eb 100644 --- a/x/market/legacy/v05/migrate.go +++ b/x/market/legacy/v05/migrate.go @@ -15,12 +15,14 @@ import ( func Migrate( marketGenState v04market.GenesisState, ) *v05market.GenesisState { + return &v05market.GenesisState{ TerraPoolDelta: sdk.ZeroDec(), Params: v05market.Params{ BasePool: marketGenState.Params.BasePool, PoolRecoveryPeriod: uint64(marketGenState.Params.PoolRecoveryPeriod), MinStabilitySpread: marketGenState.Params.MinStabilitySpread, + MaxSupplyCoin: marketGenState.Params.MaxSupplyCoin, }, } } diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index e85a09649..77ca24946 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -17,6 +17,7 @@ import ( ) func TestMigrate(t *testing.T) { + sdk.GetConfig().SetBech32PrefixForAccount(core.Bech32PrefixAccAddr, core.Bech32PrefixAccPub) encodingConfig := app.MakeEncodingConfig() @@ -32,6 +33,30 @@ func TestMigrate(t *testing.T) { BasePool: sdk.NewDec(1000000), PoolRecoveryPeriod: int64(10000), MinStabilitySpread: sdk.NewDecWithPrec(2, 2), + //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + MaxSupplyCoin: sdk.Coins{ + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, + }, }, } @@ -50,13 +75,99 @@ func TestMigrate(t *testing.T) { // Make sure about: // - BasePool to Mint & Burn pool expected := `{ - "params": { - "base_pool": "1000000.000000000000000000", - "min_stability_spread": "0.020000000000000000", - "pool_recovery_period": "10000" - }, - "terra_pool_delta": "0.000000000000000000" -}` + "params": { + "base_pool": "1000000.000000000000000000", + "min_stability_spread": "0.020000000000000000", + "pool_recovery_period": "10000", + "max_supply_coin": [ + { + "Denom": "uaud", + "Amount": "500000000" + }, + { + "Denom": "ucad", + "Amount": "500000000" + }, + { + "Denom": "uchf", + "Amount": "500000000" + }, + { + "Denom": "ucny", + "Amount": "500000000" + }, + { + "Denom": "udkk", + "Amount": "500000000" + }, + { + "Denom": "ueur", + "Amount": "500000000" + }, + { + "Denom": "ugbp", + "Amount": "500000000" + }, + { + "Denom": "uhkd", + "Amount": "500000000" + }, + { + "Denom": "uidr", + "Amount": "500000000" + }, + { + "Denom": "uinr", + "Amount": "500000000" + }, + { + "Denom": "ujpy", + "Amount": "500000000" + }, + { + "Denom": "ukrw", + "Amount": "500000000" + }, + { + "Denom": "uluna", + "Amount": "10000000000" + }, + { + "Denom": "umnt", + "Amount": "500000000" + }, + { + "Denom": "unok", + "Amount": "500000000" + }, + { + "Denom": "uphp", + "Amount": "500000000" + }, + { + "Denom": "usdr", + "Amount": "500000000" + }, + { + "Denom": "usek", + "Amount": "500000000" + }, + { + "Denom": "usgd", + "Amount": "500000000" + }, + { + "Denom": "uthb", + "Amount": "500000000" + }, + { + "Denom": "uusd", + "Amount": "500000000" + } + ] + }, + "terra_pool_delta": "0.000000000000000000" + }` assert.JSONEq(t, expected, string(indentedBz)) } diff --git a/x/market/types/expected_keepers.go b/x/market/types/expected_keepers.go index 2ef431d6b..96a7cf4d3 100644 --- a/x/market/types/expected_keepers.go +++ b/x/market/types/expected_keepers.go @@ -17,7 +17,7 @@ type BankKeeper interface { SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error - + GetSupply(ctx sdk.Context, denom string) sdk.Coin BurnCoins(ctx sdk.Context, name string, amt sdk.Coins) error MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error diff --git a/x/market/types/market.pb.go b/x/market/types/market.pb.go index 53f7a0481..30502517f 100644 --- a/x/market/types/market.pb.go +++ b/x/market/types/market.pb.go @@ -12,6 +12,7 @@ import ( github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/gogo/protobuf/proto" + ) // Reference imports to suppress errors if they are not otherwise used. @@ -32,8 +33,10 @@ type Params struct { BasePool github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=base_pool,json=basePool,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"base_pool" yaml:"base_pool"` PoolRecoveryPeriod uint64 `protobuf:"varint,2,opt,name=pool_recovery_period,json=poolRecoveryPeriod,proto3" json:"pool_recovery_period,omitempty" yaml:"pool_recovery_period"` MinStabilitySpread github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=min_stability_spread,json=minStabilitySpread,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"min_stability_spread" yaml:"min_stability_spread"` + MaxSupplyCoin github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,opt,name=max_supply_coin,json=maxSupplyCoin,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Coin" json:"max_supply_coin" yaml:"max_supply_coin"` } + func (m *Params) Reset() { *m = Params{} } func (*Params) ProtoMessage() {} func (*Params) Descriptor() ([]byte, []int) { @@ -137,6 +140,7 @@ func (this *Params) Equal(that interface{}) bool { if !this.MinStabilitySpread.Equal(that1.MinStabilitySpread) { return false } + return true } @@ -170,6 +174,7 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x1a + if m.PoolRecoveryPeriod != 0 { i = encodeVarintMarket(dAtA, i, uint64(m.PoolRecoveryPeriod)) i-- diff --git a/x/market/types/params.go b/x/market/types/params.go index 5f3ddbe12..e98a643d2 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -19,13 +19,38 @@ var ( KeyPoolRecoveryPeriod = []byte("PoolRecoveryPeriod") // Min spread KeyMinStabilitySpread = []byte("MinStabilitySpread") + KeyMaxSupplyCoin = []byte("MaxSupplyCoin") ) // Default parameter values var ( DefaultBasePool = sdk.NewDec(1000000 * core.MicroUnit) // 1000,000sdr = 1000,000,000,000usdr DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400 - DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) // 2% + DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) + //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + DefaultMaxSupplyCoin = sdk.Coins{ + {Denom: "uaud", Amount: sdk.NewInt(500000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, + {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000)}, + } ) var _ paramstypes.ParamSet = &Params{} @@ -36,6 +61,7 @@ func DefaultParams() Params { BasePool: DefaultBasePool, PoolRecoveryPeriod: DefaultPoolRecoveryPeriod, MinStabilitySpread: DefaultMinStabilitySpread, + MaxSupplyCoin: DefaultMaxSupplyCoin, } } @@ -57,6 +83,7 @@ func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs { paramstypes.NewParamSetPair(KeyBasePool, &p.BasePool, validateBasePool), paramstypes.NewParamSetPair(KeyPoolRecoveryPeriod, &p.PoolRecoveryPeriod, validatePoolRecoveryPeriod), paramstypes.NewParamSetPair(KeyMinStabilitySpread, &p.MinStabilitySpread, validateMinStabilitySpread), + paramstypes.NewParamSetPair(KeyMaxSupplyCoin, &p.MaxSupplyCoin, validateMaxSupplyCoin), } } @@ -71,7 +98,9 @@ func (p Params) Validate() error { if p.MinStabilitySpread.IsNegative() || p.MinStabilitySpread.GT(sdk.OneDec()) { return fmt.Errorf("market minimum stability spead should be a value between [0,1], is %s", p.MinStabilitySpread) } - + if len(p.MaxSupplyCoin) == 0 { + return fmt.Errorf("max supplay cannot be empty %s", p.MaxSupplyCoin) + } return nil } @@ -117,3 +146,14 @@ func validateMinStabilitySpread(i interface{}) error { return nil } +func validateMaxSupplyCoin(i interface{}) error { + v, ok := i.(sdk.Coins) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + if !v.IsValid() { + return fmt.Errorf("invalid max supply: %s", v) + } + + return nil +} From e04dab29fb788963ded555026de7554e87790157 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 09:36:04 -0300 Subject: [PATCH 04/16] Reduce the maximum supply cap by a percentage determined by governance. --- client/docs/swagger-ui/swagger.yaml | 3 + contrib/terra-operator/entrypoint.sh | 2 +- proto/terra/market/v1beta1/market.proto | 5 ++ x/market/keeper/keeper.go | 26 +++++++ x/market/keeper/msg_server.go | 32 +++++++-- x/market/keeper/params.go | 4 ++ x/market/legacy/v04/types.go | 9 +-- x/market/legacy/v05/migrate.go | 9 +-- x/market/legacy/v05/migrate_test.go | 90 +++++++++++++------------ x/market/types/market.pb.go | 48 ++++++++++++- x/market/types/params.go | 75 +++++++++++++-------- 11 files changed, 215 insertions(+), 88 deletions(-) diff --git a/client/docs/swagger-ui/swagger.yaml b/client/docs/swagger-ui/swagger.yaml index 862bc9e6e..d65190f96 100644 --- a/client/docs/swagger-ui/swagger.yaml +++ b/client/docs/swagger-ui/swagger.yaml @@ -55122,6 +55122,9 @@ definitions: max_supply_coin: type: string format: byte + percentage_supply_max_descending: + type: string + format: byte description: Params defines the parameters for the market module. terra.market.v1beta1.QueryParamsResponse: type: object diff --git a/contrib/terra-operator/entrypoint.sh b/contrib/terra-operator/entrypoint.sh index 7ddbd3eeb..8ad440161 100755 --- a/contrib/terra-operator/entrypoint.sh +++ b/contrib/terra-operator/entrypoint.sh @@ -4,7 +4,7 @@ DATADIR="${DATADIR:-/terra/.terra/data}" MONIKER="${MONIKER:-docker-node}" ENABLE_LCD="${ENABLE_LCD:-true}" -MINIMUM_GAS_PRICES=${MINIMUM_GAS_PRICES-0.01133uluna,0.15uusd,0.104938usdr,169.77ukrw,428.571umnt,0.125ueur,0.98ucny,16.37ujpy,0.11ugbp,10.88uinr,0.19ucad,0.14uchf,0.19uaud,0.2usgd,4.62uthb,1.25usek,1.25unok,0.9udkk,2180.0uidr,7.6uphp,1.17uhkd} +MINIMUM_GAS_PRICES=${MINIMUM_GAS_PRICES-0.01133uluna,0.15uusd,0.104938usdr,169.77ukrw,428.571umnt,0.125ueur,0.98ucny,16.37ujpy,0.11ugbp,10.88uinr,0.19ucad,0.14uchf,0.19uaud,0.2usgd,4.62uthb,1.25usek,1.25unok,0.9udkk,2180.0uidr,7.6uphp,1.17uhkd,0.01uivcs} SNAPSHOT_NAME="${SNAPSHOT_NAME}" SNAPSHOT_BASE_URL="${SNAPSHOT_BASE_URL:-https://getsfo.quicksync.io}" diff --git a/proto/terra/market/v1beta1/market.proto b/proto/terra/market/v1beta1/market.proto index 7994a7088..656f480d6 100644 --- a/proto/terra/market/v1beta1/market.proto +++ b/proto/terra/market/v1beta1/market.proto @@ -26,4 +26,9 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin", (gogoproto.nullable) = false ]; + bytes percentage_supply_max_descending = 4 [ + (gogoproto.moretags) = "yaml:\"percentage_supply_max_descending\"", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; } diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index a757fd207..db8655ab4 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -77,6 +77,32 @@ func (k Keeper) SetTerraPoolDelta(ctx sdk.Context, delta sdk.Dec) { store.Set(types.TerraPoolDeltaKey, bz) } +// GetTerraPoolDelta returns the gap between the TerraPool and the TerraBasePool +func (k Keeper) GetSupplyMaxDescending(ctx sdk.Context, key []byte) sdk.Int { + store := ctx.KVStore(k.storeKey) + bz := store.Get(key) + if bz == nil { + return sdk.ZeroInt() + } + + dp := sdk.IntProto{} + k.cdc.MustUnmarshal(bz, &dp) + return dp.Int +} + +// SetTerraPoolDelta updates TerraPoolDelta which is gap between the TerraPool and the BasePool +func (k Keeper) SetSupplyMaxDescending(ctx sdk.Context, key []byte, delta sdk.Int) { + fmt.Println("inclui dados deduzidos supply maximo", delta.String()) + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshal(&sdk.IntProto{Int: delta}) + store.Set(key, bz) +} +func (k Keeper) HasSupplyMaxDescending(ctx sdk.Context, key []byte) bool { + store := ctx.KVStore(k.storeKey) + return store.Has(key) + +} + // ReplenishPools replenishes each pool(Terra,Luna) to BasePool func (k Keeper) ReplenishPools(ctx sdk.Context) { poolDelta := k.GetTerraPoolDelta(ctx) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 5a302d197..85054d1c1 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -102,7 +102,22 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, if err != nil { return nil, err } + for _, offerCoin := range offerCoins { + // percentual = sdk.NewDecWithPrec(30, 2) //30% + var amount_p = sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) + if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { + var ok, amount = k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) + if ok { + k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), amount) + } else { + return nil, sdkerrors.Wrap(types.ErrZeroSwapCoin, "Need to declare the maximum limit of supply "+offerCoin.Denom) + } + } + supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) + k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), supplyMaxDescending.Sub(amount_p.TruncateInt())) + + } // Mint asked coins and credit Trader's account swapCoin, decimalCoin := swapDecCoin.TruncateDecimal() @@ -158,7 +173,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { - var ok, amount = isExists(coin.Denom, k.GetMaxSupplyCoin(ctx)) + var ok, amount = k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) if ok { @@ -166,22 +181,27 @@ func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) } - // if (totalSupply.Amount.Int64() + coin.Amount.TruncateInt().Int64()) > amount.Int64() { - // //var decoin = sdk.NewDecCoin(retDecCoin.Denom, amount) - // return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) - // } } else { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) } return nil } -func isExists(demom string, coins sdk.Coins) (result bool, amount sdk.Int) { +func (k Keeper) isExists(ctx sdk.Context, demom string, coins sdk.Coins) (result bool, amount sdk.Int) { result = false for _, coin := range coins { if coin.Denom == demom { amount = coin.Amount + if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+demom)) { + k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+demom), coin.Amount) + } else { + supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+demom)) + if coin.Amount.GT(supplyMaxDescending) { + amount = supplyMaxDescending + } + } + result = true break } diff --git a/x/market/keeper/params.go b/x/market/keeper/params.go index d14772064..1d0d2c2f1 100644 --- a/x/market/keeper/params.go +++ b/x/market/keeper/params.go @@ -33,6 +33,10 @@ func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res sdk.Coins) { func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin sdk.Coins) { k.paramSpace.Set(ctx, types.KeyMaxSupplyCoin, maxSupplyCoin) } +func (k Keeper) PercentageSupplyMaxDescending(ctx sdk.Context) (res sdk.Dec) { + k.paramSpace.Get(ctx, types.KeyPercentageSupplyMaxDescending, &res) + return +} // GetParams returns the total set of market parameters. func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) { diff --git a/x/market/legacy/v04/types.go b/x/market/legacy/v04/types.go index 7e5f27d53..f8adba9d7 100644 --- a/x/market/legacy/v04/types.go +++ b/x/market/legacy/v04/types.go @@ -13,10 +13,11 @@ const ( type ( // Params market parameters Params struct { - BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` - PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` - MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` - MaxSupplyCoin sdk.Coins `json:"max_supply_coin" yaml:"max_supply_coin"` + BasePool sdk.Dec `json:"base_pool" yaml:"base_pool"` + PoolRecoveryPeriod int64 `json:"pool_recovery_period" yaml:"pool_recovery_period"` + MinStabilitySpread sdk.Dec `json:"min_spread" yaml:"min_spread"` + MaxSupplyCoin sdk.Coins `json:"max_supply_coin" yaml:"max_supply_coin"` + PercentageSupplyMaxDescending sdk.Dec `json:"percentage_supply_max_descending" yaml:"percentage_supply_max_descending"` } // GenesisState is the struct representation of the export genesis diff --git a/x/market/legacy/v05/migrate.go b/x/market/legacy/v05/migrate.go index 6327af8eb..455eca904 100644 --- a/x/market/legacy/v05/migrate.go +++ b/x/market/legacy/v05/migrate.go @@ -19,10 +19,11 @@ func Migrate( return &v05market.GenesisState{ TerraPoolDelta: sdk.ZeroDec(), Params: v05market.Params{ - BasePool: marketGenState.Params.BasePool, - PoolRecoveryPeriod: uint64(marketGenState.Params.PoolRecoveryPeriod), - MinStabilitySpread: marketGenState.Params.MinStabilitySpread, - MaxSupplyCoin: marketGenState.Params.MaxSupplyCoin, + BasePool: marketGenState.Params.BasePool, + PoolRecoveryPeriod: uint64(marketGenState.Params.PoolRecoveryPeriod), + MinStabilitySpread: marketGenState.Params.MinStabilitySpread, + MaxSupplyCoin: marketGenState.Params.MaxSupplyCoin, + PercentageSupplyMaxDescending: marketGenState.Params.PercentageSupplyMaxDescending, }, } } diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index 77ca24946..3ed53c1e0 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -35,28 +35,29 @@ func TestMigrate(t *testing.T) { MinStabilitySpread: sdk.NewDecWithPrec(2, 2), //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() MaxSupplyCoin: sdk.Coins{ - {Denom: "uaud", Amount: sdk.NewInt(500000000)}, - {Denom: "ucad", Amount: sdk.NewInt(500000000)}, - {Denom: "uchf", Amount: sdk.NewInt(500000000)}, - {Denom: "ucny", Amount: sdk.NewInt(500000000)}, - {Denom: "udkk", Amount: sdk.NewInt(500000000)}, - {Denom: "ueur", Amount: sdk.NewInt(500000000)}, - {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, - {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, - {Denom: "uidr", Amount: sdk.NewInt(500000000)}, - {Denom: "uinr", Amount: sdk.NewInt(500000000)}, - {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, - {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, - {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, - {Denom: "umnt", Amount: sdk.NewInt(500000000)}, - {Denom: "unok", Amount: sdk.NewInt(500000000)}, - {Denom: "uphp", Amount: sdk.NewInt(500000000)}, - {Denom: "usdr", Amount: sdk.NewInt(500000000)}, - {Denom: "usek", Amount: sdk.NewInt(500000000)}, - {Denom: "usgd", Amount: sdk.NewInt(500000000)}, - {Denom: "uthb", Amount: sdk.NewInt(500000000)}, - {Denom: "uusd", Amount: sdk.NewInt(500000000)}, - }, + {Denom: "uaud", Amount: sdk.NewInt(500000000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000000)}, + {Denom: "uluna", Amount: sdk.NewInt(1000000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000000)}, + }, + PercentageSupplyMaxDescending: sdk.NewDecWithPrec(30, 2), //30%, }, } @@ -82,89 +83,90 @@ func TestMigrate(t *testing.T) { "max_supply_coin": [ { "Denom": "uaud", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ucad", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uchf", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ucny", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "udkk", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ueur", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ugbp", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uhkd", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uidr", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uinr", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ujpy", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "ukrw", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uluna", - "Amount": "10000000000" + "Amount": "1000000000000" }, { "Denom": "umnt", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "unok", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uphp", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "usdr", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "usek", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "usgd", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uthb", - "Amount": "500000000" + "Amount": "500000000000" }, { "Denom": "uusd", - "Amount": "500000000" + "Amount": "500000000000" } - ] + ], + "percentage_supply_max_descending": "0.300000000000000000", }, "terra_pool_delta": "0.000000000000000000" }` diff --git a/x/market/types/market.pb.go b/x/market/types/market.pb.go index 30502517f..951066505 100644 --- a/x/market/types/market.pb.go +++ b/x/market/types/market.pb.go @@ -34,6 +34,7 @@ type Params struct { PoolRecoveryPeriod uint64 `protobuf:"varint,2,opt,name=pool_recovery_period,json=poolRecoveryPeriod,proto3" json:"pool_recovery_period,omitempty" yaml:"pool_recovery_period"` MinStabilitySpread github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=min_stability_spread,json=minStabilitySpread,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"min_stability_spread" yaml:"min_stability_spread"` MaxSupplyCoin github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,opt,name=max_supply_coin,json=maxSupplyCoin,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Coin" json:"max_supply_coin" yaml:"max_supply_coin"` + PercentageSupplyMaxDescending github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,8,opt,name=percentage_supply_max_descending,json=percentageSupplyMaxDescending,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"percentage_supply_max_descending" yaml:"percentage_supply_max_descending"` } @@ -140,7 +141,9 @@ func (this *Params) Equal(that interface{}) bool { if !this.MinStabilitySpread.Equal(that1.MinStabilitySpread) { return false } - + if !this.PercentageSupplyMaxDescending.Equal(that1.PercentageSupplyMaxDescending) { + return false + } return true } @@ -188,6 +191,14 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i = encodeVarintMarket(dAtA, i, uint64(size)) } + { + size := m.PercentageSupplyMaxDescending.Size() + i -= size + if _, err := m.PercentageSupplyMaxDescending.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMarket(dAtA, i, uint64(size)) + } i-- dAtA[i] = 0xa return len(dAtA) - i, nil @@ -218,6 +229,8 @@ func (m *Params) Size() (n int) { } l = m.MinStabilitySpread.Size() n += 1 + l + sovMarket(uint64(l)) + l = m.PercentageSupplyMaxDescending.Size() + n += 1 + l + sovMarket(uint64(l)) return n } @@ -343,6 +356,39 @@ func (m *Params) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PercentageSupplyMaxDescending", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMarket + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthMarket + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthMarket + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.PercentageSupplyMaxDescending.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipMarket(dAtA[iNdEx:]) diff --git a/x/market/types/params.go b/x/market/types/params.go index e98a643d2..4f926475c 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -18,8 +18,9 @@ var ( // The period required to recover BasePool KeyPoolRecoveryPeriod = []byte("PoolRecoveryPeriod") // Min spread - KeyMinStabilitySpread = []byte("MinStabilitySpread") - KeyMaxSupplyCoin = []byte("MaxSupplyCoin") + KeyMinStabilitySpread = []byte("MinStabilitySpread") + KeyMaxSupplyCoin = []byte("MaxSupplyCoin") + KeyPercentageSupplyMaxDescending = []byte("PercentageSupplyMaxDescending") ) // Default parameter values @@ -29,28 +30,29 @@ var ( DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() DefaultMaxSupplyCoin = sdk.Coins{ - {Denom: "uaud", Amount: sdk.NewInt(500000000)}, - {Denom: "ucad", Amount: sdk.NewInt(500000000)}, - {Denom: "uchf", Amount: sdk.NewInt(500000000)}, - {Denom: "ucny", Amount: sdk.NewInt(500000000)}, - {Denom: "udkk", Amount: sdk.NewInt(500000000)}, - {Denom: "ueur", Amount: sdk.NewInt(500000000)}, - {Denom: "ugbp", Amount: sdk.NewInt(500000000)}, - {Denom: "uhkd", Amount: sdk.NewInt(500000000)}, - {Denom: "uidr", Amount: sdk.NewInt(500000000)}, - {Denom: "uinr", Amount: sdk.NewInt(500000000)}, - {Denom: "ujpy", Amount: sdk.NewInt(500000000)}, - {Denom: "ukrw", Amount: sdk.NewInt(500000000)}, - {Denom: "uluna", Amount: sdk.NewInt(10000000000)}, - {Denom: "umnt", Amount: sdk.NewInt(500000000)}, - {Denom: "unok", Amount: sdk.NewInt(500000000)}, - {Denom: "uphp", Amount: sdk.NewInt(500000000)}, - {Denom: "usdr", Amount: sdk.NewInt(500000000)}, - {Denom: "usek", Amount: sdk.NewInt(500000000)}, - {Denom: "usgd", Amount: sdk.NewInt(500000000)}, - {Denom: "uthb", Amount: sdk.NewInt(500000000)}, - {Denom: "uusd", Amount: sdk.NewInt(500000000)}, - } + {Denom: "uaud", Amount: sdk.NewInt(500000000000)}, + {Denom: "ucad", Amount: sdk.NewInt(500000000000)}, + {Denom: "uchf", Amount: sdk.NewInt(500000000000)}, + {Denom: "ucny", Amount: sdk.NewInt(500000000000)}, + {Denom: "udkk", Amount: sdk.NewInt(500000000000)}, + {Denom: "ueur", Amount: sdk.NewInt(500000000000)}, + {Denom: "ugbp", Amount: sdk.NewInt(500000000000)}, + {Denom: "uhkd", Amount: sdk.NewInt(500000000000)}, + {Denom: "uidr", Amount: sdk.NewInt(500000000000)}, + {Denom: "uinr", Amount: sdk.NewInt(500000000000)}, + {Denom: "ujpy", Amount: sdk.NewInt(500000000000)}, + {Denom: "ukrw", Amount: sdk.NewInt(500000000000)}, + {Denom: "uluna", Amount: sdk.NewInt(1000000000000)}, + {Denom: "umnt", Amount: sdk.NewInt(500000000000)}, + {Denom: "unok", Amount: sdk.NewInt(500000000000)}, + {Denom: "uphp", Amount: sdk.NewInt(500000000000)}, + {Denom: "usdr", Amount: sdk.NewInt(500000000000)}, + {Denom: "usek", Amount: sdk.NewInt(500000000000)}, + {Denom: "usgd", Amount: sdk.NewInt(500000000000)}, + {Denom: "uthb", Amount: sdk.NewInt(500000000000)}, + {Denom: "uusd", Amount: sdk.NewInt(500000000000)}, + } + DefaultPercentageSupplyMaxDescending = sdk.NewDecWithPrec(30, 2) //30% ) var _ paramstypes.ParamSet = &Params{} @@ -58,10 +60,11 @@ var _ paramstypes.ParamSet = &Params{} // DefaultParams creates default market module parameters func DefaultParams() Params { return Params{ - BasePool: DefaultBasePool, - PoolRecoveryPeriod: DefaultPoolRecoveryPeriod, - MinStabilitySpread: DefaultMinStabilitySpread, - MaxSupplyCoin: DefaultMaxSupplyCoin, + BasePool: DefaultBasePool, + PoolRecoveryPeriod: DefaultPoolRecoveryPeriod, + MinStabilitySpread: DefaultMinStabilitySpread, + MaxSupplyCoin: DefaultMaxSupplyCoin, + PercentageSupplyMaxDescending: DefaultPercentageSupplyMaxDescending, } } @@ -84,6 +87,7 @@ func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs { paramstypes.NewParamSetPair(KeyPoolRecoveryPeriod, &p.PoolRecoveryPeriod, validatePoolRecoveryPeriod), paramstypes.NewParamSetPair(KeyMinStabilitySpread, &p.MinStabilitySpread, validateMinStabilitySpread), paramstypes.NewParamSetPair(KeyMaxSupplyCoin, &p.MaxSupplyCoin, validateMaxSupplyCoin), + paramstypes.NewParamSetPair(KeyPercentageSupplyMaxDescending, &p.PercentageSupplyMaxDescending, validatePercentageSupplyMaxDescending), } } @@ -101,6 +105,9 @@ func (p Params) Validate() error { if len(p.MaxSupplyCoin) == 0 { return fmt.Errorf("max supplay cannot be empty %s", p.MaxSupplyCoin) } + if p.PercentageSupplyMaxDescending.IsNegative() { + return fmt.Errorf("mint base pool should be positive or zero, is %s", p.PercentageSupplyMaxDescending) + } return nil } @@ -157,3 +164,15 @@ func validateMaxSupplyCoin(i interface{}) error { return nil } +func validatePercentageSupplyMaxDescending(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.IsNegative() { + return fmt.Errorf("mint Percentage Supply Max Descending must be positive or zero: %s", v) + } + + return nil +} From a836315f60e84c6a4630a8eec6f3cf6c95c10614 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 13:20:26 -0300 Subject: [PATCH 05/16] tweaks in the test migrate file --- x/market/legacy/v05/migrate_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index 3ed53c1e0..24f6ce70c 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -166,7 +166,7 @@ func TestMigrate(t *testing.T) { "Amount": "500000000000" } ], - "percentage_supply_max_descending": "0.300000000000000000", + "percentage_supply_max_descending": "0.300000000000000000" }, "terra_pool_delta": "0.000000000000000000" }` From bf4e777b985a6ffd2dcd75f770c7797cb3c88808 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 13:25:50 -0300 Subject: [PATCH 06/16] adjustments in the test parameters file --- x/market/types/market.pb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/market/types/market.pb.go b/x/market/types/market.pb.go index babd5be28..9a36f9656 100644 --- a/x/market/types/market.pb.go +++ b/x/market/types/market.pb.go @@ -359,7 +359,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 4: + case 5: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field PercentageSupplyMaxDescending", wireType) } From 291eeb0f91aea4b7916851607d573aa51bfa212c Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 13:26:10 -0300 Subject: [PATCH 07/16] adjustments in the test parameters file --- proto/terra/market/v1beta1/market.proto | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/proto/terra/market/v1beta1/market.proto b/proto/terra/market/v1beta1/market.proto index 656f480d6..478c08712 100644 --- a/proto/terra/market/v1beta1/market.proto +++ b/proto/terra/market/v1beta1/market.proto @@ -21,12 +21,12 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - bytes max_supply_coin = 3 [ + bytes max_supply_coin = 4 [ (gogoproto.moretags) = "yaml:\"max_supply_coin\"", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin", (gogoproto.nullable) = false ]; - bytes percentage_supply_max_descending = 4 [ + bytes percentage_supply_max_descending = 5 [ (gogoproto.moretags) = "yaml:\"percentage_supply_max_descending\"", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false From d8d01515f951b45d72710da8f0a12abce182e2b6 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 13:52:31 -0300 Subject: [PATCH 08/16] migrate tweaks --- x/market/legacy/v05/migrate_test.go | 84 ++++++++++++++--------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index 24f6ce70c..41b306471 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -82,88 +82,88 @@ func TestMigrate(t *testing.T) { "pool_recovery_period": "10000", "max_supply_coin": [ { - "Denom": "uaud", - "Amount": "500000000000" + "denom": "uaud", + "amount": "500000000000" }, { - "Denom": "ucad", - "Amount": "500000000000" + "denom": "ucad", + "amount": "500000000000" }, { - "Denom": "uchf", - "Amount": "500000000000" + "denom": "uchf", + "amount": "500000000000" }, { - "Denom": "ucny", - "Amount": "500000000000" + "denom": "ucny", + "amount": "500000000000" }, { - "Denom": "udkk", - "Amount": "500000000000" + "denom": "udkk", + "amount": "500000000000" }, { - "Denom": "ueur", - "Amount": "500000000000" + "denom": "ueur", + "amount": "500000000000" }, { - "Denom": "ugbp", - "Amount": "500000000000" + "denom": "ugbp", + "amount": "500000000000" }, { - "Denom": "uhkd", - "Amount": "500000000000" + "denom": "uhkd", + "amount": "500000000000" }, { - "Denom": "uidr", - "Amount": "500000000000" + "denom": "uidr", + "amount": "500000000000" }, { - "Denom": "uinr", - "Amount": "500000000000" + "denom": "uinr", + "amount": "500000000000" }, { - "Denom": "ujpy", - "Amount": "500000000000" + "denom": "ujpy", + "amount": "500000000000" }, { - "Denom": "ukrw", - "Amount": "500000000000" + "denom": "ukrw", + "amount": "500000000000" }, { - "Denom": "uluna", - "Amount": "1000000000000" + "denom": "uluna", + "amount": "1000000000000" }, { - "Denom": "umnt", - "Amount": "500000000000" + "denom": "umnt", + "amount": "500000000000" }, { - "Denom": "unok", - "Amount": "500000000000" + "denom": "unok", + "amount": "500000000000" }, { - "Denom": "uphp", - "Amount": "500000000000" + "denom": "uphp", + "amount": "500000000000" }, { - "Denom": "usdr", - "Amount": "500000000000" + "denom": "usdr", + "amount": "500000000000" }, { - "Denom": "usek", - "Amount": "500000000000" + "denom": "usek", + "amount": "500000000000" }, { - "Denom": "usgd", - "Amount": "500000000000" + "denom": "usgd", + "amount": "500000000000" }, { - "Denom": "uthb", - "Amount": "500000000000" + "denom": "uthb", + "amount": "500000000000" }, { - "Denom": "uusd", - "Amount": "500000000000" + "denom": "uusd", + "amount": "500000000000" } ], "percentage_supply_max_descending": "0.300000000000000000" From c851133ef70f371ffd29b333bf08f586101a7db2 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 14:02:13 -0300 Subject: [PATCH 09/16] gofumpt tweaks --- x/market/keeper/msg_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 85054d1c1..8f0c882b5 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -103,7 +103,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, return nil, err } for _, offerCoin := range offerCoins { - // percentual = sdk.NewDecWithPrec(30, 2) //30% + var amount_p = sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { var ok, amount = k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) From 391746de7acdc02a59e7247829d8dfb781f61877 Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 15:08:43 -0300 Subject: [PATCH 10/16] following pattern --- x/market/legacy/v05/migrate_test.go | 2 +- x/market/types/params.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index 41b306471..29a2a3f64 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -57,7 +57,7 @@ func TestMigrate(t *testing.T) { {Denom: "uthb", Amount: sdk.NewInt(500000000000)}, {Denom: "uusd", Amount: sdk.NewInt(500000000000)}, }, - PercentageSupplyMaxDescending: sdk.NewDecWithPrec(30, 2), //30%, + PercentageSupplyMaxDescending: sdk.NewDecWithPrec(30, 2), // 30% }, } diff --git a/x/market/types/params.go b/x/market/types/params.go index 4f926475c..864fb3998 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -52,7 +52,7 @@ var ( {Denom: "uthb", Amount: sdk.NewInt(500000000000)}, {Denom: "uusd", Amount: sdk.NewInt(500000000000)}, } - DefaultPercentageSupplyMaxDescending = sdk.NewDecWithPrec(30, 2) //30% + DefaultPercentageSupplyMaxDescending = sdk.NewDecWithPrec(30, 2) // 30% ) var _ paramstypes.ParamSet = &Params{} From 2a46d5d0f5477d1967a167e6ac35fe02b1f075af Mon Sep 17 00:00:00 2001 From: igor veras Date: Thu, 27 Jul 2023 15:31:33 -0300 Subject: [PATCH 11/16] golangci-lint --- x/market/keeper/keeper.go | 1 - x/market/keeper/msg_server.go | 10 ---------- x/market/keeper/params.go | 1 + x/market/types/params.go | 4 +++- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index db8655ab4..b2bff6247 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -100,7 +100,6 @@ func (k Keeper) SetSupplyMaxDescending(ctx sdk.Context, key []byte, delta sdk.In func (k Keeper) HasSupplyMaxDescending(ctx sdk.Context, key []byte) bool { store := ctx.KVStore(k.storeKey) return store.Has(key) - } // ReplenishPools replenishes each pool(Terra,Luna) to BasePool diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 8f0c882b5..8047ed519 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -103,7 +103,6 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, return nil, err } for _, offerCoin := range offerCoins { - var amount_p = sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { var ok, amount = k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) @@ -112,11 +111,9 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } else { return nil, sdkerrors.Wrap(types.ErrZeroSwapCoin, "Need to declare the maximum limit of supply "+offerCoin.Denom) } - } supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), supplyMaxDescending.Sub(amount_p.TruncateInt())) - } // Mint asked coins and credit Trader's account swapCoin, decimalCoin := swapDecCoin.TruncateDecimal() @@ -172,16 +169,12 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, }, nil } func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { - var ok, amount = k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) if ok { - if totalSupply.Amount.Add(coin.Amount.TruncateInt()).GT(amount) { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) - } - } else { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) } @@ -189,7 +182,6 @@ func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { } func (k Keeper) isExists(ctx sdk.Context, demom string, coins sdk.Coins) (result bool, amount sdk.Int) { result = false - for _, coin := range coins { if coin.Denom == demom { amount = coin.Amount @@ -201,11 +193,9 @@ func (k Keeper) isExists(ctx sdk.Context, demom string, coins sdk.Coins) (result amount = supplyMaxDescending } } - result = true break } } - return result, amount } diff --git a/x/market/keeper/params.go b/x/market/keeper/params.go index 1d0d2c2f1..e3b84a303 100644 --- a/x/market/keeper/params.go +++ b/x/market/keeper/params.go @@ -33,6 +33,7 @@ func (k Keeper) GetMaxSupplyCoin(ctx sdk.Context) (res sdk.Coins) { func (k Keeper) SetMaxSupplyCoin(ctx sdk.Context, maxSupplyCoin sdk.Coins) { k.paramSpace.Set(ctx, types.KeyMaxSupplyCoin, maxSupplyCoin) } + func (k Keeper) PercentageSupplyMaxDescending(ctx sdk.Context) (res sdk.Dec) { k.paramSpace.Get(ctx, types.KeyPercentageSupplyMaxDescending, &res) return diff --git a/x/market/types/params.go b/x/market/types/params.go index 864fb3998..54c2df82d 100644 --- a/x/market/types/params.go +++ b/x/market/types/params.go @@ -28,7 +28,7 @@ var ( DefaultBasePool = sdk.NewDec(1000000 * core.MicroUnit) // 1000,000sdr = 1000,000,000,000usdr DefaultPoolRecoveryPeriod = core.BlocksPerDay // 14,400 DefaultMinStabilitySpread = sdk.NewDecWithPrec(2, 2) - //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + // ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() DefaultMaxSupplyCoin = sdk.Coins{ {Denom: "uaud", Amount: sdk.NewInt(500000000000)}, {Denom: "ucad", Amount: sdk.NewInt(500000000000)}, @@ -153,6 +153,7 @@ func validateMinStabilitySpread(i interface{}) error { return nil } + func validateMaxSupplyCoin(i interface{}) error { v, ok := i.(sdk.Coins) if !ok { @@ -164,6 +165,7 @@ func validateMaxSupplyCoin(i interface{}) error { return nil } + func validatePercentageSupplyMaxDescending(i interface{}) error { v, ok := i.(sdk.Dec) if !ok { From 2f132204da62f92203a38cd52afcd4e75fc93e77 Mon Sep 17 00:00:00 2001 From: igor veras Date: Fri, 28 Jul 2023 11:55:12 -0300 Subject: [PATCH 12/16] golangci-lint error --- x/market/keeper/keeper.go | 1 + x/market/keeper/msg_server.go | 5 +++-- x/market/legacy/v05/migrate.go | 1 - x/market/legacy/v05/migrate_test.go | 5 +---- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index b2bff6247..ab28f903f 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -97,6 +97,7 @@ func (k Keeper) SetSupplyMaxDescending(ctx sdk.Context, key []byte, delta sdk.In bz := k.cdc.MustMarshal(&sdk.IntProto{Int: delta}) store.Set(key, bz) } + func (k Keeper) HasSupplyMaxDescending(ctx sdk.Context, key []byte) bool { store := ctx.KVStore(k.storeKey) return store.Has(key) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 8047ed519..20bca9760 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -67,9 +67,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, if err != nil { return nil, err } - var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) - if errSup != nil { return nil, errSup } @@ -102,6 +100,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, if err != nil { return nil, err } + for _, offerCoin := range offerCoins { var amount_p = sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { @@ -168,6 +167,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, SwapFee: feeCoin, }, nil } + func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { var ok, amount = k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) @@ -180,6 +180,7 @@ func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { } return nil } + func (k Keeper) isExists(ctx sdk.Context, demom string, coins sdk.Coins) (result bool, amount sdk.Int) { result = false for _, coin := range coins { diff --git a/x/market/legacy/v05/migrate.go b/x/market/legacy/v05/migrate.go index 455eca904..94dabeb6a 100644 --- a/x/market/legacy/v05/migrate.go +++ b/x/market/legacy/v05/migrate.go @@ -15,7 +15,6 @@ import ( func Migrate( marketGenState v04market.GenesisState, ) *v05market.GenesisState { - return &v05market.GenesisState{ TerraPoolDelta: sdk.ZeroDec(), Params: v05market.Params{ diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index 29a2a3f64..c45ccd30b 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -17,9 +17,7 @@ import ( ) func TestMigrate(t *testing.T) { - sdk.GetConfig().SetBech32PrefixForAccount(core.Bech32PrefixAccAddr, core.Bech32PrefixAccPub) - encodingConfig := app.MakeEncodingConfig() clientCtx := client.Context{}. WithInterfaceRegistry(encodingConfig.InterfaceRegistry). @@ -32,8 +30,7 @@ func TestMigrate(t *testing.T) { Params: v04market.Params{ BasePool: sdk.NewDec(1000000), PoolRecoveryPeriod: int64(10000), - MinStabilitySpread: sdk.NewDecWithPrec(2, 2), - //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + MinStabilitySpread: sdk.NewDecWithPrec(2, 2), //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() MaxSupplyCoin: sdk.Coins{ {Denom: "uaud", Amount: sdk.NewInt(500000000000)}, {Denom: "ucad", Amount: sdk.NewInt(500000000000)}, From 514f4a3535c8d033ac18081f7bc72b1d1eb85a94 Mon Sep 17 00:00:00 2001 From: igor veras Date: Fri, 28 Jul 2023 12:29:53 -0300 Subject: [PATCH 13/16] error golangcli --- x/market/keeper/msg_server.go | 3 +++ x/market/legacy/v05/migrate_test.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index 20bca9760..b44248a99 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -67,6 +67,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, if err != nil { return nil, err } + var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) if errSup != nil { return nil, errSup @@ -169,6 +170,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { + var ok, amount = k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) if ok { @@ -178,6 +180,7 @@ func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { } else { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "maximum supply not configured for currency "+coin.Denom) } + return nil } diff --git a/x/market/legacy/v05/migrate_test.go b/x/market/legacy/v05/migrate_test.go index c45ccd30b..5eb32e891 100644 --- a/x/market/legacy/v05/migrate_test.go +++ b/x/market/legacy/v05/migrate_test.go @@ -30,7 +30,7 @@ func TestMigrate(t *testing.T) { Params: v04market.Params{ BasePool: sdk.NewDec(1000000), PoolRecoveryPeriod: int64(10000), - MinStabilitySpread: sdk.NewDecWithPrec(2, 2), //ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() + MinStabilitySpread: sdk.NewDecWithPrec(2, 2), // ATTENTION: The list of modes must be in alphabetical order, otherwise an error occurs in validateMaxSupplyCoin => !v.IsValid() MaxSupplyCoin: sdk.Coins{ {Denom: "uaud", Amount: sdk.NewInt(500000000000)}, {Denom: "ucad", Amount: sdk.NewInt(500000000000)}, From a53e13e53e441e81fd7e8bed0964892acb391dc0 Mon Sep 17 00:00:00 2001 From: igor veras Date: Fri, 28 Jul 2023 14:04:26 -0300 Subject: [PATCH 14/16] golang lint --- x/market/keeper/msg_server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index b44248a99..ee69a1413 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -68,7 +68,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, return nil, err } - var errSup = k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) + errSup := k.ValidateSupplyMaximum(ctx, sdk.NewDecCoin(swapDecCoin.Denom, swapDecCoin.Amount.TruncateInt())) if errSup != nil { return nil, errSup } @@ -103,9 +103,9 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } for _, offerCoin := range offerCoins { - var amount_p = sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) + amount_p := sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { - var ok, amount = k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) + ok, amount := k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) if ok { k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), amount) } else { @@ -171,7 +171,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { - var ok, amount = k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) + ok, amount := k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) if ok { if totalSupply.Amount.Add(coin.Amount.TruncateInt()).GT(amount) { From 7fb6c387e84d6ad9ec4518dcfaa5e0d94e35d135 Mon Sep 17 00:00:00 2001 From: igor veras Date: Fri, 28 Jul 2023 14:08:16 -0300 Subject: [PATCH 15/16] go lint --- x/market/keeper/msg_server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index ee69a1413..cb9306227 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -103,7 +103,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } for _, offerCoin := range offerCoins { - amount_p := sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) + amountP := sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { ok, amount := k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) if ok { @@ -113,7 +113,7 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } } supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) - k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), supplyMaxDescending.Sub(amount_p.TruncateInt())) + k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), supplyMaxDescending.Sub(amountP.TruncateInt())) } // Mint asked coins and credit Trader's account swapCoin, decimalCoin := swapDecCoin.TruncateDecimal() @@ -170,9 +170,9 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, } func (k Keeper) ValidateSupplyMaximum(ctx sdk.Context, coin sdk.DecCoin) error { - ok, amount := k.isExists(ctx, coin.Denom, k.GetMaxSupplyCoin(ctx)) - var totalSupply = k.BankKeeper.GetSupply(ctx, coin.Denom) + + totalSupply := k.BankKeeper.GetSupply(ctx, coin.Denom) if ok { if totalSupply.Amount.Add(coin.Amount.TruncateInt()).GT(amount) { return sdkerrors.Wrap(types.ErrZeroSwapCoin, "The value to be minted exceeded the maximum supply value "+amount.String()+coin.Denom) From 391bce6c024e3f915fb65dd1de9cc8d1f5b0d91e Mon Sep 17 00:00:00 2001 From: igor veras Date: Fri, 8 Sep 2023 13:41:10 -0300 Subject: [PATCH 16/16] Changed as requested --- x/market/keeper/keeper.go | 1 - x/market/keeper/msg_server.go | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index ab28f903f..3f6a8a69f 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -92,7 +92,6 @@ func (k Keeper) GetSupplyMaxDescending(ctx sdk.Context, key []byte) sdk.Int { // SetTerraPoolDelta updates TerraPoolDelta which is gap between the TerraPool and the BasePool func (k Keeper) SetSupplyMaxDescending(ctx sdk.Context, key []byte, delta sdk.Int) { - fmt.Println("inclui dados deduzidos supply maximo", delta.String()) store := ctx.KVStore(k.storeKey) bz := k.cdc.MustMarshal(&sdk.IntProto{Int: delta}) store.Set(key, bz) diff --git a/x/market/keeper/msg_server.go b/x/market/keeper/msg_server.go index cb9306227..0c8e6e3ee 100644 --- a/x/market/keeper/msg_server.go +++ b/x/market/keeper/msg_server.go @@ -104,16 +104,17 @@ func (k msgServer) handleSwapRequest(ctx sdk.Context, for _, offerCoin := range offerCoins { amountP := sdk.NewDec(offerCoin.Amount.Int64()).Mul(k.PercentageSupplyMaxDescending(ctx)) + supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) if !k.HasSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) { ok, amount := k.isExists(ctx, offerCoin.Denom, k.GetMaxSupplyCoin(ctx)) if ok { - k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), amount) + supplyMaxDescending = amount } else { return nil, sdkerrors.Wrap(types.ErrZeroSwapCoin, "Need to declare the maximum limit of supply "+offerCoin.Denom) } } - supplyMaxDescending := k.GetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom)) k.SetSupplyMaxDescending(ctx, []byte("SupplyMaxDescending"+offerCoin.Denom), supplyMaxDescending.Sub(amountP.TruncateInt())) + } // Mint asked coins and credit Trader's account swapCoin, decimalCoin := swapDecCoin.TruncateDecimal()