Skip to content

Commit

Permalink
add max increase of 7d
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminzhe committed Sep 20, 2023
1 parent 577bc12 commit 732fb7b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ task :update_nominees, [:network_name] do |_t, args|
name: identities[nominee_address],
power: nominee_powers[nominee_address],
commission: collator_commissions[nominee_address],
commission_updates_count: commission_updates_count[nominee_address] || 0,
commission_updates_count: commission_updates_count[nominee_address]&.[](:count) || 0,
commission_max_increase: commission_updates_count[nominee_address]&.[](:max_increase),
status: get_nominee_status(active_collator_addresses, waiting_collator_addresses, nominee_address)
}
]
Expand Down
53 changes: 51 additions & 2 deletions src/subsquid/commission_updates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,61 @@ def query(client, timestamp_gt = "2023-07-07T00:00:00.000000Z", id_gt = '0', try
query(client, timestamp_gt, id_gt, try)
end

# [{
# block_timestamp: '2023-09-13 12:00:00',
# commission: 0.1
# },
# ...
# ]
def commission_increase_degree(data)
return 0 if data.empty? || data.size == 1

# 将commission数据按照时间进行排序
sorted_data = data.sort_by { |entry| entry[:block_timestamp] }

max_increase = 0.0
previous_commission = sorted_data.first[:commission]

sorted_data.each do |entry|
current_commission = entry[:commission]

# 如果当前的commission大于之前的commission,计算增加的程度
if current_commission > previous_commission
increase = current_commission - previous_commission
max_increase = [max_increase, increase].max
end

previous_commission = current_commission
end

max_increase
end

# [
# {
# count: 123,
# max_increase: 0.1
# },
# ...
# ]
def commission_updates_count(client)
# get the timestamp of a week ago
timestamp_gt = (Time.now - 7 * 24 * 60 * 60).strftime("%Y-%m-%dT%H:%M:%S.000000Z")
updates = query(client, timestamp_gt)
updates.group_by { |update| update["args"]["who"] }.transform_values do |updates|
updates.length
# {
# address: [{}, {}, ...]
# }
updates_by_address = updates.group_by { |update| update["args"]["who"] }

updates_by_address.transform_values do |updates|
updates2 =
updates.map do |update|
{
block_timestamp: update["block"]["timestamp"],
commission: update["args"]["commission"].to_f
}
end
{ count: updates.length, max_increase: commission_increase_degree(updates2) / 10_000_000 }
end
end
end
Expand Down

0 comments on commit 732fb7b

Please sign in to comment.