parent
8332857303
commit
8a1d20b1c8
@ -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 |
Loading…
Reference in new issue