Skip to content

Commit

Permalink
math changes and ante
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan.balea committed Jan 11, 2025
1 parent 63f101c commit 5d891e0
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 67 deletions.
5 changes: 2 additions & 3 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions cmd/bzed/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/bze-alphateam/bze/app"
"io"
"os"
"path/filepath"

"github.com/spf13/cast"
"github.com/spf13/cobra"
Expand All @@ -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"

Expand Down
28 changes: 15 additions & 13 deletions x/burner/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
17 changes: 11 additions & 6 deletions x/burner/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{}
}
5 changes: 0 additions & 5 deletions x/cointrunk/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions x/epochs/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions x/rewards/keeper/staking_reward_distribution.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions x/rewards/keeper/staking_reward_distribution_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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)

Expand Down
5 changes: 0 additions & 5 deletions x/rewards/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
Expand Down
5 changes: 0 additions & 5 deletions x/scavenge/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions x/tokenfactory/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions x/tradebin/keeper/calculator.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions x/tradebin/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 5d891e0

Please sign in to comment.