evm
Package evm provides EVM chain interaction and transaction utilities.
import "github.com/luxfi/sdk/evm"Package evm provides EVM chain interaction and transaction utilities.
Functions
CalculateEvmFeeInLux
func CalculateEvmFeeInLux(gasUsed uint64, gasPrice *big.Int) float64ConvertToNanoLux
func ConvertToNanoLux(balance *big.Int) *big.IntConvertToNanoLux converts a balance in Lux to NanoLux. It adds 0.5 to the balance before dividing by 1e9 to round it to the nearest whole number.
ExtractWarpMessageFromLogs
func ExtractWarpMessageFromLogs(
logs []*types.Log,
) (*warp.UnsignedMessage, error)ExtractWarpMessageFromLogs gets first unsigned warp message contained in [logs].
ExtractWarpMessageFromReceipt
func ExtractWarpMessageFromReceipt(
receipt *types.Receipt,
) (*warp.UnsignedMessage, error)ExtractWarpMessageFromReceipt gets first unsigned warp message contained in [receipt].
GetClientWithoutScheme
func GetClientWithoutScheme(rpcURL string) (ethclient.Client, string, error)GetClientWithoutScheme tries to connect an ethclient to a rpc url without scheme, by trying out different possible schemes: ws, wss, https, http
GetErrorFromTrace
func GetErrorFromTrace(
trace map[string]interface{},
functionSignatureToError map[string]error,
) (error, error)GetErrorFromTrace returns golang error associated with [trace] by using [functionSignatureToError] to map function signatures to evm function selectors in [trace], and then to golang errors. First returned error is the mapped error, second error is for errors obtained executing this function.
GetEventFromLogs
func GetEventFromLogs[T any](logs []*types.Log, parser func(log types.Log) (T, error)) (T, error)GetEventFromLogs returns the first log in 'logs' that is successfully parsed by 'parser'.
GetFunctionSelector
func GetFunctionSelector(functionSignature string) stringGetFunctionSelector returns evm function selector code for the given function signature. EVM maps function and error signatures into codes that are then used in traces.
GetTxTrace
func GetTxTrace(rpcURL string, txID string) (map[string]interface{}, error)GetTxTrace returns a trace for the given [txID] on [rpcURL]. Supports [repeatsOnFailure] failures.
GetWarpMessagesFromLogs
func GetWarpMessagesFromLogs(
logs []*types.Log,
) []*warp.UnsignedMessageGetWarpMessagesFromLogs gets all unsigned warp messages contained in [logs].
HasScheme
func HasScheme(rpcURL string) (bool, error)HexToAddress
func HexToAddress(s string) common.AddressHexToAddress converts hex string to common.Address (geth type)
PrivateKeyToAddress
func PrivateKeyToAddress(privateKey string) (crypto.Address, error)PrivateKeyToAddress returns the public address associated with [privateKey].
TransactionError
func TransactionError(tx *types.Transaction, err error, msg string, args ...interface{}) errorTransactionError transforms a tx operation error into an error that contains:
- the [err] itself
- the [tx] hash (or information on the tx not being submitted)
- another descriptive [msg], together with formatted [args]
TxDump
func TxDump(description string, tx *types.Transaction) (string, error)TxDump dumps a [tx] hexa description, for it to be separately issued using external tools.
Types
Client
Client wraps over ethclient for calls used by SDK. Features:
- finds out url scheme in case it is missing, to connect to ws/wss/http/https
- repeats to try to recover from failures, generating its own context for each call
- logs rpc url in case of failure
- receives addresses and private keys as strings
func GetClient(rpcURL string) (Client, error)func (client Client) BlockByNumber(n *big.Int) (*types.Block, error)func (client Client) BlockNumber() (uint64, error)func (client Client) CalculateTxParams(
address string,
) (*big.Int, *big.Int, uint64, error)func (client Client) ChainConfig() (*evmParams.ChainConfigWithUpgradesJSON, error)func (client Client) Close()func (client Client) ContractAlreadyDeployed(
contractAddress string,
) (bool, error)func (client Client) CreateDummyBlocks(
numBlocks int,
privKeyStr string,
) errorfunc (client Client) EstimateBaseFee() (*big.Int, error)func (client Client) EstimateGasLimit(
msg ethereum.CallMsg,
) (uint64, error)func (client Client) FilterLogs(query ethereum.FilterQuery) ([]types.Log, error)func (client Client) FundAddress(
sourceAddressPrivateKeyStr string,
targetAddressStr string,
amount *big.Int,
) (*types.Receipt, error)func (client Client) GetAddressBalance(
addressStr string,
) (*big.Int, error)func (client Client) GetChainID() (*big.Int, error)func (client Client) GetContractBytecode(
contractAddressStr string,
) ([]byte, error)func (client Client) GetPrivateKeyBalance(
privateKey string,
) (*big.Int, error)func (client Client) GetTxOptsWithSigner(
prefundedPrivateKeyStr string,
) (*bind.TransactOpts, error)func (client Client) IssueTx(
txStr string,
) errorfunc (client Client) NonceAt(
addressStr string,
) (uint64, error)func (client Client) SendTransaction(
tx *types.Transaction,
) errorfunc (client Client) SetupProposerVM(
privKey string,
) errorfunc (client Client) SuggestGasTipCap() (*big.Int, error)func (client Client) TransactWithWarpMessage(
from crypto.Address,
privateKeyStr string,
warpMessage *warp.Message,
contract crypto.Address,
callData []byte,
value *big.Int,
generateRawTxOnly bool,
) (*types.Transaction, error)func (client Client) TransactionReceipt(hash common.Hash) (*types.Receipt, error)func (client Client) WaitForEVMBootstrapped(timeout time.Duration) errorfunc (client Client) WaitForNewBlock(
prevBlockNumber uint64,
totalDuration time.Duration,
) errorfunc (client Client) WaitForTransaction(
tx *types.Transaction,
) (*types.Receipt, bool, error)RawClient
RawClient wraps over rpc.Client for calls used by SDK. Used to make evm calls not available in ethclient:
- debug trace call
- debug trace transaction Features:
- finds out url scheme in case it is missing, to connect to ws/wss/http/https
- repeats to try to recover from failures, generating its own context for each call
- logs rpc url in case of failure
func GetRawClient(rpcURL string) (RawClient, error)func (client RawClient) Close()func (client RawClient) DebugTraceCall(
data map[string]string,
) (map[string]interface{}, error)func (client RawClient) DebugTraceTransaction(
txID string,
) (map[string]interface{}, error)