gleeth/rpc/methods
Ethereum JSON-RPC method wrappers.
Each public function in this module corresponds to a standard Ethereum
JSON-RPC method (e.g. eth_blockNumber, eth_getBalance). Functions
accept a Provider as their first argument and return a typed Result
with GleethError on failure.
Values
pub fn call_contract(
provider: provider.Provider,
contract_address: String,
data: String,
) -> Result(String, types.GleethError)
Execute a read-only contract call by calling eth_call.
Sends a call object with the given contract address and ABI-encoded
calldata, evaluated at the "latest" block. Returns the hex-encoded
return data.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let calldata = "0x70a08231000000000000000000000000d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
let assert Ok(result) = call_contract(p, "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", calldata)
pub fn estimate_gas(
provider: provider.Provider,
from: String,
to: String,
value: String,
data: String,
) -> Result(String, types.GleethError)
Estimate the gas required for a transaction by calling eth_estimateGas.
Any parameter may be an empty string to omit it from the call object.
pub fn get_balance(
provider: provider.Provider,
address: String,
) -> Result(String, types.GleethError)
Get the balance of an address by calling eth_getBalance.
Queries at the "latest" block. Returns the balance in wei as a
hex-encoded string.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let assert Ok(balance) = get_balance(p, "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
pub fn get_block_number(
provider: provider.Provider,
) -> Result(String, types.GleethError)
Get the latest block number by calling eth_blockNumber.
Returns the block number as a hex-encoded string.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let assert Ok(block) = get_block_number(p)
pub fn get_chain_id(
provider: provider.Provider,
) -> Result(String, types.GleethError)
Get the chain ID of the connected network by calling eth_chainId.
Returns the chain ID as a hex-encoded string (e.g. "0x1" for mainnet).
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let assert Ok(chain_id) = get_chain_id(p)
pub fn get_code(
provider: provider.Provider,
address: String,
) -> Result(String, types.GleethError)
Get the deployed bytecode at an address by calling eth_getCode.
Returns "0x" for externally-owned accounts (EOAs).
pub fn get_fee_history(
provider: provider.Provider,
block_count: Int,
newest_block: String,
reward_percentiles: List(Float),
) -> Result(types.FeeHistory, types.GleethError)
Get fee history for recent blocks by calling eth_feeHistory.
block_count- number of blocks to return (as a decimal integer)newest_block- highest block ("latest","pending", or a hex block number); defaults to"latest"when emptyreward_percentiles- percentiles of effective priority fees to include (e.g.[25.0, 50.0, 75.0])
pub fn get_gas_price(
provider: provider.Provider,
) -> Result(String, types.GleethError)
Get the current gas price in wei by calling eth_gasPrice.
Primarily useful for legacy (pre-EIP-1559) transactions.
pub fn get_logs(
provider: provider.Provider,
from_block: String,
to_block: String,
address: String,
topics: List(String),
) -> Result(List(types.Log), types.GleethError)
Get event logs matching a filter by calling eth_getLogs.
from_block and to_block default to "latest" when empty. address
and topics are omitted from the filter when empty.
pub fn get_max_priority_fee(
provider: provider.Provider,
) -> Result(String, types.GleethError)
Get the suggested max priority fee per gas by calling
eth_maxPriorityFeePerGas.
Used when building EIP-1559 (Type 2) transactions.
pub fn get_storage_at(
provider: provider.Provider,
address: String,
slot: String,
block: String,
) -> Result(String, types.GleethError)
Get the storage value at a specific slot in a contract by calling
eth_getStorageAt.
If block is an empty string it defaults to "latest".
pub fn get_transaction(
provider: provider.Provider,
hash: String,
) -> Result(types.Transaction, types.GleethError)
Get a transaction by its hash by calling eth_getTransactionByHash.
Returns a fully decoded Transaction record.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let assert Ok(tx) = get_transaction(p, "0xabc123...")
pub fn get_transaction_count(
provider: provider.Provider,
address: String,
block: String,
) -> Result(String, types.GleethError)
Get the transaction count (nonce) for an address by calling
eth_getTransactionCount.
If block is an empty string it defaults to "pending", which gives the
next usable nonce.
pub fn get_transaction_receipt(
provider: provider.Provider,
transaction_hash: String,
) -> Result(types.TransactionReceipt, types.GleethError)
Get a transaction receipt by its hash by calling
eth_getTransactionReceipt.
Returns a fully decoded TransactionReceipt record including logs and
status.
pub fn parse_transaction_receipt(
body: String,
) -> Result(types.TransactionReceipt, types.GleethError)
Parse a transaction receipt from a raw JSON-RPC response body.
This is primarily intended for testing - it decodes the JSON string directly rather than making an RPC call.
pub fn send_raw_transaction(
provider: provider.Provider,
raw_tx: String,
) -> Result(String, types.GleethError)
Broadcast a signed raw transaction to the network by calling
eth_sendRawTransaction.
raw_tx should be the hex-encoded signed transaction (e.g. "0x02f873...").
Returns the transaction hash on success.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let assert Ok(tx_hash) = send_raw_transaction(p, "0x02f873...")