Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds functions to chain client. #8

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/tt_eth/behaviours/chain_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule TTEth.Behaviours.ChainClient do
@type filter_params :: map
@type filter_id :: String.t()
@type tx_obj :: map()
@type error :: {:error, map() | binary() | atom()}

@callback eth_call(contract :: address, encoded_args) :: any
@callback eth_call(contract :: address, encoded_args, opts) :: any
Expand All @@ -26,8 +27,13 @@ defmodule TTEth.Behaviours.ChainClient do
@callback build_tx_data(address, abi_data, wallet, nonce) :: tx_data
@callback build_tx_data(address, abi_data, wallet, nonce, keyword) :: tx_data

@callback eth_get_transaction_count(account :: address, block_id) :: any
@callback eth_get_transaction_count(account :: address, block_id, opts) :: any
@callback eth_get_balance(account :: address, block_id) :: {:ok, binary()} | error
@callback eth_get_balance(account :: address, block_id, opts) :: {:ok, binary()} | error

@callback eth_get_transaction_count(account :: address, block_id) ::
{:ok, binary()} | error
@callback eth_get_transaction_count(account :: address, block_id, opts) ::
{:ok, binary()} | error

@callback eth_get_logs(filter_params) :: {:ok, any} | {:error, any}
@callback eth_get_logs(filter_params, opts) :: {:ok, any} | {:error, any}
Expand All @@ -47,6 +53,23 @@ defmodule TTEth.Behaviours.ChainClient do
@callback eth_estimate_gas(tx_obj) :: any
@callback eth_estimate_gas(tx_obj, opts) :: any

@callback eth_fee_history(
block_count :: integer(),
newest_block :: binary() | integer(),
reward_percentiles :: list(non_neg_integer())
) ::
{:ok, map()} | error
@callback eth_fee_history(
block_count :: integer(),
newest_block :: binary() | integer(),
reward_percentiles :: list(non_neg_integer()),
opts
) ::
{:ok, map()} | error

@callback eth_get_block_by_number(block_id) :: any
@callback eth_get_block_by_number(block_id, boolean) :: any

@callback eth_get_code(address, block :: binary(), opts) :: {:ok, any} | {:error, any}
@callback eth_get_code(address, block :: binary()) :: {:ok, any} | {:error, any}
end
12 changes: 12 additions & 0 deletions lib/tt_eth/chain_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ defmodule TTEth.ChainClient do
|> Base.encode16(case: :lower)
|> hex_prefix!()

@impl ChainClient
def eth_get_balance(address, block \\ "latest", opts \\ []),
do: address |> HttpClient.eth_get_balance(block, opts)

@impl ChainClient
def eth_get_transaction_count(address, block \\ "latest", opts \\ []),
do: address |> HttpClient.eth_get_transaction_count(block, opts)
Expand Down Expand Up @@ -62,10 +66,18 @@ defmodule TTEth.ChainClient do
def eth_estimate_gas(%{} = tx_obj, opts \\ []),
do: tx_obj |> HttpClient.eth_estimate_gas(opts)

@impl ChainClient
def eth_fee_history(block_count, newest_block, reward_percentiles, opts \\ []),
do: HttpClient.eth_fee_history(block_count, newest_block, reward_percentiles, opts)

@impl ChainClient
def eth_get_block_by_number("" <> block, tx_detail \\ false),
do: block |> HttpClient.eth_get_block_by_number(tx_detail)

@impl ChainClient
def eth_get_code("" <> address, block \\ "latest", opts \\ []),
do: address |> HttpClient.eth_get_code(block, opts)

## Helpers outside of the ChainClient behaviour.

def eth_get_transaction_receipt("" <> tx_hash, opts \\ []),
Expand Down
41 changes: 41 additions & 0 deletions lib/tt_eth/chain_client_mock_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule TTEth.ChainClientMockImpl do
def build_tx_data(to, abi_data, %Wallet{} = wallet, nonce, opts \\ []),
do: to |> TTEth.ChainClient.build_tx_data(abi_data, wallet, nonce, opts)

@impl ChainClient
def eth_get_balance(_address, _block \\ "latest", _opts \\ []),
do: {:ok, "0x7"}

@impl ChainClient
def eth_get_transaction_count(_address, _block \\ "latest", _opts \\ []),
do: {:ok, "0x42"}
Expand Down Expand Up @@ -47,7 +51,44 @@ defmodule TTEth.ChainClientMockImpl do
def eth_estimate_gas(%{} = _tx_obj, _opts \\ []),
do: {:ok, "0x5208"}

@impl ChainClient
def eth_fee_history(_block_count, _newest_block, _reward_percentiles, _opts \\ []),
do:
{:ok,
%{
"oldestBlock" => "0x54",
"reward" => [
[
"0x174876e800",
"0x174876e800"
],
[
"0x174876e800",
"0x174876e800"
],
[
"0x174876e800",
"0x174876e800"
]
],
"baseFeePerGas" => [
"0x0",
"0x0",
"0x0",
"0x0"
],
"gasUsedRatio" => [
0.0010253063265735019,
0.006479788956353575,
0.00006763590977418037
]
}}

@impl ChainClient
def eth_get_block_by_number(_block, _tx_detail \\ false),
do: {:ok, %{"number" => "0x1", "baseFeePerGas" => "0x10"}}

@impl ChainClient
def eth_get_code(_address, _block \\ "latest", _opts \\ []),
do: {:ok, "0x60806040523661000b57005b600080357fffffffff"}
end
Loading