Skip to content

Commit

Permalink
Merge branch 'master' into eruizgar91/w48-upstream-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Ruiz authored Dec 10, 2021
2 parents 190ae69 + a4b80bd commit bd6a537
Show file tree
Hide file tree
Showing 26 changed files with 489 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ defmodule BlockScoutWeb.API.RPC.StatsController do
render(conn, "totaltransactions.json", count: transaction_estimated_count)
end

def pendingcelo(conn, _params) do
sum_pending_celo = Chain.fetch_sum_pending_celo()
def celounlocked(conn, _params) do
sum_celo_unlocked = Chain.fetch_sum_celo_unlocked()

render(conn, "pendingcelo.json", count: sum_pending_celo)
render(conn, "celounlocked.json", count: sum_celo_unlocked)
end

defp fetch_contractaddress(params) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
<dl class="row">
<dt class="col-sm-4 col-md-4 col-lg-3 text-muted">
<%= render BlockScoutWeb.CommonComponentsView, "_i_tooltip_2.html",
text: gettext("Address balance in xDAI (doesn't include ERC20, ERC721, ERC1155 tokens).") %>
text: gettext("Address balance in CELO (doesn't include ERC20, ERC721, ERC1155 tokens).") %>
<%= gettext("Balance") %>
</dt>
<dd class="col-sm-8 col-md-8 col-lg-9" data-test="address_balance">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ defmodule BlockScoutWeb.API.RPC.StatsView do
RPCView.render("show.json", data: count)
end

def render("pendingcelo.json", %{count: sum_pending_celo}) do
RPCView.render("show.json", data: sum_pending_celo)
def render("celounlocked.json", %{count: sum_celo_unlocked}) do
RPCView.render("show.json", data: sum_celo_unlocked)
end

def render("totalfees.json", %{total_fees: total_fees}) do
Expand Down
2 changes: 1 addition & 1 deletion apps/block_scout_web/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address/overview.html.eex:166
msgid "Address balance in xDAI (doesn't include ERC20, ERC721, ERC1155 tokens)."
msgid "Address balance in CELO (doesn't include ERC20, ERC721, ERC1155 tokens)."
msgstr ""

#, elixir-format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ msgstr ""

#, elixir-format
#: lib/block_scout_web/templates/address/overview.html.eex:166
msgid "Address balance in xDAI (doesn't include ERC20, ERC721, ERC1155 tokens)."
msgid "Address balance in CELO (doesn't include ERC20, ERC721, ERC1155 tokens)."
msgstr ""

#, elixir-format
Expand Down
8 changes: 6 additions & 2 deletions apps/explorer/lib/explorer/celo/account_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,17 @@ defmodule Explorer.Celo.AccountReader do
def withdrawal_data(address) do
data = fetch_withdrawal_data(address)

{:ok, cast_address} = Explorer.Chain.Hash.Address.cast(address)

case data["getPendingWithdrawals"] do
{:ok, [values, timestamps]} ->
{:ok,
%{
address: address,
address: cast_address,
pending:
Enum.map(Enum.zip(values, timestamps), fn {v, t} -> %{address: address, amount: v, timestamp: t} end)
Enum.map(Enum.zip(values, timestamps), fn {v, t} ->
%{address: cast_address, amount: v, timestamp: DateTime.from_unix!(t)}
end)
}}

_ ->
Expand Down
10 changes: 10 additions & 0 deletions apps/explorer/lib/explorer/celo/event_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ defmodule Explorer.Celo.Events do
@gold_locked
]

def gold_unlocked,
do: [
@gold_unlocked
]

def gold_withdrawn,
do: [
@gold_withdrawn
]

def signer_events,
do: [
@validator_signer_authorized,
Expand Down
36 changes: 25 additions & 11 deletions apps/explorer/lib/explorer/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule Explorer.Chain do
CeloParams,
CeloPendingEpochOperation,
CeloSigners,
CeloUnlocked,
CeloValidator,
CeloValidatorGroup,
CeloValidatorHistory,
Expand All @@ -62,7 +63,6 @@ defmodule Explorer.Chain do
InternalTransaction,
Log,
PendingBlockOperation,
PendingCelo,
ProxyContract,
SmartContract,
SmartContractAdditionalSource,
Expand Down Expand Up @@ -7857,14 +7857,13 @@ defmodule Explorer.Chain do
end

@doc """
Returns the total amount of CELO that is in the unlocking period (pending).
Returns the total amount of CELO that is unlocked and can't or hasn't yet been withdrawn.
Details at: https://docs.celo.org/celo-codebase/protocol/proof-of-stake/locked-gold#unlocking-period
"""
@spec fetch_sum_pending_celo() :: non_neg_integer()
def fetch_sum_pending_celo do
@spec fetch_sum_celo_unlocked() :: non_neg_integer()
def fetch_sum_celo_unlocked do
query =
from(w in PendingCelo,
where: w.timestamp >= fragment("NOW()"),
from(w in CeloUnlocked,
select: sum(w.amount)
)

Expand All @@ -7877,18 +7876,33 @@ defmodule Explorer.Chain do
end

@doc """
Deletes pending CELO when passed the address and the amount
Deletes unlocked CELO when passed the address and the amount
"""
@spec delete_pending_celo(Hash.t(), non_neg_integer()) :: {integer(), nil | [term()]}
def delete_pending_celo(address, amount) do
@spec delete_celo_unlocked(Hash.t(), non_neg_integer()) :: {integer(), nil | [term()]}
def delete_celo_unlocked(address, amount) do
query =
from(pending_celo in PendingCelo,
where: pending_celo.account_address == ^address and pending_celo.amount == ^amount
from(celo_unlocked in CeloUnlocked,
where: celo_unlocked.account_address == ^address and celo_unlocked.amount == ^amount
)

Repo.delete_all(query)
end

@doc """
Insert unlocked CELO when passed the address, the amount and when the amount will be available as a unix timestamp
"""
@spec insert_celo_unlocked(Hash.t(), non_neg_integer(), non_neg_integer()) :: {integer(), nil | [term()]}
def insert_celo_unlocked(address, amount, available) do
changeset =
CeloUnlocked.changeset(%CeloUnlocked{}, %{
account_address: address,
amount: amount,
available: DateTime.from_unix!(available, :second)
})

Repo.insert(changeset)
end

@spec get_token_icon_url_by(String.t(), String.t()) :: String.t() | nil
def get_token_icon_url_by(chain_id, address_hash) do
chain_name =
Expand Down
4 changes: 2 additions & 2 deletions apps/explorer/lib/explorer/chain/address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ defmodule Explorer.Chain.Address do
CeloAccount,
CeloClaims,
CeloSigners,
CeloUnlocked,
CeloValidator,
CeloValidatorGroup,
CeloVoters,
Data,
DecompiledSmartContract,
Hash,
InternalTransaction,
PendingCelo,
ProxyContract,
SmartContract,
SmartContractAdditionalSource,
Expand Down Expand Up @@ -128,7 +128,7 @@ defmodule Explorer.Chain.Address do
has_many(:celo_voters, CeloVoters, foreign_key: :group_address_hash)
has_many(:celo_voted, CeloVoters, foreign_key: :voter_address_hash)
has_many(:celo_claims, CeloClaims, foreign_key: :address)
has_many(:pending_celo, PendingCelo, foreign_key: :account_address)
has_many(:celo_unlocked, CeloUnlocked, foreign_key: :account_address)

has_one(:implementation_contract, ProxyContract, foreign_key: :proxy_address)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Explorer.Chain.PendingCelo do
defmodule Explorer.Chain.CeloUnlocked do
@moduledoc """
Table for storing unlocked CELO that has not been withdrawn yet.
"""
Expand All @@ -15,24 +15,22 @@ defmodule Explorer.Chain.PendingCelo do
"""

@type t :: %__MODULE__{
address: Hash.Address.t(),
timestamp: DateTime.t(),
amount: Wei.t(),
index: non_neg_integer()
account_address: Hash.Address.t(),
available: DateTime.t(),
amount: Wei.t()
}

@attrs ~w(
account_address timestamp amount index
available amount
)a

@required_attrs ~w(
address
account_address
)a

@primary_key false
schema "pending_celo" do
field(:timestamp, :utc_datetime_usec)
field(:index, :integer, primary_key: true)
schema "celo_unlocked" do
field(:available, :utc_datetime_usec)
field(:amount, Wei)

belongs_to(
Expand All @@ -46,10 +44,14 @@ defmodule Explorer.Chain.PendingCelo do
timestamps(null: false, type: :utc_datetime_usec)
end

def changeset(%__MODULE__{} = pending_celo, attrs) do
pending_celo
|> cast(attrs, @attrs)
def changeset(%__MODULE__{} = celo_unlocked, %{address: a} = attrs) do
attrs = attrs |> Map.delete(:address) |> Map.put(:account_address, a)
changeset(celo_unlocked, attrs)
end

def changeset(%__MODULE__{} = celo_unlocked, attrs) do
celo_unlocked
|> cast(attrs, @attrs ++ @required_attrs)
|> validate_required(@required_attrs)
|> unique_constraint(:pending_celo_key, name: :pending_celo_account_address_index_index)
end
end
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
defmodule Explorer.Chain.Import.Runner.PendingCelo do
defmodule Explorer.Chain.Import.Runner.CeloUnlocked do
@moduledoc """
Bulk imports pending Celo to the DB table.
"""

require Ecto.Query

alias Ecto.{Changeset, Multi, Repo}
alias Explorer.Chain.{Import, PendingCelo}
alias Explorer.Chain.{CeloUnlocked, Import}
alias Explorer.Chain.Import.Runner.Util

import Ecto.Query, only: [from: 2]
Expand All @@ -16,13 +16,13 @@ defmodule Explorer.Chain.Import.Runner.PendingCelo do
# milliseconds
@timeout 60_000

@type imported :: [PendingCelo.t()]
@type imported :: [CeloUnlocked.t()]

@impl Import.Runner
def ecto_schema_module, do: PendingCelo
def ecto_schema_module, do: CeloUnlocked

@impl Import.Runner
def option_key, do: :pending_celo
def option_key, do: :celo_unlocked

@impl Import.Runner
def imported_table_row do
Expand All @@ -45,40 +45,22 @@ defmodule Explorer.Chain.Import.Runner.PendingCelo do
@impl Import.Runner
def timeout, do: @timeout

@spec insert(Repo.t(), [map()], Util.insert_options()) :: {:ok, [PendingCelo.t()]} | {:error, [Changeset.t()]}
@spec insert(Repo.t(), [map()], Util.insert_options()) :: {:ok, [CeloUnlocked.t()]} | {:error, [Changeset.t()]}
defp insert(repo, changes_list, %{timeout: timeout, timestamps: timestamps} = options) when is_list(changes_list) do
on_conflict = Map.get_lazy(options, :on_conflict, &default_on_conflict/0)

# Enforce ShareLocks order (see docs: sharelocks.md)
uniq_changes_list =
changes_list
|> Enum.sort_by(&{&1.address, &1.index})
|> Enum.uniq_by(&{&1.address, &1.index})
|> Enum.sort_by(&{&1.account_address})
|> Enum.uniq_by(&{&1.account_address})

{:ok, _} =
Import.insert_changes_list(
repo,
uniq_changes_list,
conflict_target: [:address, :index],
on_conflict: on_conflict,
for: PendingCelo,
for: CeloUnlocked,
returning: true,
timeout: timeout,
timestamps: timestamps
)
end

defp default_on_conflict do
from(
account in PendingCelo,
update: [
set: [
timestamp: fragment("EXCLUDED.timestamp"),
amount: fragment("EXCLUDED.amount"),
inserted_at: fragment("LEAST(?, EXCLUDED.inserted_at)", account.inserted_at),
updated_at: fragment("GREATEST(?, EXCLUDED.updated_at)", account.updated_at)
]
]
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Explorer.Chain.Import.Stage.AddressReferencing do
Runner.CeloValidatorStatus,
Runner.CeloEpochRewards,
Runner.CeloVoters,
Runner.PendingCelo,
Runner.CeloUnlocked,
Runner.CeloWallets,
Runner.ExchangeRate,
Runner.StakingPools,
Expand Down
1 change: 0 additions & 1 deletion apps/explorer/lib/mix/tasks/core_contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule Mix.Tasks.CoreContracts do

url
|> full_cache_build()
|> IO.inspect()
end

def full_cache_build(url) do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Explorer.Repo.Migrations.RenamePendingCeloToCeloUnlocked do
use Ecto.Migration

def change do
rename(table(:pending_celo), to: table(:celo_unlocked))
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Explorer.Repo.Migrations.UpdateCeloUnlocked do
use Ecto.Migration

def change do
drop(index(:celo_withdrawal, [:account_address, :index], unique: true))
rename(table("celo_unlocked"), :timestamp, to: :available)

alter table(:celo_unlocked) do
remove(:index)
end
end
end
Loading

0 comments on commit bd6a537

Please sign in to comment.