From 8a1d20b1c86883a5d21dee0a1c2ed0a606d55c24 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 6 Aug 2019 13:20:05 +0300 Subject: [PATCH] add POA emission rewards importer --- .../lib/explorer/chain_spec/poa/importer.ex | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 apps/explorer/lib/explorer/chain_spec/poa/importer.ex diff --git a/apps/explorer/lib/explorer/chain_spec/poa/importer.ex b/apps/explorer/lib/explorer/chain_spec/poa/importer.ex new file mode 100644 index 0000000000..45e18e6d1c --- /dev/null +++ b/apps/explorer/lib/explorer/chain_spec/poa/importer.ex @@ -0,0 +1,79 @@ +defmodule Explorer.ChainSpec.POA.Importer do + @moduledoc """ + Imports emission reward range for POA chain. + """ + + alias Explorer.Chain.Wei + alias Explorer.Repo + alias Explorer.SmartContract.Reader + alias Explorer.Chain.Block.{EmissionReward, Range} + + @reward_by_block_contract_address "0xeca443e8e1ab29971a45a9c57a6a9875701698a5" + @block_reward_amount_abi %{ + "type" => "function", + "stateMutability" => "view", + "payable" => false, + "outputs" => [%{"type" => "uint256", "name" => ""}], + "name" => "blockRewardAmount", + "inputs" => [], + "constant" => true + } + @block_reward_amount_params %{"blockRewardAmount" => []} + @emission_funds_amount_abi %{ + "type" => "function", + "stateMutability" => "view", + "payable" => false, + "outputs" => [%{"type" => "uint256", "name" => ""}], + "name" => "emissionFundsAmount", + "inputs" => [], + "constant" => true + } + @emission_funds_amount_params %{"emissionFundsAmount" => []} + @emission_funds_block_start 5_098_087 + + def import_emission_rewards do + block_reward = block_reward_amount() + emission_funds = emission_funds_amount() + + rewards = [ + %{ + block_range: %Range{from: 0, to: @emission_funds_block_start}, + reward: %Wei{value: block_reward} + }, + %{ + block_range: %Range{from: @emission_funds_block_start + 1, to: :infinity}, + reward: %Wei{value: Decimal.add(block_reward, emission_funds)} + } + ] + + {_, nil} = Repo.delete_all(EmissionReward) + {_, nil} = Repo.insert_all(EmissionReward, rewards) + end + + def block_reward_amount do + call_contract(@reward_by_block_contract_address, @block_reward_amount_abi, @block_reward_amount_params) + end + + def emission_funds_amount do + call_contract(@reward_by_block_contract_address, @emission_funds_amount_abi, @emission_funds_amount_params) + end + + defp call_contract(address, abi, params) do + abi = [abi] + + method_name = + params + |> Enum.map(fn {key, _value} -> key end) + |> List.first() + + Reader.query_contract(address, abi, params) + + value = + case Reader.query_contract(address, abi, params) do + %{^method_name => {:ok, [result]}} -> result + _ -> 0 + end + + Decimal.new(value) + end +end