-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into estimate_gas
- Loading branch information
Showing
7 changed files
with
193 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters