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

imp(erc20: Emit additional approval event in case of transferFrom #2088

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (erc20) [#2080](https://github.com/evmos/evmos/pull/2080) Add ERC20 integration test setup.
- (erc20) [#2085](https://github.com/evmos/evmos/pull/2085) Add ERC20 transfer integration tests.
- (erc20) [#2086](https://github.com/evmos/evmos/pull/2086) Add ERC20 metadata query integration tests.
- (erc20) [#2088](https://github.com/evmos/evmos/pull/2088) Emit additional approval event in case of `transferFrom`.

### Bug Fixes

Expand Down
7 changes: 7 additions & 0 deletions precompiles/erc20/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ var _ = Describe("ERC20 Extension -", func() {
_, _, err := is.factory.CallContractAndCheckLogs(owner.Priv, txArgs, approveArgs, approveCheck)
Expect(err).ToNot(HaveOccurred(), "unexpected result calling contract")

is.ExpectSendAuthzForContract(
callType, contractsData,
owner.Addr, owner.Addr, transferCoins,
)

// Transfer tokens
txArgs, transferArgs := is.getTxAndCallArgs(
callType, contractsData,
Expand Down Expand Up @@ -422,6 +427,8 @@ var _ = Describe("ERC20 Extension -", func() {
)

// Check that the allowance was removed since we authorized only the transferred amount
// FIXME: This is not working for the case where we transfer from the own account
// because the allowance is not removed on the SDK side.
is.ExpectNoSendAuthzForContract(
callType, contractsData,
owner.Addr, owner.Addr,
Comment on lines +430 to 434
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The case where we transferFrom the owner address will not be possible to fully support without adjusting our Cosmos SDK fork, since in Cosmos it's not supported to create authorizations for the same account. This is possible in ERC20s though.

However, the current alignment leads to the shown problem in this test - once set up through the EVM extension, the allowance is not reduced because in tx.go we use p.AuthzKeeper.DispatchActions(...). In that function, the case where granter == grantee is being skipped:

https://github.com/evmos/cosmos-sdk/blob/v0.47.5-evmos/x/authz/keeper/keeper.go#L97-L133

Expand Down
30 changes: 25 additions & 5 deletions precompiles/erc20/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package erc20
import (
"math/big"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -75,17 +77,24 @@ func (p Precompile) transfer(

msg := banktypes.NewMsgSend(from.Bytes(), to.Bytes(), coins)

if err := msg.ValidateBasic(); err != nil {
if err = msg.ValidateBasic(); err != nil {
return nil, err
}

sender := sdk.AccAddress(from.Bytes())
spender := sdk.AccAddress(contract.CallerAddress.Bytes()) // aka. grantee
isTransferFrom := method.Name == TransferFromMethod
spenderAddr := contract.CallerAddress
spender := sdk.AccAddress(spenderAddr.Bytes()) // aka. grantee

if sender.Equals(spender) {
var prevAllowance *big.Int
if !isTransferFrom {
msgSrv := bankkeeper.NewMsgServerImpl(p.bankKeeper)
_, err = msgSrv.Send(sdk.WrapSDKContext(ctx), msg)
} else {
_, _, prevAllowance, err = GetAuthzExpirationAndAllowance(p.AuthzKeeper, ctx, spenderAddr, from, p.tokenPair.Denom)
if err != nil {
return nil, ConvertErrToERC20Error(errorsmod.Wrapf(authz.ErrNoAuthorizationFound, err.Error()))
}

_, err = p.AuthzKeeper.DispatchActions(ctx, spender, []sdk.Msg{msg})
}

Expand All @@ -95,7 +104,18 @@ func (p Precompile) transfer(
return nil, err
}

if err := p.EmitTransferEvent(ctx, stateDB, from, to, amount); err != nil {
if err = p.EmitTransferEvent(ctx, stateDB, from, to, amount); err != nil {
return nil, err
}

// NOTE: if it's a direct transfer, we return here but if used through transferFrom,
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
// we need to emit the approval event with the new allowance.
MalteHerrmann marked this conversation as resolved.
Show resolved Hide resolved
if !isTransferFrom {
return method.Outputs.Pack(true)
}

newAllowance := new(big.Int).Sub(prevAllowance, amount)
if err = p.EmitApprovalEvent(ctx, stateDB, from, spenderAddr, newAllowance); err != nil {
return nil, err
}

Expand Down