Skip to content

Commit

Permalink
chore(test): reverted funtoken precompile test back to the isolated s…
Browse files Browse the repository at this point in the history
…tate (#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 <[email protected]>
  • Loading branch information
onikonychev and Unique-Divine authored Jul 10, 2024
1 parent d24f7a1 commit 187ad82
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion x/common/testutil/testnetwork/validator_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
100 changes: 37 additions & 63 deletions x/evm/precompile/funtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
)
}

0 comments on commit 187ad82

Please sign in to comment.