Skip to content

Commit

Permalink
Switch test-rpc provider to Anvil (#120)
Browse files Browse the repository at this point in the history
* Adapt ethers_test.exs to anvil

* Switch to anvil as the test-rpc provider

* Remove shanghai EVM version for test contracts

* Use anvil in CI action

* Only report coverage in one job

* Wait for multicall contract to be deployed
  • Loading branch information
alisinabh authored May 12, 2024
1 parent ca11584 commit 3f86686
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 216 deletions.
42 changes: 27 additions & 15 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,46 @@ jobs:
test:
name: Test - Lint - Dialyze

runs-on: ubuntu-22.04
runs-on: ubuntu-latest

env:
MIX_ENV: test
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

strategy:
matrix:
otp: ['25.x', '26.x']
elixir: ['1.15', '1.16']
variation:
- otp: '26.x'
elixir: '1.16'
report_coverage: true
- otp: '26.x'
elixir: '1.15'
report_coverage: false
- otp: '25.x'
elixir: '1.16'
report_coverage: false
- otp: '25.x'
elixir: '1.15'
report_coverage: false

steps:
- uses: actions/checkout@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{matrix.otp}}
elixir-version: ${{matrix.elixir}}

- uses: actions/setup-node@v3
with:
node-version: 16

- name: Install Ganache
run: npm install -g ganache
otp-version: ${{matrix.variation.otp}}
elixir-version: ${{matrix.variation.elixir}}

- name: Install Solidity
run: |
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Restore dependencies cache
uses: actions/cache@v3
with:
Expand All @@ -61,15 +68,20 @@ jobs:
- name: Install dependencies
run: mix deps.get

- name: Start Ganache (Background)
run: ganache --wallet.deterministic &
- name: Start Anvil (Background)
run: anvil &

- name: Prepare for tests
run: elixir test/test_prepare.exs

- name: Run tests
- name: Run tests and report coverage
if: ${{matrix.variation.report_coverage}}
run: mix coveralls.github

- name: Run tests
if: ${{!matrix.variation.report_coverage}}
run: mix coveralls

- name: Credo
run: mix credo --strict

Expand Down
48 changes: 33 additions & 15 deletions test/ethers/counter_contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ defmodule Ethers.CounterContractTest do
use ExUnit.Case
doctest Ethers.Contract

import Ethers.TestHelpers

alias Ethers.Event

alias Ethers.Contract.Test.CounterContract

@from "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1"
@from "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"

describe "contract deployment" do
test "Can deploy a contract on blockchain" do
Expand All @@ -23,6 +25,8 @@ defmodule Ethers.CounterContractTest do
from: @from
)

wait_for_transaction!(tx_hash)

assert {:ok, _address} = Ethers.deployed_address(tx_hash)
end
end
Expand Down Expand Up @@ -58,7 +62,7 @@ defmodule Ethers.CounterContractTest do

tx_data_with_default_address = %{tx_data | default_address: @from}

assert ~s'#Ethers.TxData<function get() view returns (uint256 amount)\n default_address: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1">' ==
assert ~s'#Ethers.TxData<function get() view returns (uint256 amount)\n default_address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266">' ==
inspect(tx_data_with_default_address)
end
end
Expand Down Expand Up @@ -89,7 +93,7 @@ defmodule Ethers.CounterContractTest do

filter_with_default_address = %{filter | default_address: @from}

assert ~s'#Ethers.EventFilter<event SetCalled(uint256 indexed oldAmount 101, uint256 newAmount)\n default_address: "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1">' ==
assert ~s'#Ethers.EventFilter<event SetCalled(uint256 indexed oldAmount 101, uint256 newAmount)\n default_address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266">' ==
inspect(filter_with_default_address)
end
end
Expand Down Expand Up @@ -120,15 +124,18 @@ defmodule Ethers.CounterContractTest do
end

test "sending transaction with state mutating functions", %{address: address} do
{:ok, _tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)
wait_for_transaction!(tx_hash)

{:ok, 101} = CounterContract.get() |> Ethers.call(to: address)
end

test "sending transaction with state mutating functions using bang functions", %{
address: address
} do
_tx_hash = CounterContract.set(101) |> Ethers.send!(from: @from, to: address)
tx_hash = CounterContract.set(101) |> Ethers.send!(from: @from, to: address)

wait_for_transaction!(tx_hash)

assert 101 == CounterContract.get() |> Ethers.call!(to: address)
end
Expand Down Expand Up @@ -198,7 +205,9 @@ defmodule Ethers.CounterContractTest do
setup :deploy_counter_contract

test "can get the emitted event with the correct filter", %{address: address} do
{:ok, _tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)

wait_for_transaction!(tx_hash)

assert open_filter = CounterContract.EventFilters.set_called(nil)
assert correct_filter = CounterContract.EventFilters.set_called(100)
Expand All @@ -220,6 +229,8 @@ defmodule Ethers.CounterContractTest do
test "cat get the emitted events with get_logs! function", %{address: address} do
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)

wait_for_transaction!(tx_hash)

assert filter = CounterContract.EventFilters.set_called(nil)

assert [
Expand All @@ -246,7 +257,9 @@ defmodule Ethers.CounterContractTest do
end

test "can filter logs with fromBlock and toBlock options", %{address: address} do
{:ok, _tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)
{:ok, tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)

wait_for_transaction!(tx_hash)

assert filter = CounterContract.EventFilters.set_called(nil)

Expand All @@ -264,15 +277,23 @@ defmodule Ethers.CounterContractTest do
setup :deploy_counter_contract

test "can call a view function on a previous block", %{address: address} do
{:ok, _tx_hash} = CounterContract.set(101) |> Ethers.send(from: @from, to: address)
{:ok, block_1} = Ethereumex.HttpClient.eth_block_number()
CounterContract.set(101)
|> Ethers.send!(from: @from, to: address)
|> wait_for_transaction!()

{:ok, block_1} = Ethers.current_block_number()

CounterContract.set(102)
|> Ethers.send!(from: @from, to: address)
|> wait_for_transaction!()

{:ok, _tx_hash} = CounterContract.set(102) |> Ethers.send(from: @from, to: address)
{:ok, block_2} = Ethers.current_block_number()

assert is_integer(block_2)

{:ok, _tx_hash} = CounterContract.set(103) |> Ethers.send(from: @from, to: address)
CounterContract.set(103)
|> Ethers.send!(from: @from, to: address)
|> wait_for_transaction!()

assert CounterContract.get() |> Ethers.call!(to: address, block: "latest") == 103
assert CounterContract.get() |> Ethers.call!(to: address, block: block_2) == 102
Expand All @@ -283,10 +304,7 @@ defmodule Ethers.CounterContractTest do
defp deploy_counter_contract(_ctx) do
encoded_constructor = CounterContract.constructor(100)

assert {:ok, tx_hash} =
Ethers.deploy(CounterContract, encoded_constructor: encoded_constructor, from: @from)

assert {:ok, address} = Ethers.deployed_address(tx_hash)
address = deploy(CounterContract, encoded_constructor: encoded_constructor, from: @from)

[address: address]
end
Expand Down
15 changes: 10 additions & 5 deletions test/ethers/event_mixed_index_contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ end
defmodule Ethers.EventMixedIndexContractTest do
use ExUnit.Case

import Ethers.TestHelpers

alias Ethers.Contract.Test.EventMixedIndexContract

@from "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1"
@from "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

describe "event filters" do
test "works with mixed indexed events" do
Expand All @@ -28,8 +30,8 @@ defmodule Ethers.EventMixedIndexContractTest do
},
topics: [
"0x0f1459b71050cedb12633644ebaa16569e1bb49626ab8a0f4c7d1cf0d574abe7",
"0x00000000000000000000000090f8bf6a479f320ead074411a4b0e7944ea8c9c1",
"0x00000000000000000000000090f8bf6a479f320ead074411a4b0e7944ea8c9c1"
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
],
default_address: nil
} == EventMixedIndexContract.EventFilters.transfer(@from, @from)
Expand All @@ -44,10 +46,13 @@ defmodule Ethers.EventMixedIndexContractTest do
from: @from
)

wait_for_transaction!(tx_hash)

assert {:ok, address} = Ethers.deployed_address(tx_hash)

EventMixedIndexContract.transfer(100, @from, true, @from)
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

filter = EventMixedIndexContract.EventFilters.transfer(@from, @from)

Expand All @@ -61,8 +66,8 @@ defmodule Ethers.EventMixedIndexContractTest do
transaction_index: 0,
topics_raw: [
"0x0f1459b71050cedb12633644ebaa16569e1bb49626ab8a0f4c7d1cf0d574abe7",
"0x00000000000000000000000090f8bf6a479f320ead074411a4b0e7944ea8c9c1",
"0x00000000000000000000000090f8bf6a479f320ead074411a4b0e7944ea8c9c1"
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266",
"0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266"
],
data_raw:
"0x00000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000001"
Expand Down
6 changes: 5 additions & 1 deletion test/ethers/multi_arity_contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ end
defmodule Ethers.MultiArityContractTest do
use ExUnit.Case

import Ethers.TestHelpers

alias Ethers.Contract.Test.MultiArityContract

@from "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1"
@from "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

describe "next function" do
test "can override RPC client" do
Expand All @@ -20,6 +22,8 @@ defmodule Ethers.MultiArityContractTest do
from: @from
)

wait_for_transaction!(tx_hash)

assert {:ok, address} = Ethers.deployed_address(tx_hash)

assert {:ok, 0} = MultiArityContract.next() |> Ethers.call(to: address)
Expand Down
20 changes: 10 additions & 10 deletions test/ethers/multi_clause_contract_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ end
defmodule Ethers.MultiClauseContractTest do
use ExUnit.Case

alias Ethers.Contract.Test.MultiClauseContract

import Ethers.Types, only: [typed: 2]
import Ethers.TestHelpers

@from "0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1"
alias Ethers.Contract.Test.MultiClauseContract

@from "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

setup :deploy_multi_clause_contract

Expand Down Expand Up @@ -54,6 +55,7 @@ defmodule Ethers.MultiClauseContractTest do
test "listens on the right event", %{address: address} do
MultiClauseContract.emit_event(typed({:uint, 256}, 10))
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

uint_filter = MultiClauseContract.EventFilters.multi_event(typed({:uint, 256}, 10))

Expand All @@ -62,6 +64,7 @@ defmodule Ethers.MultiClauseContractTest do

MultiClauseContract.emit_event(typed({:int, 256}, -20))
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

int_filter = MultiClauseContract.EventFilters.multi_event(typed({:int, 256}, -20))

Expand All @@ -70,6 +73,7 @@ defmodule Ethers.MultiClauseContractTest do

MultiClauseContract.emit_event("Hello")
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

string_filter = MultiClauseContract.EventFilters.multi_event(typed(:string, "Hello"))

Expand All @@ -84,6 +88,7 @@ defmodule Ethers.MultiClauseContractTest do
test "listens on the right event with nil values", %{address: address} do
MultiClauseContract.emit_event(typed({:uint, 256}, 10))
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

uint_filter = MultiClauseContract.EventFilters.multi_event(typed({:uint, 256}, nil))

Expand All @@ -92,6 +97,7 @@ defmodule Ethers.MultiClauseContractTest do

MultiClauseContract.emit_event(typed({:int, 256}, -20))
|> Ethers.send!(to: address, from: @from)
|> wait_for_transaction!()

int_filter = MultiClauseContract.EventFilters.multi_event(typed({:int, 256}, nil))

Expand Down Expand Up @@ -125,13 +131,7 @@ defmodule Ethers.MultiClauseContractTest do
defp deploy_multi_clause_contract(_ctx) do
encoded_constructor = MultiClauseContract.constructor()

assert {:ok, tx_hash} =
Ethers.deploy(MultiClauseContract,
encoded_constructor: encoded_constructor,
from: @from
)

assert {:ok, address} = Ethers.deployed_address(tx_hash)
address = deploy(MultiClauseContract, encoded_constructor: encoded_constructor, from: @from)

[address: address]
end
Expand Down
Loading

0 comments on commit 3f86686

Please sign in to comment.