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 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
4 changes: 2 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export ETH_RPC_HOST=http://localhost:8545
export ETH_RPC_HOST="https://rinkeby.infura.io/v3/8db75325ccd14936a965997dd1469177"
export ETH_USER_ADDRESS=0xafb72ccaeb7e22c8a7640f605824b0898424b3da
export ETH_USER_PK=e90d75baafee04b3d9941bd8d76abe799b391aec596515dee11a9bd55f05709c
export ETH_API_KEY=""
export ETH_CONTRACT=""
export ETH_CHAIN_ID="1234"
export ETH_CHAIN_ID="4"
8 changes: 0 additions & 8 deletions eth_client/lib/eth_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ 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
56 changes: 56 additions & 0 deletions eth_client/test/eth_client_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
defmodule EthClientTest do
use ExUnit.Case
doctest EthClient

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

setup_all do
contract = EthClient.deploy(@bin, @abi)
Comment on lines +8 to +9
Copy link

Choose a reason for hiding this comment

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

With this change, you can now remove the tags from the tests (lines 39, 64, etc)

{:ok, contract: contract}
end

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

describe "invoke/3" do
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

test "[FAILURE] invokes a function with incorrect amount of params" do
assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", []) end
end

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
test "[SUCCESS] Call" do
{:ok, res} = EthClient.call("retrieve()", [])
assert res == "0x0000000000000000000000000000000000000000000000000000000000000003"
{:ok, res} = EthClient.call("test_function()", [])
assert res == "0x0000000000000000000000000000000000000000000000000000000000000001"
end

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

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
test "[SUCCESS] Balance", %{contract: contract} do
assert 0.0 == EthClient.get_balance(contract.address)
end

test "[FAILURE] unexisting address", %{contract: _contract} do
assert_raise FunctionClauseError, fn -> EthClient.get_balance("0x123213b") end
end
end
end
1 change: 1 addition & 0 deletions eth_client/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ExUnit.configure(seed: 0)
Copy link

Choose a reason for hiding this comment

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

Why do you need to set the seed value to 0?

ExUnit.start()