Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(evm): proper eth tx logs emission for funtoken operations #2212

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2209](https://github.com/NibiruChain/nibiru/pull/2209) - refator(ci):
Simplify GitHub actions based on conditional paths, removing the need for files like ".github/workflows/skip-unit-tests.yml".
- [#2211](https://github.com/NibiruChain/nibiru/pull/2211) - ci(chaosnet): avoid building on cache injected directories
- [#2212](https://github.com/NibiruChain/nibiru/pull/2212) - fix(evm): proper eth tx logs emission for funtoken operations

## [v2.0.0-p1](https://github.com/NibiruChain/nibiru/releases/tag/v2.0.0-p1) - 2025-02-10

Expand Down
10 changes: 0 additions & 10 deletions x/evm/keeper/call_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,5 @@ func (k Keeper) CallContractWithInput(
err = fmt.Errorf("VMError: %s", evmResp.VmError)
return
}

// Success, update block gas used and bloom filter
if commit {
k.updateBlockBloom(ctx, evmResp, uint64(txConfig.LogIndex))

err = ctx.EventManager().EmitTypedEvent(&evm.EventTxLog{Logs: evmResp.Logs})
if err != nil {
return nil, errors.Wrap(err, "error emitting tx logs")
}
}
Comment on lines -85 to -94
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TxLogs is now emitted properly at the end of the execution, not on each CallContract separately

return evmResp, nil
}
8 changes: 7 additions & 1 deletion x/evm/keeper/funtoken_from_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (k *Keeper) deployERC20ForBankCoin(
k.Bank.StateDB = nil
}()
evmObj := k.NewEVM(ctx, evmMsg, evmCfg, nil /*tracer*/, stateDB)
_, err = k.CallContractWithInput(
evmResp, err := k.CallContractWithInput(
ctx, evmObj, evm.EVM_MODULE_ADDRESS, nil, true /*commit*/, input, Erc20GasLimitDeploy,
)
if err != nil {
Expand All @@ -114,5 +114,11 @@ func (k *Keeper) deployERC20ForBankCoin(
return gethcommon.Address{}, errors.Wrap(err, "failed to commit stateDB")
}

// Emit the logs from the EVM Contract deploy execution
err = ctx.EventManager().EmitTypedEvent(&evm.EventTxLog{Logs: evmResp.Logs})
if err == nil {
k.updateBlockBloom(ctx, evmResp, uint64(0))
}

Comment on lines +117 to +122
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emits OwnershipTransferred(address,address) log for the newly deployed contract.

return erc20Addr, nil
}
14 changes: 13 additions & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,12 @@ func (k Keeper) convertCoinToEvmBornCoin(
BankCoin: coin,
})

// Emit tx logs of Mint event
err = ctx.EventManager().EmitTypedEvent(&evm.EventTxLog{Logs: evmResp.Logs})
if err == nil {
k.updateBlockBloom(ctx, evmResp, uint64(k.EvmState.BlockTxIndex.GetOr(ctx, 0)))
}

return &evm.MsgConvertCoinToEvmResponse{}, nil
}

Expand Down Expand Up @@ -634,7 +640,7 @@ func (k Keeper) convertCoinToEvmBornERC20(
true,
)
evmObj := k.NewEVM(ctx, evmMsg, k.GetEVMConfig(ctx), nil /*tracer*/, stateDB)
_, _, err = k.ERC20().Transfer(
_, evmResp, err := k.ERC20().Transfer(
erc20Addr,
evm.EVM_MODULE_ADDRESS,
recipient,
Expand All @@ -660,6 +666,12 @@ func (k Keeper) convertCoinToEvmBornERC20(
BankCoin: coin,
})

// Emit tx logs of Transfer event
err = ctx.EventManager().EmitTypedEvent(&evm.EventTxLog{Logs: evmResp.Logs})
if err == nil {
k.updateBlockBloom(ctx, evmResp, uint64(k.EvmState.BlockTxIndex.GetOr(ctx, 0)))
}

return &evm.MsgConvertCoinToEvmResponse{}, nil
}

Expand Down
13 changes: 2 additions & 11 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ func (p precompileFunToken) sendToBank(
return nil, ErrInvalidArgs(err)
}

var evmResponses []*evm.MsgEthereumTxResponse

// ERC20 must have FunToken mapping
funtokens := p.evmKeeper.FunTokens.Collect(
ctx, p.evmKeeper.FunTokens.Indexes.ERC20Addr.ExactMatch(ctx, erc20),
Expand All @@ -170,7 +168,7 @@ func (p precompileFunToken) sendToBank(
}

// Caller transfers ERC20 to the EVM module account
gotAmount, transferResp, err := p.evmKeeper.ERC20().Transfer(
gotAmount, _, err := p.evmKeeper.ERC20().Transfer(
erc20, /*erc20*/
caller, /*from*/
evm.EVM_MODULE_ADDRESS, /*to*/
Expand All @@ -181,7 +179,6 @@ func (p precompileFunToken) sendToBank(
if err != nil {
return nil, fmt.Errorf("error in ERC20.transfer from caller to EVM account: %w", err)
}
evmResponses = append(evmResponses, transferResp)

// EVM account mints FunToken.BankDenom to module account
coinToSend := sdk.NewCoin(funtoken.BankDenom, math.NewIntFromBigInt(gotAmount))
Expand All @@ -190,11 +187,10 @@ func (p precompileFunToken) sendToBank(
// owns the ERC20 contract and was the original minter of the ERC20 tokens.
// Since we're sending them away and want accurate total supply tracking, the
// tokens need to be burned.
burnResp, err := p.evmKeeper.ERC20().Burn(erc20, evm.EVM_MODULE_ADDRESS, gotAmount, ctx, evmObj)
_, err := p.evmKeeper.ERC20().Burn(erc20, evm.EVM_MODULE_ADDRESS, gotAmount, ctx, evmObj)
if err != nil {
return nil, fmt.Errorf("ERC20.Burn: %w", err)
}
evmResponses = append(evmResponses, burnResp)
} else {
// NOTE: The NibiruBankKeeper needs to reference the current [vm.StateDB] before
// any operation that has the potential to use Bank send methods. This will
Expand Down Expand Up @@ -225,11 +221,6 @@ func (p precompileFunToken) sendToBank(
evm.ModuleName, evm.EVM_MODULE_ADDRESS.Hex(), caller.Hex(), err,
)
}
for _, resp := range evmResponses {
for _, log := range resp.Logs {
startResult.StateDB.AddLog(log.ToEthereum())
}
}
Comment on lines -228 to -232
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this produced duplicates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we added this in https://github.com/NibiruChain/nibiru/pull/2093/files#diff-199132a0cc4ad1df87325761675688436e1e9dc09d8b24908631937349e12f89 because we were missing logs, how did we end up with duplicates?


// TODO: UD-DEBUG: feat: Emit EVM events
// https://github.com/NibiruChain/nibiru/issues/2121
Expand Down
Loading