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

Testing the "Setup Ethereum devnet for GHA" (#69) PR #74

Closed
wants to merge 22 commits into from
Closed
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
39 changes: 20 additions & 19 deletions .github/workflows/elixir_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,7 @@ jobs:
matrix:
elixir: [1.13.0]
otp: [24.1.7]
services:
db:
image: postgres:12-alpine
ports: ["5432:5432"]
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: eth_war_game_tool_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v3
- name: Set up Elixir
Expand All @@ -55,10 +43,11 @@ jobs:
run: |
cd eth_client/
mix format --check-formatted
- name: Run Credo
run: |
cd eth_client/
mix credo --strict
# FIXME Dont merge this!!!
# - name: Run Credo
# run: |
# cd eth_client/
# mix credo --strict
- name: Retrieve PLT Cache
uses: actions/cache@v1
id: plt-cache
Expand All @@ -71,14 +60,26 @@ jobs:
cd eth_client/
mkdir -p priv/plts
mix dialyzer --plt

- name: Run tests
env:
ETH_API_KEY: ${{ secrets.ETH_API_KEY }}
ETH_CHAIN_ID: ${{ secrets.ETH_CHAIN_ID }}
ETH_RPC_HOST: ${{ secrets.ETH_RPC_HOST }}
ETH_USER_ADDRESS: ${{ secrets.ETH_USER_ADDRESS }}
ETH_USER_PK: ${{ secrets.ETH_USER_PK }}
run: |
source .envrc
cd eth_client/
mix test

- name: Code Coverage
env:
ETH_API_KEY: ${{ secrets.ETH_API_KEY }}
ETH_CHAIN_ID: ${{ secrets.ETH_CHAIN_ID }}
ETH_RPC_HOST: ${{ secrets.ETH_RPC_HOST }}
ETH_USER_ADDRESS: ${{ secrets.ETH_USER_ADDRESS }}
ETH_USER_PK: ${{ secrets.ETH_USER_PK }}
run: |
source .envrc
cd eth_client/
mix coveralls
- name: Run dialyzer
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ services:
- 43487:43487
volumes:
- ./eth_client/:/app/
environment:
- ETH_RPC_HOST
- ETH_USER_ADDRESS
- ETH_USER_PK
- ETH_API_KEY
- ETH_CONTRACT
- ETH_CHAIN_ID

# ----------------------------------------------------------------------------
# Livebook Frontend
Expand Down
11 changes: 2 additions & 9 deletions eth_client/lib/eth_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ 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)

caller = Context.user_account()
caller = Context.user_account() |> IO.inspect(label: :caller)
caller_address = String.downcase(caller.address)

nonce = nonce(caller.address)
Expand All @@ -50,6 +42,7 @@ defmodule EthClient do
{:ok, tx_hash} =
sign_transaction(raw_tx, caller.private_key)
|> Rpc.send_raw_transaction()
|> IO.inspect(label: "send_raw_transaction")

Logger.info("Deployment transaction accepted by the network, tx_hash: #{tx_hash}")
Logger.info("Waiting for confirmation...")
Expand Down
2 changes: 1 addition & 1 deletion eth_client/lib/eth_client/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule EthClient.Application do
private_key: System.fetch_env!("ETH_USER_PK")
},
etherscan_api_key: System.fetch_env!("ETH_API_KEY"),
contract: %Contract{address: System.fetch_env!("ETH_CONTRACT"), functions: nil}
contract: %Contract{address: System.get_env("ETH_CONTRACT"), functions: nil}
}
end
end
69 changes: 69 additions & 0 deletions eth_client/test/eth_client_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
defmodule EthClientTest do
use ExUnit.Case
doctest EthClient

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

setup_all [
:create_deployed_contract
]

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 FunctionClauseError, 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 == "0x0000000000000000000000000000000000000000000000000000000000000003"
{: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

defp create_deployed_contract(_context) do
contract = EthClient.deploy(@bin, @abi)
%{contract: contract} |> IO.inspect(label: :contract)
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)
ExUnit.start()