Skip to content

Commit

Permalink
feat(evm): msg to send bank coin to erc20 (#1956)
Browse files Browse the repository at this point in the history
* refactor(e2e-evm): unused vars and better type hint

Squashed commit of the following:

commit 51071dad60c12e02093d92d1311d33b70b81304f
Author: Unique-Divine <[email protected]>
Date:   Mon Jun 10 12:32:00 2024 -0500

    refactor(e2e-evm): unused vars and better type hint

commit 6729b97
Author: Unique-Divine <[email protected]>
Date:   Fri Jun 7 22:46:14 2024 -0500

    test(e2e-evm): more type safety

commit 25c1557
Author: Unique-Divine <[email protected]>
Date:   Fri Jun 7 22:01:21 2024 -0500

    Squashed commit of the following:

    commit 021d161d176112cf24e28780ad64f61155f70ea2
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 22:00:10 2024 -0500

        test(e2e): (1) Generated smart contract types for ethers. (2) TypeScript support. (3) Formatter

    commit af7e7b3
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 16:11:45 2024 -0500

        chore: another issue ticket

    commit 36745fd
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 16:07:20 2024 -0500

        chore: add issue number for TODO comment

    commit 8a76c0e
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:54:07 2024 -0500

        refactor(evm): Remove dead code and document non-EVM ante handler

    commit e4e11df
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:52:38 2024 -0500

        refactor: remove dead code

    commit cad00c0
    Merge: dc5f4dd 359e310
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:41:53 2024 -0500

        Merge branch 'main' into ud/ante-test

    commit dc5f4dd
    Author: Unique-Divine <[email protected]>
    Date:   Fri Jun 7 15:28:53 2024 -0500

        refactor: ante handler and evm cleanup

    commit f73cdc3
    Merge: d3a6ea9 290c372
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:59:39 2024 -0500

        Merge branch 'test/evm-grpc-query' of https://github.com/NibiruChain/nibiru into test/evm-grpc-query

    commit d3a6ea9
    Merge: 376596d 70ee1bf
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:59:30 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 376596d
    Author: Unique-Divine <[email protected]>
    Date:   Wed Jun 5 20:58:40 2024 -0500

        Squashed commit of the following:

        commit b5687130ff5f3d020a3b14d219fec3a816579c30
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 20:57:44 2024 -0500

            chore: run tidy

        commit 1f1f938
        Merge: 3e3cc83 bbcc6f8
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:16:30 2024 -0500

            Merge branch 'main' into ud/fix-race-condition

        commit 3e3cc83
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:15:40 2024 -0500

            chore: changelog

        commit 3876ccb
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 19:04:00 2024 -0500

            refactor: more consistent test names

        commit aaa0a19
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 18:53:09 2024 -0500

            test(oracle): Fix missing tear down step for oracle integration test

        commit 8c3c35e
        Author: Unique-Divine <[email protected]>
        Date:   Wed Jun 5 17:55:56 2024 -0500

            chore: add test comands to justfile

        commit 4916282
        Merge: 64ed0a2 e7e708d
        Author: Unique-Divine <[email protected]>
        Date:   Fri May 31 09:35:33 2024 -0500

            Merge branch 'main' into ud/fix-race-condition

        commit 64ed0a2
        Author: Unique-Divine <[email protected]>
        Date:   Fri May 31 01:44:55 2024 -0500

            fix(gosdk): tests parallel race condition

    commit 290c372
    Merge: 0d1c894 70ee1bf
    Author: Unique Divine <[email protected]>
    Date:   Wed Jun 5 20:05:19 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 0d1c894
    Merge: 9170835 ad173e9
    Author: Unique Divine <[email protected]>
    Date:   Wed Jun 5 19:34:38 2024 -0500

        Merge branch 'main' into test/evm-grpc-query

    commit 9170835
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 13:55:14 2024 +0400

        fix: removed hardcoded gas value in grpc_query test

    commit 4337858
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 13:14:34 2024 +0400

        chore: refactored eth util methods

    commit 7df84e2
    Merge: 8918498 bbcc6f8
    Author: Oleg Nikonychev <[email protected]>
    Date:   Wed Jun 5 12:23:51 2024 +0400

        chore: resolve conflicts

    commit 8918498
    Merge: 3fd45ce e7e708d
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:56:39 2024 +0400

        Merge branch 'main' into test/evm-grpc-query

    commit 3fd45ce
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:56:23 2024 +0400

        chore: changelog update

    commit 3348876
    Author: Oleg Nikonychev <[email protected]>
    Date:   Mon Jun 3 21:53:54 2024 +0400

        test(evm): grpc_query full coverage

* wip!: save progress

* chore: changelog

* chore: linter

* feat(eth): finish todos for hex.go

* feat(eth): finalize protobuf impl for eth.HexAddr

* chore: small typos in comments

* feat(evm): contract embeds, create fun token from ERC20

* chore: merge conflict

* chore: clean TODOs

* fix: TestModuleAccountEVM

* feat(evm): impl CreateFunTokenFromCoin

* fix: made from_erc20 nullable in MsgCreateFunToken

* test: fix cleanup by increasing timeout for race condition prevention

* chore: rn

* feat(evm): msg to send bank coin to erc20

* feat(evm): msg to send bank coin to erc20

* fix: lint

* fix: evm module account creation in genesis

* test(evm): coverage for send coin to erc20

* fix: comment typo

---------

Co-authored-by: Unique-Divine <[email protected]>
Co-authored-by: Unique Divine <[email protected]>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent a5ff022 commit 76bdaaa
Show file tree
Hide file tree
Showing 17 changed files with 1,751 additions and 224 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1947](https://github.com/NibiruChain/nibiru/pull/1947) - fix(evm): fix FunToken state marshalling
- [#1949](https://github.com/NibiruChain/nibiru/pull/1949) - feat(evm): add fungible token mapping queries
- [#1950](https://github.com/NibiruChain/nibiru/pull/1950) - feat(evm): Tx to create FunToken mapping from ERC20, contract embeds, and ERC20 queries.
- [#1956](https://github.com/NibiruChain/nibiru/pull/1956) - feat(evm): msg to send bank coin to erc20
- [#1958](https://github.com/NibiruChain/nibiru/pull/1958) - chore(evm): wiped deprecated evm apis: miner, personal
- [#1959](https://github.com/NibiruChain/nibiru/pull/1959) - feat(evm): Add precompile to the EVM that enables trasnfers of ERC20 tokens to "nibi" accounts as regular Ethereum transactions

Expand Down
21 changes: 21 additions & 0 deletions proto/eth/evm/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
syntax = "proto3";
package eth.evm.v1;

import "cosmos/base/v1beta1/coin.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/NibiruChain/nibiru/x/evm";

// EventEthereumTx defines the event for an Ethereum transaction
Expand Down Expand Up @@ -43,3 +46,21 @@ message EventBlockBloom {
// bloom is the bloom filter of the block
string bloom = 1;
}

// EventFunTokenFromBankCoin defines a fun token creation from the bank coin event.
message EventFunTokenFromBankCoin {
string bank_denom = 1;
string erc20_contract_address = 2;
string creator = 3;
}

// SendFunTokenToEvm defines sending fun token to erc20 event.
message EventSendFunTokenToEvm {
string sender = 1;
string erc20_contract_address = 2;
string to_eth_addr = 3;
cosmos.base.v1beta1.Coin bank_coin = 4 [
(gogoproto.moretags) = "yaml:\"bank_coin\"",
(gogoproto.nullable) = false
];
}
27 changes: 26 additions & 1 deletion proto/eth/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package eth.evm.v1;

import "cosmos/msg/v1/msg.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos_proto/cosmos.proto";
import "eth/evm/v1/evm.proto";
import "gogoproto/gogo.proto";
Expand All @@ -25,6 +26,11 @@ service Msg {
// 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);

// SendFunTokenToEvm: Sends a coin with a valid "FunToken" mapping to the
// given recipient address ("to_eth_addr") in the corresponding ERC20
// representation.
rpc SendFunTokenToEvm(MsgSendFunTokenToEvm) returns (MsgSendFunTokenToEvmResponse);
}

// MsgEthereumTx encapsulates an Ethereum transaction as an SDK message.
Expand Down Expand Up @@ -189,7 +195,7 @@ 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
(gogoproto.nullable) = true
];

// Coin denomination in the Bank Module.
Expand All @@ -203,3 +209,22 @@ message MsgCreateFunTokenResponse {
// Fungible token mapping corresponding to ERC20 tokens.
eth.evm.v1.FunToken funtoken_mapping = 1 [(gogoproto.nullable) = false];
}

// MsgSendFunTokenToEvm: Arguments to send a bank coin to ERC-20 representation
message MsgSendFunTokenToEvm {
// Hexadecimal address of the ERC20 token to which the `FunToken` maps
string to_eth_addr = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/eth.HexAddr",
(gogoproto.nullable) = false
];

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

// Bank coin to get converted to ERC20
cosmos.base.v1beta1.Coin bank_coin = 3 [
(gogoproto.moretags) = "yaml:\"bank_coin\"",
(gogoproto.nullable) = false
];
}
message MsgSendFunTokenToEvmResponse {}
80 changes: 76 additions & 4 deletions x/evm/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package cli
import (
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/eth"
"github.com/NibiruChain/nibiru/x/evm"
"github.com/NibiruChain/nibiru/x/sudo/types"

"github.com/cosmos/cosmos-sdk/client"

"github.com/spf13/cobra"
)

Expand All @@ -21,7 +25,10 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmds := []*cobra.Command{}
cmds := []*cobra.Command{
CmdCreateFunTokenFromBankCoin(),
SendFunTokenToEvm(),
}
for _, cmd := range cmds {
txCmd.AddCommand(cmd)
}
Expand All @@ -45,6 +52,71 @@ func GetQueryCmd() *cobra.Command {
for _, cmd := range cmds {
moduleQueryCmd.AddCommand(cmd)
}

return moduleQueryCmd
}

// CmdCreateFunTokenFromBankCoin broadcast MsgCreateFunToken
func CmdCreateFunTokenFromBankCoin() *cobra.Command {
cmd := &cobra.Command{
Use: "create-fun-token-from-bank-coin [denom] [flags]",
Short: `Create an erc-20 fungible token from bank coin [denom]"`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
txFactory, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}
txFactory = txFactory.
WithTxConfig(clientCtx.TxConfig).
WithAccountRetriever(clientCtx.AccountRetriever)

msg := &evm.MsgCreateFunToken{
Sender: clientCtx.GetFromAddress().String(),
FromBankDenom: args[0],
}
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txFactory, msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}

// SendFunTokenToEvm broadcast MsgSendFunTokenToEvm
func SendFunTokenToEvm() *cobra.Command {
cmd := &cobra.Command{
Use: "send-fun-token-to-erc-20 [to_eth_addr] [coin] [flags]",
Short: `Send bank [coin] to its erc-20 representation for the user [to_eth_addr]"`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
txFactory, err := tx.NewFactoryCLI(clientCtx, cmd.Flags())
if err != nil {
return err
}
txFactory = txFactory.
WithTxConfig(clientCtx.TxConfig).
WithAccountRetriever(clientCtx.AccountRetriever)

coin, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
msg := &evm.MsgSendFunTokenToEvm{
Sender: clientCtx.GetFromAddress().String(),
BankCoin: coin,
ToEthAddr: eth.MustNewHexAddrFromStr(args[0]),
}
return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txFactory, msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
1 change: 1 addition & 0 deletions x/evm/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type AccountKeeper interface {
SetAccount(ctx sdk.Context, account authtypes.AccountI)
RemoveAccount(ctx sdk.Context, account authtypes.AccountI)
GetParams(ctx sdk.Context) (params authtypes.Params)
SetModuleAccount(ctx sdk.Context, macc authtypes.ModuleAccountI)
}

// BankKeeper defines the expected interface needed to retrieve account balances.
Expand Down
Loading

0 comments on commit 76bdaaa

Please sign in to comment.