Skip to content

Commit

Permalink
chore(lint): using new math module from sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ivivanov authored and Lockwarr committed Dec 22, 2023
1 parent c8ce20e commit 30c93f1
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 39 deletions.
5 changes: 3 additions & 2 deletions wasmbinding/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package wasmbinding

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkquery "github.com/cosmos/cosmos-sdk/types/query"

"github.com/Nolus-Protocol/nolus-core/wasmbinding/bindings"

errorsmod "cosmossdk.io/errors"

"github.com/neutron-org/neutron/x/interchainqueries/types"
icatypes "github.com/neutron-org/neutron/x/interchaintxs/types"
)
Expand Down Expand Up @@ -78,7 +79,7 @@ func (qp *QueryPlugin) GetRegisteredInterchainQuery(ctx sdk.Context, req *bindin
return nil, err
}
if grpcResp == nil {
return nil, sdkerrors.Wrapf(types.ErrEmptyResult, "interchain query response empty for query id %d", req.QueryID)
return nil, errorsmod.Wrapf(types.ErrEmptyResult, "interchain query response empty for query id %d", req.QueryID)
}
query := mapGRPCRegisteredQueryToWasmBindings(*grpcResp)

Expand Down
28 changes: 14 additions & 14 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ var (
errTimeInFutureBeforeTimePassed = errors.New("time in future can not be before passed time")
)

func calcFunctionIncrement(nanoSecondsPassed sdk.Uint) sdk.Dec {
func calcFunctionIncrement(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
return types.NormMonthsRange.Mul(calcFixedIncrement(nanoSecondsPassed))
}

func calcFixedIncrement(nanoSecondsPassed sdk.Uint) sdk.Dec {
func calcFixedIncrement(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
return types.DecFromUint(nanoSecondsPassed).Quo(nanoSecondsInMonth)
}

func calcTimeDifference(blockTime sdk.Uint, prevBlockTime sdk.Uint, maxMintableSeconds sdk.Uint) sdk.Uint {
func calcTimeDifference(blockTime, prevBlockTime, maxMintableSeconds sdkmath.Uint) sdkmath.Uint {
if prevBlockTime.GT(blockTime) {
panic("new block time cannot be smaller than previous block time")
}
Expand All @@ -42,7 +42,7 @@ func calcTimeDifference(blockTime sdk.Uint, prevBlockTime sdk.Uint, maxMintableS
return nsecBetweenBlocks
}

func calcTokens(blockTime sdk.Uint, minter *types.Minter, maxMintableSeconds sdk.Uint) sdk.Uint {
func calcTokens(blockTime sdkmath.Uint, minter *types.Minter, maxMintableSeconds sdkmath.Uint) sdkmath.Uint {
if minter.TotalMinted.GTE(types.MintingCap) {
return sdkmath.ZeroUint()
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func calcTokens(blockTime sdk.Uint, minter *types.Minter, maxMintableSeconds sdk
}
}

func updateMinter(minter *types.Minter, blockTime sdk.Uint, newNormTime sdk.Dec, newlyMinted sdk.Uint) sdk.Uint {
func updateMinter(minter *types.Minter, blockTime sdkmath.Uint, newNormTime sdkmath.LegacyDec, newlyMinted sdkmath.Uint) sdkmath.Uint {
if newlyMinted.LT(sdkmath.ZeroUint()) {
// Sanity check, should not happen. However, if this were to happen,
// do not update the minter state (primary the previous block timestamp)
Expand All @@ -93,9 +93,9 @@ func updateMinter(minter *types.Minter, blockTime sdk.Uint, newNormTime sdk.Dec,

// Returns the amount of tokens that should be minted by the integral formula
// for the period between normTimePassed and the timeInFuture.
func predictMintedByIntegral(totalMinted sdk.Uint, normTimePassed, timeAhead sdk.Dec) (sdk.Uint, error) {
func predictMintedByIntegral(totalMinted sdkmath.Uint, normTimePassed, timeAhead sdkmath.LegacyDec) (sdkmath.Uint, error) {
timeAheadNs := timeAhead.Mul(nanoSecondsInMonth).TruncateInt()
normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdk.Uint(timeAheadNs)))
normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdkmath.Uint(timeAheadNs)))
if normTimePassed.GT(normTimeInFuture) {
return sdkmath.ZeroUint(), errTimeInFutureBeforeTimePassed
}
Expand All @@ -114,35 +114,35 @@ func predictMintedByIntegral(totalMinted sdk.Uint, normTimePassed, timeAhead sdk

// Returns the amount of tokens that should be minted during the fixed amount period
// for the period between NormTimePassed and the timeInFuture.
func predictMintedByFixedAmount(totalMinted sdk.Uint, normTimePassed, timeAhead sdk.Dec) (sdk.Uint, error) {
func predictMintedByFixedAmount(totalMinted sdkmath.Uint, normTimePassed, timeAhead sdkmath.LegacyDec) (sdkmath.Uint, error) {
timeAheadNs := timeAhead.Mul(nanoSecondsInMonth).TruncateInt()

normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdk.Uint(timeAheadNs)))
normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdkmath.Uint(timeAheadNs)))
if normTimePassed.GT(normTimeInFuture) {
return sdkmath.ZeroUint(), errTimeInFutureBeforeTimePassed
}

normFixedPeriod := normTimeInFuture.Sub(calcFunctionIncrement(sdk.Uint(nanoSecondsInFormula.TruncateInt())))
normFixedPeriod := normTimeInFuture.Sub(calcFunctionIncrement(sdkmath.Uint(nanoSecondsInFormula.TruncateInt())))
if normFixedPeriod.LTE(sdk.ZeroDec()) {
return sdkmath.ZeroUint(), nil
}

// convert norm time to non norm time
fixedPeriod := normFixedPeriod.Sub(types.NormOffset).Quo(types.NormMonthsRange)

newlyMinted := fixedPeriod.MulInt(sdk.Int(types.FixedMintedAmount))
newlyMinted := fixedPeriod.MulInt(sdkmath.Int(types.FixedMintedAmount))
// Trim off excess tokens if the cap is reached
if totalMinted.Add(sdk.Uint(newlyMinted.TruncateInt())).GT(types.MintingCap) {
if totalMinted.Add(sdkmath.Uint(newlyMinted.TruncateInt())).GT(types.MintingCap) {
return types.MintingCap.Sub(totalMinted), nil
}

return sdk.Uint(newlyMinted.TruncateInt()), nil
return sdkmath.Uint(newlyMinted.TruncateInt()), nil
}

// Returns the amount of tokens that should be minted
// between the NormTimePassed and the timeAhead
// timeAhead expects months represented in decimal form.
func predictTotalMinted(totalMinted sdk.Uint, normTimePassed, timeAhead sdk.Dec) sdk.Uint {
func predictTotalMinted(totalMinted sdkmath.Uint, normTimePassed, timeAhead sdkmath.LegacyDec) sdkmath.Uint {
integralAmount, err := predictMintedByIntegral(totalMinted, normTimePassed, timeAhead)
if err != nil {
return sdkmath.ZeroUint()
Expand Down
14 changes: 7 additions & 7 deletions x/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func Test_CalcTokensDuringFormula_WhenUsingVaryingIncrements_OutputExpectedToken
mintThreshold := sdk.NewUint(10_000_000) // 10 tokens
fmt.Printf("%v Returned Total, %v Total Minted(in store), %v Norm Time \n", mintedCoins, minter.TotalMinted, minter.NormTimePassed)

if types.GetAbsDiff(expectedCoins60Sec, mintedCoins).GT(mintThreshold) || types.GetAbsDiff(expectedCoins60Sec, sdk.Uint(minter.TotalMinted)).GT(mintThreshold) {
if types.GetAbsDiff(expectedCoins60Sec, mintedCoins).GT(mintThreshold) || types.GetAbsDiff(expectedCoins60Sec, sdkmath.Uint(minter.TotalMinted)).GT(mintThreshold) {
t.Errorf("Minted unexpected amount of tokens, expected [%v +/- %v] returned and in store, actual minted %v, actual in store %v",
expectedCoins60Sec, mintThreshold, mintedCoins, minter.TotalMinted)
}
Expand Down Expand Up @@ -340,8 +340,8 @@ func Test_PredictMintedByIntegral_TwelveMonthsAhead(t *testing.T) {
title string
normTimePassed sdk.Dec
timeAhead sdk.Dec
totalMinted sdk.Uint
expIntegralMinted sdk.Uint
totalMinted sdkmath.Uint
expIntegralMinted sdkmath.Uint
expError bool
}{
{
Expand Down Expand Up @@ -436,8 +436,8 @@ func Test_PredictMintedByFixedAmount_TwelveMonthsAhead(t *testing.T) {
title string
normTimePassed sdk.Dec
timeAhead sdk.Dec
totalMinted sdk.Uint
expFixedMinted sdk.Uint
totalMinted sdkmath.Uint
expFixedMinted sdkmath.Uint
expError bool
}{
{
Expand Down Expand Up @@ -528,14 +528,14 @@ func randomTimeBetweenBlocks(min uint64, max uint64, r *rand.Rand) uint64 {
return uint64(time.Second.Nanoseconds()) * (uint64(r.Int63n(sdk.NewIntFromUint64(max-min).Int64())) + min)
}

func defaultParams() (types.Minter, sdk.Uint, sdk.Uint, sdk.Uint) {
func defaultParams() (types.Minter, sdkmath.Uint, sdkmath.Uint, sdkmath.Uint) {
minter := types.InitialMinter()
mintedCoins := sdk.NewUint(0)
mintedMonth := sdk.NewUint(0)
timeOffset := sdk.NewUint(uint64(util.GetCurrentTimeUnixNano()))
return minter, mintedCoins, mintedMonth, timeOffset
}

func uintFromDec(d sdk.Dec) sdk.Uint {
func uintFromDec(d sdk.Dec) sdkmath.Uint {
return sdk.NewUint(d.TruncateInt().Uint64())
}
2 changes: 1 addition & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions x/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import (
"math/rand"
"time"

sdkmath "cosmossdk.io/math"
"github.com/Nolus-Protocol/nolus-core/x/mint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

// GenMaxMintableNanoseconds generates random MaxMintableNanoseconds in range [1-60).
func GenMaxMintableNanoseconds(r *rand.Rand) sdk.Uint {
func GenMaxMintableNanoseconds(r *rand.Rand) sdkmath.Uint {
return sdk.NewUint(uint64(time.Second.Nanoseconds() * int64(r.Intn(59)+1)))
}

// RandomizedGenState generates a random GenesisState for mint.
func RandomizedGenState(simState *module.SimulationState) {
// minter
var maxMintableNSecs sdk.Uint
var maxMintableNSecs sdkmath.Uint
simState.AppParams.GetOrGenerate(
simState.Cdc, string(types.KeyMaxMintableNanoseconds), &maxMintableNSecs, simState.Rand,
func(r *rand.Rand) { maxMintableNSecs = GenMaxMintableNanoseconds(r) },
Expand Down
2 changes: 1 addition & 1 deletion x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestRandomizedGenState1(t *testing.T) {
func TestGenMaxMintableNanoseconds(t *testing.T) {
tests := []struct {
r *rand.Rand
expectedMaxMintable sdk.Uint
expectedMaxMintable sdkmath.Uint
}{
{rand.New(rand.NewSource(1)), sdk.NewUint(uint64((4000000000)))},
{rand.New(rand.NewSource(0)), sdk.NewUint(uint64((50000000000)))},
Expand Down
3 changes: 2 additions & 1 deletion x/mint/simulation/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ package simulation
import (
"math/rand"

sdkmath "cosmossdk.io/math"
sdktypes "github.com/cosmos/cosmos-sdk/types"
)

// refactor: decide if we want to use this in simulations
// RandomMaxMintableNanoSeconds generates a random maximum mintable nano seconds in the range of [lowerRange, upperRange]
func RandomMaxMintableNanoSeconds(r *rand.Rand, lowerRange, upperRange int) sdktypes.Uint {
func RandomMaxMintableNanoSeconds(r *rand.Rand, lowerRange, upperRange int) sdkmath.Uint {
randomMaxMintableNanoSeconds := r.Intn(upperRange) + lowerRange
return sdktypes.NewUint(uint64(randomMaxMintableNanoSeconds))
}
10 changes: 5 additions & 5 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (

// NewMinter returns a new Minter object with the given inflation and annual
// provisions values.
func NewMinter(normTimePassed sdk.Dec, totalMinted, prevBlockTimestamp, inflation sdk.Uint) Minter {
func NewMinter(normTimePassed sdk.Dec, totalMinted, prevBlockTimestamp, inflation sdkmath.Uint) Minter {
return Minter{
NormTimePassed: normTimePassed,
TotalMinted: totalMinted,
Expand Down Expand Up @@ -82,7 +82,7 @@ func ValidateMinter(minter Minter) error {
return nil
}

func calcMintedTokens(m Minter) sdk.Uint {
func calcMintedTokens(m Minter) sdkmath.Uint {
if m.NormTimePassed.GTE(MonthsInFormula) {
fixedMonthsPeriod := sdk.NewUint(m.NormTimePassed.Sub(MonthsInFormula).TruncateInt().Uint64())
fixedMonthsTokens := fixedMonthsPeriod.Mul(FixedMintedAmount)
Expand All @@ -96,18 +96,18 @@ func calcMintedTokens(m Minter) sdk.Uint {

// Integral: -1.08319 x^4 + 314.871 x^3 - 44283.6 x^2 + 3.86335×10^6 x
// transformed to: (((-1.08319 x + 314.871) x - 44283.6) x +3.86335×10^6) x.
func CalcTokensByIntegral(x sdk.Dec) sdk.Uint {
func CalcTokensByIntegral(x sdk.Dec) sdkmath.Uint {
return util.ConvertToMicroNolusDec(((((QuadCoef.Mul(x).Add(CubeCoef)).Mul(x).Add(SquareCoef)).Mul(x).Add(Coef)).Mul(x)))
}

func GetAbsDiff(a sdk.Uint, b sdk.Uint) sdk.Uint {
func GetAbsDiff(a, b sdkmath.Uint) sdkmath.Uint {
if a.GTE(b) {
return a.Sub(b)
}

return b.Sub(a)
}

func DecFromUint(u sdk.Uint) sdk.Dec {
func DecFromUint(u sdkmath.Uint) sdkmath.LegacyDec {
return sdk.NewDecFromBigInt(u.BigInt())
}
6 changes: 3 additions & 3 deletions x/mint/types/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func Test_calcMintedTokens(t *testing.T) {

for _, tc := range []struct {
title string
normTimePassed sdk.Dec
expTotalMinted sdk.Uint
normTimePassed sdkmath.LegacyDec
expTotalMinted sdkmath.Uint
}{
{
title: "starting at genesis",
Expand Down Expand Up @@ -61,7 +61,7 @@ func Test_ValidateMinter(t *testing.T) {
for _, tc := range []struct {
title string
normTimePassed sdk.Dec
totalMinted sdk.Uint
totalMinted sdkmath.Uint
expErr bool
}{
{
Expand Down
4 changes: 2 additions & 2 deletions x/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

func NewParams(mintDenom string, maxMintableNanoseconds sdk.Uint) Params {
func NewParams(mintDenom string, maxMintableNanoseconds sdkmath.Uint) Params {
return Params{
MintDenom: mintDenom,
MaxMintableNanoseconds: maxMintableNanoseconds,
Expand Down Expand Up @@ -61,7 +61,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
}

func validateMaxMintableNanoseconds(i interface{}) error {
v, ok := i.(sdk.Uint)
v, ok := i.(sdkmath.Uint)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
Expand Down
2 changes: 1 addition & 1 deletion x/tax/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
}

// ProposalContents doesn't return any content functions for governance proposals.
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return nil
}

Expand Down

0 comments on commit 30c93f1

Please sign in to comment.