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(erc20): adjust ERC20 allowance behavior #2066

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -94,6 +94,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (make) [#2052](https://github.com/evmos/evmos/pull/2052) Fix Makefile targets to compile ERC20 contracts.
- (werc20) [#2057](https://github.com/evmos/evmos/pull/2057) WERC20 refactors and handling of fallback and receive functions.
- (werc20) [#2062](https://github.com/evmos/evmos/pull/2062) Remove name checking for `fallback` and `receive` functions.
- (erc20) [#2066](https://github.com/evmos/evmos/pull/2066) Adjust ERC20 EVM extension allowance behavior to align with standard ERC20 smart contracts.

### Bug Fixes

Expand Down
10 changes: 6 additions & 4 deletions precompiles/erc20/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ func (p Precompile) Allowance(

_, _, allowance, err := GetAuthzExpirationAndAllowance(p.AuthzKeeper, ctx, grantee, granter, p.tokenPair.Denom)
if err != nil {
return nil, err
// NOTE: We are not returning the error here, because we want to align the behavior with
// standard ERC20 smart contracts, which return zero if an allowance is not found.
allowance = common.Big0
}

return method.Outputs.Pack(allowance)
Expand Down Expand Up @@ -237,15 +239,15 @@ func GetAuthzExpirationAndAllowance(
denom string,
) (authz.Authorization, *time.Time, *big.Int, error) {
authorization, expiration, err := auth.CheckAuthzExists(ctx, authzKeeper, grantee, granter, SendMsgURL)
// TODO: return error if doesn't exist?
if err != nil {
return nil, nil, common.Big0, err
}

sendAuth, ok := authorization.(*banktypes.SendAuthorization)
if !ok {
// TODO: return error if invalid authorization?
return nil, nil, common.Big0, nil
return nil, nil, common.Big0, fmt.Errorf(
"expected authorization to be a %T", banktypes.SendAuthorization{},
)
}

allowance := sendAuth.SpendLimit.AmountOfNoDenomValidation(denom)
Expand Down
5 changes: 3 additions & 2 deletions precompiles/erc20/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,12 @@ func (s *PrecompileTestSuite) TestAllowance() {
errContains: "invalid spender address: invalid address",
},
{
name: "fail - no allowance exists",
name: "pass - no allowance exists should return 0",
malleate: func(_ sdk.Context, _ *app.Evmos, _ *big.Int) []interface{} {
return []interface{}{s.keyring.GetAddr(0), s.keyring.GetAddr(1)}
},
errContains: "does not exist or is expired",
expPass: true,
expAllow: common.Big0,
},
{
name: "pass - allowance exists but not for precompile token pair denom",
Expand Down