4
0
Fork 0

Added event tester test

contracts
hswick 7 years ago
parent c145a063d8
commit 5ad6a8bfb7
  1. 13
      README.md
  2. 27
      lib/exw3.ex
  3. 2
      test/examples/build/ArrayTester.bin
  4. 2
      test/examples/build/Complex.bin
  5. 1
      test/examples/build/EventTester.abi
  6. 1
      test/examples/build/EventTester.bin
  7. 2
      test/examples/build/SimpleStorage.bin
  8. 10
      test/examples/contracts/EventTester.sol
  9. 42
      test/exw3_test.exs

@ -15,3 +15,16 @@ Ethereum client for Elixir
def application do
[applications: [:exw3]]
end
# Compiling solidity
Ensure you have solc installed:
```
solc --version
```
Then if you've made changes to the example contracts you can compile them like this:
```
solc -o test/examples/build --abi --bin test/examples/contracts/* --overwrite
```

@ -83,7 +83,7 @@ defmodule ExW3 do
end
#Converts ethereum hex string to decimal number
defp to_decimal hex_string do
def to_decimal hex_string do
hex_string
|> String.slice(2..-1)
|> String.to_integer(16)
@ -105,7 +105,7 @@ defmodule ExW3 do
end
end
defp keys_to_decimal map, keys do
def keys_to_decimal map, keys do
Map.new(
Enum.map keys, fn k ->
{ k, Map.get(map, k) |> to_decimal }
@ -113,12 +113,31 @@ defmodule ExW3 do
)
end
def tx_receipt tx_id do
case Ethereumex.HttpClient.eth_get_transaction_receipt(tx_id) do
def tx_receipt tx_hash do
case Ethereumex.HttpClient.eth_get_transaction_receipt(tx_hash) do
{:ok, receipt} ->
Map.merge receipt, keys_to_decimal(receipt, ["blockNumber", "cumulativeGasUsed", "gasUsed"])
err -> err
end
end
def block block_number do
case Ethereumex.HttpClient.eth_get_block_by_number(block_number, true) do
{:ok, block} -> block
err -> err
end
end
def mine(num_blocks \\ 1) do
for _ <- 0..(num_blocks - 1) do
Ethereumex.HttpClient.request("evm_mine", [], [])
end
end
def encode_event(signature) do
ExthCrypto.Hash.Keccak.kec(signature) |> Base.encode16(case: :lower)
end
end

@ -1 +1 @@
6060604052341561000f57600080fd5b61020a8061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635cfd038d146100515780635d4e0342146100d3575b600080fd5b341561005c57600080fd5b6100956004808060a001906005806020026040519081016040528092919082600560200280828437820191505050505091905050610182565b6040518082600560200280838360005b838110156100c05780820151818401526020810190506100a5565b5050505090500191505060405180910390f35b34156100de57600080fd5b61012b600480803590602001908201803590602001908080602002602001604051908101604052809392919081815260200183836020028082843782019150505050505091905050610192565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b8381101561016e578082015181840152602081019050610153565b505050509050019250505060405180910390f35b61018a6101a2565b819050919050565b61019a6101ca565b819050919050565b60a0604051908101604052806005905b60008152602001906001900390816101b25790505090565b6020604051908101604052806000815250905600a165627a7a72305820e41e488a9545e9fa5a98095e6bc74e07ed21def3a9ba0d3867003099fab1eec00029
6060604052341561000f57600080fd5b61020a8061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635cfd038d146100515780635d4e0342146100d3575b600080fd5b341561005c57600080fd5b6100956004808060a001906005806020026040519081016040528092919082600560200280828437820191505050505091905050610182565b6040518082600560200280838360005b838110156100c05780820151818401526020810190506100a5565b5050505090500191505060405180910390f35b34156100de57600080fd5b61012b600480803590602001908201803590602001908080602002602001604051908101604052809392919081815260200183836020028082843782019150505050505091905050610192565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b8381101561016e578082015181840152602081019050610153565b505050509050019250505060405180910390f35b61018a6101a2565b819050919050565b61019a6101ca565b819050919050565b60a0604051908101604052806005905b60008152602001906001900390816101b25790505090565b6020604051908101604052806000815250905600a165627a7a72305820f19307646822c05c39f3f49818eb7eef776e9108223baeeed363c3d8ac1f65d40029

@ -1 +1 @@
6060604052341561000f57600080fd5b6040516040806103128339810160405280805190602001909190805190602001909190505081600081905550806001816000191690555050506102bb806100576000396000f30060606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631b85502e1461009357806354fcb8a6146100cb5780637b07059b146100f857806393affe511461011f5780639aa7c12014610157578063b17e413b1461017c578063cfd91f2b146101b3578063dc80035d146101e4575b600080fd5b341561009e57600080fd5b6100a6610207565b6040518083815260200182600019166000191681526020019250505060405180910390f35b34156100d657600080fd5b6100de61021b565b604051808215151515815260200191505060405180910390f35b341561010357600080fd5b61011d600480803560001916906020019091905050610232565b005b341561012a57600080fd5b610132610240565b6040518083815260200182600019166000191681526020019250505060405180910390f35b341561016257600080fd5b61017a60048080351515906020019091905050610251565b005b341561018757600080fd5b61019d600480803590602001909190505061026e565b6040518082815260200191505060405180910390f35b34156101be57600080fd5b6101c661027b565b60405180826000191660001916815260200191505060405180910390f35b34156101ef57600080fd5b6102056004808035906020019091905050610285565b005b600080602a60005401600154915091509091565b6000600260009054906101000a900460ff16905090565b806001816000191690555050565b600080600054600154915091509091565b80600260006101000a81548160ff02191690831515021790555050565b6000602a82019050919050565b6000600154905090565b80600081905550505600a165627a7a72305820fbec4ee932ff7d288a752933ae6cb2b6b01949e15a49b6b854142cc9acb3efa10029
6060604052341561000f57600080fd5b6040516040806103128339810160405280805190602001909190805190602001909190505081600081905550806001816000191690555050506102bb806100576000396000f30060606040526004361061008e576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631b85502e1461009357806354fcb8a6146100cb5780637b07059b146100f857806393affe511461011f5780639aa7c12014610157578063b17e413b1461017c578063cfd91f2b146101b3578063dc80035d146101e4575b600080fd5b341561009e57600080fd5b6100a6610207565b6040518083815260200182600019166000191681526020019250505060405180910390f35b34156100d657600080fd5b6100de61021b565b604051808215151515815260200191505060405180910390f35b341561010357600080fd5b61011d600480803560001916906020019091905050610232565b005b341561012a57600080fd5b610132610240565b6040518083815260200182600019166000191681526020019250505060405180910390f35b341561016257600080fd5b61017a60048080351515906020019091905050610251565b005b341561018757600080fd5b61019d600480803590602001909190505061026e565b6040518082815260200191505060405180910390f35b34156101be57600080fd5b6101c661027b565b60405180826000191660001916815260200191505060405180910390f35b34156101ef57600080fd5b6102056004808035906020019091905050610285565b005b600080602a60005401600154915091509091565b6000600260009054906101000a900460ff16905090565b806001816000191690555050565b600080600054600154915091509091565b80600260006101000a81548160ff02191690831515021790555050565b6000602a82019050919050565b6000600154905090565b80600081905550505600a165627a7a72305820a5876c0366e7b544e1f545407a57da0518f86811621fe36050e2beed38288ea90029

@ -0,0 +1 @@
[{"constant":false,"inputs":[{"name":"data","type":"bytes32"}],"name":"simple","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"num","type":"uint256"},{"indexed":false,"name":"data","type":"bytes32"}],"name":"Simple","type":"event"}]

@ -0,0 +1 @@
60606040523415600e57600080fd5b60df8061001c6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063501064b1146044575b600080fd5b3415604e57600080fd5b60666004808035600019169060200190919050506068565b005b7f906950f44e189193819a60a8ce35a53a0aee725af98bc984839c89d8fa2e09dd602a826040518083815260200182600019166000191681526020019250505060405180910390a1505600a165627a7a723058203db798c71abdc2ee56a59fa2fe124e8f7a3374c90d71559f08e9758d18d58e950029

@ -1 +1 @@
6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058202d68adcfe82d5e51b80e2a9202eb808515f4ba8f98e4dfcca08a018132d33e530029
6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a723058200f90c0b17ed007c4bc6353db47563e05a1e1caa853d0525e278cdef53546167a0029

@ -0,0 +1,10 @@
pragma solidity ^0.4.18;
contract EventTester {
event Simple(uint256 num, bytes32 data);
function simple(bytes32 data) public {
emit Simple(42, data);
}
}

@ -6,6 +6,7 @@ defmodule EXW3Test do
%{
simple_storage_abi: ExW3.load_abi("test/examples/build/SimpleStorage.abi"),
array_tester_abi: ExW3.load_abi("test/examples/build/ArrayTester.abi"),
event_tester_abi: ExW3.load_abi("test/examples/build/EventTester.abi"),
accounts: ExW3.accounts
}
end
@ -26,6 +27,18 @@ defmodule EXW3Test do
assert context[:simple_storage_abi] |> is_map
end
test "mines a block" do
block_number = ExW3.block_number
ExW3.mine
assert ExW3.block_number == block_number + 1
end
test "mines multiple blocks" do
block_number = ExW3.block_number
ExW3.mine 5
assert ExW3.block_number == block_number + 5
end
test "deploys simple storage and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/SimpleStorage.bin",
@ -41,9 +54,11 @@ defmodule EXW3Test do
assert result == 0
{:ok, tx_id} = ExW3.Contract.method(storage, "set", [1], %{from: Enum.at(context[:accounts], 0)})
{:ok, tx_hash} = ExW3.Contract.method(storage, "set", [1], %{from: Enum.at(context[:accounts], 0)})
receipt = ExW3.tx_receipt tx_hash
IO.inspect ExW3.tx_receipt tx_id
#IO.inspect ExW3.block receipt["blockNumber"]
{:ok, result} = ExW3.Contract.method(storage, "get")
@ -51,6 +66,29 @@ defmodule EXW3Test do
end
test "deploys event tester and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/EventTester.bin",
%{
from: Enum.at(context[:accounts], 0),
gas: 300000
}
)
event_tester = ExW3.Contract.at context[:event_tester_abi], contract_address
{:ok, tx_hash} = ExW3.Contract.method(event_tester, "simple", ["hello, there!"], %{from: Enum.at(context[:accounts], 0)})
receipt = ExW3.tx_receipt tx_hash
logs = receipt["logs"]
topic = Map.get(Enum.at(logs, 0), "topics")
assert String.slice(Enum.at(topic, 0), 2..-1) == ExW3.encode_event("Simple(uint256,bytes32)")
end
test "deploys array tester and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/ArrayTester.bin",

Loading…
Cancel
Save