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

fix(evm)!: Fungible Token conversions #2003

Merged
merged 29 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8dc1439
create funtoken_from_coin_test.go
k-yang Aug 14, 2024
710fef0
refactor FunTokenFromCoinSuite
k-yang Aug 14, 2024
9d4fa5d
create funtoken_from_erc20_test.go
k-yang Aug 14, 2024
7fbd096
refactor DeployContract
k-yang Aug 14, 2024
5b24fed
refactor MsgCreateFunToken ValidateBasic()
k-yang Aug 14, 2024
8812347
add test case for TestCreateFunTokenFromERC20
k-yang Aug 14, 2024
43eec15
refactor TestERC20Calls
k-yang Aug 14, 2024
3f65721
move TestSendFunTokenToEvm
k-yang Aug 14, 2024
c3e1b8f
refactor CreateFunTokenFromERC20
k-yang Aug 14, 2024
fff0e74
add TestSendFromEvmToCosmos
k-yang Aug 14, 2024
666ec3d
remove ApplyEvmMsgWithEmptyTxConfig
k-yang Aug 14, 2024
329b29d
Update CHANGELOG.md
k-yang Aug 14, 2024
c0dfe0b
refactor: rename SendFunTokenToEvm to ConvertCoinToEvm
k-yang Aug 14, 2024
528d82b
refactor LoadERC20BigInt
k-yang Aug 14, 2024
9b83cac
refactor erc20 Transfer call
k-yang Aug 14, 2024
ee08a82
feat: add convertCoinNativeCoin and convertCoinNativeERC20
k-yang Aug 14, 2024
cc143f1
refactor(evm); rename EventSendFunTokenToEvm to EventConvertCoinToEvm
k-yang Aug 14, 2024
31b4c6b
Merge branch 'main' into fix/evm/funtoken-conversions
k-yang Aug 14, 2024
66821b5
test: TestCreateFunTokenFromCoin
k-yang Aug 14, 2024
e54ce5c
fix: TestConvertCoinToEvmAndBack
k-yang Aug 14, 2024
b8600cb
Update funtoken_from_coin_test.go
k-yang Aug 14, 2024
5142418
refactor TestCreateFunTokenFromERC20
k-yang Aug 14, 2024
8896f13
fix: send tokens from evm to cosmos and back
k-yang Aug 14, 2024
455bd37
add negative test cases
k-yang Aug 14, 2024
60e56b6
add negative test case
k-yang Aug 14, 2024
d4528fe
add negative cases for TestConvertCoinToEvmAndBack
k-yang Aug 14, 2024
99b6290
Update CHANGELOG.md
k-yang Aug 14, 2024
1d5114f
fix: precompile test
k-yang Aug 14, 2024
ee1b14b
refactor: rename SendFunTokenToEvm to ConvertCoinToEvm
k-yang Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#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
- [#2001](https://github.com/NibiruChain/nibiru/pull/2001) - refactor(evm): simplify FunToken methods and tests
- [#2003](https://github.com/NibiruChain/nibiru/pull/2003) - fix(evm): fix FunToken conversions between Cosmos and EVM

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

Expand Down
4 changes: 2 additions & 2 deletions proto/eth/evm/v1/events.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023-2024 Nibi, Inc.

Check failure on line 1 in proto/eth/evm/v1/events.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present message "EventSendFunTokenToEvm" was deleted from file.
syntax = "proto3";
package eth.evm.v1;

Expand Down Expand Up @@ -55,8 +55,8 @@
bool is_made_from_coin = 4;
}

// SendFunTokenToEvm defines sending fun token to erc20 event.
message EventSendFunTokenToEvm {
// ConvertCoinToEvm defines sending fun token to erc20 event.
message EventConvertCoinToEvm {
string sender = 1;
string erc20_contract_address = 2;
string to_eth_addr = 3;
Expand Down
4 changes: 2 additions & 2 deletions proto/eth/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
option go_package = "github.com/NibiruChain/nibiru/v2/x/evm";

// Msg defines the evm Msg service.
service Msg {

Check failure on line 16 in proto/eth/evm/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present RPC "SendFunTokenToEvm" on service "Msg" was deleted.
// EthereumTx defines a method submitting Ethereum transactions.
rpc EthereumTx(MsgEthereumTx) returns (MsgEthereumTxResponse) {
option (google.api.http).post = "/nibiru/evm/v1/ethereum_tx";
Expand All @@ -27,10 +27,10 @@
// 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
// ConvertCoinToEvm: 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);
rpc ConvertCoinToEvm(MsgSendFunTokenToEvm) returns (MsgSendFunTokenToEvmResponse);
k-yang marked this conversation as resolved.
Show resolved Hide resolved
}

// MsgEthereumTx encapsulates an Ethereum transaction as an SDK message.
Expand Down
6 changes: 3 additions & 3 deletions x/evm/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetTxCmd() *cobra.Command {
cmds := []*cobra.Command{
CmdCreateFunTokenFromBankCoin(),
CmdCreateFunTokenFromERC20(),
SendFunTokenToEvm(),
ConvertCoinToEvm(),
}
for _, cmd := range cmds {
txCmd.AddCommand(cmd)
Expand Down Expand Up @@ -99,8 +99,8 @@ func CmdCreateFunTokenFromERC20() *cobra.Command {
return cmd
}

// SendFunTokenToEvm broadcast MsgSendFunTokenToEvm
func SendFunTokenToEvm() *cobra.Command {
// ConvertCoinToEvm broadcast MsgSendFunTokenToEvm
func ConvertCoinToEvm() *cobra.Command {
cmd := &cobra.Command{
Use: "send-funtoken-to-erc20 [to_eth_addr] [coin] [flags]",
Short: `Send bank [coin] to its erc20 representation for the user [to_eth_addr]"`,
Expand Down
134 changes: 67 additions & 67 deletions x/evm/events.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/evm/evmmodule/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Suite) TestExportInitGenesis() {
s.Require().NoError(err)

// Send fungible token coins from bank to evm
_, err = deps.EvmKeeper.SendFunTokenToEvm(
_, err = deps.EvmKeeper.ConvertCoinToEvm(
deps.Ctx,
&evm.MsgSendFunTokenToEvm{
Sender: deps.Sender.NibiruAddr.String(),
Expand Down
2 changes: 1 addition & 1 deletion x/evm/evmtest/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func AssertERC20BalanceEqual(
) {
actualBalance, err := deps.EvmKeeper.ERC20().BalanceOf(erc20, account, deps.Ctx)
assert.NoError(t, err)
assert.Zero(t, expectedBalance.Cmp(actualBalance))
assert.Zero(t, expectedBalance.Cmp(actualBalance), "expected %s, got %s", expectedBalance, actualBalance)
}

// CreateFunTokenForBankCoin: Uses the "TestDeps.Sender" account to create a
Expand Down
47 changes: 28 additions & 19 deletions x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@
) (evmResp *evm.MsgEthereumTxResponse, err error) {
input, err := e.ABI.Pack("mint", to, amount)
if err != nil {
return
return nil, fmt.Errorf("failed to pack ABI args: %w", err)

Check warning on line 58 in x/evm/keeper/erc20.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/erc20.go#L58

Added line #L58 was not covered by tests
}
commit := true
return e.CallContractWithInput(ctx, from, &contract, commit, input)
return e.CallContractWithInput(ctx, from, &contract, true, input)
}

/*
Expand All @@ -74,13 +73,23 @@
func (e erc20Calls) Transfer(
contract, from, to gethcommon.Address, amount *big.Int,
ctx sdk.Context,
) (evmResp *evm.MsgEthereumTxResponse, err error) {
) (out bool, err error) {
input, err := e.ABI.Pack("transfer", to, amount)
if err != nil {
return
return false, fmt.Errorf("failed to pack ABI args: %w", err)

Check warning on line 79 in x/evm/keeper/erc20.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/erc20.go#L79

Added line #L79 was not covered by tests
}
commit := true
return e.CallContractWithInput(ctx, from, &contract, commit, input)
resp, err := e.CallContractWithInput(ctx, from, &contract, true, input)
if err != nil {
return false, err
}

var erc20Bool ERC20Bool
err = e.ABI.UnpackIntoInterface(&erc20Bool, "transfer", resp.Ret)
if err != nil {
return false, err

Check warning on line 89 in x/evm/keeper/erc20.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/erc20.go#L89

Added line #L89 was not covered by tests
}

return erc20Bool.Value, nil
}

// BalanceOf retrieves the balance of an ERC20 token for a specific account.
Expand Down Expand Up @@ -339,31 +348,31 @@

func (k Keeper) LoadERC20BigInt(
ctx sdk.Context,
erc20Abi *gethabi.ABI,
erc20Contract gethcommon.Address,
abi *gethabi.ABI,
contract gethcommon.Address,
methodName string,
args ...any,
) (out *big.Int, err error) {
commit := false
res, err := k.CallContract(
ctx,
erc20Abi,
abi,
evm.EVM_MODULE_ADDRESS,
&erc20Contract,
commit,
&contract,
false,
methodName,
args...,
)
if err != nil {
return out, err
return nil, err

Check warning on line 366 in x/evm/keeper/erc20.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/erc20.go#L366

Added line #L366 was not covered by tests
}

erc20Val := new(ERC20BigInt)
err = erc20Abi.UnpackIntoInterface(
erc20Val, methodName, res.Ret,
erc20BigInt := new(ERC20BigInt)
err = abi.UnpackIntoInterface(
erc20BigInt, methodName, res.Ret,
)
if err != nil {
return out, err
return nil, err

Check warning on line 374 in x/evm/keeper/erc20.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/erc20.go#L374

Added line #L374 was not covered by tests
}
return erc20Val.Value, err

return erc20BigInt.Value, nil
}
Loading
Loading