Skip to content

Commit

Permalink
Add DCM supply bot
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAurelius committed Feb 14, 2024
1 parent 86139dc commit ea063bf
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ jobs:
WEB3_PROVIDER_POLYGON_URL: ${{ secrets.WEB3_PROVIDER_POLYGON_URL }}
DISCORD_BOT_TOKEN_REBASE: ${{ secrets.DISCORD_BOT_TOKEN_REBASE }}
DISCORD_BOT_WEBHOOK_REBASE: ${{ secrets.DISCORD_BOT_WEBHOOK_REBASE }}
DISCORD_BOT_TOKEN_RUNWAY: ${{ secrets.DISCORD_BOT_TOKEN_RUNWAY }}
DISCORD_BOT_TOKEN_DAO_FEE: ${{ secrets.DISCORD_BOT_TOKEN_DAO_FEE }}
DISCORD_BOT_TOKEN_DAO_BALANCE: ${{ secrets.DISCORD_BOT_TOKEN_DAO_BALANCE }}
DISCORD_BOT_TOKEN_DCM_SUPPLY: ${{ secrets.DISCORD_BOT_TOKEN_DCM_SUPPLY }}
DISCORD_BOT_TOKEN_KLIMA_BOND_ALERTS: ${{ secrets.DISCORD_BOT_TOKEN_KLIMA_BOND_ALERTS }}
DISCORD_BOT_TOKEN_KLIMA_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_KLIMA_PRICE }}
DISCORD_BOT_TOKEN_BCT_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_BCT_PRICE }}
Expand Down
16 changes: 16 additions & 0 deletions k8s/dcm_supply/deployment_set_bot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: bot
spec:
template:
spec:
containers:
- name: bot
args: ["src.dcm_supply.main"]
env:
- name: DISCORD_BOT_TOKEN
valueFrom:
secretKeyRef:
name: discord-bots-secret
key: DISCORD_BOT_TOKEN_DCM_SUPPLY
13 changes: 13 additions & 0 deletions k8s/dcm_supply/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../base

namePrefix: dcm-supply-

commonLabels:
bot: dcm-supply

patchesStrategicMerge:
- deployment_set_bot.yaml
2 changes: 1 addition & 1 deletion k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ resources:
- ./treasury_carbon
- ./treasury_market
- ./ubo_price
- ./runway
- ./retirement_fee_info
- ./dao_fee
- ./dao_balance
- ./dcm_supply
- ./cco2_price

namespace: discord-bots
Expand Down
1 change: 1 addition & 0 deletions k8s/secret.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DISCORD_BOT_TOKEN_UBO_PRICE=${DISCORD_BOT_TOKEN_UBO_PRICE}
DISCORD_BOT_TOKEN_RETIREMENT_FEE_INFO=${DISCORD_BOT_TOKEN_RETIREMENT_FEE_INFO}
DISCORD_BOT_TOKEN_DAO_FEE=${DISCORD_BOT_TOKEN_DAO_FEE}
DISCORD_BOT_TOKEN_DAO_BALANCE=${DISCORD_BOT_TOKEN_DAO_BALANCE}
DISCORD_BOT_TOKEN_DCM_SUPPLY=${DISCORD_BOT_TOKEN_DCM_SUPPLY}
DISCORD_BOT_WEBHOOK_REBASE=${DISCORD_BOT_WEBHOOK_REBASE}
DISCORD_WEBHOOK_BROKEN_BOND_ALERT=${DISCORD_WEBHOOK_BROKEN_BOND_ALERT}

Expand Down
1 change: 1 addition & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@
KLIMA_PROTOCOL_SUBGRAPH = 'https://api.thegraph.com/subgraphs/name/klimadao/klimadao-protocol-metrics'
KLIMA_CARBON_SUBGRAPH = 'https://api.thegraph.com/subgraphs/name/klimadao/polygon-bridged-carbon'
KLIMA_BONDS_SUBGRAPH = 'https://api.thegraph.com/subgraphs/name/klimadao/klimadao-bonds'
POLYGON_DIGITAL_CARBON_SUBGRAPH = 'https://api.thegraph.com/subgraphs/name/klimadao/polygon-digital-carbon'
Empty file added src/dcm_supply/__init__.py
Empty file.
99 changes: 99 additions & 0 deletions src/dcm_supply/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import os
import json
import math

import requests
from discord.ext import tasks
from subgrounds.subgrounds import Subgrounds

from ..utils import (
get_discord_client,
update_nickname,
update_presence,
get_last_metric,
get_last_carbon,
prettify_number
)

BOT_TOKEN = os.environ["DISCORD_BOT_TOKEN"]

# Initialized Discord client
client = get_discord_client()

sg = Subgrounds()


def get_rebases_per_day():
"""
Calculates the average number of rebases per day based on the average
block production time for the previous 1 million blocks
"""
avg_block_secs = float(
json.loads(requests.get("https://klimadao.finance/api/block-rate").content)[
"blockRate30Day"
]
)

secs_per_rebase = 11520 * avg_block_secs

return 24 / (secs_per_rebase / 60 / 60)


def get_info():
last_metric = get_last_metric(sg)
total_carbon = sg.query([last_metric.treasuryCarbon])

last_carbon = get_last_carbon(sg)
current_sma, credit_supply = sg.query(
[last_carbon.creditSMA, last_carbon.creditSupply]
)
return total_carbon, current_sma, credit_supply


@client.event
async def on_ready():
print("Logged in as {0.user}".format(client))
if not update_info.is_running():
update_info.start()


@tasks.loop(seconds=300)
async def update_info():
rebases_per_day = get_rebases_per_day()

treasury_carbon, carbon_sma, credit_supply = get_info()

carbon_sma = carbon_sma / 1e18
credit_supply = credit_supply / 1e18

print(treasury_carbon)
print(carbon_sma)
if (
treasury_carbon is not None
and carbon_sma is not None
and rebases_per_day is not None
):
sma_percent = carbon_sma / credit_supply
# ie, annualized reward %
supply_change_annual = math.pow(1 + sma_percent, 365 * rebases_per_day) - 1
else:
return

yield_text = f"{supply_change_annual*100:,.2f}% Δ DCM Supply"
print(yield_text)

success = await update_nickname(client, yield_text)
if not success:
return

presence_txt = f'Total DCM Supply: {prettify_number(credit_supply)}t'
success = await update_presence(
client,
presence_txt,
type='playing'
)
if not success:
return


client.run(BOT_TOKEN)
12 changes: 11 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from web3 import Web3

from .constants import KLIMA_PROTOCOL_SUBGRAPH
from .constants import KLIMA_PROTOCOL_SUBGRAPH, POLYGON_DIGITAL_CARBON_SUBGRAPH

PROVIDER_POLYGON_URL = os.environ['WEB3_PROVIDER_POLYGON_URL']
PROVIDER_ETH_URL = os.environ['WEB3_PROVIDER_ETH_URL']
Expand Down Expand Up @@ -95,6 +95,16 @@ def get_last_metric(sg):
return last_metric


def get_last_carbon(sg):
kpm = sg.load_subgraph(POLYGON_DIGITAL_CARBON_SUBGRAPH)

last_carbon = kpm.Query.epoches(
orderBy=kpm.Epoch.epoch, orderDirection='desc', first=1
)

return last_carbon


def prettify_number(number):
num = float('{:.3g}'.format(number))
magnitude = 0
Expand Down

0 comments on commit ea063bf

Please sign in to comment.