Merge pull request #8583 from blockscout/mf-rootstock-widget
Add stats widget for rootstockpull/8594/head
commit
231d72d1b0
@ -0,0 +1,31 @@ |
||||
defmodule Explorer.Chain.Cache.RootstockLockedBTC do |
||||
@moduledoc """ |
||||
Caches the number of BTC locked in 2WP on Rootstock chain. |
||||
""" |
||||
|
||||
require Logger |
||||
alias Explorer.Chain |
||||
alias Explorer.Chain.{Address, Wei} |
||||
|
||||
use Explorer.Chain.MapCache, |
||||
name: :locked_rsk, |
||||
key: :locked_value, |
||||
global_ttl: Application.get_env(:explorer, __MODULE__)[:global_ttl], |
||||
ttl_check_interval: :timer.seconds(1) |
||||
|
||||
defp handle_fallback(:locked_value) do |
||||
rootstock_bridge_address_str = Application.get_env(:explorer, Explorer.Chain.Transaction)[:rootstock_bridge_address] |
||||
rootstock_locking_cap = Application.get_env(:explorer, __MODULE__)[:locking_cap] |> Decimal.new() |
||||
|
||||
with {:ok, rootstock_bridge_address_hash} <- Chain.string_to_address_hash(rootstock_bridge_address_str), |
||||
{:ok, %Address{fetched_coin_balance: balance}} when not is_nil(balance) <- |
||||
Chain.hash_to_address(rootstock_bridge_address_hash) do |
||||
{:update, rootstock_locking_cap |> Wei.from(:ether) |> Wei.sub(balance)} |
||||
else |
||||
_ -> |
||||
{:return, nil} |
||||
end |
||||
end |
||||
|
||||
defp handle_fallback(_key), do: {:return, nil} |
||||
end |
@ -0,0 +1,38 @@ |
||||
defmodule Explorer.Chain.Cache.RootstockLockedBTCTest do |
||||
use Explorer.DataCase |
||||
|
||||
alias Explorer.Chain.Cache.RootstockLockedBTC |
||||
alias Explorer.Chain.{Transaction, Wei} |
||||
|
||||
@bridge_address "0x0000000000000000000000000000000001000006" |
||||
|
||||
setup do |
||||
transaction_configuration = Application.get_env(:explorer, Transaction) |
||||
Application.put_env(:explorer, Transaction, rootstock_bridge_address: @bridge_address) |
||||
|
||||
:ok |
||||
|
||||
Supervisor.terminate_child(Explorer.Supervisor, RootstockLockedBTC.child_id()) |
||||
Supervisor.restart_child(Explorer.Supervisor, RootstockLockedBTC.child_id()) |
||||
|
||||
on_exit(fn -> |
||||
Application.put_env(:explorer, Transaction, transaction_configuration) |
||||
end) |
||||
|
||||
:ok |
||||
end |
||||
|
||||
test "returns nil in case if there is no bridged address in the database" do |
||||
result = RootstockLockedBTC.get_locked_value() |
||||
|
||||
assert is_nil(result) |
||||
end |
||||
|
||||
test "updates cache if initial value is zero and returns converted wei" do |
||||
insert(:address, hash: @bridge_address, fetched_coin_balance: 42_000_000_000_000_000_000) |
||||
|
||||
result = RootstockLockedBTC.get_locked_value() |
||||
|
||||
assert result == Wei.from(Decimal.new(21_000_000), :ether) |> Wei.sub(Wei.from(Decimal.new(42), :ether)) |
||||
end |
||||
end |
Loading…
Reference in new issue