Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to PDC subgraph #86

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Set up Python environment
uses: actions/setup-python@v1
with:
python-version: "3.8"
python-version: "3.9"
- name: flake8 Lint
uses: py-actions/flake8@v1
with:
Expand Down
2 changes: 0 additions & 2 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@

if os.environ.get('ENV') == 'production':
KLIMA_PROTOCOL_SUBGRAPH = GRAPH_BASE_URL + PROTOCOL_SUBGRAPH_ID
KLIMA_CARBON_SUBGRAPH = GRAPH_BASE_URL + CARBON_SUBGRAPH_ID
KLIMA_BONDS_SUBGRAPH = GRAPH_BASE_URL + BONDS_SUBGRAPH_ID
POLYGON_DIGITAL_CARBON_SUBGRAPH = GRAPH_BASE_URL + POLYGON_DIGITAL_CARBON_ID
else:
KLIMA_PROTOCOL_SUBGRAPH = GRAPH_DEV_BASE_URL + 'staging-klimadao-protocol-metrics' + GRAPH_VERSION_SUFFIX
KLIMA_CARBON_SUBGRAPH = GRAPH_DEV_BASE_URL + 'staging-polygon-bridged-carbon' + GRAPH_VERSION_SUFFIX
KLIMA_BONDS_SUBGRAPH = GRAPH_DEV_BASE_URL + 'staging-klimadao-bonds' + GRAPH_VERSION_SUFFIX
POLYGON_DIGITAL_CARBON_SUBGRAPH = GRAPH_DEV_BASE_URL + 'staging-polygon-digital-carbon' + GRAPH_VERSION_SUFFIX
65 changes: 41 additions & 24 deletions src/retirement_fee_info/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os
from discord.ext import tasks
from web3 import Web3

from subgrounds.subgrounds import Subgrounds

from ..time_utils import get_days_ago_timestamp

from ..constants import KLIMA_CARBON_SUBGRAPH
from ..constants import POLYGON_DIGITAL_CARBON_SUBGRAPH, \
BCT_ADDRESS, UBO_ADDRESS, NBO_ADDRESS, MCO2_ADDRESS, NCT_ADDRESS

from ..utils import get_discord_client, \
update_nickname, update_presence, \
prettify_number
Expand All @@ -17,59 +20,71 @@

sg = Subgrounds()

offsets = ["BCT", "MCO2", "UBO", "NBO", "NCT"]
pool_names = ["BCT", "MCO2", "UBO", "NBO", "NCT"]
pool_addresses = {
'BCT': BCT_ADDRESS,
'NBO': NBO_ADDRESS,
'UBO': UBO_ADDRESS,
'NCT': NCT_ADDRESS,
'MCO2': MCO2_ADDRESS
}
counter = 0


def get_info():
global counter
currentOffset = offsets[counter]
current_pool = pool_names[counter]

ts = get_days_ago_timestamp(7)
w_offset_amount = get_retirement_fees(sg,
ts,
currentOffset)
w_retired_amount = get_retirement_fees(sg, ts, current_pool)

counter += 1
if counter == len(offsets):
if counter == len(pool_names):
counter = 0

return w_offset_amount, currentOffset
return w_retired_amount, current_pool


def get_retirement_fees(sg, timestamp, offset):
def get_retirement_fees(sg, timestamp, pool):
'''
param `sg`: Initialized subgrounds object
param `timestamp`: Timestamp used for cutoff date (data created after
the provided date will be fetched)
param `offset`: Specific Offset for which amount and fee will be retrieved
param `pool`: Specific pool for which amount and fee will be retrieved

returns:
`w_offset_amount`: Total amount provided from retirements
`w_pool_amount`: Total amount provided from retirements
'''

try:
kbm = sg.load_subgraph(KLIMA_CARBON_SUBGRAPH)
pdc = sg.load_subgraph(POLYGON_DIGITAL_CARBON_SUBGRAPH)

retirement_df = kbm.Query.dailyKlimaRetirements(
where=[kbm.DailyKlimaRetirement.timestamp > timestamp]
retirement_df = pdc.Query.dailyKlimaRetireSnapshots(
where=[pdc.Query.dailyKlimaRetireSnapshots.timestamp > timestamp]
)

retirement_df = sg.query_df(
[retirement_df.amount,
retirement_df.feeAmount,
retirement_df.token])
retirement_df.pool])

if retirement_df.size == 0:
return 0, 0, 0

w_offset_amount = retirement_df.loc[
retirement_df['dailyKlimaRetirements_token'] == offset,
'dailyKlimaRetirements_amount'].sum()
retirement_df['dailyKlimaRetireSnapshots_pool'] = retirement_df.dailyKlimaRetireSnapshots_pool.apply(
lambda x: Web3.to_checksum_address(x)
)

w_pool_amount = retirement_df.loc[
retirement_df['dailyKlimaRetireSnapshots_pool'] == pool_addresses[pool],
'dailyKlimaRetireSnapshots_amount'
].sum()

return w_offset_amount
return w_pool_amount

except Exception:
# TODO: clean up this bad practice and add better monitoring for bots going down
except Exception as e:
print(e)
return None


Expand All @@ -83,12 +98,14 @@ async def on_ready():
# Loop time set to 2mins
@tasks.loop(seconds=120)
async def update_info():
offset_amount, offset = get_info()
pool_amount, pool = get_info()

print(pool_amount, pool)

if offset_amount and offset is not None:
if pool_amount and pool is not None:

offset_text = f'Retired {offset}: {prettify_number(offset_amount)}t'
success = await update_nickname(client, offset_text)
pool_text = f'Retired {pool}: {prettify_number(pool_amount)}t'
success = await update_nickname(client, pool_text)
if not success:
return

Expand Down