Skip to content

Commit

Permalink
add status to nominees api
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Aug 23, 2023
1 parent 31d9f38 commit 0ce33a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 14 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ end
# Multi networks
##########################################
require 'logger'
# nominee = active collators + waiting collators + other nominees who have nominations backing
task :update_nominees, [:network_name] do |_t, args|
logger = Logger.new($stdout)
logger.level = Logger::DEBUG
Expand All @@ -100,8 +101,8 @@ task :update_nominees, [:network_name] do |_t, args|

ring_pool = get_storage(rpc, metadata, 'darwinia_staking', 'ring_pool', nil, nil)
kton_pool = get_storage(rpc, metadata, 'darwinia_staking', 'kton_pool', nil, nil)
nominee_commissions = get_nominee_commissions(rpc, metadata)
collators = get_collators(rpc, metadata)
collator_commissions = get_collator_commissions(rpc, metadata) # includes active and waiting collators
active_collator_addresses = get_active_collators(rpc, metadata)

# 1. Get all nominators with their nominees
# ---------------------------------------
Expand Down Expand Up @@ -156,8 +157,17 @@ task :update_nominees, [:network_name] do |_t, args|

# 5. Set the collators committee
# ---------------------------------------
result = nominee_powers.keys.map do |key|
[key, { power: nominee_powers[key], commission: nominee_commissions[key], is_collator: collators.include?(key) }]
all_nominee_addresses = (nominee_powers.keys + collator_commissions.keys).uniq
waiting_collator_addresses = collator_commissions.keys - active_collator_addresses
result = all_nominee_addresses.map do |nominee_address|
[
nominee_address,
{
power: nominee_powers[nominee_address],
commission: collator_commissions[nominee_address],
status: get_nominee_status(active_collator_addresses, waiting_collator_addresses, nominee_address)
}
]
end.to_h
# logger.debug JSON.pretty_generate(result)

Expand Down
12 changes: 10 additions & 2 deletions src/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,29 @@ def get_nominee_power(rpc, metadata, address)
calc_power(total[:staked_ring], total[:staked_kton], ring_pool, kton_pool)
end

def get_nominee_commissions(rpc, metadata)
def get_collator_commissions(rpc, metadata)
storages = get_storage(rpc, metadata, 'darwinia_staking', 'collators', nil, nil)
storages.map do |storage|
address = "0x#{storage[:storage_key][-40..]}"
[address, storage[:storage] / 10_000_000]
end.to_h
end

def get_collators(rpc, metadata)
def get_active_collators(rpc, metadata)
storages = get_storage(rpc, metadata, 'darwinia_staking', 'exposures', nil, nil)
storages.map do |storage|
"0x#{storage[:storage_key][-40..]}"
end
end

def get_nominee_status(active_collator_addresses, waiting_collator_addresses, nominee_address)
if active_collator_addresses.include?(nominee_address)
'working'
else
waiting_collator_addresses.include?(nominee_address) ? 'waiting' : 'quit'
end
end

# require_relative '../config/config'
# config = get_config
# crab_metadata = JSON.parse(File.read(config[:metadata][:crab]))
Expand Down

0 comments on commit 0ce33a3

Please sign in to comment.