Skip to content

Commit

Permalink
add the darwinia & crab nominees' power api
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Aug 21, 2023
1 parent e744d14 commit 2a8d6ff
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 74 deletions.
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
FROM ruby:3.1.2

# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

# Get Rust
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

gem 'scale_rb', '0.1.9'
# gem "scale_rb", path: "/Users/wuminzhe/Projects/wuminzhe/scale_rb"
gem 'scale_rb', '0.1.10'
# gem 'scale_rb', path: '/workspaces/scale_rb'
gem 'sinatra'

gem 'async', '~> 2.5'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ GEM
rubyzip (~> 2.3)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
scale_rb (0.1.9)
scale_rb (0.1.10)
base58
blake2b_rs (~> 0.1.4)
xxhash
Expand Down Expand Up @@ -119,7 +119,7 @@ DEPENDENCIES
lightly
mongo (~> 2)
puma
scale_rb (= 0.1.9)
scale_rb (= 0.1.10)
sinatra

BUNDLED WITH
Expand Down
119 changes: 104 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
require "scale_rb"
require "./src/utils"
require "./src/supplies"
require "./src/metadata"
require "./src/track-goerli"
require "./src/track-pangolin"
require "./config/config.rb"
require 'scale_rb'
require './src/utils'
require './src/supplies'
require './src/metadata'
require './src/track-goerli'
require './src/track-pangolin'
require './src/storage'
require './src/account'
require './config/config'
config = get_config

task default: %w[gen_supplies_data_loop]

task :update_metadata_loop do
loop_do do
Rake::Task["update_darwinia_metadata"].invoke
Rake::Task["update_crab_metadata"].invoke
Rake::Task["update_pangolin_metadata"].invoke
Rake::Task['update_darwinia_metadata'].reenable
Rake::Task['update_darwinia_metadata'].invoke
Rake::Task['update_crab_metadata'].reenable
Rake::Task['update_crab_metadata'].invoke
Rake::Task['update_pangolin_metadata'].reenable
Rake::Task['update_pangolin_metadata'].invoke
end
end

task :gen_supplies_data_loop do
loop_do { Rake::Task["gen_darwinia_supplies_data"].invoke }
loop_do do
Rake::Task['gen_darwinia_supplies_data'].reenable
Rake::Task['gen_darwinia_supplies_data'].invoke
end
end

task :update_nominees_loop do
loop_do(60 * 2) do
Rake::Task['update_nominees'].reenable
Rake::Task['update_nominees'].invoke('darwinia')
Rake::Task['update_nominees'].reenable
Rake::Task['update_nominees'].invoke('crab')
end
end

##########################################
Expand All @@ -28,13 +45,13 @@ task :gen_darwinia_supplies_data do
ethereum_rpc = config[:ethereum_rpc]
darwinia_rpc = config[:darwinia_rpc]
darwinia_metadata = JSON.parse(File.read(config[:metadata][:darwinia]))
generate_supplies("darwinia", ethereum_rpc, darwinia_rpc, darwinia_metadata)
generate_supplies('darwinia', ethereum_rpc, darwinia_rpc, darwinia_metadata)
end

task :update_darwinia_metadata do
darwinia_rpc = config[:darwinia_rpc]
darwinia_metadata_path = config[:metadata][:darwinia]
update_metadata("darwinia", darwinia_rpc, darwinia_metadata_path)
update_metadata('darwinia', darwinia_rpc, darwinia_metadata_path)
end

##########################################
Expand All @@ -43,7 +60,7 @@ end
task :update_crab_metadata do
crab_rpc = config[:crab_rpc]
crab_metadata_path = config[:metadata][:crab]
update_metadata("crab", crab_rpc, crab_metadata_path)
update_metadata('crab', crab_rpc, crab_metadata_path)
end

##########################################
Expand All @@ -52,7 +69,7 @@ end
task :update_pangolin_metadata do
pangolin_rpc = config[:pangolin_rpc]
pangolin_metadata_path = config[:metadata][:pangolin]
update_metadata("pangolin", pangolin_rpc, pangolin_metadata_path)
update_metadata('pangolin', pangolin_rpc, pangolin_metadata_path)
end

##########################################
Expand All @@ -65,3 +82,75 @@ end
task :update_pangolin_goerli_messages do
track_pangolin
end

require 'logger'
task :update_nominees, [:network_name] do |_t, args|
logger = Logger.new($stdout)
logger.level = Logger::DEBUG

network_name = args[:network_name]
logger.debug "updating #{network_name} nominees..."

rpc = config["#{network_name}_rpc".to_sym]
metadata = JSON.parse(File.read(config[:metadata][network_name.to_sym]))

ring_pool = get_storage(rpc, metadata, 'darwinia_staking', 'ring_pool', nil, nil)
kton_pool = get_storage(rpc, metadata, 'darwinia_staking', 'kton_pool', nil, nil)

# 1. Get all nominators with their nominees
# ---------------------------------------
# { nominator: nominee }
nominators =
get_storage(
rpc,
metadata,
'darwinia_staking',
'nominators',
nil,
nil
)

nominator_nominee_mapping =
nominators.map do |item|
nominator_address = "0x#{item[:storage_key][-40..]}"
nominee_address = item[:storage].to_hex
[nominator_address, nominee_address]
end.to_h

# 2. Get all nominators' staking info
# ---------------------------------------
# {
# "nominator" => {:staked_ring=>10000000000000000000, :staked_kton=>0},
# }
nominator_addresses = nominator_nominee_mapping.keys
nominator_staking_infos = get_accounts_staking_info(rpc, metadata, nominator_addresses)

# 3. Sum up nominators' staking info of each nominee
# ---------------------------------------
# {
# "nominee" => {:staked_ring=>total, :staked_kton=>total},
# }
result =
nominator_addresses.each_with_object({}) do |nominator_address, acc|
nominator_staking_info = nominator_staking_infos[nominator_address]
nominee_address = nominator_nominee_mapping[nominator_address]
acc[nominee_address] =
{
staked_ring: (acc[nominee_address]&.[](:staked_ring) || 0) + nominator_staking_info[:staked_ring],
staked_kton: (acc[nominee_address]&.[](:staked_kton) || 0) + nominator_staking_info[:staked_kton]
}
end

# 4. Calculate power of each nominee
# ---------------------------------------
nominee_powers = result.map do |nominee_address, staking_info|
power = calc_power(staking_info[:staked_ring], staking_info[:staked_kton], ring_pool, kton_pool)
[nominee_address, power]
end.to_h
logger.debug JSON.pretty_generate(nominee_powers)

# 5. write to file
# ---------------------------------------
logger.debug "writing #{network_name} nominees to file..."
write_data_to_file(nominee_powers, "#{network_name}-nominees.json")
end
11 changes: 4 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ services:
command: rake gen_supplies_data_loop
volumes:
- ./data:/usr/src/app/data
update_goerli_pangolin_messages:
update_nominees:
build: .
env_file:
- .env
command: rake update_goerli_pangolin_messages
update_pangolin_goerli_messages:
build: .
env_file:
- .env
command: rake update_pangolin_goerli_messages
command: rake update_nominees_loop
volumes:
- ./data:/usr/src/app/data
server:
build: .
env_file:
Expand Down
Loading

0 comments on commit 2a8d6ff

Please sign in to comment.