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: enhance token module #2925

Merged
merged 12 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* (IRISHub) [\#2908](https://github.com/irisnet/irishub/pull/2908) Add ICA module.
* (IRISHub) [\#2909](https://github.com/irisnet/irishub/pull/2909) Add cosmos LSM.
dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved
* (IRISHub) [\#2925](https://github.com/irisnet/irishub/pull/2925) Enhance token module.

### Improvements

Expand Down
41 changes: 23 additions & 18 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ import (
mintkeeper "github.com/irisnet/irishub/v3/modules/mint/keeper"
minttypes "github.com/irisnet/irishub/v3/modules/mint/types"
iristypes "github.com/irisnet/irishub/v3/types"
"github.com/irisnet/irishub/v3/wrapper"
)

// AppKeepers defines a structure used to consolidate all
Expand Down Expand Up @@ -440,23 +441,6 @@ func New(
appKeepers.keys[guardiantypes.StoreKey],
)

appKeepers.TokenKeeper = tokenkeeper.NewKeeper(
appCodec,
appKeepers.keys[tokentypes.StoreKey],
appKeepers.BankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
).WithSwapRegistry(tokenv1.SwapRegistry{
iristypes.NativeToken.MinUnit: tokenv1.SwapParams{
MinUnit: iristypes.EvmToken.MinUnit,
Ratio: sdk.OneDec(),
},
iristypes.EvmToken.MinUnit: tokenv1.SwapParams{
MinUnit: iristypes.NativeToken.MinUnit,
Ratio: sdk.OneDec(),
},
})

appKeepers.RecordKeeper = recordkeeper.NewKeeper(
appCodec,
appKeepers.keys[recordtypes.StoreKey],
Expand Down Expand Up @@ -534,7 +518,7 @@ func New(
AddRoute(farmtypes.RouterKey, farm.NewCommunityPoolCreateFarmProposalHandler(appKeepers.FarmKeeper))

appKeepers.GovKeeper.SetHooks(govtypes.NewMultiGovHooks(
farmkeeper.NewGovHook(appKeepers.FarmKeeper),
wrapper.NewFarmGovHook(farmkeeper.NewGovHook(appKeepers.FarmKeeper)),
))

appKeepers.GovKeeper.SetLegacyRouter(govRouter)
Expand All @@ -561,6 +545,27 @@ func New(
cast.ToString(appOpts.Get(srvflags.EVMTracer)),
appKeepers.GetSubspace(evmtypes.ModuleName),
)

appKeepers.TokenKeeper = tokenkeeper.NewKeeper(
appCodec,
appKeepers.keys[tokentypes.StoreKey],
appKeepers.BankKeeper,
appKeepers.AccountKeeper,
wrapper.NewEVMKeeper(appKeepers.EvmKeeper),
wrapper.NewICS20Keeper(appKeepers.IBCTransferKeeper),
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
).WithSwapRegistry(tokenv1.SwapRegistry{
iristypes.NativeToken.MinUnit: tokenv1.SwapParams{
MinUnit: iristypes.EvmToken.MinUnit,
Ratio: sdk.OneDec(),
},
iristypes.EvmToken.MinUnit: tokenv1.SwapParams{
MinUnit: iristypes.NativeToken.MinUnit,
Ratio: sdk.OneDec(),
},
})
appKeepers.EvmKeeper = appKeepers.EvmKeeper.SetHooks(appKeepers.TokenKeeper.Hooks())
return appKeepers
}

Expand Down
12 changes: 4 additions & 8 deletions cmd/iris/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,13 @@ func InitTestnet(
return err
}

accTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)
accStakingTokens := sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction)
accEvmTokens := sdk.TokensFromConsensusPower(5000, PowerReduction)
accIrisTokens := sdk.TokensFromConsensusPower(5000, sdk.DefaultPowerReduction)
accStakingTokens := sdk.TokensFromConsensusPower(15e8, sdk.DefaultPowerReduction)
accEvmTokens := sdk.TokensFromConsensusPower(5e8, PowerReduction)

coins := sdk.Coins{
sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), accTokens),
coins := sdk.NewCoins(
sdk.NewCoin(sdk.DefaultBondDenom, accStakingTokens),
sdk.NewCoin(iristypes.EvmToken.MinUnit, accEvmTokens),
sdk.NewCoin(nativeIrisMinUnit, accIrisTokens),
}
)

genBalances = append(
genBalances,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/cosmos/iavl v0.20.1 // indirect
github.com/cosmos/ibc-go/v7 v7.3.0
github.com/evmos/ethermint v0.22.0
github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674 //release/v1.8.0-lsm
github.com/irisnet/irismod v1.8.1-0.20240419092732-1eefdb19bf41 //release/v1.8.0-lsm
)

require (
Expand Down Expand Up @@ -232,6 +232,6 @@ require (
replace github.com/zondax/hid => github.com/zondax/hid v0.9.0

// use bianjieai fork of ethermint
replace github.com/evmos/ethermint => github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240403080035-de4b07ecf255 //release/v0.2…20231207-lsm
replace github.com/evmos/ethermint => github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240419023144-3cea3d782107 //release/v0.2…20231207-lsm

replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s=
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240403080035-de4b07ecf255 h1:Rngrww/saMItIkpSozg5poBQiRHsE52iZnio5zII8EQ=
github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240403080035-de4b07ecf255/go.mod h1:x9BFez6AUL9Yksv4zZd3QLmSazwOkyNd3h2zFV2B4RU=
github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240419023144-3cea3d782107 h1:ssrDDPlhLILV0UaQD+D/mRqAvQ5FeQrWB3lltaP5YTc=
github.com/bianjieai/ethermint v0.22.0-irishub-20231207.0.20240419023144-3cea3d782107/go.mod h1:x9BFez6AUL9Yksv4zZd3QLmSazwOkyNd3h2zFV2B4RU=
github.com/bianjieai/nft-transfer v1.1.3-ibc-v7.3.0 h1:399lErsTpI+faTerw5Q4OuVlXAmAQSuibVvxHR3C6OY=
github.com/bianjieai/nft-transfer v1.1.3-ibc-v7.3.0/go.mod h1:u2PNH4v8CD4AWU4Rf7yt8/qqQtrrwwHiw03qQSKERhg=
github.com/bianjieai/tibc-go v0.5.0 h1:/J1OQe4gwUUkS3Q6+nm0EsTY7MNAOPNzfnpvgIWlvKM=
Expand Down Expand Up @@ -846,8 +846,8 @@ github.com/influxdata/promql/v2 v2.12.0/go.mod h1:fxOPu+DY0bqCTCECchSRtWfc+0X19y
github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bSgUQ7q5ZLSO+bKBGqJiCBGAl+9DxyW63zLTujjUlOE=
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po=
github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674 h1:7fBGecB0olPrkoeY0Y7wn202BQ4Q2zF8o+KwYFzNYQs=
github.com/irisnet/irismod v1.8.1-0.20240304075720-718d99d97674/go.mod h1:kvPd2HckP0Mr4BZRtUa2RjDNH1EfOP3xHESHRIXayOY=
github.com/irisnet/irismod v1.8.1-0.20240419092732-1eefdb19bf41 h1:WU15MSBJK66jhONVcc63DkCeM6LFhhWFPYoIi+mYyB0=
github.com/irisnet/irismod v1.8.1-0.20240419092732-1eefdb19bf41/go.mod h1:TQDhEmxqCIzo7OjyNy//UI0xKDuMtONjoy5LzUeRhVo=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e/go.mod h1:G1CVv03EnqU1wYL2dFwXxW2An0az9JTl/ZsqXQeBlkU=
Expand Down
14 changes: 4 additions & 10 deletions modules/evm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
type Keeper struct {
evmkeeper *evmkeeper.Keeper
bankKeeper types.BankKeeper
hasHook bool
}

var _ types.MsgServer = &Keeper{}
Expand Down Expand Up @@ -139,15 +138,10 @@ func (k *Keeper) EthereumTx(
return response, nil
}

// SetHooks sets the hooks for the EVM module
// It should be called only once during initialization, it panic if called more than once.
func (k *Keeper) SetHooks(eh types.EvmHooks) *Keeper {
return &Keeper{
evmkeeper: k.evmkeeper.SetHooks(eh),
hasHook: true,
}
}

// UpdateParams updates the parameters for the EVM module.
//
// It takes a context.Context object and a *types.MsgUpdateParams object as parameters.
// The function returns a *types.MsgUpdateParamsResponse object and an error.
func (k *Keeper) UpdateParams(
goCtx context.Context,
msg *types.MsgUpdateParams,
Expand Down
2 changes: 1 addition & 1 deletion modules/evm/moudle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewAppModule(
) AppModule {
return AppModule{
AppModule: ethermint.NewAppModule(k, ak, ss),
k: &Keeper{k, bankKeeper, false},
k: &Keeper{k, bankKeeper},
ss: ss,
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/evm/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction, fee
// snapshot to contain the tx processing and post processing in same scope
var commit func()
tmpCtx := ctx
if k.hasHook {
if k.evmkeeper.Hooks() != nil {
dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved
// Create a cache context to revert state when tx hooks fails,
// the cache context is only committed when both tx and hooks executed successfully.
// Didn't use `Snapshot` because the context stack has exponential complexity on certain operations,
Expand Down
3 changes: 2 additions & 1 deletion modules/mint/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -52,6 +53,6 @@ func (s *IntegrationTestSuite) TestMint() {
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(bz.Bytes(), respType))
params := respType.(*minttypes.Params)
s.Require().Equal("stake", params.MintDenom)
s.Require().Equal(sdk.DefaultBondDenom, params.MintDenom)
s.Require().Equal("0.040000000000000000", params.Inflation.String())
}
2 changes: 1 addition & 1 deletion modules/mint/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { inflation = GenInflation(r) },
)

params := types.Params{Inflation: inflation, MintDenom: types.MintDenom}
params := types.Params{Inflation: inflation, MintDenom: sdk.DefaultBondDenom}
mintGenesis := types.NewGenesisState(types.DefaultMinter(), params)

bz, err := json.MarshalIndent(&mintGenesis, "", " ")
Expand Down
3 changes: 1 addition & 2 deletions modules/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
// params store for inflation params
KeyInflation = []byte("Inflation")
KeyMintDenom = []byte("MintDenom")
MintDenom = sdk.DefaultBondDenom
)

// ParamTable for mint module
Expand All @@ -41,7 +40,7 @@ func NewParams(mintDenom string, inflation sdk.Dec) Params {
func DefaultParams() Params {
return Params{
Inflation: sdk.NewDecWithPrec(4, 2),
MintDenom: MintDenom,
MintDenom: sdk.DefaultBondDenom,
}
}

Expand Down
2 changes: 2 additions & 0 deletions types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func init() {
Mintable: true,
Owner: sdk.AccAddress(crypto.AddressHash([]byte(tokentypes.ModuleName))).String(),
}
sdk.DefaultBondDenom = NativeToken.MinUnit
dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved


userHomeDir, err := os.UserHomeDir()
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions wrapper/farm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package wrapper

import (
"github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

farmkeeper "github.com/irisnet/irismod/modules/farm/keeper"
)

var _ govtypes.GovHooks = farmGovHook{}

type farmGovHook struct {
gh farmkeeper.GovHook
}

// NewFarmGovHook creates a new farmGovHook instance.
//
// It takes a parameter of type farmkeeper.GovHook and returns a farmGovHook.
func NewFarmGovHook(gh farmkeeper.GovHook) govtypes.GovHooks {
return farmGovHook{
gh: gh,
}
}

// AfterProposalDeposit implements types.GovHooks.
func (f farmGovHook) AfterProposalDeposit(ctx types.Context, proposalID uint64, depositorAddr types.AccAddress) error {
f.gh.AfterProposalDeposit(ctx, proposalID, depositorAddr)
return nil
}

// AfterProposalFailedMinDeposit implements types.GovHooks.
func (f farmGovHook) AfterProposalFailedMinDeposit(ctx types.Context, proposalID uint64) error {
f.gh.AfterProposalFailedMinDeposit(ctx, proposalID)
return nil
}

// AfterProposalSubmission implements types.GovHooks.
func (f farmGovHook) AfterProposalSubmission(ctx types.Context, proposalID uint64) error {
f.gh.AfterProposalSubmission(ctx, proposalID)
return nil
}

// AfterProposalVote implements types.GovHooks.
func (f farmGovHook) AfterProposalVote(ctx types.Context, proposalID uint64, voterAddr types.AccAddress) error {
f.gh.AfterProposalVote(ctx, proposalID,voterAddr)
return nil
}

// AfterProposalVotingPeriodEnded implements types.GovHooks.
func (f farmGovHook) AfterProposalVotingPeriodEnded(ctx types.Context, proposalID uint64) error {
f.gh.AfterProposalVotingPeriodEnded(ctx, proposalID)
return nil
}
105 changes: 105 additions & 0 deletions wrapper/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package wrapper

import (
"context"
"math/big"
"strings"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/evmos/ethermint/crypto/ethsecp256k1"
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
evmtypes "github.com/evmos/ethermint/x/evm/types"

ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"

tokentypes "github.com/irisnet/irismod/modules/token/types"
irismodtypes "github.com/irisnet/irismod/types"
)

var (
_ tokentypes.EVMKeeper = (*evmKeeper)(nil)
_ tokentypes.ICS20Keeper = (*ics20Keeper)(nil)
)

// NewEVMKeeper wraps the given evmkeeper.Keeper and returns a new evmKeeper.
//
// ek: The evmkeeper.Keeper to be wrapped.
// Returns a pointer to the wrapped evmKeeper.
func NewEVMKeeper(ek *evmkeeper.Keeper) tokentypes.EVMKeeper {
return &evmKeeper{ek: ek}
}

type evmKeeper struct {
ek *evmkeeper.Keeper
}

// ApplyMessage implements types.EVMKeeper.
func (e *evmKeeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*irismodtypes.Result, error) {
res, err := e.ek.ApplyMessage(ctx, msg, tracer, commit)
if err != nil {
return nil, err
}
return &irismodtypes.Result{
Hash: res.Hash,
Logs: evmtypes.LogsToEthereum(res.Logs),
Ret: res.Ret,
VMError: res.VmError,
GasUsed: res.GasUsed,
}, nil
}

// ChainID implements types.EVMKeeper.
func (e *evmKeeper) ChainID() *big.Int {
return e.ek.ChainID()
}

// EstimateGas implements types.EVMKeeper.
func (e *evmKeeper) EstimateGas(ctx context.Context, req *irismodtypes.EthCallRequest) (uint64, error) {
res, err := e.ek.EstimateGas(ctx, &evmtypes.EthCallRequest{
Args: req.Args,
GasCap: req.GasCap,
ProposerAddress: req.ProposerAddress,
ChainId: req.ChainID,
})
if err != nil {
return 0, err
}
return res.Gas, nil
}

// SupportedKey implements types.EVMKeeper.
func (e *evmKeeper) SupportedKey(pubKey cryptotypes.PubKey) bool {
_, ok := pubKey.(*ethsecp256k1.PubKey)
return ok
}

// NewICS20Keeper wraps the given ibctransferkeeper.Keeper into an ics20Keeper.
//
// Parameters:
// - ik: the ibctransferkeeper.Keeper to be wrapped.
//
// Return:
// - *ics20Keeper: the wrapped ics20Keeper.
func NewICS20Keeper(ik ibctransferkeeper.Keeper) tokentypes.ICS20Keeper {
return &ics20Keeper{ik: ik}
}

type ics20Keeper struct {
ik ibctransferkeeper.Keeper
}

// HasTrace implements types.ICS20Keeper.
func (i *ics20Keeper) HasTrace(ctx sdk.Context, denom string) bool {
hash, err := ibctransfertypes.ParseHexHash(strings.TrimPrefix(denom, "ibc/"))
if err != nil {
return false
}
_, has := i.ik.GetDenomTrace(ctx, hash)
return has
}
Loading