Skip to content

Commit

Permalink
feat(evm): contract embeds, create fun token from ERC20
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Jul 1, 2024
1 parent 45d1b7f commit ce59cc7
Show file tree
Hide file tree
Showing 25 changed files with 1,887 additions and 232 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1922](https://github.com/NibiruChain/nibiru/pull/1922) - feat(evm): tracer option is read from the config.
- [#1936](https://github.com/NibiruChain/nibiru/pull/1936) - feat(evm): EVM fungible token protobufs and encoding tests
- [#1947](https://github.com/NibiruChain/nibiru/pull/1947) - fix(evm): fix FunToken state marshalling
- [#1950](https://github.com/NibiruChain/nibiru/pull/1950) - feat(evm): Tx to create FunToken mapping from ERC20, contract embeds, and ERC20 queries.

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

Expand Down
6 changes: 3 additions & 3 deletions app/server/config/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ const (
// DefaultMaxTxGasWanted is the default gas wanted for each eth tx returned in ante handler in check tx mode
DefaultMaxTxGasWanted = 0

// DefaultGasCap is the default cap on gas that can be used in eth_call/estimateGas
DefaultGasCap uint64 = 25000000
// DefaultEthCallGasLimit is the default cap on gas that can be used in eth_call/estimateGas
DefaultEthCallGasLimit uint64 = 25_000_000

// DefaultFilterCap is the default cap for total number of filters that can be created
DefaultFilterCap int32 = 200
Expand Down Expand Up @@ -246,7 +246,7 @@ func DefaultJSONRPCConfig() *JSONRPCConfig {
API: GetDefaultAPINamespaces(),
Address: DefaultJSONRPCAddress,
WsAddress: DefaultJSONRPCWsAddress,
GasCap: DefaultGasCap,
GasCap: DefaultEthCallGasLimit,
EVMTimeout: DefaultEVMTimeout,
TxFeeCap: DefaultTxFeeCap,
FilterCap: DefaultFilterCap,
Expand Down
4 changes: 2 additions & 2 deletions app/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ which accepts a path for the resulting pprof file.
cmd.Flags().StringSlice(JSONRPCAPI, config.GetDefaultAPINamespaces(), "Defines a list of JSON-RPC namespaces that should be enabled")
cmd.Flags().String(JSONRPCAddress, config.DefaultJSONRPCAddress, "the JSON-RPC server address to listen on")
cmd.Flags().String(JSONWsAddress, config.DefaultJSONRPCWsAddress, "the JSON-RPC WS server address to listen on")
cmd.Flags().Uint64(JSONRPCGasCap, config.DefaultGasCap, "Sets a cap on gas that can be used in eth_call/estimateGas unit is unibi (0=infinite)") //nolint:lll
cmd.Flags().Float64(JSONRPCTxFeeCap, config.DefaultTxFeeCap, "Sets a cap on transaction fee that can be sent via the RPC APIs (1 = default 1 nibi)") //nolint:lll
cmd.Flags().Uint64(JSONRPCGasCap, config.DefaultEthCallGasLimit, "Sets a cap on gas that can be used in eth_call/estimateGas unit is unibi (0=infinite)") //nolint:lll
cmd.Flags().Float64(JSONRPCTxFeeCap, config.DefaultTxFeeCap, "Sets a cap on transaction fee that can be sent via the RPC APIs (1 = default 1 nibi)") //nolint:lll
cmd.Flags().Int32(JSONRPCFilterCap, config.DefaultFilterCap, "Sets the global cap for total number of filters that can be created")
cmd.Flags().Duration(JSONRPCEVMTimeout, config.DefaultEVMTimeout, "Sets a timeout used for eth_call (0=infinite)")
cmd.Flags().Duration(JSONRPCHTTPTimeout, config.DefaultHTTPTimeout, "Sets a read/write timeout for json-rpc http server (0=infinite)")
Expand Down
6 changes: 4 additions & 2 deletions eth/rpc/rpcapi/eth_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

nibirucommon "github.com/NibiruChain/nibiru/x/common"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/evm/embeds"
"github.com/NibiruChain/nibiru/x/evm/evmtest"

"github.com/stretchr/testify/suite"
Expand All @@ -42,7 +43,7 @@ type TestSuite struct {
fundedAccEthAddr gethcommon.Address
fundedAccNibiAddr sdk.AccAddress

contractData evmtest.CompiledEvmContract
contractData embeds.CompiledEvmContract
}

func TestSuite_RunAll(t *testing.T) {
Expand All @@ -63,7 +64,8 @@ func (s *TestSuite) SetupSuite() {
s.network = network
s.ethClient = network.Validators[0].JSONRPCClient

s.contractData = evmtest.SmartContract_FunToken.Load(s.T())
s.contractData, err = embeds.SmartContract_FunToken.Load()
s.Require().NoError(err)

testAccPrivateKey, _ := crypto.GenerateKey()
s.fundedAccPrivateKey = testAccPrivateKey
Expand Down
27 changes: 27 additions & 0 deletions proto/eth/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ service Msg {
// UpdateParams defined a governance operation for updating the x/evm module parameters.
// The authority is hard-coded to the Cosmos SDK x/gov module account
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);

// CreateFunToken: Create a "FunToken" mapping. Either the ERC20 contract
// address can be given to create the mapping to a bank coin, or the
// denomination for a bank coin can be given to create the mapping to an ERC20.
rpc CreateFunToken(MsgCreateFunToken) returns (MsgCreateFunTokenResponse);
}

// MsgEthereumTx encapsulates an Ethereum transaction as an SDK message.
Expand Down Expand Up @@ -176,3 +181,25 @@ message MsgUpdateParams {
// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}

// MsgCreateFunToken: Arguments to create a "FunToken" mapping. Either the ERC20
// contract address can be given to create the mapping to a bank coin, or the
// denomination for a bank coin can be given to create the mapping to an ERC20.
message MsgCreateFunToken {
// Hexadecimal address of the ERC20 token to which the `FunToken` maps
string from_erc20 = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/eth.HexAddr",
(gogoproto.nullable) = false
];

// Coin denomination in the Bank Module.
string from_bank_denom = 2;

// Sender: Address for the signer of the transaction.
string sender = 3;
}

message MsgCreateFunTokenResponse {
// Fungible token mapping corresponding to ERC20 tokens.
eth.evm.v1.FunToken funtoken_mapping = 1 [(gogoproto.nullable) = false];
}
19 changes: 16 additions & 3 deletions x/evm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package evm

import (
"github.com/NibiruChain/collections"
"github.com/ethereum/go-ethereum/common"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
gethcommon "github.com/ethereum/go-ethereum/common"
)

const (
Expand Down Expand Up @@ -49,12 +50,12 @@ const (
var KeyPrefixBzAccState = KeyPrefixAccState.Prefix()

// PrefixAccStateEthAddr returns a prefix to iterate over a given account storage.
func PrefixAccStateEthAddr(address common.Address) []byte {
func PrefixAccStateEthAddr(address gethcommon.Address) []byte {
return append(KeyPrefixBzAccState, address.Bytes()...)
}

// StateKey defines the full key under which an account state is stored.
func StateKey(address common.Address, key []byte) []byte {
func StateKey(address gethcommon.Address, key []byte) []byte {
return append(PrefixAccStateEthAddr(address), key...)
}

Expand All @@ -71,3 +72,15 @@ const (
// CallTypeSmart call type is used in case of smart contract methods calls
CallTypeSmart
)

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

var evmModuleAddr gethcommon.Address
4 changes: 4 additions & 0 deletions x/evm/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package evm
import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gethcore "github.com/ethereum/go-ethereum/core"
gethcoretypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -29,6 +30,9 @@ type BankKeeper interface {
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error
BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error

GetDenomMetaData(ctx sdk.Context, denom string) (metadata bank.Metadata, isFound bool)
SetDenomMetaData(ctx sdk.Context, denomMetaData bank.Metadata)
}

// StakingKeeper returns the historical headers kept in store.
Expand Down
76 changes: 76 additions & 0 deletions x/evm/embeds/ERC20Minter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
* @dev {ERC20} token, including:
*
* - ability for holders to burn (destroy) their tokens
* - a minter role that allows for token minting (creation)
*
* The contract owner is set automatically in the constructor as the
* deployer due to "Ownable".
*
* The Context contract is inherited indirectly through "ERC20" and "Ownable".
*
* The account that deploys the contract will be able to mint and burn tokens.
*/
contract ERC20Minter is ERC20, ERC20Burnable, Ownable {
uint8 private _decimals;

/**
* @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` to the
* account that deploys the contract and customizes tokens decimals
*
* See {ERC20-constructor}.
*/
constructor(string memory name, string memory symbol, uint8 decimals_)
ERC20(name, symbol) {
_setupDecimals(decimals_);
}

/**
* @dev Sets `_decimals` as `decimals_ once at Deployment'
*/
function _setupDecimals(uint8 decimals_) private {
_decimals = decimals_;
}

/**
* @dev Overrides the `decimals()` method with custom `_decimals`
*/
function decimals() public view virtual override returns (uint8) {
return _decimals;
}

/**
* @dev Creates `amount` new tokens for `to`.
*
* See {ERC20-_mint}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function mint(address to, uint256 amount) public virtual onlyOwner {
_mint(to, amount);
}

/**
* @dev Destroys `amount` new tokens for `to`.
*
* See {ERC20-_burn}.
*
* Requirements:
*
* - the caller must have the `MINTER_ROLE`.
*/
function burnCoins(address from, uint256 amount) public virtual onlyOwner {
_burn(from, amount);
}

}
Loading

0 comments on commit ce59cc7

Please sign in to comment.