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

chore(erc20): revert unnecessary logic on genesis #2292

Merged
merged 11 commits into from
Jan 25, 2024
1 change: 1 addition & 0 deletions .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ jobs:
VALIDATE_GO: false
DEFAULT_BRANCH: "main"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FILTER_REGEX_EXCLUDE: .*.jsonnet
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (stride-outpost) [#2207](https://github.com/evmos/evmos/pull/2207) Update Stride outpost to include additional arguments.
- (incentives) [#2221](https://github.com/evmos/evmos/pull/2221) Burn the usage incentives pool balance during v16 upgrade.
- (erc20) [#2217](https://github.com/evmos/evmos/pull/2217) Add logic to deploy token pairs contracts on genesis.
- (erc20) [#2292](https://github.com/evmos/evmos/pull/2292) Revert unnecessary logic on genesis (reverts #2217).

### API Breaking

Expand Down
100 changes: 100 additions & 0 deletions tests/nix_tests/configs/osmosis-outpost.jsonnet

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions x/erc20/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ func InitGenesis(
panic("the erc20 module account has not been set")
}

if err := k.SetGenesisTokenPairs(ctx, data.TokenPairs); err != nil {
panic(err)
for _, pair := range data.TokenPairs {
id := pair.GetID()
k.SetTokenPair(ctx, pair)
k.SetDenomMap(ctx, pair.Denom, id)
k.SetERC20Map(ctx, pair.GetERC20Contract(), id)
}
}

Expand Down
30 changes: 0 additions & 30 deletions x/erc20/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/suite"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -81,17 +80,14 @@ func (suite *GenesisTestSuite) TestERC20InitGenesis() {
testCases := []struct {
name string
genesisState types.GenesisState
malleate func()
}{
{
name: "empty genesis",
genesisState: types.GenesisState{},
malleate: nil,
},
{
name: "default genesis",
genesisState: *types.DefaultGenesisState(),
malleate: nil,
},
{
name: "custom genesis",
Expand All @@ -106,23 +102,10 @@ func (suite *GenesisTestSuite) TestERC20InitGenesis() {
},
},
),
malleate: func() {
suite.app.TransferKeeper.SetDenomTrace(
suite.ctx,
transfertypes.DenomTrace{
BaseDenom: "uosmo",
Path: "transfer/channel-0",
},
)
},
},
}

for _, tc := range testCases {
if tc.malleate != nil {
tc.malleate()
}

suite.Require().NotPanics(func() {
erc20.InitGenesis(suite.ctx, suite.app.Erc20Keeper, suite.app.AccountKeeper, tc.genesisState)
})
Expand All @@ -133,9 +116,6 @@ func (suite *GenesisTestSuite) TestERC20InitGenesis() {
suite.Require().Equal(tc.genesisState.Params, params)
if len(tokenPairs) > 0 {
suite.Require().Equal(tc.genesisState.TokenPairs, tokenPairs)
// check ERC20 contract was created successfully
acc := suite.app.EvmKeeper.GetAccount(suite.ctx, common.HexToAddress(osmoERC20ContractAddr))
suite.Require().True(acc.IsContract())
} else {
suite.Require().Len(tc.genesisState.TokenPairs, 0)
}
Expand All @@ -146,17 +126,14 @@ func (suite *GenesisTestSuite) TestErc20ExportGenesis() {
testGenCases := []struct {
name string
genesisState types.GenesisState
malleate func()
}{
{
name: "empty genesis",
genesisState: types.GenesisState{},
malleate: nil,
},
{
name: "default genesis",
genesisState: *types.DefaultGenesisState(),
malleate: nil,
},
{
name: "custom genesis",
Expand All @@ -171,16 +148,10 @@ func (suite *GenesisTestSuite) TestErc20ExportGenesis() {
},
},
),
malleate: func() {
suite.app.TransferKeeper.SetDenomTrace(suite.ctx, osmoDenomTrace)
},
},
}

for _, tc := range testGenCases {
if tc.malleate != nil {
tc.malleate()
}
erc20.InitGenesis(suite.ctx, suite.app.Erc20Keeper, suite.app.AccountKeeper, tc.genesisState)
suite.Require().NotPanics(func() {
genesisExported := erc20.ExportGenesis(suite.ctx, suite.app.Erc20Keeper)
Expand All @@ -194,6 +165,5 @@ func (suite *GenesisTestSuite) TestErc20ExportGenesis() {
suite.Require().Len(genesisExported.TokenPairs, 0)
}
})
// }
}
}
51 changes: 16 additions & 35 deletions x/erc20/keeper/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package keeper

import (
"encoding/json"
"fmt"
"math"
"math/big"

errorsmod "cosmossdk.io/errors"
Expand All @@ -31,11 +29,25 @@ func (k Keeper) DeployERC20Contract(
ctx sdk.Context,
coinMetadata banktypes.Metadata,
) (common.Address, error) {
data, err := getContractDataBz(coinMetadata)
decimals := uint8(0)
if len(coinMetadata.DenomUnits) > 0 {
decimalsIdx := len(coinMetadata.DenomUnits) - 1
decimals = uint8(coinMetadata.DenomUnits[decimalsIdx].Exponent)
}
ctorArgs, err := contracts.ERC20MinterBurnerDecimalsContract.ABI.Pack(
"",
coinMetadata.Name,
coinMetadata.Symbol,
decimals,
)
if err != nil {
return common.Address{}, err
return common.Address{}, errorsmod.Wrapf(types.ErrABIPack, "coin metadata is invalid %s: %s", coinMetadata.Name, err.Error())
}

data := make([]byte, len(contracts.ERC20MinterBurnerDecimalsContract.Bin)+len(ctorArgs))
copy(data[:len(contracts.ERC20MinterBurnerDecimalsContract.Bin)], contracts.ERC20MinterBurnerDecimalsContract.Bin)
copy(data[len(contracts.ERC20MinterBurnerDecimalsContract.Bin):], ctorArgs)

nonce, err := k.accountKeeper.GetSequence(ctx, types.ModuleAddress.Bytes())
if err != nil {
return common.Address{}, err
Expand Down Expand Up @@ -230,34 +242,3 @@ func (k Keeper) monitorApprovalEvent(res *evmtypes.MsgEthereumTxResponse) error

return nil
}

// getContractDataBz is a helper function to get the contract bytecode
// needed to deploy an ERC20 contract based on the input coin metadata
func getContractDataBz(coinMetadata banktypes.Metadata) ([]byte, error) {
decimals := uint8(0)
if len(coinMetadata.DenomUnits) > 0 {
decimalsIdx := len(coinMetadata.DenomUnits) - 1
exp := coinMetadata.DenomUnits[decimalsIdx].Exponent
if exp > math.MaxUint8 {
return nil, fmt.Errorf("coin metadata is invalid. Denom unit exponent should be less or equal than %d, got %d", math.MaxUint8, exp)
}
decimals = uint8(exp)
}

// Get the input values for the ERC20 contract constructor method
constructorArgs, err := contracts.ERC20MinterBurnerDecimalsContract.ABI.Pack(
"", // constructor
coinMetadata.Name,
coinMetadata.Symbol,
decimals,
)
if err != nil {
return nil, errorsmod.Wrapf(types.ErrABIPack, "coin metadata is invalid %s: %s", coinMetadata.Name, err.Error())
}

data := make([]byte, len(contracts.ERC20MinterBurnerDecimalsContract.Bin)+len(constructorArgs))
copy(data[:len(contracts.ERC20MinterBurnerDecimalsContract.Bin)], contracts.ERC20MinterBurnerDecimalsContract.Bin)
copy(data[len(contracts.ERC20MinterBurnerDecimalsContract.Bin):], constructorArgs)

return data, nil
}
155 changes: 0 additions & 155 deletions x/erc20/keeper/genesis.go

This file was deleted.

Loading
Loading