Skip to content

Test scripts for ethclient.ex #65

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
10 changes: 1 addition & 9 deletions eth_client/lib/eth_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ defmodule EthClient do

@local_host_chain_id 1234
@local_host_rpc "http://localhost:8545"

# Modify the code so that the only thing we do in Rust is the EC signature and Keccak hashing
# View the state of a contract (all its variables, etc). This will require parsing the ABI
# Add the ability to check if a transaction is a contract deployment or not
# Check balance
# Fix gas limit
# Change shell text based on context
# Get list of nodes


def deploy(bin_path) do
{:ok, data} = File.read(bin_path)
data = add_0x(data)
Expand Down
67 changes: 67 additions & 0 deletions eth_client/test/eth_client_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
defmodule EthClientTest do
use ExUnit.Case
doctest EthClient
alias EthClient.Account
alias EthClient.Context

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

setup %{bin: bin, abi: abi} do
contract = EthClient.deploy(bin, abi)
{:ok, contract: contract}
end

describe "deploy/2" do
@tag bin: @bin, abi: @abi
test "[SUCCESS] Succesful deploy", %{contract: contract} do
assert contract == EthClient.Context.contract()
end
end

describe "invoke/3" do
@tag bin: @bin, abi: @abi
test "[SUCCESS] invokes a function it returns the transaction hash" do
{:ok, tx_ans} = EthClient.invoke("store(uint256)", [3], 0)
assert tx_ans != nil
end

@tag bin: @bin, abi: @abi
test "[FAILURE] invokes a function with incorrect amount of params" do
assert_raise UndefinedFunctionError, fn -> EthClient.invoke("store(uint256)", []) end
end

@tag bin: @bin, abi: @abi
test "[FAILURE] invokes a contract function with incorrect params" do
assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", [], 0) end
end
end

describe "call/2" do
@tag bin: @bin, abi: @abi
test "[SUCCESS] Call" do
{:ok, res} = EthClient.call("retrieve()", [])
assert res == "0x0000000000000000000000000000000000000000000000000000000000000000"
{:ok, res} = EthClient.call("test_function()", [])
assert res == "0x0000000000000000000000000000000000000000000000000000000000000001"
end

@tag bin: @bin, abi: @abi
test "[FAILURE] calls a function with incorrect amount of params" do
assert_raise UndefinedFunctionError, fn -> EthClient.call("retrieve()") end
end

@tag bin: @bin, abi: @abi
test "[FAILURE] calls a contract function with incorrect params" do
assert_raise FunctionClauseError, fn -> EthClient.call("retrieve()", [12]) end
end
end

describe "balance/1" do
@tag bin: @bin, abi: @abi
test "[SUCCESS] Balance", %{contract: contract} do
assert 0.0 == EthClient.get_balance(contract.address)
end

@tag bin: @bin, abi: @abi
test "[FAILURE] unexisting address", %{contract: contract} do
assert_raise FunctionClauseError, fn -> EthClient.get_balance("0x123213b") end
end
end
end