Skip to content

Commit

Permalink
chore: merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Oct 30, 2024
1 parent 6a64e47 commit 70b6e5e
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 87 deletions.
3 changes: 1 addition & 2 deletions app/evmante/evmante_can_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ func (ctd CanTransferDecorator) AnteHandle(
BaseFeeWei: baseFeeWeiPerGas,
}

stateDB := statedb.New(
stateDB := ctd.NewStateDB(
ctx,
ctd.EVMKeeper,
statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes())),
)
evmInstance := ctd.EVMKeeper.NewEVM(ctx, coreMsg, cfg, evm.NewNoOpTracer(), stateDB)
Expand Down

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions x/evm/embeds/contracts/TestNativeSendThenPrecompileSend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ contract TestNativeSendThenPrecompileSend {
bool isSent = nativeRecipient.send(nativeAmount);
require(isSent, "Failed to send native token");

(bool success, ) = FUNTOKEN_PRECOMPILE_ADDRESS.call(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
precompileAmount,
precompileRecipient
uint256 sentAmount = FUNTOKEN_PRECOMPILE.bankSend(
erc20,
precompileAmount,
precompileRecipient
);
require(
sentAmount == precompileAmount,
string.concat(
"IFunToken.bankSend succeeded but transferred the wrong amount",
"sentAmount ",
Strings.toString(sentAmount),
"expected ",
Strings.toString(precompileAmount)
)
);

require(success, string.concat("Failed to call precompile bankSend"));
}
}
16 changes: 16 additions & 0 deletions x/evm/evmtest/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ func BigPow(amount *big.Int, pow10Exp uint8) (powAmount *big.Int) {
pow10 := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(pow10Exp)), nil)
return new(big.Int).Mul(amount, pow10)
}

type FunTokenBalanceAssert struct {
FunToken evm.FunToken
Account gethcommon.Address
BalanceBank *big.Int
BalanceERC20 *big.Int
}

func (bals FunTokenBalanceAssert) Assert(t *testing.T, deps TestDeps) {
AssertERC20BalanceEqual(
t, deps, bals.FunToken.Erc20Addr.Address, bals.Account, bals.BalanceERC20,
)
AssertBankBalanceEqual(
t, deps, bals.FunToken.BankDenom, bals.Account, bals.BalanceBank,
)
}
25 changes: 12 additions & 13 deletions x/evm/keeper/bank_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ var (

type NibiruBankKeeper struct {
bankkeeper.BaseKeeper
StateDB *statedb.StateDB
StateDB *statedb.StateDB
TxStateDB *statedb.StateDB
}

func (evmKeeper *Keeper) NewStateDB(
ctx sdk.Context, txConfig statedb.TxConfig,
) *statedb.StateDB {
stateDB := statedb.New(ctx, evmKeeper, txConfig)
evmKeeper.Bank.ResetStateDB(stateDB)
evmKeeper.Bank.StateDB = stateDB
return stateDB
}

func (evmKeeper *Keeper) NewTxStateDB(
ctx sdk.Context, txConfig statedb.TxConfig,
) *statedb.StateDB {
stateDB := statedb.New(ctx, evmKeeper, txConfig)
evmKeeper.Bank.StateDB = stateDB
evmKeeper.Bank.TxStateDB = stateDB
return stateDB
}

Expand All @@ -44,17 +54,6 @@ func (bk NibiruBankKeeper) MintCoins(
return nil
}

func (bk *NibiruBankKeeper) ResetStateDB(db *statedb.StateDB) {
bk.StateDB = db
}

// s.Require().Equal(
// statedb.FromVM(evmObj).GetBalance(
// eth.NibiruAddrToEthAddr(randomAcc),
// ).String(),
// "420"+strings.Repeat("0", 12),
// )

func (bk NibiruBankKeeper) BurnCoins(
ctx sdk.Context,
moduleName string,
Expand Down
4 changes: 3 additions & 1 deletion x/evm/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ func (k Keeper) CallContract(
if err != nil {
return nil, fmt.Errorf("failed to pack ABI args: %w", err)
}
evmResp, _, err = k.CallContractWithInput(ctx, fromAcc, contract, commit, contractInput)
evmResp, _, err = k.CallContractWithInput(
ctx, fromAcc, contract, commit, contractInput,
)
return evmResp, err
}

Expand Down
125 changes: 81 additions & 44 deletions x/evm/keeper/funtoken_from_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
bankDenom := evm.EVMBankDenom

// Initial setup
funTokenErc20Addr := s.fundAndCreateFunToken(deps, 100)
funToken := s.fundAndCreateFunToken(deps, 100)

s.T().Log("Convert bank coin to erc-20")
_, err := deps.EvmKeeper.ConvertCoinToEvm(
Expand All @@ -193,7 +193,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
deps.Ctx,
&evm.EventConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
Erc20ContractAddress: funTokenErc20Addr.String(),
Erc20ContractAddress: funToken.Erc20Addr.String(),
ToEthAddr: alice.EthAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10)),
},
Expand All @@ -208,7 +208,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
s.Require().Equal(sdk.NewInt(90), senderBalance.Amount)

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

Expand All @@ -233,7 +233,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
&precompile.PrecompileAddr_FunToken,
true,
"bankSend",
funTokenErc20Addr.Address,
funToken.Erc20Addr.Address,
big.NewInt(10),
deps.Sender.NibiruAddr.String(),
)
Expand All @@ -248,7 +248,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
s.Require().Equal(sdk.NewInt(100), senderBalance.Amount)

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

Expand All @@ -260,7 +260,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
&precompile.PrecompileAddr_FunToken,
true,
"bankSend",
funTokenErc20Addr.Address,
funToken.Erc20Addr.Address,
big.NewInt(10),
deps.Sender.NibiruAddr.String(),
)
Expand All @@ -286,13 +286,14 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
bankDenom := evm.EVMBankDenom

// Initial setup
funTokenErc20Addr := s.fundAndCreateFunToken(deps, 10e6)
sendAmt := big.NewInt(10)
funtoken := s.fundAndCreateFunToken(deps, sendAmt.Int64())

s.T().Log("Deploy Test Contract")
deployResp, err := evmtest.DeployContract(
&deps,
embeds.SmartContract_TestNativeSendThenPrecompileSendJson,
funTokenErc20Addr.Address,
funtoken.Erc20Addr.Address,
)
s.Require().NoError(err)

Expand All @@ -304,26 +305,50 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
deps.App.BankKeeper,
deps.Ctx,
testContractNibiAddr,
sdk.NewCoins(sdk.NewCoin(bankDenom, sdk.NewInt(10e6)))),
sdk.NewCoins(sdk.NewCoin(bankDenom, sdk.NewIntFromBigInt(sendAmt)))),
)
evmtest.AssertBankBalanceEqual(
s.T(), deps, bankDenom, testContractAddr, sendAmt,
)
evmtest.AssertBankBalanceEqual(
s.T(), deps, bankDenom, evm.EVM_MODULE_ADDRESS, big.NewInt(0),
)

s.T().Log("Convert bank coin to erc-20: give test contract 10 WNIBI (erc20)")
_, err = deps.EvmKeeper.ConvertCoinToEvm(
sdk.WrapSDKContext(deps.Ctx),
&evm.MsgConvertCoinToEvm{
Sender: deps.Sender.NibiruAddr.String(),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewInt(10e6)),
BankCoin: sdk.NewCoin(bankDenom, sdk.NewIntFromBigInt(sendAmt)),
ToEthAddr: eth.EIP55Addr{Address: testContractAddr},
},
)
s.Require().NoError(err)
evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: testContractAddr,
BalanceBank: sendAmt,
BalanceERC20: sendAmt,
}.Assert(s.T(), deps)
evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: evm.EVM_MODULE_ADDRESS,
BalanceBank: sendAmt,
BalanceERC20: big.NewInt(0),
}.Assert(s.T(), deps)

// Alice hex and Alice bech32 is the same address in different representation,
// so funds are expected to be available in Alice's bank wallet
alice := evmtest.NewEthPrivAcc()
evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: alice.EthAddr,
BalanceBank: big.NewInt(0),
BalanceERC20: big.NewInt(0),
}.Assert(s.T(), deps)

s.T().Log("call test contract")
_, err = deps.EvmKeeper.CallContract(
evmResp, err := deps.EvmKeeper.CallContract(
deps.Ctx,
embeds.SmartContract_TestNativeSendThenPrecompileSendJson.ABI,
deps.Sender.EthAddr,
Expand All @@ -332,34 +357,34 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
"nativeSendThenPrecompileSend",
[]any{
alice.EthAddr,
evm.NativeToWei(big.NewInt(10e6)), // for native evm send: 18 decimals
evm.NativeToWei(sendAmt), // native send uses wei units
alice.NibiruAddr.String(),
big.NewInt(10e6), // for precompile bankSend: 6 decimals
sendAmt, // amount for precompile bankSend
}...,
)
s.Require().NoError(err)

// Check 1: Alice has 20 NIBI in bank
aliceBankBalance := deps.App.BankKeeper.GetBalance(deps.Ctx, alice.NibiruAddr, bankDenom)
s.Require().Equal(sdk.NewInt(20e6), aliceBankBalance.Amount)

// Check 2: Alice has 0 WNIBI on ERC20
aliceERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funTokenErc20Addr.Address, alice.EthAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Zero(big.NewInt(0).Cmp(aliceERC20Balance))

// Check 3: test contract has 0 NIBI in bank
testContractBankBalance := deps.App.BankKeeper.GetBalance(deps.Ctx, testContractNibiAddr, bankDenom)
s.Require().Equal(sdk.NewInt(0), testContractBankBalance.Amount)

// Check 4: test contract has 0 WNIBI on ERC20
testContractERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funTokenErc20Addr.Address, testContractAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Zero(big.NewInt(0).Cmp(testContractERC20Balance))

// Check 5: module balance has 0 NIBI escrowed
moduleBalance := deps.App.BankKeeper.GetBalance(deps.Ctx, authtypes.NewModuleAddress(evm.ModuleName), bankDenom)
s.Require().Equal(sdk.NewInt(0), moduleBalance.Amount)
s.Empty(evmResp.VmError)

evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: alice.EthAddr,
BalanceBank: new(big.Int).Mul(sendAmt, big.NewInt(2)),
BalanceERC20: big.NewInt(0),
}.Assert(s.T(), deps)

evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: testContractAddr,
BalanceBank: big.NewInt(0),
BalanceERC20: big.NewInt(0),
}.Assert(s.T(), deps)

evmtest.FunTokenBalanceAssert{
FunToken: funtoken,
Account: evm.EVM_MODULE_ADDRESS,
BalanceBank: big.NewInt(0),
BalanceERC20: big.NewInt(0),
}.Assert(s.T(), deps)
}

// TestERC20TransferThenPrecompileSend
Expand All @@ -381,13 +406,13 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
bankDenom := evm.EVMBankDenom

// Initial setup
funTokenErc20Addr := s.fundAndCreateFunToken(deps, 10e6)
funToken := s.fundAndCreateFunToken(deps, 10e6)

s.T().Log("Deploy Test Contract")
deployResp, err := evmtest.DeployContract(
&deps,
embeds.SmartContract_TestERC20TransferThenPrecompileSend,
funTokenErc20Addr.Address,
funToken.Erc20Addr.Address,
)
s.Require().NoError(err)

Expand Down Expand Up @@ -427,12 +452,12 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
s.Require().Equal(sdk.NewInt(9e6), aliceBankBalance.Amount)

// Check 2: Alice has 1 WNIBI on ERC20
aliceERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funTokenErc20Addr.Address, alice.EthAddr, deps.Ctx)
aliceERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funToken.Erc20Addr.Address, alice.EthAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Zero(big.NewInt(1e6).Cmp(aliceERC20Balance))

// Check 3: test contract has 0 WNIBI on ERC20
testContractERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funTokenErc20Addr.Address, testContractAddr, deps.Ctx)
testContractERC20Balance, err := deps.EvmKeeper.ERC20().BalanceOf(funToken.Erc20Addr.Address, testContractAddr, deps.Ctx)
s.Require().NoError(err)
s.Require().Zero(big.NewInt(0).Cmp(testContractERC20Balance))

Expand All @@ -442,7 +467,7 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
}

// fundAndCreateFunToken creates initial setup for tests
func (s *FunTokenFromCoinSuite) fundAndCreateFunToken(deps evmtest.TestDeps, unibiAmount int64) eth.EIP55Addr {
func (s *FunTokenFromCoinSuite) fundAndCreateFunToken(deps evmtest.TestDeps, unibiAmount int64) evm.FunToken {
bankDenom := evm.EVMBankDenom

s.T().Log("Setup: Create a coin in the bank state")
Expand All @@ -451,12 +476,15 @@ func (s *FunTokenFromCoinSuite) fundAndCreateFunToken(deps evmtest.TestDeps, uni
{
Denom: bankDenom,
Exponent: 0,
Aliases: nil,
},
{
Denom: "NIBI",
Exponent: 6,
},
},
Base: bankDenom,
Display: bankDenom,
Name: bankDenom,
Display: "NIBI",
Name: "NIBI",
Symbol: "NIBI",
})

Expand All @@ -477,7 +505,16 @@ func (s *FunTokenFromCoinSuite) fundAndCreateFunToken(deps evmtest.TestDeps, uni
},
)
s.Require().NoError(err)
return createFunTokenResp.FuntokenMapping.Erc20Addr

erc20Decimals, err := deps.EvmKeeper.LoadERC20Decimals(
deps.Ctx,
embeds.SmartContract_ERC20Minter.ABI,
createFunTokenResp.FuntokenMapping.Erc20Addr.Address,
)
s.Require().NoError(err)
s.Require().Equal(erc20Decimals, uint8(6))

return createFunTokenResp.FuntokenMapping
}

type FunTokenFromCoinSuite struct {
Expand Down
1 change: 0 additions & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ func (k *Keeper) ApplyEvmMsg(ctx sdk.Context,

// The dirty states in `StateDB` is either committed or discarded after return
if commit {
fmt.Println("stateDB.Commit in ApplyEvmMsg")
if err := stateDB.Commit(); err != nil {
return nil, evmObj, fmt.Errorf("failed to commit stateDB: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (k *Keeper) SetAccBalance(
ctx sdk.Context, addr gethcommon.Address, amountEvmDenom *big.Int,
) error {
addrBech32 := eth.EthAddrToNibiruAddr(addr)
balance := k.Bank.BaseKeeper.GetBalance(ctx, addrBech32, evm.EVMBankDenom).Amount.BigInt()
balance := k.Bank.GetBalance(ctx, addrBech32, evm.EVMBankDenom).Amount.BigInt()
delta := new(big.Int).Sub(amountEvmDenom, balance)
bk := k.Bank.BaseKeeper

Expand Down
Loading

0 comments on commit 70b6e5e

Please sign in to comment.