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): grpc_query full coverage #1907

Merged
merged 11 commits into from
Jun 6, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1889](https://github.com/NibiruChain/nibiru/pull/1889) - feat: implemented basic evm tx methods
- [#1895](https://github.com/NibiruChain/nibiru/pull/1895) - refactor(geth): Reference go-ethereum as a submodule for easier change tracking with upstream
- [#1901](https://github.com/NibiruChain/nibiru/pull/1901) - test(evm): more e2e test contracts for edge cases
- [#1907](https://github.com/NibiruChain/nibiru/pull/1907) - test(evm): grpc_query full coverage
- [#1909](https://github.com/NibiruChain/nibiru/pull/1909) - chore(evm): set is_london true by default and removed from config

#### Dapp modules: perp, spot, oracle, etc
Expand Down
2 changes: 1 addition & 1 deletion x/evm/evmtest/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewEthAccInfo() EthPrivKeyAcc {
}

func EthAddrToNibiruAddr(ethAddr gethcommon.Address) sdk.AccAddress {
return sdk.AccAddress(ethAddr.Bytes())
return ethAddr.Bytes()
Unique-Divine marked this conversation as resolved.
Show resolved Hide resolved
}

type EthPrivKeyAcc struct {
Expand Down
101 changes: 95 additions & 6 deletions x/evm/evmtest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
package evmtest

import (
"encoding/json"
"fmt"
"math/big"
"testing"

gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
gethparams "github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"

srvconfig "github.com/NibiruChain/nibiru/app/server/config"

"github.com/NibiruChain/nibiru/x/evm"
)

type GethTxType = uint8

var (
GethTxType_LegacyTx GethTxType = gethcore.LegacyTxType
GethTxType_AccessListTx GethTxType = gethcore.AccessListTxType
GethTxType_DynamicFeeTx GethTxType = gethcore.DynamicFeeTxType
)

func NewEthTx(
deps *TestDeps, txData gethcore.TxData, nonce uint64,
) (ethCoreTx *gethcore.Transaction, err error) {
Expand Down Expand Up @@ -131,3 +132,91 @@
ethTxMsg.From = deps.Sender.EthAddr.Hex()
return ethTxMsg, ethTxMsg.Sign(deps.GethSigner(), deps.Sender.KeyringSigner)
}

// ExecuteNibiTransfer executes nibi transfer
func ExecuteNibiTransfer(deps *TestDeps, t *testing.T) *evm.MsgEthereumTx {
nonce := deps.StateDB().GetNonce(deps.Sender.EthAddr)
recipient := NewEthAccInfo().EthAddr

Check warning on line 139 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L137-L139

Added lines #L137 - L139 were not covered by tests

txArgs := evm.JsonTxArgs{
From: &deps.Sender.EthAddr,
To: &recipient,
Nonce: (*hexutil.Uint64)(&nonce),

Check warning on line 144 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L141-L144

Added lines #L141 - L144 were not covered by tests
}
ethTxMsg, err := GenerateAndSignEthTxMsg(txArgs, deps)
require.NoError(t, err)

Check warning on line 147 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L146-L147

Added lines #L146 - L147 were not covered by tests

resp, err := deps.Chain.EvmKeeper.EthereumTx(deps.GoCtx(), ethTxMsg)
require.NoError(t, err)
require.Empty(t, resp.VmError)
return ethTxMsg

Check warning on line 152 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L149-L152

Added lines #L149 - L152 were not covered by tests
}

// ExecuteERC20Transfer deploys contract, executes transfer and returns tx hash
func ExecuteERC20Transfer(deps *TestDeps, t *testing.T) (*evm.MsgEthereumTx, []*evm.MsgEthereumTx) {

Check warning on line 156 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L156

Added line #L156 was not covered by tests
// TX 1: Deploy ERC-20 contract
contractData := SmartContract_FunToken.Load(t)
nonce := deps.StateDB().GetNonce(deps.Sender.EthAddr)
txArgs := evm.JsonTxArgs{
From: &deps.Sender.EthAddr,
Nonce: (*hexutil.Uint64)(&nonce),
Data: (*hexutil.Bytes)(&contractData.Bytecode),

Check warning on line 163 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L158-L163

Added lines #L158 - L163 were not covered by tests
}
ethTxMsg, err := GenerateAndSignEthTxMsg(txArgs, deps)
require.NoError(t, err)

Check warning on line 166 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L165-L166

Added lines #L165 - L166 were not covered by tests

resp, err := deps.Chain.EvmKeeper.EthereumTx(deps.GoCtx(), ethTxMsg)
require.NoError(t, err)
require.Empty(t, resp.VmError)

Check warning on line 170 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L168-L170

Added lines #L168 - L170 were not covered by tests

// Contract address is deterministic
contractAddress := crypto.CreateAddress(deps.Sender.EthAddr, nonce)
deps.Chain.Commit()
predecessors := []*evm.MsgEthereumTx{
ethTxMsg,

Check warning on line 176 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L173-L176

Added lines #L173 - L176 were not covered by tests
}

// TX 2: execute ERC-20 contract transfer
input, err := contractData.ABI.Pack(
"transfer", NewEthAccInfo().EthAddr, new(big.Int).SetUint64(1000),
)
require.NoError(t, err)
nonce = deps.StateDB().GetNonce(deps.Sender.EthAddr)
txArgs = evm.JsonTxArgs{
From: &deps.Sender.EthAddr,
To: &contractAddress,
Nonce: (*hexutil.Uint64)(&nonce),
Data: (*hexutil.Bytes)(&input),

Check warning on line 189 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L180-L189

Added lines #L180 - L189 were not covered by tests
}
ethTxMsg, err = GenerateAndSignEthTxMsg(txArgs, deps)
require.NoError(t, err)

Check warning on line 192 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L191-L192

Added lines #L191 - L192 were not covered by tests

resp, err = deps.Chain.EvmKeeper.EthereumTx(deps.GoCtx(), ethTxMsg)
require.NoError(t, err)
require.Empty(t, resp.VmError)

Check warning on line 196 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L194-L196

Added lines #L194 - L196 were not covered by tests

return ethTxMsg, predecessors

Check warning on line 198 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L198

Added line #L198 was not covered by tests
}

// GenerateAndSignEthTxMsg estimates gas, sets gas limit and sings the tx
func GenerateAndSignEthTxMsg(txArgs evm.JsonTxArgs, deps *TestDeps) (*evm.MsgEthereumTx, error) {
estimateArgs, err := json.Marshal(&txArgs)
if err != nil {
return nil, err

Check warning on line 205 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L202-L205

Added lines #L202 - L205 were not covered by tests
}
res, err := deps.Chain.EvmKeeper.EstimateGas(deps.GoCtx(), &evm.EthCallRequest{
Args: estimateArgs,
GasCap: srvconfig.DefaultGasCap,
ProposerAddress: []byte{},
ChainId: deps.Chain.EvmKeeper.EthChainID(deps.Ctx).Int64(),
})
if err != nil {
return nil, err

Check warning on line 214 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L207-L214

Added lines #L207 - L214 were not covered by tests
}
txArgs.Gas = (*hexutil.Uint64)(&res.Gas)

Check warning on line 216 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L216

Added line #L216 was not covered by tests

txMsg := txArgs.ToTransaction()
gethSigner := deps.Sender.GethSigner(deps.Chain.EvmKeeper.EthChainID(deps.Ctx))
keyringSigner := deps.Sender.KeyringSigner
return txMsg, txMsg.Sign(gethSigner, keyringSigner)

Check warning on line 221 in x/evm/evmtest/tx.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/tx.go#L218-L221

Added lines #L218 - L221 were not covered by tests
}
Loading
Loading