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

tests rpc v1 #78

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
8 changes: 4 additions & 4 deletions eth_client/lib/eth_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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 @@ -166,7 +166,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 @@ -180,7 +180,7 @@ defmodule EthClient do
gas
end

defp gas_price do
def gas_price do
{gas_price, ""} =
Rpc.gas_price()
|> remove_leading_0x()
Expand All @@ -194,7 +194,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
2 changes: 1 addition & 1 deletion eth_client/test/eth_contract_opcodes_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule EthClientTest.Contract.Opcodes do
use ExUnit.Case
doctest EthClient
# doctest EthClient
alias EthClient.Contract.Opcodes
import ExUnit.CaptureIO

Expand Down
123 changes: 123 additions & 0 deletions eth_client/test/eth_rpc_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
defmodule EthClientTest.Rpc do
use ExUnit.Case
alias EthClient
alias EthClient.Context
alias EthClient.Rpc

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

def transaction_deploy do
:ok = EthClient.set_chain("rinkeby")
Context.set_rpc_host("https://rinkeby.infura.io/v3/56725ef0a57448cdb2c5dc36c277460b")
{:ok, data} = File.read(@bin_path)
data = "0x" <> data

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

%{
data: data,
caller_address: caller_address
}
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
caller = Context.user_account()

raw_tx =
EthClient.build_raw_tx(
0,
EthClient.nonce(caller.address),
EthClient.gas_limit(state[:data], caller.address),
EthClient.gas_price(),
data: state[:data]
)

tx_hash = EthClient.sign_transaction(raw_tx, Context.user_account().private_key)

{:ok, tx_hash} = Rpc.send_raw_transaction(tx_hash)
assert tx_hash =~ "0x"
end

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

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

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

test "[SUCCESS] Get Transaction by Hash" do
{:ok, transaction_map} =
Rpc.get_transaction_by_hash(
"0x3c249fcbb9ac3095cbe8196e6dba5d0aa0d868f2ebc7c857c8a22144fcbad07c"
)

assert %{
"blockHash" => _,
"blockNumber" => _,
"from" => _,
"gas" => _,
"gasPrice" => _,
"hash" => _,
"input" => _,
"nonce" => _,
"r" => _,
"s" => _,
"to" => _to,
"transactionIndex" => _,
"type" => _,
"v" => _,
"value" => _
} = transaction_map
end

test "[SUCCESS] Get Transaction Receipt" do
{:ok, receipt_map} =
Rpc.get_transaction_receipt(
"0x3c249fcbb9ac3095cbe8196e6dba5d0aa0d868f2ebc7c857c8a22144fcbad07c"
)

assert %{
"blockHash" => _blockhash,
"blockNumber" => _blocknumber,
"from" => _from,
"logs" => [],
"status" => _status,
"to" => _to,
"type" => _type
} = receipt_map
end

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

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

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

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