Skip to content

Commit

Permalink
Merge branch 'main' into estimate_gas
Browse files Browse the repository at this point in the history
  • Loading branch information
GAtom22 committed Nov 2, 2023
2 parents b6256b7 + b826687 commit be75ba3
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ 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) [#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`.

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion precompiles/common/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func PackNum(value reflect.Value) []byte {
func LoadABI(fs embed.FS, path string) (abi.ABI, error) {
abiBz, err := fs.ReadFile(path)
if err != nil {
return abi.ABI{}, fmt.Errorf("error loading the staking ABI %s", err)
return abi.ABI{}, fmt.Errorf("error loading the ABI %s", err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
Expand Down
2 changes: 1 addition & 1 deletion precompiles/erc20/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

// abiPath defines the path to the ERC-20 precompile ABI JSON file.
const abiPath = "./abi.json"
const abiPath = "abi.json"

// Embed abi json file to the executable binary. Needed when importing as dependency.
//
Expand Down
107 changes: 107 additions & 0 deletions precompiles/erc20/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package erc20_test

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/evmos/evmos/v15/precompiles/authorization"
cmn "github.com/evmos/evmos/v15/precompiles/common"
erc20precompile "github.com/evmos/evmos/v15/precompiles/erc20"
utiltx "github.com/evmos/evmos/v15/testutil/tx"
)

//nolint:dupl // this is not a duplicate of the approval events test
func (s *PrecompileTestSuite) TestEmitTransferEvent() {
testcases := []struct {
name string
from common.Address
to common.Address
amount *big.Int
}{
{
name: "pass",
from: utiltx.GenerateAddress(),
to: utiltx.GenerateAddress(),
amount: big.NewInt(100),
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
s.SetupTest()
stateDB := s.network.GetStateDB()

err := s.precompile.EmitTransferEvent(
s.network.GetContext(), stateDB, tc.from, tc.to, tc.amount,
)
s.Require().NoError(err, "expected transfer event to be emitted successfully")

log := stateDB.Logs()[0]
s.Require().Equal(log.Address, s.precompile.Address())

// Check event signature matches the one emitted
event := s.precompile.ABI.Events[erc20precompile.EventTypeTransfer]
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.network.GetContext().BlockHeight()))

// Check the fully unpacked event matches the one emitted
var transferEvent erc20precompile.EventTransfer
err = cmn.UnpackLog(s.precompile.ABI, &transferEvent, erc20precompile.EventTypeTransfer, *log)
s.Require().NoError(err, "unable to unpack log into transfer event")

s.Require().Equal(tc.from, transferEvent.From, "expected different from address")
s.Require().Equal(tc.to, transferEvent.To, "expected different to address")
s.Require().Equal(tc.amount, transferEvent.Value, "expected different amount")
})
}
}

//nolint:dupl // this is not a duplicate of the transfer events test
func (s *PrecompileTestSuite) TestEmitApprovalEvent() {
testcases := []struct {
name string
owner common.Address
spender common.Address
amount *big.Int
}{
{
name: "pass",
owner: utiltx.GenerateAddress(),
spender: utiltx.GenerateAddress(),
amount: big.NewInt(100),
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
s.SetupTest()

stateDB := s.network.GetStateDB()

err := s.precompile.EmitApprovalEvent(
s.network.GetContext(), stateDB, tc.owner, tc.spender, tc.amount,
)
s.Require().NoError(err, "expected approval event to be emitted successfully")

log := stateDB.Logs()[0]
s.Require().Equal(log.Address, s.precompile.Address())

// Check event signature matches the one emitted
event := s.precompile.ABI.Events[authorization.EventTypeApproval]
s.Require().Equal(crypto.Keccak256Hash([]byte(event.Sig)), common.HexToHash(log.Topics[0].Hex()))
s.Require().Equal(log.BlockNumber, uint64(s.network.GetContext().BlockHeight()))

// Check the fully unpacked event matches the one emitted
var approvalEvent erc20precompile.EventApproval
err = cmn.UnpackLog(s.precompile.ABI, &approvalEvent, authorization.EventTypeApproval, *log)
s.Require().NoError(err, "unable to unpack log into approval event")

s.Require().Equal(tc.owner, approvalEvent.Owner, "expected different owner address")
s.Require().Equal(tc.spender, approvalEvent.Spender, "expected different spender address")
s.Require().Equal(tc.amount, approvalEvent.Value, "expected different amount")
})
}
}
60 changes: 60 additions & 0 deletions precompiles/erc20/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package erc20_test

import (
"testing"

erc20precompile "github.com/evmos/evmos/v15/precompiles/erc20"
"github.com/evmos/evmos/v15/testutil/integration/evmos/factory"
"github.com/evmos/evmos/v15/testutil/integration/evmos/grpc"
testkeyring "github.com/evmos/evmos/v15/testutil/integration/evmos/keyring"
"github.com/evmos/evmos/v15/testutil/integration/evmos/network"
utiltx "github.com/evmos/evmos/v15/testutil/tx"
erc20types "github.com/evmos/evmos/v15/x/erc20/types"
"github.com/stretchr/testify/suite"
)

var s *PrecompileTestSuite

// PrecompileTestSuite is the implementation of the TestSuite interface for ERC20 precompile
// unit tests.
type PrecompileTestSuite struct {
suite.Suite

network *network.UnitTestNetwork
factory factory.TxFactory
grpcHandler grpc.Handler
keyring testkeyring.Keyring

precompile *erc20precompile.Precompile
}

func TestPrecompileTestSuite(t *testing.T) {
s = new(PrecompileTestSuite)
suite.Run(t, s)
}

func (s *PrecompileTestSuite) SetupTest() {
keyring := testkeyring.New(1)
integrationNetwork := network.NewUnitTestNetwork(
network.WithPreFundedAccounts(keyring.GetAllAccAddrs()...),
)
grpcHandler := grpc.NewIntegrationHandler(integrationNetwork)
txFactory := factory.New(integrationNetwork, grpcHandler)

// Create dummy token pair to instantiate the precompile
tokenPair := erc20types.NewTokenPair(utiltx.GenerateAddress(), "xmpl", erc20types.OWNER_MODULE)

precompile, err := erc20precompile.NewPrecompile(
tokenPair,
integrationNetwork.App.BankKeeper,
integrationNetwork.App.AuthzKeeper,
integrationNetwork.App.TransferKeeper,
)
s.Require().NoError(err, "failed to create erc20 precompile")

s.factory = txFactory
s.grpcHandler = grpcHandler
s.keyring = keyring
s.precompile = precompile
s.network = integrationNetwork
}
14 changes: 14 additions & 0 deletions precompiles/erc20/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ func ParseAllowanceArgs(args []interface{}) (

return owner, spender, nil
}

// EventTransfer defines the event data for the ERC20 Transfer events.
type EventTransfer struct {
From common.Address
To common.Address
Value *big.Int
}

// EventApproval defines the event data for the ERC20 Approval events.
type EventApproval struct {
Owner common.Address
Spender common.Address
Value *big.Int
}
14 changes: 9 additions & 5 deletions precompiles/outposts/osmosis/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
cmn "github.com/evmos/evmos/v15/precompiles/common"
"github.com/evmos/evmos/v15/precompiles/outposts/osmosis"
evmosutiltx "github.com/evmos/evmos/v15/testutil/tx"
"github.com/evmos/evmos/v15/x/evm/statedb"
)

func (s *PrecompileTestSuite) TestSwapEvent() {
Expand All @@ -27,16 +28,17 @@ func (s *PrecompileTestSuite) TestSwapEvent() {
output common.Address
amount *big.Int
receiver string
postCheck func(input common.Address, output common.Address, amount *big.Int, receiver string)
postCheck func(input common.Address, output common.Address, amount *big.Int, receiver string, stateDB *statedb.StateDB)
}{
{
"pass - correct event emitted",
evmosAddress,
osmoAddress,
big.NewInt(transferAmount),
receiver,
func(input common.Address, output common.Address, amount *big.Int, receiver string) {
stateDB := s.network.GetStateDB()
func(input common.Address, output common.Address, amount *big.Int, receiver string, stateDB *statedb.StateDB) {
s.Require().Len(stateDB.Logs(), 1, "expected one log in the stateDB")

swapLog := stateDB.Logs()[0]
s.Require().Equal(
swapLog.Address,
Expand Down Expand Up @@ -93,17 +95,19 @@ func (s *PrecompileTestSuite) TestSwapEvent() {
err := s.network.NextBlock()
s.Require().NoError(err)

stateDB := s.network.GetStateDB()

err = s.precompile.EmitSwapEvent(
s.network.GetContext(),
s.network.GetStateDB(),
stateDB,
sender,
tc.input,
tc.output,
tc.amount,
tc.receiver,
)
s.Require().NoError(err)
tc.postCheck(tc.input, tc.output, tc.amount, tc.receiver)
tc.postCheck(tc.input, tc.output, tc.amount, tc.receiver, stateDB)
})
}
}

0 comments on commit be75ba3

Please sign in to comment.