Files
zeekayandHanzo Dev da299df77d the corporate name is Lux Industries Inc
Lux Partners Limited is not the entity behind this work. Every place the
old name stood — copyright lines, site footers, brand config, legal and
compliance text — now names the company that actually holds it.

lux.fund is the exception and is not renamed to match: that site belongs
to Lux Assets Management LLC, a different company, and it says so.

Two kinds of line were left for a person rather than rewritten. A dated
news item recording that Lux Partners Limited was established in the Isle
of Man is a historical fact, and renaming it would falsify the record. And
a sentence asserting that the company is domiciled in the Isle of Man is a
claim about a jurisdiction, not a name — an Inc or an LLC is not an Isle
of Man company, so swapping the name there would assert something untrue.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-28 13:05:15 -07:00

101 lines
3.1 KiB
Go

// Copyright (C) 2025, Lux Industries Inc All rights reserved.
// See the file LICENSE for licensing terms.
package evm
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/luxfi/constants"
"github.com/luxfi/crypto"
"github.com/luxfi/geth/core/types"
)
// GetEventFromLogs returns the first log in 'logs' that is successfully parsed by 'parser'.
func GetEventFromLogs[T any](logs []*types.Log, parser func(log types.Log) (T, error)) (T, error) {
cumErrMsg := ""
for i, log := range logs {
event, err := parser(*log)
if err == nil {
return event, nil
}
if cumErrMsg != "" {
cumErrMsg += "; "
}
cumErrMsg += fmt.Sprintf("log %d -> %s", i, err.Error())
}
return *new(T), fmt.Errorf("failed to find %T event in receipt logs: [%s]", *new(T), cumErrMsg)
}
// TransactionError 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]
func TransactionError(tx *types.Transaction, err error, msg string, args ...interface{}) error {
msgSuffix := ": %w"
if tx != nil {
msgSuffix += fmt.Sprintf(" (txHash=%s)", tx.Hash().String())
} else {
msgSuffix += " (tx failed to be submitted)"
}
args = append(args, err)
return fmt.Errorf(msg+msgSuffix, args...)
}
// TxDump dumps a [tx] hexa description, for it to be separately issued using external tools.
func TxDump(description string, tx *types.Transaction) (string, error) {
if tx == nil {
return "", fmt.Errorf("can't dump nil tx")
}
bs, err := tx.MarshalBinary()
if err != nil {
return "", fmt.Errorf("failure marshalling raw evm tx: %w", err)
}
txDump := ""
txDump += fmt.Sprintf("Tx Dump For %s:\n", description)
txDump += fmt.Sprintf("0x%s\n", hex.EncodeToString(bs))
txDump += "Calldata Dump:\n"
txDump += fmt.Sprintf("0x%s\n", hex.EncodeToString(tx.Data()))
if len(tx.AccessList()) > 0 {
txDump += "Access List Dump:\n"
for _, t := range tx.AccessList() {
txDump += fmt.Sprintf(" Address: %s\n", t.Address)
for _, s := range t.StorageKeys {
txDump += fmt.Sprintf(" Storage: %s\n", s)
}
}
}
return txDump, nil
}
// PrivateKeyToAddress returns the public address associated with [privateKey].
func PrivateKeyToAddress(privateKey string) (crypto.Address, error) {
pk, err := crypto.HexToECDSA(privateKey)
if err != nil {
return crypto.Address{}, err
}
return crypto.PubkeyToAddress(pk.PublicKey), nil
}
// ConvertToNanoLux 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.
func ConvertToNanoLux(balance *big.Int) *big.Int {
divisor := big.NewInt(int64(constants.Lux))
half := new(big.Int).Div(divisor, big.NewInt(2))
adjusted := new(big.Int).Add(balance, half)
return new(big.Int).Div(adjusted, divisor)
}
func CalculateEvmFeeInLux(gasUsed uint64, gasPrice *big.Int) float64 {
gasUsedBig := new(big.Int).SetUint64(gasUsed)
totalCost := new(big.Int).Mul(gasUsedBig, gasPrice)
totalCostInNanoLux := ConvertToNanoLux(totalCost)
result, _ := new(big.Float).SetInt(totalCostInNanoLux).Float64()
return result / float64(constants.Lux)
}