forked from cerc-io/laconicd-deprecated
rpc: transaction receipt test (#678)
* Problem: No test on the transaction receipt api Closes: #582 - add receipt rpc test for erc20 transfer logs * lower gas fee * build with go 1.17 in CI * use go 1.17 in test-solidity * fix merge
This commit is contained in:
@@ -63,7 +63,7 @@ func DoBenchmark(b *testing.B, txBuilder TxBuilder) {
|
||||
|
||||
func BenchmarkTokenTransfer(b *testing.B) {
|
||||
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
||||
input, err := ContractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
input, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
require.NoError(b, err)
|
||||
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
||||
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
||||
@@ -72,7 +72,7 @@ func BenchmarkTokenTransfer(b *testing.B) {
|
||||
|
||||
func BenchmarkEmitLogs(b *testing.B) {
|
||||
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
||||
input, err := ContractABI.Pack("benchmarkLogs", big.NewInt(1000))
|
||||
input, err := types.ERC20Contract.ABI.Pack("benchmarkLogs", big.NewInt(1000))
|
||||
require.NoError(b, err)
|
||||
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
||||
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 4100000, big.NewInt(1), nil, nil, input, nil)
|
||||
@@ -81,7 +81,7 @@ func BenchmarkEmitLogs(b *testing.B) {
|
||||
|
||||
func BenchmarkTokenTransferFrom(b *testing.B) {
|
||||
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
||||
input, err := ContractABI.Pack("transferFrom", suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(0))
|
||||
input, err := types.ERC20Contract.ABI.Pack("transferFrom", suite.address, common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(0))
|
||||
require.NoError(b, err)
|
||||
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
||||
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
||||
@@ -90,7 +90,7 @@ func BenchmarkTokenTransferFrom(b *testing.B) {
|
||||
|
||||
func BenchmarkTokenMint(b *testing.B) {
|
||||
DoBenchmark(b, func(suite *KeeperTestSuite, contract common.Address) *types.MsgEthereumTx {
|
||||
input, err := ContractABI.Pack("mint", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
input, err := types.ERC20Contract.ABI.Pack("mint", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
require.NoError(b, err)
|
||||
nonce := suite.app.EvmKeeper.GetNonce(suite.address)
|
||||
return types.NewTx(suite.app.EvmKeeper.ChainID(), nonce, &contract, big.NewInt(0), 410000, big.NewInt(1), nil, nil, input, nil)
|
||||
|
||||
@@ -514,9 +514,9 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
|
||||
}, false, 0, false},
|
||||
// estimate gas of an erc20 contract deployment, the exact gas number is checked with geth
|
||||
{"contract deployment", func() {
|
||||
ctorArgs, err := ContractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
suite.Require().NoError(err)
|
||||
data := append(ContractBin, ctorArgs...)
|
||||
data := append(types.ERC20Contract.Bin, ctorArgs...)
|
||||
args = types.TransactionArgs{
|
||||
From: &suite.address,
|
||||
Data: (*hexutil.Bytes)(&data),
|
||||
@@ -526,7 +526,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
|
||||
{"erc20 transfer", func() {
|
||||
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
suite.Commit()
|
||||
transferData, err := ContractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
suite.Require().NoError(err)
|
||||
args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)}
|
||||
}, true, 51880, false},
|
||||
@@ -549,9 +549,9 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
|
||||
gasCap = 20000
|
||||
}, false, 0, true},
|
||||
{"contract deployment w/ dynamicTxFee", func() {
|
||||
ctorArgs, err := ContractABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
ctorArgs, err := types.ERC20Contract.ABI.Pack("", &suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
suite.Require().NoError(err)
|
||||
data := append(ContractBin, ctorArgs...)
|
||||
data := append(types.ERC20Contract.Bin, ctorArgs...)
|
||||
args = types.TransactionArgs{
|
||||
From: &suite.address,
|
||||
Data: (*hexutil.Bytes)(&data),
|
||||
@@ -560,7 +560,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() {
|
||||
{"erc20 transfer w/ dynamicTxFee", func() {
|
||||
contractAddr := suite.DeployTestContract(suite.T(), suite.address, sdk.NewIntWithDecimal(1000, 18).BigInt())
|
||||
suite.Commit()
|
||||
transferData, err := ContractABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
transferData, err := types.ERC20Contract.ABI.Pack("transfer", common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec"), big.NewInt(1000))
|
||||
suite.Require().NoError(err)
|
||||
args = types.TransactionArgs{To: &contractAddr, From: &suite.address, Data: (*hexutil.Bytes)(&transferData)}
|
||||
}, true, 51880, true},
|
||||
|
||||
@@ -27,7 +27,6 @@ import (
|
||||
ethermint "github.com/tharsis/ethermint/types"
|
||||
"github.com/tharsis/ethermint/x/evm/types"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||
@@ -40,29 +39,6 @@ import (
|
||||
"github.com/tendermint/tendermint/version"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed ERC20Contract.json
|
||||
compiledContractJSON []byte
|
||||
ContractBin []byte
|
||||
ContractABI abi.ABI
|
||||
)
|
||||
|
||||
func init() {
|
||||
var tmp struct {
|
||||
Abi string
|
||||
Bin string
|
||||
}
|
||||
err := json.Unmarshal(compiledContractJSON, &tmp)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ContractBin = common.FromHex(tmp.Bin)
|
||||
err = json.Unmarshal([]byte(tmp.Abi), &ContractABI)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var testTokens = sdk.NewIntWithDecimal(1000, 18)
|
||||
|
||||
type KeeperTestSuite struct {
|
||||
@@ -193,10 +169,10 @@ func (suite *KeeperTestSuite) DeployTestContract(t require.TestingT, owner commo
|
||||
ctx := sdk.WrapSDKContext(suite.ctx)
|
||||
chainID := suite.app.EvmKeeper.ChainID()
|
||||
|
||||
ctorArgs, err := ContractABI.Pack("", owner, supply)
|
||||
ctorArgs, err := types.ERC20Contract.ABI.Pack("", owner, supply)
|
||||
require.NoError(t, err)
|
||||
|
||||
data := append(ContractBin, ctorArgs...)
|
||||
data := append(types.ERC20Contract.Bin, ctorArgs...)
|
||||
args, err := json.Marshal(&types.TransactionArgs{
|
||||
From: &suite.address,
|
||||
Data: (*hexutil.Bytes)(&data),
|
||||
@@ -250,7 +226,7 @@ func (suite *KeeperTestSuite) TransferERC20Token(t require.TestingT, contractAdd
|
||||
ctx := sdk.WrapSDKContext(suite.ctx)
|
||||
chainID := suite.app.EvmKeeper.ChainID()
|
||||
|
||||
transferData, err := ContractABI.Pack("transfer", to, amount)
|
||||
transferData, err := types.ERC20Contract.ABI.Pack("transfer", to, amount)
|
||||
require.NoError(t, err)
|
||||
args, err := json.Marshal(&types.TransactionArgs{To: &contractAddr, From: &from, Data: (*hexutil.Bytes)(&transferData)})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
// embed compiled smart contract
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
)
|
||||
|
||||
// HexString is a byte array that serializes to hex
|
||||
type HexString []byte
|
||||
|
||||
// MarshalJSON serializes ByteArray to hex
|
||||
func (s HexString) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(fmt.Sprintf("%x", string(s)))
|
||||
}
|
||||
|
||||
// UnmarshalJSON deserializes ByteArray to hex
|
||||
func (s *HexString) UnmarshalJSON(data []byte) error {
|
||||
var x string
|
||||
if err := json.Unmarshal(data, &x); err != nil {
|
||||
return err
|
||||
}
|
||||
str, err := hex.DecodeString(x)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*s = str
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompiledContract contains compiled bytecode and abi
|
||||
type CompiledContract struct {
|
||||
ABI abi.ABI
|
||||
Bin HexString
|
||||
}
|
||||
|
||||
type jsonCompiledContract struct {
|
||||
ABI string
|
||||
Bin HexString
|
||||
}
|
||||
|
||||
// MarshalJSON serializes ByteArray to hex
|
||||
func (s CompiledContract) MarshalJSON() ([]byte, error) {
|
||||
abi1, err := json.Marshal(s.ABI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(jsonCompiledContract{ABI: string(abi1), Bin: s.Bin})
|
||||
}
|
||||
|
||||
// UnmarshalJSON deserializes ByteArray to hex
|
||||
func (s *CompiledContract) UnmarshalJSON(data []byte) error {
|
||||
var x jsonCompiledContract
|
||||
if err := json.Unmarshal(data, &x); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Bin = x.Bin
|
||||
if err := json.Unmarshal([]byte(x.ABI), &s.ABI); err != nil {
|
||||
fmt.Println("unmarshal abi fail", x.ABI, string(data))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
//go:embed ERC20Contract.json
|
||||
erc20JSON []byte
|
||||
|
||||
// ERC20Contract is the compiled test erc20 contract
|
||||
ERC20Contract CompiledContract
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := json.Unmarshal(erc20JSON, &ERC20Contract)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if len(ERC20Contract.Bin) == 0 {
|
||||
panic("load contract failed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user