From 187ad820f79ca7b606eaed24b0092b7a76960626 Mon Sep 17 00:00:00 2001 From: Oleg Nikonychev Date: Wed, 10 Jul 2024 20:25:18 +0400 Subject: [PATCH] chore(test): reverted funtoken precompile test back to the isolated state (#1961) * chore(test): returned funtoken precompile test back to the isolated state * chore: changelog update * refactor: PR feedback to use evmtest util fn --------- Co-authored-by: Unique-Divine --- CHANGELOG.md | 1 + .../testutil/testnetwork/validator_node.go | 4 +- x/evm/precompile/funtoken_test.go | 100 +++++++----------- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c01c9950e..d8baac699 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -81,6 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#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 - [#1960](https://github.com/NibiruChain/nibiru/pull/1960) - test(network): graceful cleanup for more consistent CI runs +- [#1961](https://github.com/NibiruChain/nibiru/pull/1961) - chore(test): reverted funtoken precompile test back to the isolated state #### Dapp modules: perp, spot, oracle, etc diff --git a/x/common/testutil/testnetwork/validator_node.go b/x/common/testutil/testnetwork/validator_node.go index b69662352..932201429 100644 --- a/x/common/testutil/testnetwork/validator_node.go +++ b/x/common/testutil/testnetwork/validator_node.go @@ -118,7 +118,9 @@ func stopValidatorNode(v *Validator) { // Close the API server. // Any blocked "Accept" operations will be unblocked and return errors. err := v.api.Close() - v.Logger.Logf("❌ Error closing the API server server: %w", err) + if err != nil { + v.Logger.Logf("❌ Error closing the API server: %w", err) + } } if v.grpc != nil { diff --git a/x/evm/precompile/funtoken_test.go b/x/evm/precompile/funtoken_test.go index 1aecefa03..08b9f9fe7 100644 --- a/x/evm/precompile/funtoken_test.go +++ b/x/evm/precompile/funtoken_test.go @@ -5,10 +5,6 @@ import ( "math/big" "testing" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/NibiruChain/nibiru/eth" "github.com/NibiruChain/nibiru/x/common/testutil" "github.com/NibiruChain/nibiru/x/evm" "github.com/NibiruChain/nibiru/x/evm/embeds" @@ -73,82 +69,66 @@ func (s *Suite) FunToken_PrecompileExists() { s.ErrorContains(err, "Precompile error") } -// FunToken_HappyPath: runs a whole process of sending fun token from bank to evm and back -// Steps: -// 1. Mint a new bank coin: ibc/usdc -// 2. Create fungible token from the bank coin -// 3. Send coins from bank to evm account -// 4. Send some coins between evm accounts -// 5. Send part of the tokens from evm to a bank account func (s *Suite) FunToken_HappyPath() { precompileAddr := precompile.PrecompileAddr_FuntokenGateway abi := embeds.Contract_Funtoken.ABI deps := evmtest.NewTestDeps() - bankDenom := "ibc/usdc" - amountToSendToEvm := int64(69_420) - amountToSendWithinEvm := int64(1) - amountToSendFromEvm := int64(419) - theUser := deps.Sender.EthAddr theEvm := evm.ModuleAddressEVM() s.True(deps.K.PrecompileSet().Has(precompileAddr.ToAddr()), "did not see precompile address during \"InitPrecompiles\"") - // Create new fungible token s.T().Log("Create FunToken mapping and ERC20") + bankDenom := "ibc/usdc" funtoken := evmtest.CreateFunTokenForBankCoin(&deps, bankDenom, &s.Suite) contract := funtoken.Erc20Addr.ToAddr() - // Mint coins for the sender - spendableCoins := sdk.NewCoins(sdk.NewInt64Coin(bankDenom, amountToSendToEvm)) - err := deps.Chain.BankKeeper.MintCoins(deps.Ctx, evm.ModuleName, spendableCoins) - s.Require().NoError(err) - err = deps.Chain.BankKeeper.SendCoinsFromModuleToAccount( - deps.Ctx, evm.ModuleName, deps.Sender.NibiruAddr, spendableCoins, - ) - s.Require().NoError(err) - s.T().Log("Balances of the ERC20 should start empty") evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(0)) evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(0)) - // Send fungible tokens from bank to EVM - _, err = deps.K.SendFunTokenToEvm( - deps.Ctx, - &evm.MsgSendFunTokenToEvm{ - Sender: deps.Sender.NibiruAddr.String(), - BankCoin: sdk.Coin{Denom: bankDenom, Amount: math.NewInt(amountToSendToEvm)}, - ToEthAddr: eth.MustNewHexAddrFromStr(theUser.String()), - }, - ) - s.Require().NoError(err, "failed to send FunToken to EVM") + s.T().Log("Mint tokens - Fail from non-owner") + { + from := theUser + to := theUser + input, err := embeds.Contract_ERC20Minter.ABI.Pack("mint", to, big.NewInt(69_420)) + s.NoError(err) + _, err = evmtest.DoEthTx(&deps, contract, from, input) + s.ErrorContains(err, "Ownable: caller is not the owner") + } - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(amountToSendToEvm)) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(0)) + s.T().Log("Mint tokens - Success") + { + from := theEvm + to := theUser + input, err := embeds.Contract_ERC20Minter.ABI.Pack("mint", to, big.NewInt(69_420)) + s.NoError(err) + + _, err = evmtest.DoEthTx(&deps, contract, from, input) + s.NoError(err) + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(69_420)) + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(0)) + } - // Transfer tokens to another account within EVM s.T().Log("Transfer - Success (sanity check)") randomAcc := testutil.AccAddress() - amountRemaining := amountToSendToEvm - amountToSendWithinEvm { from := theUser to := theEvm - _, err := deps.K.ERC20().Transfer(contract, from, to, big.NewInt(amountToSendWithinEvm), deps.Ctx) + _, err := deps.K.ERC20().Transfer(contract, from, to, big.NewInt(1), deps.Ctx) s.NoError(err) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(amountRemaining)) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(amountToSendWithinEvm)) - s.Equal( - int64(0), - deps.Chain.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.Int64(), + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(69_419)) + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(1)) + s.Equal("0", + deps.Chain.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.String(), ) } - // Send fungible token from EVM to bank s.T().Log("Send using precompile") - - callArgs := precompile.ArgsFunTokenBankSend(contract, big.NewInt(amountToSendFromEvm), randomAcc) + amtToSend := int64(419) + callArgs := precompile.ArgsFunTokenBankSend(contract, big.NewInt(amtToSend), randomAcc) methodName := string(precompile.FunTokenMethod_BankSend) input, err := abi.Pack(methodName, callArgs...) s.NoError(err) @@ -157,21 +137,15 @@ func (s *Suite) FunToken_HappyPath() { _, err = evmtest.DoEthTx(&deps, precompileAddr.ToAddr(), from, input) s.Require().NoError(err) - evmtest.AssertERC20BalanceEqual( - s.T(), - &deps, - contract, - theUser, - big.NewInt(amountRemaining-amountToSendFromEvm), - ) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(amountToSendWithinEvm)) - s.Equal(fmt.Sprintf("%d", amountToSendFromEvm), + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(69_419-amtToSend)) + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(1)) + s.Equal(fmt.Sprintf("%d", amtToSend), deps.Chain.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.String(), ) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(amountRemaining-amountToSendFromEvm)) - evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(amountToSendWithinEvm)) - s.Equal( - amountToSendFromEvm, - deps.Chain.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.Int64(), + + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theUser, big.NewInt(69_000)) + evmtest.AssertERC20BalanceEqual(s.T(), &deps, contract, theEvm, big.NewInt(1)) + s.Equal("419", + deps.Chain.BankKeeper.GetBalance(deps.Ctx, randomAcc, funtoken.BankDenom).Amount.String(), ) }