Add bridged tokens functionality, could be enabled by compile time en… (#9169)
* Add bridged tokens functionality, could be enabled by compile time env var * Process reviewer's comment * Fix credo * Process review comments * Reset GA cache * Fix warningpull/9189/head
parent
464054e6ad
commit
a47d83f0be
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,29 @@ |
||||
defmodule Explorer.Repo.BridgedTokens.Migrations.AddBridgedTokens do |
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
alter table(:tokens) do |
||||
add(:bridged, :boolean, null: true) |
||||
end |
||||
|
||||
create table(:bridged_tokens, primary_key: false) do |
||||
add(:foreign_chain_id, :numeric, null: false) |
||||
add(:foreign_token_contract_address_hash, :bytea, null: false) |
||||
add(:exchange_rate, :decimal) |
||||
add(:custom_metadata, :string, null: true) |
||||
add(:lp_token, :boolean, null: true) |
||||
add(:custom_cap, :decimal, null: true) |
||||
add(:type, :string, null: true) |
||||
|
||||
add( |
||||
:home_token_contract_address_hash, |
||||
references(:tokens, column: :contract_address_hash, on_delete: :delete_all, type: :bytea), |
||||
null: false |
||||
) |
||||
|
||||
timestamps() |
||||
end |
||||
|
||||
create(unique_index(:bridged_tokens, :home_token_contract_address_hash)) |
||||
end |
||||
end |
@ -0,0 +1,52 @@ |
||||
defmodule Indexer.BridgedTokens.CalcLpTokensTotalLiquidity do |
||||
@moduledoc """ |
||||
Periodically updates LP tokens total liquidity |
||||
""" |
||||
|
||||
use GenServer |
||||
|
||||
require Logger |
||||
|
||||
alias Explorer.Chain.BridgedToken |
||||
|
||||
@interval :timer.minutes(20) |
||||
|
||||
def start_link([init_opts, gen_server_opts]) do |
||||
start_link(init_opts, gen_server_opts) |
||||
end |
||||
|
||||
def start_link(init_opts, gen_server_opts) do |
||||
GenServer.start_link(__MODULE__, init_opts, gen_server_opts) |
||||
end |
||||
|
||||
@impl GenServer |
||||
def init(opts) do |
||||
interval = opts[:interval] || @interval |
||||
|
||||
Process.send_after(self(), :calc_total_liquidity, interval) |
||||
|
||||
{:ok, %{interval: interval}} |
||||
end |
||||
|
||||
@impl GenServer |
||||
def handle_info(:calc_total_liquidity, %{interval: interval} = state) do |
||||
Logger.debug(fn -> "Calc LP tokens total liquidity" end) |
||||
|
||||
calc_total_liquidity() |
||||
|
||||
Process.send_after(self(), :calc_total_liquidity, interval) |
||||
|
||||
{:noreply, state} |
||||
end |
||||
|
||||
# don't handle other messages (e.g. :ssl_closed) |
||||
def handle_info(_, state) do |
||||
{:noreply, state} |
||||
end |
||||
|
||||
defp calc_total_liquidity do |
||||
BridgedToken.calc_lp_tokens_total_liquidity() |
||||
|
||||
Logger.debug(fn -> "Total liquidity fetched for LP tokens" end) |
||||
end |
||||
end |
@ -0,0 +1,44 @@ |
||||
defmodule Indexer.BridgedTokens.SetAmbBridgedMetadataForTokens do |
||||
@moduledoc """ |
||||
Sets token metadata for bridged tokens from AMB extensions. |
||||
""" |
||||
|
||||
use GenServer |
||||
|
||||
require Logger |
||||
|
||||
alias Explorer.Chain.BridgedToken |
||||
|
||||
def start_link([init_opts, gen_server_opts]) do |
||||
start_link(init_opts, gen_server_opts) |
||||
end |
||||
|
||||
def start_link(init_opts, gen_server_opts) do |
||||
GenServer.start_link(__MODULE__, init_opts, gen_server_opts) |
||||
end |
||||
|
||||
@impl GenServer |
||||
def init(_opts) do |
||||
send(self(), :process_amb_tokens) |
||||
|
||||
{:ok, %{}} |
||||
end |
||||
|
||||
@impl GenServer |
||||
def handle_info(:process_amb_tokens, state) do |
||||
fetch_amb_bridged_tokens_metadata() |
||||
|
||||
{:noreply, state} |
||||
end |
||||
|
||||
# don't handle other messages (e.g. :ssl_closed) |
||||
def handle_info(_, state) do |
||||
{:noreply, state} |
||||
end |
||||
|
||||
defp fetch_amb_bridged_tokens_metadata do |
||||
:ok = BridgedToken.process_amb_tokens() |
||||
|
||||
Logger.debug(fn -> "Bridged status fetched for AMB tokens" end) |
||||
end |
||||
end |
@ -0,0 +1,54 @@ |
||||
defmodule Indexer.BridgedTokens.SetOmniBridgedMetadataForTokens do |
||||
@moduledoc """ |
||||
Periodically checks unprocessed tokens and sets bridged status. |
||||
""" |
||||
|
||||
use GenServer |
||||
|
||||
require Logger |
||||
|
||||
alias Explorer.Chain.BridgedToken |
||||
|
||||
@interval :timer.minutes(20) |
||||
|
||||
def start_link([init_opts, gen_server_opts]) do |
||||
start_link(init_opts, gen_server_opts) |
||||
end |
||||
|
||||
def start_link(init_opts, gen_server_opts) do |
||||
GenServer.start_link(__MODULE__, init_opts, gen_server_opts) |
||||
end |
||||
|
||||
@impl GenServer |
||||
def init(opts) do |
||||
interval = opts[:interval] || @interval |
||||
|
||||
send(self(), :reveal_unprocessed_tokens) |
||||
|
||||
{:ok, %{interval: interval}} |
||||
end |
||||
|
||||
@impl GenServer |
||||
def handle_info(:reveal_unprocessed_tokens, %{interval: interval} = state) do |
||||
Logger.debug(fn -> "Reveal unprocessed tokens" end) |
||||
|
||||
{:ok, token_addresses} = BridgedToken.unprocessed_token_addresses_to_reveal_bridged_tokens() |
||||
|
||||
fetch_omni_bridged_tokens_metadata(token_addresses) |
||||
|
||||
Process.send_after(self(), :reveal_unprocessed_tokens, interval) |
||||
|
||||
{:noreply, state} |
||||
end |
||||
|
||||
# don't handle other messages (e.g. :ssl_closed) |
||||
def handle_info(_, state) do |
||||
{:noreply, state} |
||||
end |
||||
|
||||
defp fetch_omni_bridged_tokens_metadata(token_addresses) do |
||||
:ok = BridgedToken.fetch_omni_bridged_tokens_metadata(token_addresses) |
||||
|
||||
Logger.debug(fn -> "Bridged status fetched for tokens" end) |
||||
end |
||||
end |
Loading…
Reference in new issue