-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose names locked/burned fees on stats (#822)
- Loading branch information
Showing
7 changed files
with
186 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
priv/migrations/20220803120000_add_name_fees_to_stats.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule AeMdw.Migrations.AddNameFeesToStats do | ||
@moduledoc """ | ||
Add auctions_burned and auctions_locked attribute to delta and total stats. | ||
""" | ||
|
||
alias AeMdw.Collection | ||
alias AeMdw.Db.Model | ||
alias AeMdw.Db.State | ||
alias AeMdw.Db.WriteMutation | ||
alias AeMdw.Util | ||
|
||
require Model | ||
|
||
@spec run(boolean()) :: {:ok, {non_neg_integer(), non_neg_integer()}} | ||
def run(_from_start?) do | ||
begin = DateTime.utc_now() | ||
state = State.new() | ||
|
||
{mutations, _last_burned_locked} = | ||
state | ||
|> Collection.stream(Model.DeltaStat, nil) | ||
|> Stream.map(fn height -> | ||
{State.fetch!(state, Model.DeltaStat, height), | ||
State.fetch!(state, Model.TotalStat, height + 1)} | ||
end) | ||
|> Enum.flat_map_reduce({0, 0}, fn {old_delta_stat, old_total_stat}, | ||
{prev_burned, prev_locked} -> | ||
delta_stat = Model.delta_stat(index: height) = transform_delta_stat(old_delta_stat) | ||
total_stat = transform_total_stat(old_total_stat) | ||
|
||
delta_burned = height_int_amount(state, height, :lock_name) | ||
delta_spend = height_int_amount(state, height, :spend_name) | ||
delta_refund = height_int_amount(state, height, :refund_name) | ||
delta_locked = delta_spend - delta_refund | ||
new_total_burned = prev_burned + delta_burned | ||
new_total_locked = prev_locked + delta_locked | ||
|
||
new_delta_stat = | ||
Model.delta_stat(delta_stat, | ||
burned_in_auctions: delta_burned, | ||
locked_in_auctions: delta_locked | ||
) | ||
|
||
new_total_stat = | ||
Model.total_stat(total_stat, | ||
burned_in_auctions: new_total_burned, | ||
locked_in_auctions: new_total_locked | ||
) | ||
|
||
{ | ||
[ | ||
WriteMutation.new(Model.DeltaStat, new_delta_stat), | ||
WriteMutation.new(Model.TotalStat, new_total_stat) | ||
], | ||
{new_total_burned, new_total_locked} | ||
} | ||
end) | ||
|
||
State.commit(state, mutations) | ||
|
||
duration = DateTime.diff(DateTime.utc_now(), begin) | ||
|
||
{:ok, {div(length(mutations), 2), duration}} | ||
end | ||
|
||
defp transform_delta_stat( | ||
{:delta_stat, height, auctions_started, names_activated, names_expired, names_revoked, | ||
oracles_registered, oracles_expired, contracts_created, block_reward, dev_reward} | ||
) do | ||
Model.delta_stat( | ||
index: height, | ||
auctions_started: auctions_started, | ||
names_activated: names_activated, | ||
names_expired: names_expired, | ||
names_revoked: names_revoked, | ||
oracles_registered: oracles_registered, | ||
oracles_expired: oracles_expired, | ||
contracts_created: contracts_created, | ||
block_reward: block_reward, | ||
dev_reward: dev_reward | ||
) | ||
end | ||
|
||
defp transform_total_stat( | ||
{:total_stat, height, block_reward, dev_reward, total_supply, active_auctions, | ||
active_names, inactive_names, active_oracles, inactive_oracles, contracts} | ||
) do | ||
Model.total_stat( | ||
index: height, | ||
block_reward: block_reward, | ||
dev_reward: dev_reward, | ||
total_supply: total_supply, | ||
active_auctions: active_auctions, | ||
active_names: active_names, | ||
inactive_names: inactive_names, | ||
active_oracles: active_oracles, | ||
inactive_oracles: inactive_oracles, | ||
contracts: contracts | ||
) | ||
end | ||
|
||
defp height_int_amount(state, height, kind) do | ||
kind_str = "fee_#{kind}" | ||
|
||
state | ||
|> Collection.stream(Model.KindIntTransferTx, {kind_str, {height, -1}, Util.min_bin(), nil}) | ||
|> Stream.take_while(&match?({^kind_str, {^height, _mbi}, _address, _ref_txi}, &1)) | ||
|> Stream.map(fn {kind_str, block_index, address, ref_txi} -> | ||
{block_index, kind_str, address, ref_txi} | ||
end) | ||
|> Stream.map(&State.fetch!(state, Model.IntTransferTx, &1)) | ||
|> Enum.reduce(0, fn Model.int_transfer_tx(amount: amount), amount_acc -> | ||
amount_acc + amount | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters