gleeth/crypto/transaction
Types
Access list entry for EIP-2930
pub type AccessListEntry {
AccessListEntry(address: String, storage_keys: List(String))
}
Constructors
-
AccessListEntry(address: String, storage_keys: List(String))
A decoded transaction, either legacy or EIP-1559.
pub type DecodedTransaction {
DecodedLegacy(SignedTransaction)
DecodedEip1559(SignedEip1559Transaction)
}
Constructors
-
DecodedLegacy(SignedTransaction) -
DecodedEip1559(SignedEip1559Transaction)
EIP-1559 transaction parameters (Type 2 transactions)
pub type Eip1559Transaction {
Eip1559Transaction(
to: String,
value: String,
gas_limit: String,
max_fee_per_gas: String,
max_priority_fee_per_gas: String,
nonce: String,
data: String,
chain_id: Int,
access_list: List(AccessListEntry),
)
}
Constructors
-
Eip1559Transaction( to: String, value: String, gas_limit: String, max_fee_per_gas: String, max_priority_fee_per_gas: String, nonce: String, data: String, chain_id: Int, access_list: List(AccessListEntry), )
Represents a signed EIP-1559 (Type 2) transaction
pub type SignedEip1559Transaction {
SignedEip1559Transaction(
to: String,
value: String,
gas_limit: String,
max_fee_per_gas: String,
max_priority_fee_per_gas: String,
nonce: String,
data: String,
chain_id: Int,
access_list: List(AccessListEntry),
v: String,
r: String,
s: String,
raw_transaction: String,
)
}
Constructors
-
SignedEip1559Transaction( to: String, value: String, gas_limit: String, max_fee_per_gas: String, max_priority_fee_per_gas: String, nonce: String, data: String, chain_id: Int, access_list: List(AccessListEntry), v: String, r: String, s: String, raw_transaction: String, )
Represents a signed Ethereum transaction
pub type SignedTransaction {
SignedTransaction(
to: String,
value: String,
gas_limit: String,
gas_price: String,
nonce: String,
data: String,
chain_id: Int,
v: String,
r: String,
s: String,
raw_transaction: String,
)
}
Constructors
-
SignedTransaction( to: String, value: String, gas_limit: String, gas_price: String, nonce: String, data: String, chain_id: Int, v: String, r: String, s: String, raw_transaction: String, )
Error types for transaction operations
pub type TransactionError {
InvalidAddress(String)
InvalidAmount(String)
InvalidGas(String)
InvalidNonce(String)
InvalidChainId(String)
InvalidData(String)
SigningFailed(String)
EncodingFailed(String)
}
Constructors
-
InvalidAddress(String) -
InvalidAmount(String) -
InvalidGas(String) -
InvalidNonce(String) -
InvalidChainId(String) -
InvalidData(String) -
SigningFailed(String) -
EncodingFailed(String)
Transaction type enumeration
pub type TransactionType {
Legacy
AccessList
Eip1559
}
Constructors
-
Legacy -
AccessList -
Eip1559
Represents an Ethereum transaction before signing
pub type UnsignedTransaction {
UnsignedTransaction(
to: String,
value: String,
gas_limit: String,
gas_price: String,
nonce: String,
data: String,
chain_id: Int,
)
}
Constructors
-
UnsignedTransaction( to: String, value: String, gas_limit: String, gas_price: String, nonce: String, data: String, chain_id: Int, )
Values
pub fn create_contract_call(
contract_address: String,
call_data: String,
gas_limit: String,
gas_price: String,
nonce: String,
chain_id: Int,
) -> Result(UnsignedTransaction, TransactionError)
Create a contract interaction transaction (zero value).
Convenience wrapper around create_legacy_transaction with value set to
"0x0". All numeric parameters are 0x-prefixed hex strings.
pub fn create_eip1559_transaction(
to: String,
value: String,
gas_limit: String,
max_fee_per_gas: String,
max_priority_fee_per_gas: String,
nonce: String,
data: String,
chain_id: Int,
access_list: List(AccessListEntry),
) -> Result(Eip1559Transaction, TransactionError)
Create and validate an EIP-1559 (Type 2) transaction.
All numeric parameters must be 0x-prefixed hex strings. Fee parameters
from methods.get_gas_price and methods.get_max_priority_fee can be
passed directly.
to- recipient addressvalue- amount in wei as hexgas_limit- gas limit as hexmax_fee_per_gas- maximum total fee per gas in wei as hexmax_priority_fee_per_gas- tip per gas in wei as hexnonce- sender’s transaction count as hexdata- calldata as hex ("0x"for simple transfers)chain_id- network chain ID as integeraccess_list- EIP-2930 access list (pass[]if not needed)
pub fn create_eth_transfer(
to: String,
value_wei: String,
gas_limit: String,
gas_price: String,
nonce: String,
chain_id: Int,
) -> Result(UnsignedTransaction, TransactionError)
Create a simple ETH transfer transaction (no calldata).
Convenience wrapper around create_legacy_transaction with data set to
"0x". All numeric parameters are 0x-prefixed hex strings.
pub fn create_legacy_transaction(
to: String,
value: String,
gas_limit: String,
gas_price: String,
nonce: String,
data: String,
chain_id: Int,
) -> Result(UnsignedTransaction, TransactionError)
Create a new unsigned legacy (Type 0) transaction.
All numeric parameters must be 0x-prefixed hex strings. The Ethereum
JSON-RPC API returns values in this format, so results from
methods.get_gas_price, methods.get_transaction_count, etc. can be
passed directly.
to- recipient address ("0x...", 40 hex chars after prefix)value- amount in wei as hex (e.g."0xde0b6b3a7640000"for 1 ETH)gas_limit- gas limit as hex (e.g."0x5208"for 21000)gas_price- gas price in wei as hexnonce- sender’s transaction count as hexdata- calldata as hex ("0x"for simple transfers)chain_id- network chain ID as integer (1 for mainnet, 11155111 for Sepolia)
Examples
let assert Ok(tx) = transaction.create_legacy_transaction(
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
"0xde0b6b3a7640000", // 1 ETH in wei
"0x5208", // 21000 gas
"0x3b9aca00", // 1 gwei gas price
"0x0", // nonce 0
"0x", // no calldata
1, // mainnet
)
pub fn decode(
raw_hex: String,
) -> Result(DecodedTransaction, TransactionError)
Decode a raw transaction hex string, auto-detecting the type. Legacy transactions start with an RLP list prefix (0xc0-0xff). EIP-1559 transactions start with 0x02.
pub fn decode_eip1559(
raw_hex: String,
) -> Result(SignedEip1559Transaction, TransactionError)
Decode a raw signed EIP-1559 (Type 2) transaction from its hex string. The input must start with 0x02 (the type prefix).
pub fn decode_legacy(
raw_hex: String,
) -> Result(SignedTransaction, TransactionError)
Decode a raw signed legacy transaction from its RLP-encoded hex string. Recovers chain_id from the EIP-155 v value: chain_id = (v - 35) / 2.
pub fn error_to_string(error: TransactionError) -> String
Convert TransactionError to string
pub fn get_eip1559_transaction_hash(
tx: SignedEip1559Transaction,
) -> String
Get transaction hash from a signed EIP-1559 transaction
pub fn get_transaction_hash(tx: SignedTransaction) -> String
Get transaction hash from a signed legacy transaction
pub fn hash_raw_transaction(raw_transaction: String) -> String
Hash a raw transaction hex string to get its transaction hash
pub fn sign_eip1559_transaction(
transaction: Eip1559Transaction,
wallet: wallet.Wallet,
) -> Result(SignedEip1559Transaction, TransactionError)
Sign an EIP-1559 (Type 2) transaction with a wallet. Produces a type-prefixed RLP-encoded raw transaction: 0x02 || RLP([…]).
pub fn sign_transaction(
transaction: UnsignedTransaction,
wallet: wallet.Wallet,
) -> Result(SignedTransaction, TransactionError)
Sign a legacy (EIP-155) transaction with a wallet. Produces an RLP-encoded raw transaction suitable for eth_sendRawTransaction.
pub fn signed_eip1559_transaction_to_string(
tx: SignedEip1559Transaction,
) -> String
Convert signed EIP-1559 transaction to string for display
pub fn signed_transaction_to_string(
tx: SignedTransaction,
) -> String
Convert signed transaction to string for display