evmtest

package
v2.0.0-rc.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 33 Imported by: 0

Documentation

Overview

Copyright (c) 2023-2024 Nibi, Inc.

Copyright (c) 2023-2024 Nibi, Inc.

Index

Constants

View Source
const (
	// FunTokenGasLimitSendToEvm consists of gas for 3 calls:
	// 1. transfer erc20 from sender to module
	//    ~60_000 gas for regular erc20 transfer (our own ERC20Minter contract)
	//    could be higher for user created contracts, let's cap with 200_000
	// 2. mint native coin (made from erc20) or burn erc20 token (made from coin)
	//	  ~60_000 gas for either mint or burn
	// 3. send from module to account:
	//	  ~65_000 gas (bank send)
	FunTokenGasLimitSendToEvm uint64 = 400_000
)

Variables

View Source
var DefaultEthCallGasLimit = srvconfig.DefaultEthCallGasLimit
View Source
var NextNoOpAnteHandler sdk.AnteHandler = func(
	ctx sdk.Context, tx sdk.Tx, simulate bool,
) (newCtx sdk.Context, err error) {
	return ctx, nil
}

Functions

func AssertBankBalanceEqual

func AssertBankBalanceEqual(
	t *testing.T,
	deps TestDeps,
	denom string,
	account gethcommon.Address,
	expectedBalance *big.Int,
)

func AssertBankBalanceEqualWithDescription

func AssertBankBalanceEqualWithDescription(
	t *testing.T,
	deps TestDeps,
	denom string,
	account gethcommon.Address,
	expectedBalance *big.Int,
	description string,
)

func AssertERC20BalanceEqual

func AssertERC20BalanceEqual(
	t *testing.T,
	deps TestDeps,
	erc20, account gethcommon.Address,
	expectedBalance *big.Int,
)

func AssertERC20BalanceEqualWithDescription

func AssertERC20BalanceEqualWithDescription(
	t *testing.T,
	deps TestDeps,
	erc20, account gethcommon.Address,
	expectedBalance *big.Int,
	description string,
)

func BigPow

func BigPow(amount *big.Int, pow10Exp uint8) (powAmount *big.Int)

BigPow multiplies "amount" by 10 to the "pow10Exp".

func CallContractTx

func CallContractTx(
	deps *TestDeps,
	contractAddr gethcommon.Address,
	input []byte,
	sender EthPrivKeyAcc,
) (ethTxMsg *evm.MsgEthereumTx, resp *evm.MsgEthereumTxResponse, err error)

func CreateContractMsgEthereumTx

func CreateContractMsgEthereumTx(
	args ArgsCreateContract,
) (msgEthereumTx *evm.MsgEthereumTx, err error)

func CreateFunTokenForBankCoin

func CreateFunTokenForBankCoin(
	deps *TestDeps, bankDenom string, s *suite.Suite,
) (funtoken evm.FunToken)

CreateFunTokenForBankCoin: Uses the "TestDeps.Sender" account to create a "FunToken" mapping for a new coin

func DeployAndExecuteERC20Transfer

func DeployAndExecuteERC20Transfer(
	deps *TestDeps, t *testing.T,
) (
	erc20Transfer *evm.MsgEthereumTx,
	predecessors []*evm.MsgEthereumTx,
	contractAddr gethcommon.Address,
)

DeployAndExecuteERC20Transfer deploys contract, executes transfer and returns tx hash

func ExecuteContractMsgEthereumTx

func ExecuteContractMsgEthereumTx(args ArgsExecuteContract) (msgEthereumTx *evm.MsgEthereumTx, err error)

func ExecuteNibiTransfer

func ExecuteNibiTransfer(deps *TestDeps, t *testing.T) *evm.MsgEthereumTx

ExecuteNibiTransfer executes nibi transfer

func GasLimitCreateContract

func GasLimitCreateContract() *big.Int

func GenerateEthTxMsgAndSigner

func GenerateEthTxMsgAndSigner(
	jsonTxArgs evm.JsonTxArgs, deps *TestDeps, sender EthPrivKeyAcc,
) (evmTxMsg *evm.MsgEthereumTx, gethSigner gethcore.Signer, krSigner keyring.Signer, err error)

GenerateEthTxMsgAndSigner estimates gas, sets gas limit and returns signer for the tx.

Usage:

```go
evmTxMsg, gethSigner, krSigner, _ := GenerateEthTxMsgAndSigner(
    jsonTxArgs, &deps, sender,
)
err := evmTxMsg.Sign(gethSigner, sender.KeyringSigner)
```

func HappyCreateContractTx

func HappyCreateContractTx(deps *TestDeps) *evm.MsgEthereumTx

func HappyTransferTx

func HappyTransferTx(deps *TestDeps, nonce uint64) *evm.MsgEthereumTx

func NewEthTxMsgAsCmt

func NewEthTxMsgAsCmt(t *testing.T) (
	txBz cmt.Tx,
	ethTxMsgs []*evm.MsgEthereumTx,
	clientCtx client.Context,
)

NewEthTxMsgAsCmt: Helper that returns an Ethereum tx msg converted into tx bytes used in the Consensus Engine.

func NewEthTxMsgFromTxData

func NewEthTxMsgFromTxData(
	deps *TestDeps,
	txType GethTxType,
	innerTxData []byte,
	nonce uint64,
	to *gethcommon.Address,
	value *big.Int,
	gas uint64,
	accessList gethcore.AccessList,
) (*evm.MsgEthereumTx, error)

NewEthTxMsgFromTxData creates an Ethereum transaction message based on the specified txType (Legacy, AccessList, or DynamicFee). This function populates transaction fields like nonce, recipient, value, and gas, with an optional access list for AccessList and DynamicFee types. The transaction is signed using the provided dependencies.

Parameters:

  • deps: Required dependencies including the sender address and signer.
  • txType: Transaction type (Legacy, AccessList, or DynamicFee).
  • innerTxData: Byte slice of transaction data (input).
  • nonce: Transaction nonce.
  • to: Recipient address.
  • value: ETH value (in wei) to transfer.
  • gas: Gas limit for the transaction.
  • accessList: Access list for AccessList and DynamicFee types.

Returns:

  • *evm.MsgEthereumTx: Ethereum transaction message ready for submission.
  • error: Any error encountered during creation or signing.

func NewEthTxMsgs

func NewEthTxMsgs(count uint64) (ethTxMsgs []*evm.MsgEthereumTx)

func NewSigner

func NewSigner(sk cryptotypes.PrivKey) keyring.Signer

func NonEvmMsgTx

func NonEvmMsgTx(deps *TestDeps) sdk.Tx

func TransferWei

func TransferWei(
	deps *TestDeps,
	to gethcommon.Address,
	amountWei *big.Int,
) error

func TxTemplateAccessListTx

func TxTemplateAccessListTx() *gethcore.AccessListTx

func TxTemplateDynamicFeeTx

func TxTemplateDynamicFeeTx() *gethcore.DynamicFeeTx

func TxTemplateLegacyTx

func TxTemplateLegacyTx() *gethcore.LegacyTx

func ValidLegacyTx

func ValidLegacyTx() *evm.LegacyTx

ValidLegacyTx: Useful initial condition for tests Exported only for use in tests.

Types

type ArgsCreateContract

type ArgsCreateContract struct {
	EthAcc        EthPrivKeyAcc
	EthChainIDInt *big.Int
	GasPrice      *big.Int
	Nonce         uint64
	GasLimit      *big.Int
}

ArgsCreateContract: Arguments to call with `CreateContractTxMsg` to make Ethereum transactions that create contracts.

It is recommended to use a gas price of `big.NewInt(1)` for simpler op code calculations in gas units.

type ArgsExecuteContract

type ArgsExecuteContract struct {
	EthAcc          EthPrivKeyAcc
	EthChainIDInt   *big.Int
	ContractAddress *gethcommon.Address
	Data            []byte
	GasPrice        *big.Int
	Nonce           uint64
	GasLimit        *big.Int
}

ArgsExecuteContract: Arguments to call with `ExecuteContractTxMsg` to make Ethereum transactions that execute contracts.

type DeployContractResult

type DeployContractResult struct {
	TxResp       *evm.MsgEthereumTxResponse
	EthTxMsg     *evm.MsgEthereumTx
	ContractData embeds.CompiledEvmContract
	Nonce        uint64
	ContractAddr gethcommon.Address
}

func DeployContract

func DeployContract(
	deps *TestDeps,
	contract embeds.CompiledEvmContract,
	args ...any,
) (result *DeployContractResult, err error)

type EthPrivKeyAcc

type EthPrivKeyAcc struct {
	EthAddr       gethcommon.Address
	NibiruAddr    sdk.AccAddress
	PrivKey       *ethsecp256k1.PrivKey
	KeyringSigner keyring.Signer
}

func NewEthPrivAcc

func NewEthPrivAcc() EthPrivKeyAcc

NewEthPrivAcc returns an Ethereum private key, its corresponding Eth address, Nibiru address, and keyring signer.

func NewEthPrivAccs

func NewEthPrivAccs(n int) []EthPrivKeyAcc

NewEthPrivAccs calls [NewEthAccInfo] n times.

type FunTokenBalanceAssert

type FunTokenBalanceAssert struct {
	FunToken     evm.FunToken
	Account      gethcommon.Address
	BalanceBank  *big.Int
	BalanceERC20 *big.Int
	Description  string
}

func (FunTokenBalanceAssert) Assert

func (bals FunTokenBalanceAssert) Assert(t *testing.T, deps TestDeps)

type GethTxType

type GethTxType = uint8

GethTxType represents different Ethereum transaction types as defined in go-ethereum, such as Legacy, AccessList, and DynamicFee transactions.

type Signer

type Signer struct {
	// contains filtered or unexported fields
}

Signer defines a type that is used on testing for signing MsgEthereumTx

func (Signer) Sign

func (s Signer) Sign(_ string, msg []byte) ([]byte, cryptotypes.PubKey, error)

Sign signs the message using the underlying private key

func (Signer) SignByAddress

func (s Signer) SignByAddress(address sdk.Address, msg []byte) ([]byte, cryptotypes.PubKey, error)

SignByAddress sign byte messages with a user key providing the address.

type TestDeps

type TestDeps struct {
	App       *app.NibiruApp
	Ctx       sdk.Context
	EncCfg    codec.EncodingConfig
	EvmKeeper *keeper.Keeper
	GenState  *evm.GenesisState
	Sender    EthPrivKeyAcc
}

func NewTestDeps

func NewTestDeps() TestDeps

func (*TestDeps) GethSigner

func (deps *TestDeps) GethSigner() gethcore.Signer

func (TestDeps) GoCtx

func (deps TestDeps) GoCtx() context.Context

func (TestDeps) NewStateDB

func (deps TestDeps) NewStateDB() *statedb.StateDB

func (TestDeps) ResetGasMeter

func (deps TestDeps) ResetGasMeter()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL