Skip to content

Commit

Permalink
fix: add burn method when coin is made from evm side
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Jan 6, 2025
1 parent 1fd8a32 commit a142fa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ func (p precompileFunToken) sendToEvm(
return nil, fmt.Errorf("failed to send coins to module: %w", err)
}

// burn if funtoken was created from EVM side
if !funtoken.IsMadeFromCoin {
err := p.evmKeeper.Bank.BurnCoins(ctx, evm.ModuleName, sdk.NewCoins(coinToSend))
if err != nil {
return nil, fmt.Errorf("failed to burn coins: %w", err)
}
}

// 2) mint (or unescrow) the ERC20
erc20Addr := funtoken.Erc20Addr.Address
actualAmt, err := p.mintOrUnescrowERC20(
Expand Down
14 changes: 13 additions & 1 deletion x/evm/precompile/funtoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ func (s *FuntokenSuite) TestSendToEvm() {
bankBal := deps.App.BankKeeper.GetBalance(deps.Ctx, deps.Sender.NibiruAddr, bankDenom).Amount.BigInt()
s.EqualValues(wantBank, bankBal, "did user lose 1000 unibi from bank?")

// check the evm module account balance
wantEvm := big.NewInt(1000)
evmBal := deps.App.BankKeeper.GetBalance(deps.Ctx, evm.EVM_MODULE_ADDRESS[:], bankDenom).Amount.BigInt()
s.EqualValues(wantEvm, evmBal, "did evm module gain 1000 unibi?")

s.T().Log("Check the user gained 1000 in ERC20 representation")
evmtest.AssertERC20BalanceEqual(s.T(), deps, erc20Addr, deps.Sender.EthAddr, big.NewInt(1000))
}
Expand Down Expand Up @@ -559,7 +564,7 @@ func (s *FuntokenSuite) TestBankMsgSend() {
}

func bigTokens(n int64) *big.Int {
e18 := big.NewInt(1_000_000_000_000_000_000) // 1e18
e18 := big.NewInt(1e18) // 1e18
return new(big.Int).Mul(big.NewInt(n), e18)
}

Expand Down Expand Up @@ -648,6 +653,13 @@ func (s *FuntokenSuite) TestSendToEvm_NotMadeFromCoin() {
balAfter := deps.App.BankKeeper.GetBalance(deps.Ctx, alice.NibiruAddr, bankBal.Denom).Amount.BigInt()
s.Require().EqualValues(bigTokens(0), balAfter)

// check bob has 500 tokens again => 500 * 1e18
evmtest.AssertERC20BalanceEqual(s.T(), deps, erc20Addr, bob.EthAddr, bigTokens(500))

// check evm module account's balance
evmBal := deps.App.BankKeeper.GetBalance(deps.Ctx, evm.EVM_MODULE_ADDRESS[:], bankBal.Denom).Amount.BigInt()
s.Require().EqualValues(bigTokens(0), evmBal)

// user has 500 tokens again => 500 * 1e18
evmtest.AssertERC20BalanceEqual(s.T(), deps, erc20Addr, bob.EthAddr, bigTokens(500))
}

0 comments on commit a142fa8

Please sign in to comment.