Skip to content

Commit

Permalink
refactor(evm): clean up EVM module address (#1989)
Browse files Browse the repository at this point in the history
* refactor(evm): clean up EVM module address

* Update CHANGELOG.md
  • Loading branch information
k-yang authored Aug 8, 2024
1 parent d71d67d commit be07667
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1982](https://github.com/NibiruChain/nibiru/pull/1982) - feat(evm): add GlobalMinGasPrices
- [#1983](https://github.com/NibiruChain/nibiru/pull/1983) - chore(evm): remove ExtensionOptionsWeb3Tx and ExtensionOptionDynamicFeeTx
- [#1984](https://github.com/NibiruChain/nibiru/pull/1984) - refactor(evm): embeds
- [#1985](https://github.com/NibiruChain/nibiru/pull/1985) - feat(evm)!: Use atto denomination for the wei units in the EVM so that NIBI is "ether" to clients. Only micronibi (unibi) amounts can be transferred. All clients follow the constraint equation, 1 ether == 1 NIBI == 10^6 unibi == 10^18 wei.
- [#1985](https://github.com/NibiruChain/nibiru/pull/1985) - feat(evm)!: Use atto denomination for the wei units in the EVM so that NIBI is "ether" to clients. Only micronibi (unibi) amounts can be transferred. All clients follow the constraint equation, 1 ether == 1 NIBI == 10^6 unibi == 10^18 wei.
- [#1986](https://github.com/NibiruChain/nibiru/pull/1986) - feat(evm): Combine both account queries into "/eth.evm.v1.Query/EthAccount", accepting both nibi-prefixed Bech32 addresses and Ethereum-type hexadecimal addresses as input.
- [#1989](https://github.com/NibiruChain/nibiru/pull/1989) - refactor(evm): simplify evm module address

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

Expand Down
17 changes: 4 additions & 13 deletions x/evm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,11 @@ const (
CallTypeSmart
)

// ModuleAddressEVM: Module account address as a `gethcommon.Address`.
func ModuleAddressEVM() gethcommon.Address {
if evmModuleAddr == zeroAddr {
evmModuleAddr = gethcommon.BytesToAddress(
authtypes.NewModuleAddress(ModuleName).Bytes(),
)
}
return evmModuleAddr
}
var EVM_MODULE_ADDRESS gethcommon.Address

var (
zeroAddr gethcommon.Address
evmModuleAddr gethcommon.Address
)
func init() {
EVM_MODULE_ADDRESS = gethcommon.BytesToAddress(authtypes.NewModuleAddress(ModuleName))
}

// NativeToWei converts a "unibi" amount to "wei" units for the EVM.
//
Expand Down
2 changes: 1 addition & 1 deletion x/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *TestSuite) TestFunToken() {
}

func (s *TestSuite) TestModuleAddressEVM() {
addr := evm.ModuleAddressEVM()
addr := evm.EVM_MODULE_ADDRESS
s.Equal(addr.Hex(), "0x603871c2ddd41c26Ee77495E2E31e6De7f9957e0")

// Sanity check
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (k Keeper) LoadERC20String(
) (out string, err error) {
res, err := k.CallContract(
ctx, erc20Abi,
evm.ModuleAddressEVM(),
evm.EVM_MODULE_ADDRESS,
&erc20Contract,
false, methodName,
)
Expand All @@ -323,7 +323,7 @@ func (k Keeper) loadERC20Uint8(
) (out uint8, err error) {
res, err := k.CallContract(
ctx, erc20Abi,
evm.ModuleAddressEVM(),
evm.EVM_MODULE_ADDRESS,
&erc20Contract,
false, methodName,
)
Expand Down Expand Up @@ -352,7 +352,7 @@ func (k Keeper) LoadERC20BigInt(
res, err := k.CallContract(
ctx,
erc20Abi,
evm.ModuleAddressEVM(),
evm.EVM_MODULE_ADDRESS,
&erc20Contract,
commit,
methodName,
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (s *Suite) TestSendFunTokenToEvm() {
recipientERC20Balance, err := deps.EvmKeeper.CallContract(
deps.Ctx,
embeds.SmartContract_ERC20Minter.ABI,
evm.ModuleAddressEVM(),
evm.EVM_MODULE_ADDRESS,
&funTokenErc20Addr,
false,
"balanceOf",
Expand Down Expand Up @@ -438,7 +438,7 @@ func (s *Suite) TestERC20Calls() {
contract := funtoken.Erc20Addr.ToAddr()

theUser := deps.Sender.EthAddr
theEvm := evm.ModuleAddressEVM()
theEvm := evm.EVM_MODULE_ADDRESS

s.T().Log("Mint tokens - Fail from non-owner")
{
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/funtoken_from_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (k *Keeper) DeployERC20ForBankCoin(
}
bytecodeForCall := append(erc20Embed.Bytecode, packedArgs...)

fromEvmAddr := evm.ModuleAddressEVM()
fromEvmAddr := evm.EVM_MODULE_ADDRESS
nonce := k.GetAccNonce(ctx, fromEvmAddr)
erc20Addr = crypto.CreateAddress(fromEvmAddr, nonce)
erc20Contract := (*gethcommon.Address)(nil) // nil >> doesn't exist yet
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (k *Keeper) SendFunTokenToEvm(
evmResp, err := k.CallContract(
ctx,
embeds.SmartContract_ERC20Minter.ABI,
evm.ModuleAddressEVM(),
evm.EVM_MODULE_ADDRESS,
&erc20ContractAddr,
true,
"mint",
Expand Down
8 changes: 4 additions & 4 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (p precompileFunToken) bankSend(
}

// Caller transfers ERC20 to the EVM account
transferTo := evm.ModuleAddressEVM()
transferTo := evm.EVM_MODULE_ADDRESS
_, err = p.EvmKeeper.ERC20().Transfer(erc20, caller, transferTo, amount, ctx)
if err != nil {
err = fmt.Errorf("failed to send from caller to the EVM account: %w", err)
Expand All @@ -166,15 +166,15 @@ func (p precompileFunToken) bankSend(
err = p.BankKeeper.MintCoins(ctx, evm.ModuleName, coins)
if err != nil {
err = fmt.Errorf("mint failed for module \"%s\" (%s): contract caller %s: %w",
evm.ModuleName, evm.ModuleAddressEVM().Hex(), caller.Hex(), err,
evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err,
)
return
}

err = p.BankKeeper.SendCoinsFromModuleToAccount(ctx, evm.ModuleName, toAddr, coins)
if err != nil {
err = fmt.Errorf("send failed for module \"%s\" (%s): contract caller %s: %w",
evm.ModuleName, evm.ModuleAddressEVM().Hex(), caller.Hex(), err,
evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err,
)
return
}
Expand All @@ -184,7 +184,7 @@ func (p precompileFunToken) bankSend(
// Since we're sending them away and want accurate total supply tracking, the
// tokens need to be burned.
if funtoken.IsMadeFromCoin {
caller := evm.ModuleAddressEVM()
caller := evm.EVM_MODULE_ADDRESS
_, err = p.EvmKeeper.ERC20().Burn(erc20, caller, amount, ctx)
if err != nil {
err = fmt.Errorf("ERC20.Burn: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions x/evm/precompile/funtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *Suite) FunToken_PrecompileExists() {
err, fmt.Sprintf("argument count mismatch: got %d for 3", len(callArgs)),
"callArgs: ", callArgs)

fromEvmAddr := evm.ModuleAddressEVM()
fromEvmAddr := evm.EVM_MODULE_ADDRESS
contractAddr := precompileAddr.ToAddr()
commit := true
bytecodeForCall := packedArgs
Expand All @@ -75,7 +75,7 @@ func (s *Suite) FunToken_HappyPath() {
deps := evmtest.NewTestDeps()

theUser := deps.Sender.EthAddr
theEvm := evm.ModuleAddressEVM()
theEvm := evm.EVM_MODULE_ADDRESS

s.True(deps.EvmKeeper.PrecompileSet().Has(precompileAddr.ToAddr()),
"did not see precompile address during \"InitPrecompiles\"")
Expand Down

0 comments on commit be07667

Please sign in to comment.