Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update dec and int to use cosmossdk.io/math #1872

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1776](https://github.com/NibiruChain/nibiru/pull/1776) - feat(inflation): make inflation params a collection and add commands to update them
- [#1820](https://github.com/NibiruChain/nibiru/pull/1820) - feat(ica): add interchain accounts
- [#1864](https://github.com/NibiruChain/nibiru/pull/1864) - fix(ica): add ICA controller stack
- [#1872](https://github.com/NibiruChain/nibiru/pull/1872) - chore(math): use cosmossdk.io/math to replace sdk types

#### Nibiru EVM

Expand All @@ -57,7 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1855](https://github.com/NibiruChain/nibiru/pull/1855) - feat(eth-pubsub): Implement in-memory EventBus for real-time topic management and event distribution
- [#1856](https://github.com/NibiruChain/nibiru/pull/1856) - feat(eth-rpc): Conversion types and functions between Ethereum txs and blocks and Tendermint ones.
- [#1861](https://github.com/NibiruChain/nibiru/pull/1861) - feat(eth-rpc): RPC backend, Ethereum tracer, KV indexer, and RPC APIs
- [#1869](https://github.com/NibiruChain/nibiru/pull/1869) - feat(eth): Module and start of keeper tests
- [#1869](https://github.com/NibiruChain/nibiru/pull/1869) - feat(eth): Module and start of keeper tests

#### Dapp modules: perp, spot, oracle, etc

Expand Down
3 changes: 2 additions & 1 deletion app/ante/commission.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ante

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func MAX_COMMISSION() sdk.Dec { return sdk.MustNewDecFromStr("0.25") }
func MAX_COMMISSION() sdk.Dec { return math.LegacyMustNewDecFromStr("0.25") }

var _ sdk.AnteDecorator = (*AnteDecoratorStakingCommission)(nil)

Expand Down
19 changes: 10 additions & 9 deletions app/ante/commission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ante_test
import (
"testing"

"cosmossdk.io/math"
sdkclienttx "github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -32,16 +33,16 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {

valAddr := sdk.ValAddress(testutil.AccAddress()).String()
commissionRatePointer := new(sdk.Dec)
*commissionRatePointer = sdk.NewDecWithPrec(10, 2)
*commissionRatePointer = math.LegacyNewDecWithPrec(10, 2)
happyMsgs := []sdk.Msg{
&stakingtypes.MsgCreateValidator{
Description: mockDescription,
Commission: stakingtypes.CommissionRates{
Rate: sdk.NewDecWithPrec(6, 2), // 6%
MaxRate: sdk.NewDec(420),
MaxChangeRate: sdk.NewDec(420),
Rate: math.LegacyNewDecWithPrec(6, 2), // 6%
MaxRate: math.LegacyNewDec(420),
MaxChangeRate: math.LegacyNewDec(420),
},
MinSelfDelegation: sdk.NewInt(1),
MinSelfDelegation: math.NewInt(1),
DelegatorAddress: testutil.AccAddress().String(),
ValidatorAddress: valAddr,
Pubkey: &codectypes.Any{},
Expand All @@ -58,12 +59,12 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {
createSadMsgs := func() []sdk.Msg {
sadMsgCreateVal := new(stakingtypes.MsgCreateValidator)
*sadMsgCreateVal = *(happyMsgs[0]).(*stakingtypes.MsgCreateValidator)
sadMsgCreateVal.Commission.Rate = sdk.NewDecWithPrec(26, 2)
sadMsgCreateVal.Commission.Rate = math.LegacyNewDecWithPrec(26, 2)

sadMsgEditVal := new(stakingtypes.MsgEditValidator)
*sadMsgEditVal = *(happyMsgs[1]).(*stakingtypes.MsgEditValidator)
newCommissionRate := new(sdk.Dec)
*newCommissionRate = sdk.NewDecWithPrec(26, 2)
*newCommissionRate = math.LegacyNewDecWithPrec(26, 2)
sadMsgEditVal.CommissionRate = newCommissionRate

return []sdk.Msg{
Expand Down Expand Up @@ -110,8 +111,8 @@ func (s *AnteTestSuite) TestAnteDecoratorStakingCommission() {
} {
s.T().Run(tc.name, func(t *testing.T) {
txGasCoins := sdk.NewCoins(
sdk.NewCoin("unibi", sdk.NewInt(1_000)),
sdk.NewCoin("utoken", sdk.NewInt(500)),
sdk.NewCoin("unibi", math.NewInt(1_000)),
sdk.NewCoin("utoken", math.NewInt(500)),
)

encCfg := app.MakeEncodingConfig()
Expand Down
5 changes: 3 additions & 2 deletions app/ante/fixed_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

sdkioerrors "cosmossdk.io/errors"
"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -239,8 +240,8 @@ func (suite *AnteTestSuite) TestOraclePostPriceTransactionsHaveFixedPrice() {
} else {
suite.NoError(err)
}
want := sdk.NewInt(int64(tc.expectedGas))
got := sdk.NewInt(int64(suite.ctx.GasMeter().GasConsumed()))
want := math.NewInt(int64(tc.expectedGas))
got := math.NewInt(int64(suite.ctx.GasMeter().GasConsumed()))
suite.Equal(want.String(), got.String())
})
}
Expand Down
5 changes: 3 additions & 2 deletions app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (suite *IBCTestSuite) TestHandleMsgTransfer() {
path := NewIBCTestingTransferPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)

amount, ok := sdk.NewIntFromString("9223372036854775808") // 2^63 (one above int64)
amount, ok := math.NewIntFromString("9223372036854775808") // 2^63 (one above int64)
suite.Require().True(ok)
coinToSendToB := sdk.NewCoin(sdk.DefaultBondDenom, amount)

Expand Down Expand Up @@ -196,7 +197,7 @@ func (suite *IBCTestSuite) TestHandleMsgTransfer() {
// check that module account escrow address is empty
escrowAddress := transfertypes.GetEscrowAddress(packet.GetDestPort(), packet.GetDestChannel())
balance = chainBApp.BankKeeper.GetBalance(suite.chainB.GetContext(), escrowAddress, sdk.DefaultBondDenom)
suite.Require().Equal(sdk.NewCoin(sdk.DefaultBondDenom, sdk.ZeroInt()), balance)
suite.Require().Equal(sdk.NewCoin(sdk.DefaultBondDenom, math.ZeroInt()), balance)

// check that balance on chain B is empty
balance = chainCApp.BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), voucherDenomTrace.IBCDenom())
Expand Down
3 changes: 2 additions & 1 deletion app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"

"cosmossdk.io/math"
sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -68,7 +69,7 @@ var _ module.HasGenesisBasics = (*StakingModule)(nil)
func (StakingModule) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
genState := stakingtypes.DefaultGenesisState()
genState.Params.BondDenom = appconst.BondDenom
genState.Params.MinCommissionRate = sdk.MustNewDecFromStr("0.05")
genState.Params.MinCommissionRate = math.LegacyMustNewDecFromStr("0.05")
return cdc.MustMarshalJSON(genState)
}

Expand Down
4 changes: 2 additions & 2 deletions app/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"cosmossdk.io/math"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -42,7 +42,7 @@ func (s *TestSuite) TestGenesis() {
gens = append(gens, getDefaultStakingGenesis())

genStaking := getDefaultStakingGenesis()
genStaking.Params.MinCommissionRate = sdk.ZeroDec()
genStaking.Params.MinCommissionRate = math.LegacyZeroDec()
gens = append(gens, genStaking)

for _, tc := range []struct {
Expand Down
4 changes: 3 additions & 1 deletion app/wasmext/wasm_cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

wasmcli "github.com/CosmWasm/wasmd/x/wasm/client/cli"

"cosmossdk.io/math"

"github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/testutil/cli"
Expand All @@ -26,7 +28,7 @@ var commonArgs = []string{
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees,
sdk.NewCoins(sdk.NewCoin(denoms.NIBI, sdk.NewInt(10_000_000))).String()),
sdk.NewCoins(sdk.NewCoin(denoms.NIBI, math.NewInt(10_000_000))).String()),
}

type IntegrationTestSuite struct {
Expand Down
5 changes: 3 additions & 2 deletions cmd/nibid/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"

"cosmossdk.io/math"
tmconfig "github.com/cometbft/cometbft/config"
tmos "github.com/cometbft/cometbft/libs/os"
tmrand "github.com/cometbft/cometbft/libs/rand"
Expand Down Expand Up @@ -215,8 +216,8 @@ func InitTestnet(
valPubKeys[i],
sdk.NewCoin(denoms.NIBI, valTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(sdk.OneDec(), sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(),
stakingtypes.NewCommissionRates(math.LegacyOneDec(), math.LegacyOneDec(), math.LegacyOneDec()),
math.OneInt(),
)
if err != nil {
return err
Expand Down
62 changes: 62 additions & 0 deletions contrib/scripts/feat-perp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
set -e

# add_genesis_reserve_amt: Used to configure initial reserve values of genesis
# perp markets.
add_genesis_reserve_amt() {
local M=1000000
local num_users=300000
local faucet_nusd_amt=100
local reserve_amt=$(($num_users * $faucet_nusd_amt * $M))
echo "$reserve_amt"
}

# add_genesis_perp_markets_with_coingecko_prices: Queries Coingecko to set the
# initial values to for x/perp markets in the genesis.
add_genesis_perp_markets_with_coingecko_prices() {
local temp_json_fname="tmp_market_prices.json"
curl -X 'GET' \
'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cethereum&vs_currencies=usd' \
-H 'accept: application/json' \
>$temp_json_fname

local reserve_amt=$(add_genesis_reserve_amt)

price_btc=$(cat tmp_market_prices.json | jq -r '.bitcoin.usd')
price_btc=${price_btc%.*}
if [ -z "$price_btc" ]; then
return 1
fi

check_fail() {
if [ $? -eq 0 ]; then
echo "Command \"$*\" executed successfully."
else
echo "Command \"$*\" failed."
exit 1
fi
}

check_fail nibid genesis add-genesis-perp-market --pair="ubtc:unusd" --sqrt-depth="$reserve_amt" --price-multiplier="$price_btc" --oracle-pair="ubtc:uusd"

price_eth=$(cat tmp_market_prices.json | jq -r '.ethereum.usd')
price_eth=${price_eth%.*}
if [ -z "$price_eth" ]; then
return 1
fi

check_fail nibid genesis add-genesis-perp-market --pair="ueth:unusd" --sqrt-depth=$reserve_amt --price-multiplier="$price_eth" --oracle-pair="ueth:uusd"

echo 'tmp_market_prices: '
cat $temp_json_fname | jq .
rm -f $temp_json_fname
}

add_genesis_perp_markets_offline() {
local reserve_amt
reserve_amt=$(add_genesis_reserve_amt)
price_btc="20000"
price_eth="2000"
nibid genesis add-genesis-perp-market --pair="ubtc:unusd" --sqrt-depth="$reserve_amt" --price-multiplier="$price_btc" --oracle-pair="ubtc:uusd"
nibid genesis add-genesis-perp-market --pair="ueth:unusd" --sqrt-depth="$reserve_amt" --price-multiplier="$price_eth" --oracle-pair="ueth:uusd"
}
Loading
Loading