Skip to content

Commit

Permalink
refactor(evm): clean up erc20 and funtoken methods (#2000)
Browse files Browse the repository at this point in the history
* refactor: simplify DeployERC20ForBankCoin

* refactor: erc20 keeper methods

* remove err array

* clean up CallContractWithInput

* refactor bankSend precompile

* refactor PrecompileAddr_FuntokenGateway

* clean up precompile

* refactor: remove NibiruPrecompile

* Update CHANGELOG.md

* fix: DeployERC20ForBankCoin uses wrong nonce

* fix: precompile tests

* test(evm): fix TestDeployERC20ForBankCoin assertions

* test(evm): refactor TestCreateFunTokenFromERC20

* refactor TestCreateFunTokenFromCoin

* remove DoEthTx

* simplify AssertERC20BalanceEqual

* simplify CreateFunTokenForBankCoin

* remove PrivKeyE from EthPrivKeyAcc

* remove NewEthTxMsg

* remove GoCtx

* remove GethSigner

* rename NewEthAccInfo to EthPrivAcc

* remove CreateContractGethCoreMsg

* rename to ExecuteContractMsgEthereumTx
  • Loading branch information
k-yang authored Aug 14, 2024
1 parent 7a20389 commit fdcda38
Show file tree
Hide file tree
Showing 37 changed files with 345 additions and 453 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1989](https://github.com/NibiruChain/nibiru/pull/1989) - refactor(evm): simplify evm module address
- [#1996](https://github.com/NibiruChain/nibiru/pull/1996) - perf(evm-keeper-precompile): implement sorted map for `k.precompiles` to remove dead code
- [#1997](https://github.com/NibiruChain/nibiru/pull/1997) - refactor(evm): Remove unnecessary params: "enable_call", "enable_create".
- [#2000](https://github.com/NibiruChain/nibiru/pull/2000) - refactor(evm): simplify ERC-20 keeper methods

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

Expand Down
11 changes: 5 additions & 6 deletions app/evmante/evmante_can_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evmante_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/app/evmante"
"github.com/NibiruChain/nibiru/v2/eth"
Expand Down Expand Up @@ -34,9 +35,8 @@ func (s *TestSuite) TestCanTransferDecorator() {
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.App.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := txMsg.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
err := txMsg.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand All @@ -52,9 +52,8 @@ func (s *TestSuite) TestCanTransferDecorator() {
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.App.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := txMsg.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
err := txMsg.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand Down
6 changes: 3 additions & 3 deletions app/evmante/evmante_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/app/ante"
"github.com/NibiruChain/nibiru/v2/app/evmante"
Expand Down Expand Up @@ -52,9 +53,8 @@ func (s *TestSuite) TestAnteHandlerEVM() {
txMsg := evmtest.HappyTransferTx(deps, 0)
txBuilder := deps.EncCfg.TxConfig.NewTxBuilder()

gethSigner := deps.Sender.GethSigner(deps.App.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := txMsg.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
err := txMsg.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand Down
2 changes: 1 addition & 1 deletion app/evmante/evmante_mempool_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *TestSuite) TestMempoolGasFeeDecorator() {
fees := sdk.NewCoins(sdk.NewInt64Coin("unibi", int64(gasLimit)))
msg := &banktypes.MsgSend{
FromAddress: deps.Sender.NibiruAddr.String(),
ToAddress: evmtest.NewEthAccInfo().NibiruAddr.String(),
ToAddress: evmtest.NewEthPrivAcc().NibiruAddr.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("unibi", 1)),
}
return buildTx(deps, true, msg, gasLimit, fees)
Expand Down
11 changes: 5 additions & 6 deletions app/evmante/evmante_sigverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/app/evmante"
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
Expand Down Expand Up @@ -42,9 +43,8 @@ func (s *TestSuite) TestEthSigVerificationDecorator() {
name: "sad: ethereum tx invalid chain id",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := evmtest.HappyCreateContractTx(deps)
gethSigner := deps.Sender.GethSigner(InvalidChainID)
keyringSigner := deps.Sender.KeyringSigner
err := tx.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(InvalidChainID)
err := tx.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)
return tx
},
Expand All @@ -54,9 +54,8 @@ func (s *TestSuite) TestEthSigVerificationDecorator() {
name: "happy: signed ethereum tx",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := evmtest.HappyCreateContractTx(deps)
gethSigner := deps.Sender.GethSigner(deps.App.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err := tx.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
err := tx.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)
return tx
},
Expand Down
13 changes: 6 additions & 7 deletions app/evmante/evmante_validate_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"

"github.com/NibiruChain/nibiru/v2/app/evmante"
"github.com/NibiruChain/nibiru/v2/eth"
Expand Down Expand Up @@ -56,9 +57,8 @@ func (s *TestSuite) TestEthValidateBasicDecorator() {
name: "sad: tx not implementing protoTxProvider",
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
tx := evmtest.HappyCreateContractTx(deps)
gethSigner := deps.Sender.GethSigner(InvalidChainID)
keyringSigner := deps.Sender.KeyringSigner
err := tx.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(InvalidChainID)
err := tx.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)
return tx
},
Expand Down Expand Up @@ -113,9 +113,8 @@ func (s *TestSuite) TestEthValidateBasicDecorator() {
s.Require().NoError(err)
txMsg := evmtest.HappyCreateContractTx(deps)

gethSigner := deps.Sender.GethSigner(deps.App.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
err = txMsg.Sign(gethSigner, keyringSigner)
gethSigner := gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
err = txMsg.Sign(gethSigner, deps.Sender.KeyringSigner)
s.Require().NoError(err)

tx, err := txMsg.BuildTx(txBuilder, eth.EthBaseDenom)
Expand All @@ -142,7 +141,7 @@ func (s *TestSuite) TestEthValidateBasicDecorator() {
fees := sdk.NewCoins(sdk.NewInt64Coin("unibi", int64(gasLimit)))
msg := &banktypes.MsgSend{
FromAddress: deps.Sender.NibiruAddr.String(),
ToAddress: evmtest.NewEthAccInfo().NibiruAddr.String(),
ToAddress: evmtest.NewEthPrivAcc().NibiruAddr.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("unibi", 1)),
}
return buildTx(deps, true, msg, gasLimit, fees)
Expand Down
4 changes: 2 additions & 2 deletions eth/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestValidateAddress(t *testing.T) {
"zero address", common.Address{}.String(), false,
},
{
"valid address", evmtest.NewEthAccInfo().EthAddr.Hex(), false,
"valid address", evmtest.NewEthPrivAcc().EthAddr.Hex(), false,
},
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func TestValidateNonZeroAddress(t *testing.T) {
"zero address", common.Address{}.String(), true,
},
{
"valid address", evmtest.NewEthAccInfo().EthAddr.Hex(), false,
"valid address", evmtest.NewEthPrivAcc().EthAddr.Hex(), false,
},
}

Expand Down
2 changes: 1 addition & 1 deletion eth/encoding/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestTxEncoding(t *testing.T) {
ethAcc := evmtest.NewEthAccInfo()
ethAcc := evmtest.NewEthPrivAcc()
addr, key := ethAcc.EthAddr, ethAcc.PrivKey
signer := evmtest.NewSigner(key)

Expand Down
6 changes: 3 additions & 3 deletions eth/eth_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (s *Suite) TestEthAddrToNibiruAddr() {
accInfo := evmtest.NewEthAccInfo()
accInfo := evmtest.NewEthPrivAcc()
s.Equal(
accInfo.EthAddr,
eth.NibiruAddrToEthAddr(accInfo.NibiruAddr),
Expand All @@ -18,7 +18,7 @@ func (s *Suite) TestEthAddrToNibiruAddr() {

s.T().Log("unit operation - hex -> nibi -> hex")
{
addr := evmtest.NewEthAccInfo().NibiruAddr
addr := evmtest.NewEthPrivAcc().NibiruAddr
s.Equal(
addr,
eth.EthAddrToNibiruAddr(
Expand All @@ -29,7 +29,7 @@ func (s *Suite) TestEthAddrToNibiruAddr() {

s.T().Log("unit operation - nibi -> hex -> nibi")
{
addr := evmtest.NewEthAccInfo().EthAddr
addr := evmtest.NewEthPrivAcc().EthAddr
s.Equal(
addr,
eth.NibiruAddrToEthAddr(
Expand Down
28 changes: 14 additions & 14 deletions eth/rpc/backend/account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func (s *BackendSuite) TestGetCode() {
}{
{
"fail - BlockHash and BlockNumber are both nil ",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{},
func(addr common.Address) {},
false,
nil,
},
{
"fail - query client errors on getting Code",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(addr common.Address) {
queryClient := s.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
Expand All @@ -52,7 +52,7 @@ func (s *BackendSuite) TestGetCode() {
},
{
"pass",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(addr common.Address) {
queryClient := s.backend.queryClient.QueryClient.(*mocks.EVMQueryClient)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s *BackendSuite) TestGetCode() {
func (s *BackendSuite) TestGetProof() {
blockNrInvalid := rpc.NewBlockNumber(big.NewInt(1))
blockNr := rpc.NewBlockNumber(big.NewInt(4))
address1 := evmtest.NewEthAccInfo().EthAddr
address1 := evmtest.NewEthPrivAcc().EthAddr

testCases := []struct {
name string
Expand Down Expand Up @@ -199,7 +199,7 @@ func (s *BackendSuite) TestGetStorageAt() {
}{
{
"fail - BlockHash and BlockNumber are both nil",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
"0x0",
rpc.BlockNumberOrHash{},
func(addr common.Address, key string, storage string) {},
Expand All @@ -208,7 +208,7 @@ func (s *BackendSuite) TestGetStorageAt() {
},
{
"fail - query client errors on getting Storage",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
"0x0",
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(addr common.Address, key string, storage string) {
Expand All @@ -220,7 +220,7 @@ func (s *BackendSuite) TestGetStorageAt() {
},
{
"pass",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
"0x0",
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(addr common.Address, key string, storage string) {
Expand Down Expand Up @@ -260,7 +260,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
}{
{
"fail - BlockHash and BlockNumber are both nil",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{},
func(bn rpc.BlockNumber, addr common.Address) {
},
Expand All @@ -269,7 +269,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
},
{
"fail - tendermint client failed to get block",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpc.BlockNumber, addr common.Address) {
client := s.backend.clientCtx.Client.(*mocks.Client)
Expand All @@ -280,7 +280,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
},
{
"fail - query client failed to get balance",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpc.BlockNumber, addr common.Address) {
client := s.backend.clientCtx.Client.(*mocks.Client)
Expand All @@ -294,7 +294,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
},
{
"fail - invalid balance",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpc.BlockNumber, addr common.Address) {
client := s.backend.clientCtx.Client.(*mocks.Client)
Expand All @@ -308,7 +308,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
},
{
"fail - pruned node state",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpc.BlockNumber, addr common.Address) {
client := s.backend.clientCtx.Client.(*mocks.Client)
Expand All @@ -322,7 +322,7 @@ func (s *BackendSuite) TestGetEvmGasBalance() {
},
{
"pass",
evmtest.NewEthAccInfo().EthAddr,
evmtest.NewEthPrivAcc().EthAddr,
rpc.BlockNumberOrHash{BlockNumber: &blockNr},
func(bn rpc.BlockNumber, addr common.Address) {
client := s.backend.clientCtx.Client.(*mocks.Client)
Expand Down Expand Up @@ -393,7 +393,7 @@ func (s *BackendSuite) TestGetTransactionCount() {
s.Run(fmt.Sprintf("Case %s", tc.name), func() {
s.SetupTest()

addr := evmtest.NewEthAccInfo().EthAddr
addr := evmtest.NewEthPrivAcc().EthAddr
if tc.accExists {
addr = common.BytesToAddress(s.acc.Bytes())
}
Expand Down
6 changes: 3 additions & 3 deletions eth/rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (s *BackendSuite) SetupTest() {
}

// Create Account with set sequence
s.acc = sdk.AccAddress(evmtest.NewEthAccInfo().EthAddr.Bytes())
s.acc = sdk.AccAddress(evmtest.NewEthPrivAcc().EthAddr.Bytes())
accounts := map[string]client.TestAccount{}
accounts[s.acc.String()] = client.TestAccount{
Address: s.acc,
Num: uint64(1),
Seq: uint64(1),
}

ethAcc := evmtest.NewEthAccInfo()
ethAcc := evmtest.NewEthPrivAcc()
from, priv := ethAcc.EthAddr, ethAcc.PrivKey
s.from = from
s.signer = evmtest.NewSigner(priv)
Expand Down Expand Up @@ -180,7 +180,7 @@ func (s *BackendSuite) generateTestKeyring(clientDir string) (keyring.Keyring, e
}

func (s *BackendSuite) signAndEncodeEthTx(msgEthereumTx *evm.MsgEthereumTx) []byte {
ethAcc := evmtest.NewEthAccInfo()
ethAcc := evmtest.NewEthPrivAcc()
from, priv := ethAcc.EthAddr, ethAcc.PrivKey
signer := evmtest.NewSigner(priv)

Expand Down
Loading

0 comments on commit fdcda38

Please sign in to comment.