Skip to content

Commit

Permalink
refactor: TestCreateFunTokenFromCoin
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jan 17, 2025
1 parent a8dd8a6 commit eb47f0a
Showing 1 changed file with 109 additions and 120 deletions.
229 changes: 109 additions & 120 deletions x/evm/keeper/funtoken_from_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import (

func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {
deps := evmtest.NewTestDeps()

// Compute contract address. FindERC20 should fail
nonce := deps.NewStateDB().GetNonce(deps.Sender.EthAddr)
contractAddress := crypto.CreateAddress(deps.Sender.EthAddr, nonce)
evmObj, _ := deps.NewEVM()
metadata, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, evmObj, contractAddress)
s.Require().Error(err)
s.Require().Nil(metadata)
s.Run("Compute contract address. FindERC20 should fail", func() {
evmObj, _ := deps.NewEVM()
metadata, err := deps.EvmKeeper.FindERC20Metadata(
deps.Ctx,
evmObj,
crypto.CreateAddress(evm.EVM_MODULE_ADDRESS, deps.EvmKeeper.GetAccNonce(deps.Ctx, evm.EVM_MODULE_ADDRESS)),
)
s.Require().Error(err)
s.Require().Nil(metadata)
})

s.T().Log("Setup: Create a coin in the bank state")
bankDenom := "sometoken"
Expand All @@ -48,123 +50,110 @@ func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {
Symbol: "TOKEN",
})

s.T().Log("sad: not enough funds to create fun token")
_, err = deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().ErrorContains(err, "insufficient funds")

// Give the sender funds for the fee
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))

s.T().Log("sad: invalid bank denom")
_, err = deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: "doesn't exist",
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().Error(err)

s.T().Log("happy: CreateFunToken for the bank coin")
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
createFuntokenResp, err := deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().NoError(err)

erc20Addr := createFuntokenResp.FuntokenMapping.Erc20Addr

s.Equal(
createFuntokenResp.FuntokenMapping,
evm.FunToken{
Erc20Addr: erc20Addr,
BankDenom: bankDenom,
IsMadeFromCoin: true,
},
)

s.T().Log("Expect ERC20 to be deployed")
_, err = deps.EvmKeeper.Code(deps.Ctx, &evm.QueryCodeRequest{
Address: erc20Addr.String(),
s.Run("insufficient funds to create funtoken", func() {
s.T().Log("sad: not enough funds to create fun token")
_, err := deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().ErrorContains(err, "insufficient funds")
})
s.Require().NoError(err)

s.T().Log("Expect ERC20 metadata on contract")
info, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, evmObj, erc20Addr.Address)
s.Require().NoError(err, info)
s.Equal(
keeper.ERC20Metadata{
Name: bankDenom,
Symbol: "TOKEN",
Decimals: 0,
}, *info,
)
s.Run("invalid bank denom", func() {
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
_, err := deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: "doesn't exist",
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().Error(err)
})

// Event "EventFunTokenCreated" must present
testutil.RequireContainsTypedEvent(
s.T(),
deps.Ctx,
&evm.EventFunTokenCreated{
BankDenom: bankDenom,
Erc20ContractAddress: erc20Addr.String(),
Creator: deps.Sender.NibiruAddr.String(),
IsMadeFromCoin: true,
},
)
s.Run("happy: CreateFunToken for the bank coin", func() {
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
expectedErc20Addr := crypto.CreateAddress(evm.EVM_MODULE_ADDRESS, deps.EvmKeeper.GetAccNonce(deps.Ctx, evm.EVM_MODULE_ADDRESS))
createFuntokenResp, err := deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().NoError(err)

s.Equal(
createFuntokenResp.FuntokenMapping,
evm.FunToken{
Erc20Addr: eth.EIP55Addr{Address: expectedErc20Addr},
BankDenom: bankDenom,
IsMadeFromCoin: true,
},
)
actualErc20Addr := createFuntokenResp.FuntokenMapping.Erc20Addr

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

s.T().Log("Expect ERC20 metadata on contract")
evmObj, _ := deps.NewEVM()
info, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, evmObj, actualErc20Addr.Address)
s.Require().NoError(err, info)
s.Equal(
keeper.ERC20Metadata{
Name: bankDenom,
Symbol: "TOKEN",
Decimals: 0,
}, *info,
)

s.T().Log("sad: CreateFunToken for the bank coin: already registered")
// Give the sender funds for the fee
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
_, err = deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().ErrorContains(err, "funtoken mapping already created")
// Event "EventFunTokenCreated" must present
testutil.RequireContainsTypedEvent(
s.T(),
deps.Ctx,
&evm.EventFunTokenCreated{
BankDenom: bankDenom,
Erc20ContractAddress: actualErc20Addr.String(),
Creator: deps.Sender.NibiruAddr.String(),
IsMadeFromCoin: true,
},
)
})

s.T().Log("sad: bank denom metadata not registered")
// Give the sender funds for the fee
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
_, err = deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: "some random denom",
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().ErrorContains(err, "bank coin denom should have bank metadata for denom")
s.Run("sad: CreateFunToken for the bank coin: already registered", func() {
// Give the sender funds for the fee
s.Require().NoError(testapp.FundAccount(
deps.App.BankKeeper,
deps.Ctx,
deps.Sender.NibiruAddr,
deps.EvmKeeper.FeeForCreateFunToken(deps.Ctx),
))
_, err := deps.EvmKeeper.CreateFunToken(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgCreateFunToken{
FromBankDenom: bankDenom,
Sender: deps.Sender.NibiruAddr.String(),
},
)
s.Require().ErrorContains(err, "funtoken mapping already created")
})
}

func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
Expand Down

0 comments on commit eb47f0a

Please sign in to comment.