Build a Scrape task that grabs the latest block.

pull/2/head
CJ Bryan and Matt Olenick 7 years ago
parent 668c0f7b1f
commit 0da5f67763
  1. 5
      config/config.exs
  2. 72
      lib/mix/tasks/scrape.ex
  3. 5
      mix.exs
  4. 1
      mix.lock
  5. 17
      test/mix/tasks/scrape_test.exs

@ -27,3 +27,8 @@ config :logger, :console,
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
config :ethereumex,
scheme: "https",
host: "sokol.poa.network",
port: 443

@ -0,0 +1,72 @@
defmodule Mix.Tasks.Scrape do
use Mix.Task
alias Explorer.Block
alias Explorer.Repo
import Ethereumex.HttpClient, only: [
eth_block_number: 0,
eth_get_block_by_number: 2
]
@shortdoc "Scrape the blockchain."
@moduledoc false
def run(_) do
persist()
end
def persist do
{:ok, _} = Repo.insert(changeset())
end
def changeset do
Block.changeset(attributes(), %{})
end
def attributes do
%Block{
hash: hash(),
number: number(),
timestamp: timestamp(),
gas_used: gas_used(),
parent_hash: "0x0",
nonce: "0",
miner: "0x0",
difficulty: "0",
total_difficulty: "0",
size: "0",
gas_limit: "0",
}
end
def hash do
latest_block()["hash"]
end
def number do
decode_integer_field(latest_block()["number"])
end
def timestamp do
epoch_to_datetime(decode_integer_field(latest_block()["timestamp"]))
end
def gas_used do
decode_integer_field(latest_block()["gasUsed"])
end
def latest_block do
Mix.Task.run "app.start"
{:ok, latest_block_number} = eth_block_number()
{:ok, latest_block} = eth_get_block_by_number(latest_block_number, true)
latest_block
end
def decode_integer_field(field) do
{"0x", base_16} = String.split_at(field, 2)
String.to_integer(base_16, 16)
end
def epoch_to_datetime(epoch) do
Timex.from_unix(epoch)
end
end

@ -29,8 +29,8 @@ defmodule Explorer.Mixfile do
defp elixirc_paths(_), do: ["lib"]
# Specifies extra applications to start per environment
defp extra_applications(:prod), do: [:timex, :timex_ecto, :set_locale, :logger, :runtime_tools, :phoenix_pubsub_redis, :new_relixir]
defp extra_applications(_), do: [:timex, :timex_ecto, :set_locale, :logger, :runtime_tools]
defp extra_applications(:prod), do: [:ethereumex, :timex, :timex_ecto, :set_locale, :logger, :runtime_tools, :phoenix_pubsub_redis, :new_relixir]
defp extra_applications(_), do: [:ethereumex, :timex, :timex_ecto, :set_locale, :logger, :runtime_tools]
# Specifies your project dependencies.
#
@ -39,6 +39,7 @@ defmodule Explorer.Mixfile do
[
{:cowboy, "~> 1.0"},
{:credo, "~> 0.8", only: [:dev, :test], runtime: false},
{:ethereumex, github: "exthereum/ethereumex", commit: "262f1d81ae163ffb46e127283658249dac1c8318"}, # Waiting for this version to be pushed to Hex.
{:ex_machina, "~> 2.1", only: [:test]},
{:gettext, "~> 0.11"},
{:junit_formatter, ">= 0.0.0"},

@ -8,6 +8,7 @@
"db_connection": {:hex, :db_connection, "1.1.3", "89b30ca1ef0a3b469b1c779579590688561d586694a3ce8792985d4d7e575a61", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"decimal": {:hex, :decimal, "1.4.1", "ad9e501edf7322f122f7fc151cce7c2a0c9ada96f2b0155b8a09a795c2029770", [:mix], [], "hexpm"},
"ecto": {:hex, :ecto, "2.2.8", "a4463c0928b970f2cee722cd29aaac154e866a15882c5737e0038bbfcf03ec2c", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, repo: "hexpm", optional: true]}], "hexpm"},
"ethereumex": {:git, "https://github.com/exthereum/ethereumex.git", "262f1d81ae163ffb46e127283658249dac1c8318", []},
"ex_machina": {:hex, :ex_machina, "2.1.0", "4874dc9c78e7cf2d429f24dc3c4005674d4e4da6a08be961ffccc08fb528e28b", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.4", "f0bdda195c0e46e987333e986452ec523aed21d784189144f647c43eaf307064", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.14.0", "1a019a2e51d5ad3d126efe166dcdf6563768e5d06c32a99ad2281a1fa94b4c72", [:mix], [], "hexpm"},

@ -0,0 +1,17 @@
defmodule Scrape.Test do
use Explorer.DataCase
alias Explorer.Block
alias Explorer.Repo
test "it downloads a new block" do
Mix.Tasks.Scrape.run([])
last_block = Block
|> order_by(desc: :inserted_at)
|> limit(1)
|> Repo.all
|> List.first
assert(last_block.height)
end
end
Loading…
Cancel
Save