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

test(evm): Add tests for bank wrapper #65

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf
github.com/ethereum/go-ethereum v1.11.5
github.com/evmos/os/example_chain v0.0.0-20240924163020-b2a4187dad50
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.4
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
Expand All @@ -48,6 +47,7 @@ require (
github.com/tidwall/sjson v1.2.5
github.com/tyler-smith/go-bip39 v1.1.0
github.com/zondax/hid v0.9.2
go.uber.org/mock v0.5.0
golang.org/x/crypto v0.29.0
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
golang.org/x/net v0.31.0
Expand Down Expand Up @@ -135,6 +135,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,8 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
Expand Down
2 changes: 1 addition & 1 deletion x/erc20/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
erc20mocks "github.com/evmos/os/x/erc20/types/mocks"
"github.com/evmos/os/x/evm/statedb"
evmtypes "github.com/evmos/os/x/evm/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/mock"
gomock "go.uber.org/mock/gomock"
)

func (suite *KeeperTestSuite) TestConvertERC20NativeERC20() {
Expand Down
2 changes: 1 addition & 1 deletion x/erc20/types/mocks/BankKeeper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions x/evm/wrappers/testutil/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package testutil

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/os/x/evm/types"
)

var _ types.BankKeeper = &MockBank{}

type MockBank struct {
Balances map[string]sdk.Coin
HasPermission bool
}

func NewMockBank() *MockBank {
return &MockBank{
HasPermission: true,
}
}

// SendCoinsFromModuleToAccount implements types.BankKeeper.
func (m *MockBank) SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {

Check failure on line 25 in x/evm/wrappers/testutil/mock.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
evmCoin := amt[0]

senderBalance := m.Balances[senderModule]
if senderBalance.Amount.LT(evmCoin.Amount) {
return fmt.Errorf("insufficient balance: %s < %s", senderBalance.Amount, evmCoin.Amount)
}

m.Balances[recipientAddr.String()] = m.Balances[recipientAddr.String()].Add(evmCoin)
m.Balances[senderModule] = m.Balances[senderModule].Sub(evmCoin)

return nil
}

// SendCoinsFromAccountToModule implements types.BankKeeper.
func (m *MockBank) SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {

Check failure on line 40 in x/evm/wrappers/testutil/mock.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
evmCoin := amt[0]

senderBalance := m.Balances[senderAddr.String()]
if senderBalance.Amount.LT(evmCoin.Amount) {
return fmt.Errorf("insufficient balance: %s < %s", senderBalance.Amount, evmCoin.Amount)
}

m.Balances[recipientModule] = m.Balances[recipientModule].Add(evmCoin)
m.Balances[senderAddr.String()] = m.Balances[senderAddr.String()].Sub(evmCoin)

return nil
}

// BurnCoins implements types.BankKeeper.
func (m *MockBank) BurnCoins(ctx context.Context, moduleName string, amt sdk.Coins) error {

Check failure on line 55 in x/evm/wrappers/testutil/mock.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
evmCoin := amt[0]

moduleBalance := m.Balances[moduleName]
if moduleBalance.Amount.LT(evmCoin.Amount) {
return fmt.Errorf("insufficient balance: %s < %s", moduleBalance.Amount, evmCoin.Amount)
}

m.Balances[moduleName] = m.Balances[moduleName].Sub(evmCoin)

return nil
}

// MintCoins implements types.BankKeeper.
func (m *MockBank) MintCoins(ctx context.Context, moduleName string, amt sdk.Coins) error {
if !m.HasPermission {
return fmt.Errorf("permission denied")
}

evmCoin := amt[0]

m.Balances[moduleName] = m.Balances[moduleName].Add(evmCoin)

return nil
}

// GetBalance implements types.BankKeeper.
func (m *MockBank) GetBalance(ctx context.Context, addr sdk.AccAddress, denom string) sdk.Coin {
return m.Balances[addr.String()]
}

// NOTE: Below methods are not implemented because are not used from the wrapper but are required to
// implement the interface.

// GetAllBalances implements types.BankKeeper.
func (m *MockBank) GetAllBalances(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
panic("unimplemented")
}

// IsSendEnabledCoins implements types.BankKeeper.
func (m *MockBank) IsSendEnabledCoins(ctx context.Context, coins ...sdk.Coin) error {
panic("unimplemented")
}

// SendCoins implements types.BankKeeper.
func (m *MockBank) SendCoins(ctx context.Context, from sdk.AccAddress, to sdk.AccAddress, amt sdk.Coins) error {
panic("unimplemented")
}
Loading