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)
A builder for constructing EIP-1559 (Type 2) transactions step by step.
pub type Eip1559Builder {
Eip1559Builder(
to: option.Option(String),
value: option.Option(String),
gas_limit: option.Option(String),
max_fee_per_gas: option.Option(String),
max_priority_fee_per_gas: option.Option(String),
nonce: option.Option(String),
data: option.Option(String),
chain_id: option.Option(Int),
access_list: List(AccessListEntry),
)
}
Constructors
-
Eip1559Builder( to: option.Option(String), value: option.Option(String), gas_limit: option.Option(String), max_fee_per_gas: option.Option(String), max_priority_fee_per_gas: option.Option(String), nonce: option.Option(String), data: option.Option(String), chain_id: option.Option(Int), access_list: List(AccessListEntry), )
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), )
A builder for constructing legacy (Type 0) transactions step by step.
pub type LegacyBuilder {
LegacyBuilder(
to: option.Option(String),
value: option.Option(String),
gas_limit: option.Option(String),
gas_price: option.Option(String),
nonce: option.Option(String),
data: option.Option(String),
chain_id: option.Option(Int),
)
}
Constructors
-
LegacyBuilder( to: option.Option(String), value: option.Option(String), gas_limit: option.Option(String), gas_price: option.Option(String), nonce: option.Option(String), data: option.Option(String), chain_id: option.Option(Int), )
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 build_eip1559() -> Eip1559Builder
Create a new empty EIP-1559 transaction builder.
pub fn build_legacy() -> LegacyBuilder
Create a new empty legacy transaction builder.
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 eip1559_access_list(
builder: Eip1559Builder,
entries: List(AccessListEntry),
) -> Eip1559Builder
Set the access list on an EIP-1559 builder.
pub fn eip1559_chain(
builder: Eip1559Builder,
id: Int,
) -> Eip1559Builder
Set the chain ID on an EIP-1559 builder.
pub fn eip1559_data_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the calldata as a raw hex string on an EIP-1559 builder.
pub fn eip1559_gas_limit_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the gas limit as a raw hex string on an EIP-1559 builder.
pub fn eip1559_gas_limit_int(
builder: Eip1559Builder,
limit: Int,
) -> Eip1559Builder
Set the gas limit from an integer on an EIP-1559 builder.
pub fn eip1559_max_fee_gwei(
builder: Eip1559Builder,
gwei_str: String,
) -> Eip1559Builder
Set the max fee per gas from a gwei amount on an EIP-1559 builder.
pub fn eip1559_max_fee_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the max fee per gas as a raw hex string on an EIP-1559 builder.
pub fn eip1559_max_priority_fee_gwei(
builder: Eip1559Builder,
gwei_str: String,
) -> Eip1559Builder
Set the max priority fee per gas from a gwei amount on an EIP-1559 builder.
pub fn eip1559_max_priority_fee_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the max priority fee per gas as a raw hex string on an EIP-1559 builder.
pub fn eip1559_nonce_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the nonce as a raw hex string on an EIP-1559 builder.
pub fn eip1559_nonce_int(
builder: Eip1559Builder,
n: Int,
) -> Eip1559Builder
Set the nonce from an integer on an EIP-1559 builder.
pub fn eip1559_to(
builder: Eip1559Builder,
address: String,
) -> Eip1559Builder
Set the recipient address on an EIP-1559 builder.
pub fn eip1559_value_ether(
builder: Eip1559Builder,
ether_str: String,
) -> Eip1559Builder
Set the value from an ether amount string on an EIP-1559 builder.
pub fn eip1559_value_hex(
builder: Eip1559Builder,
hex_val: String,
) -> Eip1559Builder
Set the value as a raw hex string on an EIP-1559 builder.
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 legacy_chain(
builder: LegacyBuilder,
id: Int,
) -> LegacyBuilder
Set the chain ID on a legacy builder.
pub fn legacy_data_hex(
builder: LegacyBuilder,
hex_val: String,
) -> LegacyBuilder
Set the calldata as a raw hex string on a legacy builder.
pub fn legacy_gas_limit_hex(
builder: LegacyBuilder,
hex_val: String,
) -> LegacyBuilder
Set the gas limit as a raw hex string on a legacy builder.
pub fn legacy_gas_limit_int(
builder: LegacyBuilder,
limit: Int,
) -> LegacyBuilder
Set the gas limit from an integer on a legacy builder.
pub fn legacy_gas_price_gwei(
builder: LegacyBuilder,
gwei_str: String,
) -> LegacyBuilder
Set the gas price from a gwei amount string on a legacy builder.
pub fn legacy_gas_price_hex(
builder: LegacyBuilder,
hex_val: String,
) -> LegacyBuilder
Set the gas price as a raw hex string on a legacy builder.
pub fn legacy_nonce_hex(
builder: LegacyBuilder,
hex_val: String,
) -> LegacyBuilder
Set the nonce as a raw hex string on a legacy builder.
pub fn legacy_nonce_int(
builder: LegacyBuilder,
n: Int,
) -> LegacyBuilder
Set the nonce from an integer on a legacy builder.
pub fn legacy_to(
builder: LegacyBuilder,
address: String,
) -> LegacyBuilder
Set the recipient address on a legacy builder.
pub fn legacy_value_ether(
builder: LegacyBuilder,
ether_str: String,
) -> LegacyBuilder
Set the value from an ether amount string on a legacy builder. Converts ether to wei hex using the wei module.
pub fn legacy_value_hex(
builder: LegacyBuilder,
hex_val: String,
) -> LegacyBuilder
Set the value as a raw hex string on a legacy builder.
pub fn recover_sender(
signed: SignedTransaction,
) -> Result(String, TransactionError)
Recover the sender address from a signed legacy transaction. Reconstructs the EIP-155 signing hash and uses ECDSA recovery.
pub fn recover_sender_eip1559(
signed: SignedEip1559Transaction,
) -> Result(String, TransactionError)
Recover the sender address from a signed EIP-1559 transaction. For EIP-1559, v is directly the recovery_id (0 or 1).
pub fn sign_eip1559(
builder: Eip1559Builder,
w: wallet.Wallet,
) -> Result(SignedEip1559Transaction, TransactionError)
Validate and sign an EIP-1559 builder, producing a SignedEip1559Transaction. Returns an error if required fields are missing.
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_legacy(
builder: LegacyBuilder,
w: wallet.Wallet,
) -> Result(SignedTransaction, TransactionError)
Validate and sign a legacy builder, producing a SignedTransaction. Returns an error if required fields (to, value, gas_limit, gas_price, nonce, chain_id) are missing.
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