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

add Ethers.get_transaction #74

Merged
merged 4 commits into from
Jan 8, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### New features

- Add `Ethers.get_transaction/2` function to query native chain transaction by transaction hash.

## v0.2.1 (2024-01-04)

### New features
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,19 @@ MyERC20Token.transfer("0x[Recipient]", 1000)
All contributions are very welcome (as simple as fixing typos). Please feel free to open issues and
push Pull Requests. Just remember to be respectful to everyone!

To run the tests locally, you need to run [ganache](https://github.com/trufflesuite/ganache).
After installing ganache, just run the following in a new window the you can run the tests on
the same machine.
To run the tests locally, follow below steps:
- Install [ethereum](https://geth.ethereum.org/docs/getting-started/installing-geth) and [solc](https://docs.soliditylang.org/en/latest/installing-solidity.html). For example, on MacOS
```
brew install ethereum
npm install -g solc
```
- Run [ganache](https://github.com/trufflesuite/ganache).
After installing ganache, just run the following in a new window

```
> ganache --wallet.deterministic
```

Then you should be able to run tests through `mix test`.
## Acknowledgements

Ethers was possible to make thanks to the great contributors of the following libraries.
Expand Down
20 changes: 20 additions & 0 deletions lib/ethers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ defmodule Ethers do
end
end

@doc """
Returns the native transaction (ETH) by transaction hash.

## Parameters
- tx_hash: Transaction hash which the transaction is queried for.
- overrides:
- rpc_opts: Specific RPC options to specify for this request.
"""
@spec get_transaction(Types.t_hash(), Keyword.t()) ::
{:ok, map()} | {:error, term()}
def get_transaction(tx_hash, opts \\ []) when is_binary(tx_hash) do
{rpc_client, rpc_opts} = get_rpc_client(opts)

rpc_client.eth_get_transaction_by_hash(tx_hash, rpc_opts)
|> post_process(nil, :get_transaction)
end

@doc """
Deploys a contract to the blockchain.

Expand Down Expand Up @@ -625,6 +642,9 @@ defmodule Ethers do
defp post_process({:ok, _}, _tx_hash, :deployed_address),
do: {:error, :no_contract_address}

defp post_process({:ok, nil}, _tx_hash, :get_transaction),
do: {:error, :transaction_not_found}

defp post_process({:ok, result}, _tx_data, _action),
do: {:ok, result}

Expand Down
36 changes: 36 additions & 0 deletions test/ethers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule EthersTest do
alias Ethers.ExecutionError

@from "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1"
@to "0x95cED938F7991cd0dFcb48F0a06a40FA1aF46EBC"

describe "current_gas_price" do
test "returns the correct gas price" do
Expand Down Expand Up @@ -60,6 +61,41 @@ defmodule EthersTest do
end
end

describe "get_transaction" do
test "returns correct transaction by tx_hash" do
{:ok, tx_hash} =
HelloWorldContract.set_hello("hello local signer")
|> Ethers.send(
from: @from,
to: @to,
signer: Ethers.Signer.Local,
signer_opts: [
private_key: "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
]
)

downcased_to_addr = String.downcase(@to)

assert {:ok,
%{
"hash" => ^tx_hash,
"from" => @from,
"to" => ^downcased_to_addr
}} = Ethers.get_transaction(tx_hash)
end

test "returns error by non-existent tx_hash" do
assert {:error, :transaction_not_found} =
Ethers.get_transaction(
"0x5194596d703a53f65dcb1d7df60fcfa1f7d904ad3145887677a6ab68a425d8d3"
)
end

test "returns correct transaction by invalid tx_hash" do
assert {:error, _err} = Ethers.get_transaction("invalid tx_hash")
end
end

describe "contract deployment" do
test "can deploy a contract given a module which has the binary" do
assert {:ok, tx} = Ethers.deploy(HelloWorldContract, from: @from)
Expand Down
Loading