From 5d891e0ecece67e33a5327e84306a153844ca11b Mon Sep 17 00:00:00 2001 From: "stefan.balea" Date: Sat, 11 Jan 2025 02:08:13 +0200 Subject: [PATCH] math changes and ante --- app/ante.go | 5 ++-- cmd/bzed/root.go | 2 -- x/burner/keeper/abci.go | 28 ++++++++++--------- x/burner/module.go | 17 +++++++---- x/cointrunk/module.go | 5 ---- x/epochs/module.go | 5 ---- .../keeper/staking_reward_distribution.go | 17 +++++------ .../staking_reward_distribution_test.go | 7 +++-- x/rewards/module.go | 5 ---- x/scavenge/module.go | 5 ---- x/tokenfactory/module.go | 5 ---- x/tradebin/keeper/calculator.go | 5 ++-- x/tradebin/module.go | 5 ---- 13 files changed, 44 insertions(+), 67 deletions(-) diff --git a/app/ante.go b/app/ante.go index 91082af6..828c70f9 100644 --- a/app/ante.go +++ b/app/ante.go @@ -31,14 +31,13 @@ func NewAnteHandler(options ante.HandlerOptions) (sdk.AnteHandler, error) { anteDecorators := []sdk.AnteDecorator{ ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first - ante.NewRejectExtensionOptionsDecorator(), - ante.NewMempoolFeeDecorator(), + ante.NewExtensionOptionsDecorator(nil), ante.NewValidateBasicDecorator(), NewValidateTxFeeDenomsDecorator(), //use our own validate basic to enforce denoms that should be used for fees ante.NewTxTimeoutHeightDecorator(), ante.NewValidateMemoDecorator(options.AccountKeeper), ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper), - ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper), + ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, nil), ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators ante.NewValidateSigCountDecorator(options.AccountKeeper), ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer), diff --git a/cmd/bzed/root.go b/cmd/bzed/root.go index 9ebe7805..62d283d0 100644 --- a/cmd/bzed/root.go +++ b/cmd/bzed/root.go @@ -5,7 +5,6 @@ import ( "github.com/bze-alphateam/bze/app" "io" "os" - "path/filepath" "github.com/spf13/cast" "github.com/spf13/cobra" @@ -24,7 +23,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/server" servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/snapshots" "github.com/cosmos/cosmos-sdk/store" sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/burner/keeper/abci.go b/x/burner/keeper/abci.go index 32b01849..365114e9 100644 --- a/x/burner/keeper/abci.go +++ b/x/burner/keeper/abci.go @@ -44,22 +44,10 @@ func (k Keeper) WithdrawLuckyRaffleParticipants(ctx sdk.Context, height int64) { continue } - //get ticket price to enter the raffle - ticketPriceInt, ok := sdk.NewIntFromString(raffle.TicketPrice) - if !ok { - //should never happen - logger.Error("could not parse ticket price") - } - if k.IsLucky(ctx, &raffle, creatorAddr.String()) { logger.With("address", creatorAddr.String(), "denom", raffle.Denom).Info("user won raffle") //user won - winRatio := sdk.MustNewDecFromStr(raffle.Ratio) - wonAmount := currentPot.Amount.Sub(ticketPriceInt).ToDec().Mul(winRatio).TruncateInt() - if !wonAmount.IsPositive() { - wonAmount = currentPot.Amount.Sub(ticketPriceInt) - } - wonCoin := sdk.NewCoin(currentPot.Denom, wonAmount) + wonCoin := k.getWonCoin(&raffle, currentPot) err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.RaffleModuleName, creatorAddr, sdk.NewCoins(wonCoin)) if err != nil { @@ -110,3 +98,17 @@ func (k Keeper) WithdrawLuckyRaffleParticipants(ctx sdk.Context, height int64) { k.RemoveRaffleParticipant(ctx, participant) } } + +func (k Keeper) getWonCoin(raffle *types.Raffle, pot sdk.Coin) sdk.Coin { + //get ticket price to enter the raffle + ticketPrice := sdk.MustNewDecFromStr(raffle.TicketPrice) + winRatio := sdk.MustNewDecFromStr(raffle.Ratio) + potAmount := sdk.NewDec(pot.Amount.Int64()) + + prize := potAmount.Sub(ticketPrice).Mul(winRatio).TruncateInt() + if !prize.IsPositive() { + prize = pot.Amount.SubRaw(pot.Amount.Int64()) + } + + return sdk.NewCoin(pot.Denom, prize) +} diff --git a/x/burner/module.go b/x/burner/module.go index 6d1afb59..dce0b92a 100644 --- a/x/burner/module.go +++ b/x/burner/module.go @@ -4,10 +4,10 @@ import ( "context" "encoding/json" "fmt" + "github.com/bze-alphateam/bze/bzeutils" "github.com/cosmos/cosmos-sdk/telemetry" "time" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -74,10 +74,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { // this line is used by starport scaffolding # 2 @@ -178,7 +174,16 @@ func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} // returns no validator updates. func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - am.keeper.WithdrawLuckyRaffleParticipants(ctx, ctx.BlockHeight()) + wrappedFunc := func(ctx sdk.Context) error { + am.keeper.WithdrawLuckyRaffleParticipants(ctx, ctx.BlockHeight()) + + return nil + } + + err := bzeutils.ApplyFuncIfNoError(ctx, wrappedFunc) + if err != nil { + am.keeper.Logger(ctx).Error("error on burner module EndBlock", "err", err) + } return []abci.ValidatorUpdate{} } diff --git a/x/cointrunk/module.go b/x/cointrunk/module.go index b70169ee..7c3968b1 100644 --- a/x/cointrunk/module.go +++ b/x/cointrunk/module.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -71,10 +70,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { diff --git a/x/epochs/module.go b/x/epochs/module.go index 76406ac4..d4b02223 100644 --- a/x/epochs/module.go +++ b/x/epochs/module.go @@ -6,7 +6,6 @@ import ( "fmt" // this line is used by starport scaffolding # 1 - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -72,10 +71,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { // this line is used by starport scaffolding # 2 diff --git a/x/rewards/keeper/staking_reward_distribution.go b/x/rewards/keeper/staking_reward_distribution.go index 68caf8a1..c6f0b5e9 100644 --- a/x/rewards/keeper/staking_reward_distribution.go +++ b/x/rewards/keeper/staking_reward_distribution.go @@ -1,6 +1,7 @@ package keeper import ( + "cosmossdk.io/math" "fmt" "github.com/bze-alphateam/bze/x/rewards/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -55,31 +56,31 @@ func (k Keeper) getDistributeRewardHandler() func(ctx sdk.Context, reward types. } func (k Keeper) distributeStakingRewards(sr *types.StakingReward, rewardAmount string) error { - stakedAmount, ok := sdk.NewIntFromString(sr.StakedAmount) - if !ok { - return fmt.Errorf("could not transform staked amount from storage into int") + stakedAmount, err := math.LegacyNewDecFromStr(sr.StakedAmount) + if err != nil { + return fmt.Errorf("could not transform staked amount from storage into int: %w", err) } if !stakedAmount.IsPositive() { return fmt.Errorf("no stakers found") } - reward, ok := sdk.NewIntFromString(rewardAmount) - if !ok { - return fmt.Errorf("could not transform reward amount to int") + reward, err := math.LegacyNewDecFromStr(rewardAmount) + if err != nil { + return fmt.Errorf("could not transform reward amount to int: %w", err) } if !reward.IsPositive() { return fmt.Errorf("reward amount should be positive") } - sFloat, err := sdk.NewDecFromStr(sr.DistributedStake) + sFloat, err := math.LegacyNewDecFromStr(sr.DistributedStake) if err != nil { return err } //S = S + r / T; - sFloat = sFloat.Add(reward.ToDec().Quo(stakedAmount.ToDec())) + sFloat = sFloat.Add(reward.Quo(stakedAmount)) sr.DistributedStake = sFloat.String() return nil diff --git a/x/rewards/keeper/staking_reward_distribution_test.go b/x/rewards/keeper/staking_reward_distribution_test.go index 3331a3ac..431ea4b8 100644 --- a/x/rewards/keeper/staking_reward_distribution_test.go +++ b/x/rewards/keeper/staking_reward_distribution_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "cosmossdk.io/math" "fmt" "github.com/bze-alphateam/bze/x/rewards/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,8 +22,8 @@ func (suite *IntegrationTestSuite) TestDistributeAllStakingRewards() { } suite.k.SetStakingReward(suite.ctx, initial) - rewardAmt, ok := sdk.NewIntFromString(initial.PrizeAmount) - suite.Require().True(ok) + rewardAmt, err := math.LegacyNewDecFromStr(initial.PrizeAmount) + suite.Require().NoError(err) for i := uint32(0); i < initial.Duration; i++ { suite.k.DistributeAllStakingRewards(suite.ctx) @@ -33,7 +34,7 @@ func (suite *IntegrationTestSuite) TestDistributeAllStakingRewards() { staked, ok := sdk.NewIntFromString(storage.StakedAmount) suite.Require().True(ok) - newDistribution := rewardAmt.ToDec().Quo(staked.ToDec()) + newDistribution := rewardAmt.Quo(staked.ToDec()) distributed, err := sdk.NewDecFromStr(initial.DistributedStake) suite.Require().NoError(err) diff --git a/x/rewards/module.go b/x/rewards/module.go index bcde4ef4..387eeb03 100644 --- a/x/rewards/module.go +++ b/x/rewards/module.go @@ -6,7 +6,6 @@ import ( "fmt" // this line is used by starport scaffolding # 1 - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -72,10 +71,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { _ = types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) diff --git a/x/scavenge/module.go b/x/scavenge/module.go index f23519e0..ddcbe345 100644 --- a/x/scavenge/module.go +++ b/x/scavenge/module.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -71,10 +70,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { diff --git a/x/tokenfactory/module.go b/x/tokenfactory/module.go index 2f5b270d..c7be1dc3 100644 --- a/x/tokenfactory/module.go +++ b/x/tokenfactory/module.go @@ -6,7 +6,6 @@ import ( "fmt" // this line is used by starport scaffolding # 1 - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" @@ -72,10 +71,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { // this line is used by starport scaffolding # 2 diff --git a/x/tradebin/keeper/calculator.go b/x/tradebin/keeper/calculator.go index 3d8f03b9..b3a25bd0 100644 --- a/x/tradebin/keeper/calculator.go +++ b/x/tradebin/keeper/calculator.go @@ -1,6 +1,7 @@ package keeper import ( + "cosmossdk.io/math" "fmt" "github.com/bze-alphateam/bze/x/tradebin/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -37,12 +38,12 @@ func CalculateMinAmount(price string) sdk.Int { // When the user submits an order we have to capture the coins needed for that order to be placed. // This function returns the sdk.Coins that we have to send from user's account to module and back func (k Keeper) GetOrderSdkCoin(orderType, orderPrice string, orderAmount sdk.Int, market *types.Market) (coin sdk.Coin, dust sdk.Dec, err error) { - var amount sdk.Int + var amount math.Int var denom string switch orderType { case types.OrderTypeBuy: denom = market.Quote - oAmount := orderAmount.ToDec() + oAmount := sdk.NewDec(orderAmount.Int64()) oPrice, err := sdk.NewDecFromStr(orderPrice) if err != nil { return coin, dust, sdkerrors.Wrapf(types.ErrInvalidOrderPrice, "error when transforming order price: %v", err) diff --git a/x/tradebin/module.go b/x/tradebin/module.go index 5944479b..6c993eec 100644 --- a/x/tradebin/module.go +++ b/x/tradebin/module.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "github.com/cosmos/cosmos-sdk/telemetry" - "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/spf13/cobra" "time" @@ -72,10 +71,6 @@ func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncod return genState.Validate() } -// RegisterRESTRoutes registers the capability module's REST service handlers. -func (AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, rtr *mux.Router) { -} - // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the module. func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))