Skip to content

Commit 4c45959

Browse files
authored
Add Ethers.get_transaction_count/2 (#110)
1 parent c4018a6 commit 4c45959

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Enhancements
6+
7+
- Add `Ethers.get_transaction_count/2`
8+
39
## v0.4.2 (2024-04-04)
410

511
### Enhancements

lib/ethers.ex

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ defmodule Ethers do
7070

7171
@option_keys [:rpc_client, :rpc_opts, :signer, :signer_opts, :tx_type]
7272
@hex_decode_post_process [
73-
:current_gas_price,
7473
:current_block_number,
74+
:current_gas_price,
7575
:estimate_gas,
7676
:get_balance,
77+
:get_transaction_count,
7778
:max_priority_fee_per_gas
7879
]
7980
@rpc_actions_map %{
@@ -139,6 +140,29 @@ defmodule Ethers do
139140
end
140141
end
141142

143+
@doc """
144+
Returns the transaction count of an address.
145+
146+
## Parameters
147+
- account: Account which the transaction count is queried for.
148+
- overrides:
149+
- block: The block you want to query the transaction count in (defaults to latest).
150+
- rpc_client: The RPC module to use for this request (overrides default).
151+
- rpc_opts: Specific RPC options to specify for this request.
152+
"""
153+
@spec get_transaction_count(Types.t_address(), Keyword.t()) ::
154+
{:ok, non_neg_integer()} | {:error, term()}
155+
def get_transaction_count(account, overrides \\ []) do
156+
{opts, overrides} = Keyword.split(overrides, @option_keys)
157+
158+
{rpc_client, rpc_opts} = get_rpc_client(opts)
159+
160+
with {:ok, account, block} <- pre_process(account, overrides, :get_transaction_count, opts) do
161+
rpc_client.eth_get_transaction_count(account, block, rpc_opts)
162+
|> post_process(nil, :get_transaction_count)
163+
end
164+
end
165+
142166
@doc """
143167
Returns the native transaction (ETH) by transaction hash.
144168
@@ -571,7 +595,8 @@ defmodule Ethers do
571595
end
572596
end
573597

574-
defp pre_process(account, overrides, :get_balance = _action, _opts) do
598+
defp pre_process(account, overrides, action, _opts)
599+
when action in [:get_balance, :get_transaction_count] do
575600
block =
576601
case Keyword.get(overrides, :block, "latest") do
577602
number when is_integer(number) -> Utils.integer_to_hex(number)

lib/ethers/transaction.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ defmodule Ethers.Transaction do
229229
end
230230

231231
defp fill_action(:chain_id, _tx), do: :chain_id
232-
defp fill_action(:nonce, tx), do: {:get_transaction_count, [tx.from, "latest"]}
232+
defp fill_action(:nonce, tx), do: {:get_transaction_count, tx.from, block: "latest"}
233233
defp fill_action(:max_fee_per_gas, _tx), do: :gas_price
234234
defp fill_action(:max_priority_fee_per_gas, _tx), do: :max_priority_fee_per_gas
235235
defp fill_action(:gas_price, _tx), do: :gas_price
@@ -257,6 +257,10 @@ defmodule Ethers.Transaction do
257257
{:ok, {:max_priority_fee_per_gas, Utils.integer_to_hex(v_int)}}
258258
end
259259

260+
defp do_post_process(:nonce, {:ok, nonce}) when is_integer(nonce) do
261+
{:ok, {:nonce, Utils.integer_to_hex(nonce)}}
262+
end
263+
260264
defp do_post_process(key, {:ok, v_hex}) do
261265
{:ok, {key, v_hex}}
262266
end

test/ethers_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ defmodule EthersTest do
6767
end
6868
end
6969

70+
describe "get_transaction_count" do
71+
test "returns the correct transaction count" do
72+
assert {:ok, c} =
73+
Ethers.get_transaction_count("0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E")
74+
75+
assert is_integer(c)
76+
assert c >= 0
77+
78+
{:ok, _} =
79+
Ethers.send(%{
80+
from: "0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E",
81+
to: "0xaadA6BF26964aF9D7eEd9e03E53415D37aA96045",
82+
value: 1000
83+
})
84+
85+
assert {:ok, c + 1} ==
86+
Ethers.get_transaction_count("0xACa94ef8bD5ffEE41947b4585a84BdA5a3d3DA6E")
87+
end
88+
end
89+
7090
describe "get_transaction" do
7191
test "returns correct transaction by tx_hash" do
7292
{:ok, tx_hash} =

0 commit comments

Comments
 (0)