8000 tests(erc20): add erc20 query tests by MalteHerrmann · Pull Request #2023 · evmos/evmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tests(erc20): add erc20 query tests #2023

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

Merged
merged 28 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
877d281
add name query tests
Nov 8, 2023
949c7ac
extract getting the base denom from IBC voucher to own method
Nov 8, 2023
0fda882
Add check for empty denom in symbol query
Nov 9, 2023
3799780
add symbol tests
Nov 9, 2023
20c83eb
add decimals tests
Nov 9, 2023
faf6afe
return error for unexpected denom prefix in decimals query
Nov 9, 2023
b965ac0
fix iterating through metadata denom units
Nov 9, 2023
adc3919
fix decimals tests
Nov 9, 2023
6ad3152
add more decimals test cases
Nov 9, 2023
0bfac57
add changelog entry
Nov 9, 2023
1d811a1
Merge branch 'main' into malte/add-erc20-query-tests
MalteHerrmann Nov 9, 2023
163660a
fix tests after inflation v1 rename and slight adjustments
Nov 9, 2023
8780ccd
address linter
Nov 9, 2023
e0965a5
Merge branch 'main' into malte/add-erc20-query-tests
MalteHerrmann Nov 9, 2023
8f4767b
run make format
MalteHerrmann Nov 9, 2023
e686d1b
Merge branch 'main' into malte/add-erc20-query-tests
0xstepit Nov 9, 2023
b79bd0b
remove checks to empty denomination
Nov 9, 2023
37d476c
throw error if display denom not found in metadata
Nov 9, 2023
1c6c474
add total supply tests
Nov 9, 2023
d69ae15
add balance of tests
Nov 9, 2023
7a41acb
add allowance tests
Nov 9, 2023
8276634
remove TODO
Nov 9, 2023
4a9c569
Update precompiles/erc20/query_test.go
MalteHerrmann Nov 9, 2023
1d3cd50
refactor output checking for query tests
Nov 9, 2023
de543de
move requireOut to test utils
Nov 9, 2023
1b6bd3c
split name&symbol tests from decimals tests
Nov 9, 2023
9cf075b
fix doc comment
Nov 9, 2023
51a81b2
Merge branch 'main' into malte/add-erc20-query-tests
MalteHerrmann Nov 9, 2023
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 @@ -81,6 +81,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (utils) [#2010](https://github.com/evmos/evmos/pull/2010) Add utils function to create ibc denom trace.
- (erc20) [#2012](https://github.com/evmos/evmos/pull/2012) Adjust ERC20 extension approvals to handle multiple denominations.
- (osmosis-outpost) [#2017](https://github.com/evmos/evmos/pull/2017) Refactor types, errors and precompile struct.
- (erc20) [#2023](https://github.com/evmos/evmos/pull/2023) Add tests for ERC20 precompile queries.
- (osmosis-outpost) [#2025](https://github.com/evmos/evmos/pull/2025) Use a struct to wrap parsed parameters from Solidity.

### Bug Fixes
Expand Down
71 changes: 43 additions & 28 deletions precompiles/erc20/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
banktypes "github. 10000 com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/evmos/evmos/v15/precompiles/authorization"
auth "github.com/evmos/evmos/v15/precompiles/authorization"
transferkeeper "github.com/evmos/evmos/v15/x/ibc/transfer/keeper"

"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -43,7 +43,7 @@ const (
)

// Name returns the name of the token. If the token metadata is registered in the
// bank module, it returns its name. Otherwise it returns the base denomination of
// bank module, it returns its name. Otherwise, it returns the base denomination of
// the token capitalized (eg. uatom -> Atom).
func (p Precompile) Name(
ctx sdk.Context,
Expand All @@ -57,20 +57,12 @@ func (p Precompile) Name(
return method.Outputs.Pack(metadata.Name)
}

// Infer the denomination name from the coin denomination base denom
denomTrace, err := GetDenomTrace(p.transferKeeper, ctx, p.tokenPair.Denom)
baseDenom, err := p.getBaseDenomFromIBCVoucher(ctx, p.tokenPair.Denom)
if err != nil {
// FIXME: return 'not supported' (same error as when you call the method on an ERC20.sol)
return nil, err
}

// safety check
if len(denomTrace.BaseDenom) < 3 {
// FIXME: return not supported (same error as when you call the method on an ERC20.sol)
return nil, nil
}

name := strings.ToUpper(string(denomTrace.BaseDenom[1])) + denomTrace.BaseDenom[2:]
name := strings.ToUpper(string(baseDenom[1])) + baseDenom[2:]
return method.Outputs.Pack(name)
}

Expand All @@ -89,19 +81,12 @@ func (p Precompile) Symbol(
return method.Outputs.Pack(metadata.Symbol)
}

denomTrace, err := GetDenomTrace(p.transferKeeper, ctx, p.tokenPair.Denom)
baseDenom, err := p.getBaseDenomFromIBCVoucher(ctx, p.tokenPair.Denom)
if err != nil {
// FIXME: return not supported (same error as when you call the method on an ERC20.sol)
return nil, err
}

// safety check
if len(denomTrace.BaseDenom) < 3 {
// FIXME: return not supported (same error as when you call the method on an ERC20.sol)
return nil, nil
}

symbol := strings.ToUpper(denomTrace.BaseDenom[1:])
symbol := strings.ToUpper(baseDenom[1:])
return method.Outputs.Pack(symbol)
}

Expand Down Expand Up @@ -131,22 +116,34 @@ func (p Precompile) Decimals(
return method.Outputs.Pack(uint8(18))
}
// FIXME: return not supported (same error as when you call the method on an ERC20.sol)
return nil, nil
return nil, fmt.Errorf(
"invalid base denomination; should be either micro ('u[...]') or atto ('a[...]'); got: %q",
denomTrace.BaseDenom,
)
}

var decimals uint32
for i := len(metadata.DenomUnits); i >= 0; i-- {
var (
decimals uint32
displayFound bool
)
for i := len(metadata.DenomUnits) - 1; i >= 0; i-- {
if metadata.DenomUnits[i].Denom == metadata.Display {
decimals = metadata.DenomUnits[i].Exponent
displayFound = true
break
}
}

if !displayFound {
// FIXME: return not supported (same error as when E0C5 you call the method on an ERC20.sol)
return nil, fmt.Errorf("display denomination not found for denom: %q", p.tokenPair.Denom)
}

if decimals > math.MaxUint8 {
return nil, errors.New("uint8 overflow: invalid decimals")
return nil, fmt.Errorf("uint8 overflow: invalid decimals: %d", decimals)
}

return method.Outputs.Pack(uint8(decimals)) //#nosec G701
return method.Outputs.Pack(uint8(decimals)) //#nosec G701 // we are checking for overflow above
}

// TotalSupply returns the amount of tokens in existence. It fetches the supply
Expand Down Expand Up @@ -208,7 +205,7 @@ func (p Precompile) Allowance(
}

// GetDenomTrace returns the denomination trace from the corresponding IBC denomination. If the
// denomination is is not an IBC voucher or the trace is not found, it returns an error.
// denomination is not an IBC voucher or the trace is not found, it returns an error.
func GetDenomTrace(
transferKeeper transferkeeper.Keeper,
ctx sdk.Context,
Expand Down Expand Up @@ -239,7 +236,7 @@ func GetAuthzExpirationAndAllowance(
grantee, granter common.Address,
denom string,
) (authz.Authorization, *time.Time, *big.Int, error) {
authorization, expiration, err := authorization.CheckAuthzExists(ctx, authzKeeper, grantee, granter, SendMsgURL)
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
Expand All @@ -254,3 +251,21 @@ func GetAuthzExpirationAndAllowance(
allowance := sendAuth.SpendLimit.AmountOfNoDenomValidation(denom)
return authorization, expiration, allowance.BigInt(), nil
}

// getBaseDenomFromIBCVoucher returns the base denomination from the given IBC voucher denomination.
func (p Precompile) getBaseDenomFromIBCVoucher(ctx sdk.Context, denom string) (string, error) {
// Infer the denomination name from the coin denomination base denom
denomTrace, err := GetDenomTrace(p.transferKeeper, ctx, denom)
if err != nil {
// FIXME: return 'not supported' (same error as when you call the method on an ERC20.sol)
return "", err
}

// safety check
if len(denomTrace.BaseDenom) < 3 {
// FIXME: return not supported (same error as when you call the method on an ERC20.sol)
return "", fmt.Errorf("invalid base denomination; should be at least length 3; got: %q", denomTrace.BaseDenom)
}

return denomTrace.BaseDenom, nil
}
Loading
0