save contract from genesis file

pull/2875/head
Ayrat Badykov 5 years ago
parent 7788f4efd3
commit d9a9d4563b
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 2
      apps/explorer/lib/explorer/chain_spec/genesis_data.ex
  2. 17
      apps/explorer/lib/explorer/chain_spec/parity/importer.ex
  3. 62
      apps/explorer/test/explorer/chain_spec/parity/importer_test.exs

@ -62,7 +62,7 @@ defmodule Explorer.ChainSpec.GenesisData do
case fetch_spec(path) do case fetch_spec(path) do
{:ok, chain_spec} -> {:ok, chain_spec} ->
Importer.import_emission_rewards(chain_spec) Importer.import_emission_rewards(chain_spec)
{:ok, _} = Importer.import_genesis_coin_balances(chain_spec) {:ok, _} = Importer.import_genesis_accounts(chain_spec)
{:error, reason} -> {:error, reason} ->
Logger.warn(fn -> "Failed to fetch genesis data. #{inspect(reason)}" end) Logger.warn(fn -> "Failed to fetch genesis data. #{inspect(reason)}" end)

@ -24,10 +24,10 @@ defmodule Explorer.ChainSpec.Parity.Importer do
end end
end end
def import_genesis_coin_balances(chain_spec) do def import_genesis_accounts(chain_spec) do
balance_params = balance_params =
chain_spec chain_spec
|> genesis_coin_balances() |> genesis_accounts()
|> Stream.map(fn balance_map -> |> Stream.map(fn balance_map ->
Map.put(balance_map, :block_number, 0) Map.put(balance_map, :block_number, 0)
end) end)
@ -35,8 +35,8 @@ defmodule Explorer.ChainSpec.Parity.Importer do
address_params = address_params =
balance_params balance_params
|> Stream.map(fn %{address_hash: hash} -> |> Stream.map(fn %{address_hash: hash} = map ->
%{hash: hash} Map.put(map, :hash, hash)
end) end)
|> Enum.to_list() |> Enum.to_list()
@ -71,7 +71,7 @@ defmodule Explorer.ChainSpec.Parity.Importer do
{_, nil} = Repo.insert_all(EmissionReward, ordered_rewards) {_, nil} = Repo.insert_all(EmissionReward, ordered_rewards)
end end
def genesis_coin_balances(chain_spec) do def genesis_accounts(chain_spec) do
accounts = chain_spec["accounts"] accounts = chain_spec["accounts"]
if accounts do if accounts do
@ -102,11 +102,14 @@ defmodule Explorer.ChainSpec.Parity.Importer do
|> Stream.filter(fn {_address, map} -> |> Stream.filter(fn {_address, map} ->
!is_nil(map["balance"]) !is_nil(map["balance"])
end) end)
|> Stream.map(fn {address, %{"balance" => value}} -> |> Stream.map(fn {address, %{"balance" => value} = params} ->
{:ok, address_hash} = AddressHash.cast(address) {:ok, address_hash} = AddressHash.cast(address)
balance = parse_number(value) balance = parse_number(value)
%{address_hash: address_hash, value: balance} nonce = parse_number(params["nonce"] || "0")
code = params["code"]
%{address_hash: address_hash, value: balance, nonce: nonce, contract_code: code}
end) end)
|> Enum.to_list() |> Enum.to_list()
end end

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save