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,tests): add erc20 types tests #1994

Merged
merged 11 commits into from
Nov 3, 2023
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (distribution) [#1954](https://github.com/evmos/evmos/pull/1954) Add `ClaimRewards` unit and event tests.
- (osmosis-outpost) [#1944](https://github.com/evmos/evmos/pull/1944) Add more validation to Osmosis outpost.
- (precompiles) [#1973](https://github.com/evmos/evmos/pull/1973) Use `LoadABI` from precompiles common package in outposts.
- (erc20) [#1983](https://github.com/evmos/evmos/pull/1983) Add ERC-20 Precompile queries.
- (erc20) [#1984](https://github.com/evmos/evmos/pull/1984) Add tests for ERC20 precompile events.
- (ci) [#1985](https://github.com/evmos/evmos/pull/1985) Add CI action to check for a Changelog entry on PRs to `main`.
- (erc20) [#1983](https://github.com/evmos/evmos/pull/1983) Add ERC-20 Precompile queries.
- (erc20) [#1994](https://github.com/evmos/evmos/pull/1994) Add tests for ERC20 precompile type utilities.

### Bug Fixes

Expand Down
230 changes: 230 additions & 0 deletions precompiles/erc20/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package erc20_test

import (
"math/big"

"github.com/evmos/evmos/v15/precompiles/erc20"
utiltx "github.com/evmos/evmos/v15/testutil/tx"
)

func (s *PrecompileTestSuite) TestParseTransferArgs() {

Check failure on line 10 in precompiles/erc20/types_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

10-60 lines are duplicate of `precompiles/erc20/types_test.go:128-178` (dupl)
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
to := utiltx.GenerateAddress()
amount := big.NewInt(100)

testcases := []struct {
name string
args []interface{}
expPass bool
errContains string
}{
{
name: "pass - correct arguments",
args: []interface{}{
to,
amount,
},
expPass: true,
},
{
name: "fail - invalid to address",
args: []interface{}{
"invalid address",
amount,
},
errContains: "invalid to address",
},
{
name: "fail - invalid amount",
args: []interface{}{
to,
"invalid amount",
},
errContains: "invalid amount",
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
to, amount, err := erc20.ParseTransferArgs(tc.args)
if tc.expPass {
s.Require().NoError(err, "unexpected error parsing the transfer arguments")
s.Require().Equal(to, tc.args[0], "expected different to address")
s.Require().Equal(amount, tc.args[1], "expected different amount")
} else {
s.Require().Error(err, "expected an error parsing the transfer arguments")
s.Require().ErrorContains(err, tc.errContains, "expected different error message")
}
})
}
}

func (s *PrecompileTestSuite) TestParseTransferFromArgs() {
from := utiltx.GenerateAddress()
to := utiltx.GenerateAddress()
amount := big.NewInt(100)

testcases := []struct {
name string
args []interface{}
expPass bool
errContains string
}{
{
name: "pass - correct arguments",
args: []interface{}{
from,
to,
amount,
},
expPass: true,
},
{
name: "fail - invalid from address",
args: []interface{}{
"invalid address",
to,
amount,
},
errContains: "invalid from address",
},
{
name: "fail - invalid to address",
args: []interface{}{
from,
"invalid address",
amount,
},
errContains: "invalid to address",
},
{
name: "fail - invalid amount",
args: []interface{}{
from,
to,
"invalid amount",
},
errContains: "invalid amount",
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
from, to, amount, err := erc20.ParseTransferFromArgs(tc.args)
if tc.expPass {
s.Require().NoError(err, "unexpected error parsing the transferFrom arguments")
s.Require().Equal(from, tc.args[0], "expected different from address")
s.Require().Equal(to, tc.args[1], "expected different to address")
s.Require().Equal(amount, tc.args[2], "expected different amount")
} else {
s.Require().Error(err, "expected an error parsing the transferFrom arguments")
s.Require().ErrorContains(err, tc.errContains, "expected different error message")
}
})
}
}

func (s *PrecompileTestSuite) TestParseApproveArgs() {

Check failure on line 128 in precompiles/erc20/types_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

128-178 lines are duplicate of `precompiles/erc20/types_test.go:10-60` (dupl)
spender := utiltx.GenerateAddress()
amount := big.NewInt(100)

testcases := []struct {
name string
args []interface{}
expPass bool
errContains string
}{
{
name: "pass - correct arguments",
args: []interface{}{
spender,
amount,
},
expPass: true,
},
{
name: "fail - invalid spender address",
args: []interface{}{
"invalid address",
amount,
},
errContains: "invalid spender address",
},
{
name: "fail - invalid amount",
args: []interface{}{
spender,
"invalid amount",
},
errContains: "invalid amount",
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
spender, amount, err := erc20.ParseApproveArgs(tc.args)
if tc.expPass {
s.Require().NoError(err, "unexpected error parsing the approve arguments")
s.Require().Equal(spender, tc.args[0], "expected different spender address")
s.Require().Equal(amount, tc.args[1], "expected different amount")
} else {
s.Require().Error(err, "expected an error parsing the approve arguments")
s.Require().ErrorContains(err, tc.errContains, "expected different error message")
}
})
}
}

func (s *PrecompileTestSuite) TestParseAllowanceArgs() {
owner := utiltx.GenerateAddress()
spender := utiltx.GenerateAddress()

testcases := []struct {
name string
args []interface{}
expPass bool
errContains string
}{
{
name: "pass - correct arguments",
args: []interface{}{
owner,
spender,
},
expPass: true,
},
{
name: "fail - invalid owner address",
args: []interface{}{
"invalid address",
spender,
},
errContains: "invalid owner address",
},
{
name: "fail - invalid spender address",
args: []interface{}{
owner,
"invalid address",
},
errContains: "invalid spender address",
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
owner, spender, err := erc20.ParseAllowanceArgs(tc.args)
if tc.expPass {
s.Require().NoError(err, "unexpected error parsing the allowance arguments")
s.Require().Equal(owner, tc.args[0], "expected different owner address")
s.Require().Equal(spender, tc.args[1], "expected different spender address")
} else {
s.Require().Error(err, "expected an error parsing the allowance arguments")
s.Require().ErrorContains(err, tc.errContains, "expected different error message")
}
})
}
}