Skip to content

Commit

Permalink
fix: update apy time with average commission rate for iiss4
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Mar 23, 2024
1 parent db96ddd commit a7dbf37
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
43 changes: 43 additions & 0 deletions icon_governance/alembic/versions/345e65377c3d_v0_7_1_apys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""v0.7.1-apys
Revision ID: 345e65377c3d
Revises: 2945fc0e4d84
Create Date: 2024-03-23 15:33:47.765666
"""
from alembic import op
import sqlalchemy as sa
import sqlmodel
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '345e65377c3d'
down_revision = '2945fc0e4d84'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('apy_time', 'total_wage',
existing_type=postgresql.DOUBLE_PRECISION(precision=53),
nullable=False)
op.alter_column('apy_time', 'active_preps',
existing_type=sa.INTEGER(),
nullable=False)
op.drop_column('apy_time', 'cps_apy')
op.drop_column('apy_time', 'relay_apy')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('apy_time', sa.Column('relay_apy', postgresql.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=False))
op.add_column('apy_time', sa.Column('cps_apy', postgresql.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=False))
op.alter_column('apy_time', 'active_preps',
existing_type=sa.INTEGER(),
nullable=True)
op.alter_column('apy_time', 'total_wage',
existing_type=postgresql.DOUBLE_PRECISION(precision=53),
nullable=True)
# ### end Alembic commands ###
2 changes: 0 additions & 2 deletions icon_governance/models/apy_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class ApyTime(SQLModel, table=True):

staking_apy: float = Field(nullable=False)
prep_apy: float = Field(nullable=False)
cps_apy: float = Field(nullable=False)
relay_apy: float = Field(nullable=False)

total_delegated: float = Field(nullable=False)
total_stake: float = Field(nullable=False)
Expand Down
47 changes: 30 additions & 17 deletions icon_governance/utils/apys.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Apys(BaseModel):

staking_apy: float
prep_apy: float
cps_apy: float
relay_apy: float
total_stake: float
total_delegated: float
total_bonded: float
total_power: float

total_wage: float
active_preps: int


def get_apys(height: int = None) -> Apys:
iiss_info = getIISSInfo(height=height)
Expand All @@ -36,26 +37,38 @@ def get_apys(height: int = None) -> Apys:
total_power = int(network_info["totalPower"], 0) / 10**18

i_global = int(iiss_info["variable"]["Iglobal"], 0) / 10**18
i_voter = None
i_wage = None
i_prep = int(iiss_info["variable"]["Iprep"], 0) / 100
i_cps = int(iiss_info["variable"]["Icps"], 0) / 100
i_relay = int(iiss_info["variable"]["Irelay"], 0) / 100

active_preps = len([i for i in get_preps_info['preps'] if i['grade'] in ['0x0', '0x1']])
# Proxy for transition to iiss 4.0 where rates are not multiplied by 100
if int(network_info["iissVersion"], 0) <= 3 or "Iwage" not in iiss_info["variable"]:
# iiss 3.0
i_prep = int(iiss_info["variable"]["Iprep"], 0) / 100
i_cps = int(iiss_info["variable"]["Icps"], 0) / 100
i_relay = int(iiss_info["variable"]["Irelay"], 0) / 100
i_wage = None
i_voter = int(iiss_info["variable"]["Ivoter"], 0) / 100

staking_apy = i_voter * i_global * 12 / (total_delegated + total_bonded)
total_wage = 0
prep_apy = i_prep * i_global * 12 / total_stake
else:
# iiss 4.0
i_prep = int(iiss_info["variable"]["Iprep"], 0) / 100 / 100
i_cps = int(iiss_info["variable"]["Icps"], 0) / 100 / 100
i_relay = int(iiss_info["variable"]["Irelay"], 0) / 100 / 100
i_wage = int(iiss_info["variable"]["Iwage"], 0) / 100 / 100

# https://forum.icon.community/t/changes-to-icon-economic-policy-iiss-4/3612
staking_apy = i_wage * i_global * 12 / (total_delegated + total_bonded)
# Take the sum of the product of each active validators power times the inverse
# of their commission (ie what each validator is paying out) and then divide
# that by total power. This is meant to approximate the i_voter which is in
# IISSv4 null - so now we are just trying get the weighted average of the
# commissions.
voter_percent = sum([
(1 - int(i['commissionRate'], 0) / 100 / 100) * int(i['power'], 0)
for i in get_preps_info['preps']
if i['grade'] in ['0x0', '0x1'] and 'commissionRate' in i
]) / 1e18 / total_power
i_voter = i_prep * voter_percent
total_wage = i_wage * i_global

prep_apy = (1 - voter_percent) * i_prep * i_global * 12 / total_stake

staking_apy = i_voter * i_global * 12 / (total_delegated + total_bonded)

apys = Apys(
i_global=i_global,
Expand All @@ -65,13 +78,13 @@ def get_apys(height: int = None) -> Apys:
i_relay=i_relay,
i_wage=i_wage,
staking_apy=staking_apy,
prep_apy=i_prep * i_global * 12 / total_stake,
cps_apy=i_cps * i_global * 12 / total_stake,
relay_apy=i_relay * i_global * 12 / total_stake,
prep_apy=prep_apy,
total_delegated=total_delegated,
total_stake=total_stake,
total_bonded=total_bonded,
total_power=total_power,
total_wage=total_wage,
active_preps=active_preps,
)

return apys
7 changes: 2 additions & 5 deletions tests/integration/utils/test_apys.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ def test_get_staking_apy():
("https://api.lisbon.icon.community/api/v3", 2000000, 27),
("https://api.berlin.icon.community/api/v3", 2000000, 55),
# iiss 4.0
("https://api.icon.community/api/v3", 75000000, 6),
("https://api.lisbon.icon.community/api/v3", 33000000, 24),
("https://api.berlin.icon.community/api/v3", 19000000, 0),
# Wait for rev 4
# ("https://api.icon.community/api/v3", 44000000, 7),
("https://api.berlin.icon.community/api/v3", 19000000, 2389),
# Broken
# ("https://api.berlin.icon.community/api/v3", None, 0),
# ("https://api.lisbon.icon.community/api/v3", None, 0),
# TODO: Update this for iiss 4
# ("https://api.icon.community/api/v3", 75000000, 6.4),
],
)
def test_get_staking_apy_height(url, height, expected_value, tmp_set_settings):
Expand Down

0 comments on commit a7dbf37

Please sign in to comment.