gleeth/nonce
Nonce manager for tracking and auto-incrementing transaction nonces.
Fetches the initial nonce from the network and increments locally on
each call to next, avoiding redundant RPC round-trips when sending
multiple transactions in sequence.
Types
Tracks the current nonce for an address.
Use next to get the next nonce and advance the counter.
pub type NonceManager {
NonceManager(
address: String,
current: option.Option(Int),
provider_url: String,
)
}
Constructors
-
NonceManager( address: String, current: option.Option(Int), provider_url: String, )
Values
pub fn new(
provider: provider.Provider,
address: String,
) -> Result(NonceManager, types.GleethError)
Create a new nonce manager for the given address. Fetches the current pending nonce from the network.
Examples
let assert Ok(p) = provider.new("http://localhost:8545")
let address = wallet.get_address(w)
let assert Ok(nm) = nonce.new(p, address)
pub fn next(
manager: NonceManager,
) -> Result(#(NonceManager, String), types.GleethError)
Get the next nonce as a hex string and return an updated manager.
The first call returns the nonce fetched during new.
Subsequent calls increment locally without an RPC call.
Examples
let assert Ok(nm) = nonce.new(provider, address)
// First call returns the nonce fetched from the network
let assert Ok(#(nm, nonce_hex)) = nonce.next(nm)
// nonce_hex is e.g. "0x5"
// Second call increments locally - no RPC round-trip
let assert Ok(#(nm, next_hex)) = nonce.next(nm)
// next_hex is "0x6"
pub fn reset(
manager: NonceManager,
provider: provider.Provider,
) -> Result(NonceManager, types.GleethError)
Reset the nonce manager by re-fetching the nonce from the network. Useful after a transaction fails or when the on-chain nonce may have changed due to activity from another client.
Examples
// After a failed transaction, resync with the network
let assert Ok(nm) = nonce.reset(nm, provider)
// Continue sending with the correct nonce
let assert Ok(#(nm, nonce_hex)) = nonce.next(nm)