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

refactor: converting funtoken eth address to string in proto #2118

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 1 addition & 4 deletions proto/eth/evm/v1/evm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ option go_package = "github.com/NibiruChain/nibiru/v2/x/evm";
// "Coin" type in Golang.
message FunToken {
// Hexadecimal address of the ERC20 token to which the `FunToken` maps
string erc20_addr = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/v2/eth.EIP55Addr",
(gogoproto.nullable) = false
];
string erc20_addr = 1;

// bank_denom: Coin denomination in the Bank Module.
string bank_denom = 2;
Expand Down
14 changes: 4 additions & 10 deletions proto/eth/evm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,16 @@ message MsgUpdateParamsResponse {}
// 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/v2/eth.EIP55Addr",
(gogoproto.nullable) = true
];
// Hexadecimal address of the ERC20 token to which the `FunToken` maps (optional)
// The field can be null if not provided.
string from_erc20 = 1;

// 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];
Expand All @@ -253,10 +250,7 @@ message MsgCreateFunTokenResponse {
// MsgConvertCoinToEvm: Arguments to send a Bank Coin to ERC-20 representation
message MsgConvertCoinToEvm {
// Hexadecimal address of the ERC20 token to which the `FunToken` maps
string to_eth_addr = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/v2/eth.EIP55Addr",
(gogoproto.nullable) = false
];
string to_eth_addr = 1;

// Sender: Address for the signer of the transaction.
string sender = 2;
Expand Down
2 changes: 1 addition & 1 deletion x/evm/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *Suite) TestCmdQueryFunToken() {
name: "happy: query funtoken (erc20 addr)",
args: []string{
"funtoken",
dummyFuntoken.Erc20Addr.String(),
dummyFuntoken.Erc20Addr,
},
wantErr: "",
},
Expand Down
4 changes: 2 additions & 2 deletions x/evm/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CmdCreateFunToken() *cobra.Command {
if err != nil {
return err
}
msg.FromErc20 = &erc20Addr
msg.FromErc20 = erc20Addr.String()
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
Expand Down Expand Up @@ -115,7 +115,7 @@ func CmdConvertCoinToEvm() *cobra.Command {
msg := &evm.MsgConvertCoinToEvm{
Sender: clientCtx.GetFromAddress().String(),
BankCoin: coin,
ToEthAddr: eip55Addr,
ToEthAddr: eip55Addr.String(),
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
4 changes: 2 additions & 2 deletions x/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// tokens that are valid ERC20s with the same address.
// https://github.com/NibiruChain/nibiru/issues/1933
func (fun FunToken) ID() []byte {
return NewFunTokenID(fun.Erc20Addr.Address, fun.BankDenom)
return NewFunTokenID(gethcommon.HexToAddress(fun.Erc20Addr), fun.BankDenom)
}

func NewFunTokenID(erc20 gethcommon.Address, bankDenom string) []byte {
Expand Down Expand Up @@ -43,7 +43,7 @@ func NewFunToken(
return FunToken{
Erc20Addr: eth.EIP55Addr{
Address: erc20,
},
}.String(),
BankDenom: bankDenom,
IsMadeFromCoin: isMadeFromCoin,
}
Expand Down
176 changes: 89 additions & 87 deletions x/evm/evm.pb.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions x/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *TestSuite) TestFunToken() {
s.Require().NoError(err)

funtoken := evm.FunToken{
Erc20Addr: eip55Addr,
Erc20Addr: eip55Addr.String(),
BankDenom: tc.bankDenom,
}
if tc.wantErr {
Expand Down Expand Up @@ -109,10 +109,10 @@ func (s *TestSuite) TestFunToken() {
addrB, err := eth.NewEIP55AddrFromStr(tc.B)
s.Require().NoError(err)

funA := evm.FunToken{Erc20Addr: addrA}
funB := evm.FunToken{Erc20Addr: addrB}
funA := evm.FunToken{Erc20Addr: addrA.String()}
funB := evm.FunToken{Erc20Addr: addrB.String()}

s.EqualValues(funA.Erc20Addr.Address, funB.Erc20Addr.Address)
s.EqualValues(funA.Erc20Addr, funB.Erc20Addr)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/evmmodule/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func InitGenesis(
// Create fungible token mappings
for _, funToken := range genState.FuntokenMappings {
err := k.FunTokens.SafeInsert(
ctx, gethcommon.HexToAddress(funToken.Erc20Addr.String()), funToken.BankDenom, funToken.IsMadeFromCoin,
ctx, gethcommon.HexToAddress(funToken.Erc20Addr), funToken.BankDenom, funToken.IsMadeFromCoin,
)
if err != nil {
panic(fmt.Errorf("failed creating funtoken: %w", err))
Expand Down
8 changes: 4 additions & 4 deletions x/evm/evmmodule/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *Suite) TestExportInitGenesis() {
// Create fungible token from bank coin
funToken := evmtest.CreateFunTokenForBankCoin(&deps, "unibi", &s.Suite)
s.Require().NoError(err)
funTokenAddr := funToken.Erc20Addr.Address
funTokenAddr := funToken.Erc20Addr

// Fund sender's wallet
spendableCoins := sdk.NewCoins(sdk.NewInt64Coin("unibi", totalSupply.Int64()))
Expand All @@ -83,7 +83,7 @@ func (s *Suite) TestExportInitGenesis() {
&evm.MsgConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
BankCoin: sdk.Coin{Denom: "unibi", Amount: math.NewInt(amountToSendC.Int64())},
ToEthAddr: eip55Addr,
ToEthAddr: eip55Addr.String(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -117,12 +117,12 @@ func (s *Suite) TestExportInitGenesis() {
iter := deps.EvmKeeper.FunTokens.Indexes.BankDenom.ExactMatch(deps.Ctx, "unibi")
funTokens := deps.EvmKeeper.FunTokens.Collect(deps.Ctx, iter)
s.Require().Len(funTokens, 1)
s.Require().Equal(funTokenAddr.String(), funTokens[0].Erc20Addr.String())
s.Require().Equal(funTokenAddr, funTokens[0].Erc20Addr)
s.Require().Equal("unibi", funTokens[0].BankDenom)
s.Require().True(funTokens[0].IsMadeFromCoin)

// Check that fungible token balance of user C is correct
balance, err = deps.EvmKeeper.ERC20().BalanceOf(funTokenAddr, toUserC, deps.Ctx)
balance, err = deps.EvmKeeper.ERC20().BalanceOf(gethcommon.HexToAddress(funTokenAddr), toUserC, deps.Ctx)
s.Require().NoError(err)
s.Require().Equal(amountToSendC, balance)
}
4 changes: 2 additions & 2 deletions x/evm/evmtest/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func CreateFunTokenForBankCoin(
s.T().Log("Expect ERC20 to be deployed")
_, err = deps.EvmKeeper.Code(deps.Ctx,
&evm.QueryCodeRequest{
Address: erc20.String(),
Address: erc20,
},
)
s.NoError(err)
Expand Down Expand Up @@ -114,7 +114,7 @@ type FunTokenBalanceAssert struct {

func (bals FunTokenBalanceAssert) Assert(t *testing.T, deps TestDeps) {
AssertERC20BalanceEqualWithDescription(
t, deps, bals.FunToken.Erc20Addr.Address, bals.Account, bals.BalanceERC20,
t, deps, gethcommon.HexToAddress(bals.FunToken.Erc20Addr), bals.Account, bals.BalanceERC20,
bals.Description,
)
AssertBankBalanceEqualWithDescription(
Expand Down
4 changes: 3 additions & 1 deletion x/evm/keeper/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package keeper_test
import (
"math/big"

gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/NibiruChain/nibiru/v2/x/evm"
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
)
Expand All @@ -12,7 +14,7 @@ func (s *Suite) TestERC20Calls() {
deps := evmtest.NewTestDeps()
bankDenom := "ibc/btc"
funtoken := evmtest.CreateFunTokenForBankCoin(&deps, bankDenom, &s.Suite)
contract := funtoken.Erc20Addr.Address
contract := gethcommon.HexToAddress(funtoken.Erc20Addr)

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 @@ -43,7 +43,7 @@ func (k *Keeper) createFunTokenFromCoin(
funtoken = &evm.FunToken{
Erc20Addr: eth.EIP55Addr{
Address: erc20Addr,
},
}.String(),
BankDenom: bankDenom,
IsMadeFromCoin: true,
}
Expand Down
35 changes: 18 additions & 17 deletions x/evm/keeper/funtoken_from_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -104,12 +105,12 @@ func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {

s.T().Log("Expect ERC20 to be deployed")
_, err = deps.EvmKeeper.Code(deps.Ctx, &evm.QueryCodeRequest{
Address: erc20Addr.String(),
Address: erc20Addr,
})
s.Require().NoError(err)

s.T().Log("Expect ERC20 metadata on contract")
info, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, erc20Addr.Address)
info, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, gethcommon.HexToAddress(erc20Addr))
s.Require().NoError(err, info)
s.Equal(
keeper.ERC20Metadata{
Expand All @@ -125,7 +126,7 @@ func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {
deps.Ctx,
&evm.EventFunTokenCreated{
BankDenom: bankDenom,
Erc20ContractAddress: erc20Addr.String(),
Erc20ContractAddress: erc20Addr,
Creator: deps.Sender.NibiruAddr.String(),
IsMadeFromCoin: true,
},
Expand Down Expand Up @@ -182,7 +183,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10)),
ToEthAddr: eth.EIP55Addr{
Address: alice.EthAddr,
},
}.String(),
},
)
s.Require().NoError(err)
Expand All @@ -193,7 +194,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
deps.Ctx,
&evm.EventConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
Erc20ContractAddress: funToken.Erc20Addr.String(),
Erc20ContractAddress: funToken.Erc20Addr,
ToEthAddr: alice.EthAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10)),
},
Expand All @@ -208,7 +209,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
s.Require().Equal(sdk.NewInt(90), senderBalance.Amount)

// Check 3: erc-20 balance
balance, err := deps.EvmKeeper.ERC20().BalanceOf(funToken.Erc20Addr.Address, alice.EthAddr, deps.Ctx)
balance, err := deps.EvmKeeper.ERC20().BalanceOf(gethcommon.HexToAddress(funToken.Erc20Addr), alice.EthAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Zero(balance.Cmp(big.NewInt(10)))

Expand All @@ -220,7 +221,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(100)),
ToEthAddr: eth.EIP55Addr{
Address: alice.EthAddr,
},
}.String(),
},
)
s.Require().ErrorContains(err, "insufficient funds")
Expand All @@ -236,7 +237,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
true,
evmtest.FunTokenGasLimitSendToEvm,
"sendToBank",
funToken.Erc20Addr.Address,
gethcommon.HexToAddress(funToken.Erc20Addr),
big.NewInt(10),
deps.Sender.NibiruAddr.String(),
)
Expand All @@ -251,7 +252,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
s.Require().Equal(sdk.NewInt(100), senderBalance.Amount)

// Check 3: erc-20 balance
balance, err = deps.EvmKeeper.ERC20().BalanceOf(funToken.Erc20Addr.Address, alice.EthAddr, deps.Ctx)
balance, err = deps.EvmKeeper.ERC20().BalanceOf(gethcommon.HexToAddress(funToken.Erc20Addr), alice.EthAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Equal("0", balance.String())

Expand All @@ -264,7 +265,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
true,
evmtest.FunTokenGasLimitSendToEvm,
"sendToBank",
funToken.Erc20Addr.Address,
gethcommon.HexToAddress(funToken.Erc20Addr),
big.NewInt(10),
deps.Sender.NibiruAddr.String(),
)
Expand Down Expand Up @@ -301,7 +302,7 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
deployResp, err := evmtest.DeployContract(
&deps,
embeds.SmartContract_TestNativeSendThenPrecompileSendJson,
funtoken.Erc20Addr.Address,
gethcommon.HexToAddress(funtoken.Erc20Addr),
)
s.Require().NoError(err)

Expand All @@ -328,7 +329,7 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
&evm.MsgConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewIntFromBigInt(sendAmt)),
ToEthAddr: eth.EIP55Addr{Address: testContractAddr},
ToEthAddr: eth.EIP55Addr{Address: testContractAddr}.String(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -468,7 +469,7 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
deployResp, err := evmtest.DeployContract(
&deps,
embeds.SmartContract_TestERC20TransferThenPrecompileSend,
funToken.Erc20Addr.Address,
gethcommon.HexToAddress(funToken.Erc20Addr),
)
s.Require().NoError(err)

Expand All @@ -480,7 +481,7 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
&evm.MsgConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10e6)),
ToEthAddr: eth.EIP55Addr{Address: testContractAddr},
ToEthAddr: eth.EIP55Addr{Address: testContractAddr}.String(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -557,7 +558,7 @@ func (s *FunTokenFromCoinSuite) TestPrecompileSelfCallRevert() {
deployResp, err := evmtest.DeployContract(
&deps,
embeds.SmartContract_TestPrecompileSelfCallRevert,
funToken.Erc20Addr.Address,
gethcommon.HexToAddress(funToken.Erc20Addr),
)
s.Require().NoError(err)

Expand All @@ -569,7 +570,7 @@ func (s *FunTokenFromCoinSuite) TestPrecompileSelfCallRevert() {
&evm.MsgConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10e6)),
ToEthAddr: eth.EIP55Addr{Address: testContractAddr},
ToEthAddr: eth.EIP55Addr{Address: testContractAddr}.String(),
},
)
s.Require().NoError(err)
Expand Down Expand Up @@ -687,7 +688,7 @@ func (s *FunTokenFromCoinSuite) fundAndCreateFunToken(deps evmtest.TestDeps, uni
erc20Decimals, err := deps.EvmKeeper.LoadERC20Decimals(
deps.Ctx,
embeds.SmartContract_ERC20Minter.ABI,
createFunTokenResp.FuntokenMapping.Erc20Addr.Address,
gethcommon.HexToAddress(createFunTokenResp.FuntokenMapping.Erc20Addr),
)
s.Require().NoError(err)
s.Require().Equal(erc20Decimals, uint8(6))
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/funtoken_from_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (k *Keeper) createFunTokenFromERC20(
funtoken = &evm.FunToken{
Erc20Addr: eth.EIP55Addr{
Address: erc20,
},
}.String(),
BankDenom: bankDenom,
IsMadeFromCoin: false,
}
Expand Down
Loading
Loading