Skip to content
8 changes: 4 additions & 4 deletions eth_client/lib/eth_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ defmodule EthClient do
Logger.info("#{chain_name} is not a supported chain.")
end

defp nonce(address) do
def nonce(address) do
{nonce, ""} =
Rpc.get_transaction_count(address)
|> remove_leading_0x()
Expand All @@ -173,7 +173,7 @@ defmodule EthClient do
nonce
end

defp gas_limit(data, caller_address, recipient_address \\ nil) do
def gas_limit(data, caller_address, recipient_address \\ nil) do
{gas, ""} =
%{
from: caller_address,
Expand All @@ -187,7 +187,7 @@ defmodule EthClient do
gas
end

defp gas_price do
def gas_price do
{gas_price, ""} =
Rpc.gas_price()
|> remove_leading_0x()
Expand All @@ -201,7 +201,7 @@ defmodule EthClient do

defp wei_to_ether(amount), do: amount / 1.0e19

defp build_raw_tx(amount, nonce, gas_limit, gas_price, opts) do
def build_raw_tx(amount, nonce, gas_limit, gas_price, opts) do
recipient = opts[:recipient]
data = opts[:data]
chain_id = Context.chain_id()
Expand Down
99 changes: 99 additions & 0 deletions eth_client/test/eth_rpc_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
defmodule EthClientTest.Rpc do
use ExUnit.Case
alias EthClient
alias EthClient.Context
alias EthClient.Rpc

@bin_path "../contracts/src/bin/Storage.bin"

defp add_0x(data), do: "0x" <> data

def transaction_deploy do
:ok = EthClient.set_chain("local")
{:ok, data} = File.read(@bin_path)
data = add_0x(data)

caller = Context.user_account()
caller_address = String.downcase(caller.address)
contract_address = Context.contract().address

nonce = EthClient.nonce(caller.address)
gas_limit = EthClient.gas_limit(data, caller_address)

raw_tx =
EthClient.build_raw_tx(
0,
nonce,
gas_limit,
EthClient.gas_price(),
data: data
)

tx_hash = EthClient.sign_transaction(raw_tx, caller.private_key)

%{
data: data,
caller: caller,
caller_address: caller_address,
contract_address: contract_address,
nonce: nonce,
gas_limit: gas_limit,
raw_tx: raw_tx,
tx_hash: tx_hash
}
end

setup_all do
transaction_deploy()
Copy link
Contributor

@mmsc2 mmsc2 Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this can be achieved using "EthClient.deploy(bin_path)", as all the variables should have been setted up in env variables, there should not be a need to set them by hand. You can also use 'EthClient,Context.all()" to get all the context configurations. We can discuss this if it is needed.

end

describe "Rpc Module" do
test "[SUCCESS] Send raw transaction", state do
assert {:ok, tx_hash} = Rpc.send_raw_transaction(state[:tx_hash])
end

test "[SUCCESS] Estimate Gas", state do
transaction_map = %{data: state[:data], from: state[:caller_address], to: nil}
assert {:ok, gas} = Rpc.estimate_gas(transaction_map)
end

test "[SUCCESS] Get Transaction Count", state do
assert {:ok, count} = Rpc.get_transaction_count(state[:caller_address])
end

test "[SUCCESS] Gas Price" do
assert {:ok, price} = Rpc.gas_price()
end

test "[SUCCESS] Get Transaction by Hash" do
assert {:ok, %{}} =
Rpc.get_transaction_by_hash(
"0xf6bebadd44e6d5e1446f6456ae4c4fcb8309631747714199e505aa4cec1c2019"
)
end

test "[SUCCESS] Get Transaction Receipt" do
assert {:ok, %{}} =
Rpc.get_transaction_receipt(
"0xf6bebadd44e6d5e1446f6456ae4c4fcb8309631747714199e505aa4cec1c2019"
)
end

test "[SUCCESS] Get Code", state do
assert {:ok, "0x"} = Rpc.get_code(state[:caller_address])
end

test "[SUCCESS] Get Call", state do
call_map = %{}
assert {:ok, "0x"} = Rpc.call(call_map)
end

test "[SUCCESS] Get Logs" do
assert {:ok, []} = Rpc.get_logs(%{})
end

test "[SUCCESS] Get Balance", state do
assert {:ok, balance} = Rpc.get_balance(state[:caller_address])
end
end
end