Remove chain based stakes snapshotting porcess

staking
Eduard Sachava 5 years ago committed by Victor Baranov
parent a555271420
commit 2dee6aa179
  1. 7
      apps/block_scout_web/assets/js/pages/stakes/become_candidate.js
  2. 12
      apps/explorer/lib/explorer/staking/contract_reader.ex
  3. 78
      apps/explorer/lib/explorer/staking/contract_state.ex
  4. 80
      apps/explorer/priv/contracts_abi/posdao/BlockRewardAuRa.json

@ -39,18 +39,13 @@ async function becomeCandidate ($modal, store, msg) {
lockModal($modal)
const stakingContract = store.getState().stakingContract
const blockRewardContract = store.getState().blockRewardContract
const decimals = store.getState().tokenDecimals
const stake = new BigNumber($modal.find('[candidate-stake]').val().replace(',', '.').trim()).shiftedBy(decimals).integerValue()
const miningAddress = $modal.find('[mining-address]').val().trim().toLowerCase()
try {
if (!await stakingContract.methods.areStakeAndWithdrawAllowed().call()) {
if (await blockRewardContract.methods.isSnapshotting().call()) {
openErrorModal('Error', 'Staking actions are temporarily restricted. Please try again in a few blocks.')
} else {
openErrorModal('Error', 'The current staking epoch is ending, and staking actions are temporarily restricted. Please try again when the new epoch starts.')
}
openErrorModal('Error', 'The current staking epoch is ending, and staking actions are temporarily restricted. Please try again when the new epoch starts.')
return false
}

@ -31,9 +31,7 @@ defmodule Explorer.Staking.ContractReader do
inactive_delegators: {:staking, "poolDelegatorsInactive", [staking_address]},
staked_amount: {:staking, "stakeAmountTotal", [staking_address]},
self_staked_amount: {:staking, "stakeAmount", [staking_address, staking_address]},
block_reward: {:block_reward, "validatorRewardPercent", [staking_address]},
stakers: {:block_reward, "snapshotStakers", [staking_address]},
reward_percents: {:block_reward, "snapshotRewardPercents", [staking_address]}
block_reward: {:block_reward, "validatorRewardPercent", [staking_address]}
]
end
@ -59,14 +57,6 @@ defmodule Explorer.Staking.ContractReader do
]
end
def pools_snapshot_requests(block_number) do
[staking_addresses: {:block_reward, "snapshotStakingAddresses", [], block_number}]
end
def stakers_snapshot_requests(pool_address, block_number) do
[{pool_address, {:block_reward, "snapshotStakers", [pool_address], block_number}}]
end
def perform_requests(requests, contracts, abi) do
requests
|> generate_requests(contracts)

@ -11,7 +11,6 @@ defmodule Explorer.Staking.ContractState do
alias Explorer.Chain.Events.{Publisher, Subscriber}
alias Explorer.SmartContract.Reader
alias Explorer.Staking.ContractReader
alias Explorer.Token.{BalanceReader, MetadataRetriever}
@table_name __MODULE__
@table_keys [
@ -158,11 +157,6 @@ defmodule Explorer.Staking.ContractState do
Enum.map(responses.inactive_delegators, &{pool_address, &1, false})
end)
delegator_rewards =
Enum.into(pool_staking_responses, %{}, fn {pool_address, responses} ->
{pool_address, Enum.into(Enum.zip(responses.stakers, responses.reward_percents), %{})}
end)
delegator_responses =
delegators
|> Enum.map(fn {pool_address, delegator_address, _} ->
@ -219,27 +213,10 @@ defmodule Explorer.Staking.ContractState do
delegator_entries =
Enum.map(delegator_responses, fn {{pool_address, delegator_address, is_active}, response} ->
staking_response = pool_staking_responses[pool_address]
reward_ratio =
if is_validator[staking_response.mining_address_hash] do
reward_ratio = delegator_rewards[pool_address][delegator_address]
if reward_ratio do
reward_ratio / 10_000
end
else
ratio(
response.stake_amount - response.ordered_withdraw,
staking_response.staked_amount - staking_response.self_staked_amount
) * min(0.7, 1 - staking_response.block_reward / 1_000_000)
end
Map.merge(response, %{
delegator_address_hash: delegator_address,
pool_address_hash: pool_address,
is_active: is_active,
reward_ratio: reward_ratio
is_active: is_active
})
end)
@ -250,64 +227,11 @@ defmodule Explorer.Staking.ContractState do
timeout: :infinity
})
if token && previous_epoch != global_responses.epoch_number do
update_tokens(token.contract_address_hash, contracts, abi, global_responses.epoch_start_block - 1, block_number)
end
Publisher.broadcast(:staking_update)
end
defp update_tokens(token_contract_address_hash, contracts, abi, last_epoch_block_number, block_number) do
now = DateTime.utc_now()
token_params =
token_contract_address_hash
|> MetadataRetriever.get_functions_of()
|> Map.merge(%{
contract_address_hash: token_contract_address_hash,
type: "ERC-20"
})
addresses =
block_number
|> ContractReader.pools_snapshot_requests()
|> ContractReader.perform_requests(contracts, abi)
|> Map.fetch!(:staking_addresses)
|> Enum.flat_map(&ContractReader.stakers_snapshot_requests(&1, last_epoch_block_number))
|> ContractReader.perform_requests(contracts, abi)
|> Map.values()
|> List.flatten()
|> Enum.uniq()
balance_params =
addresses
|> Enum.map(
&%{
token_contract_address_hash: token_contract_address_hash,
address_hash: &1,
block_number: block_number
}
)
|> BalanceReader.get_balances_of()
|> Enum.zip(addresses)
|> Enum.map(fn {{:ok, balance}, address} ->
%{
address_hash: address,
token_contract_address_hash: token_contract_address_hash,
block_number: block_number,
value: balance,
value_fetched_at: now
}
end)
{:ok, _} =
Chain.import(%{
addresses: %{params: Enum.map(addresses, &%{hash: &1}), on_conflict: :nothing},
address_current_token_balances: %{params: balance_params},
tokens: %{params: [token_params]}
})
end
defp get_token(address) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address),
{:ok, token} <- Chain.token_from_address_hash(address_hash) do

@ -139,20 +139,6 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "isSnapshotting",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
@ -223,20 +209,6 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "snapshotTotalStakeAmount",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
@ -530,58 +502,6 @@
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_validatorStakingAddress",
"type": "address"
}
],
"name": "snapshotRewardPercents",
"outputs": [
{
"name": "result",
"type": "uint256[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_validatorStakingAddress",
"type": "address"
}
],
"name": "snapshotStakers",
"outputs": [
{
"name": "result",
"type": "address[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "snapshotStakingAddresses",
"outputs": [
{
"name": "",
"type": "address[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [

Loading…
Cancel
Save