Skip to content

Commit

Permalink
Add CCO2 price feed bot based on KLIMA/CCO2 pool
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAurelius committed Jan 18, 2024
1 parent 5c8a2dc commit 9a67519
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
16 changes: 16 additions & 0 deletions k8s/cco2_price/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.cco2_price.main"]
env:
- name: DISCORD_BOT_TOKEN
valueFrom:
secretKeyRef:
name: discord-bots-secret
key: DISCORD_BOT_TOKEN_CCO2_PRICE
13 changes: 13 additions & 0 deletions k8s/cco2_price/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: cco2-price-

commonLabels:
bot: cco2-price

patchesStrategicMerge:
- deployment_set_bot.yaml
3 changes: 2 additions & 1 deletion k8s/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ resources:
- ./retirement_fee_info
- ./dao_fee
- ./dao_balance
- ./cco2_price

namespace: discord-bots

secretGenerator:
- name: discord-bots-secret
envs:
envs:
- secret.properties
1 change: 1 addition & 0 deletions k8s/secret.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DISCORD_BOT_TOKEN_MCO2_PRICE=${DISCORD_BOT_TOKEN_MCO2_PRICE}
DISCORD_BOT_TOKEN_MOSS_PRICE=${DISCORD_BOT_TOKEN_MOSS_PRICE}
DISCORD_BOT_TOKEN_NBO_PRICE=${DISCORD_BOT_TOKEN_NBO_PRICE}
DISCORD_BOT_TOKEN_NCT_PRICE=${DISCORD_BOT_TOKEN_NCT_PRICE}
DISCORD_BOT_TOKEN_CCO2_PRICE=${DISCORD_BOT_TOKEN_CCO2_PRICE}
DISCORD_BOT_TOKEN_REBASE=${DISCORD_BOT_TOKEN_REBASE}
DISCORD_BOT_TOKEN_STAKING_REWARDS=${DISCORD_BOT_TOKEN_STAKING_REWARDS}
DISCORD_BOT_TOKEN_SUPPLY_CC=${DISCORD_BOT_TOKEN_SUPPLY_CC}
Expand Down
Empty file added src/cco2_price/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions src/cco2_price/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
from discord.ext import tasks

from ..constants import CCO2_ADDRESS, KLIMA_CCO2_POOL, KLIMA_DECIMALS
from ..contract_info import uni_v2_pool_price, token_supply, klima_usdc_price
from ..utils import get_polygon_web3, \
get_discord_client, load_abi, prettify_number, \
update_nickname, update_presence

BOT_TOKEN = os.environ["DISCORD_BOT_TOKEN"]

# Initialized Discord client
client = get_discord_client()

# Initialize web3
web3 = get_polygon_web3()

# Load ABIs
cco2_abi = load_abi('carbon_pool.json')


@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():
klima_price = klima_usdc_price(web3)
token_price = uni_v2_pool_price(
web3, KLIMA_CCO2_POOL,
KLIMA_DECIMALS
)
supply = token_supply(web3, CCO2_ADDRESS, cco2_abi)

if klima_price is not None and token_price is not None and supply is not None:
price = klima_price * token_price
price_text = f'${price:,.2f} CCO2'

print(price_text)

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

supply_text = f'Supply: {prettify_number(supply)}'
success = await update_presence(
client,
supply_text
)
if not success:
return

client.run(BOT_TOKEN)
2 changes: 2 additions & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
NCT_ADDRESS = Web3.to_checksum_address('0xD838290e877E0188a4A44700463419ED96c16107')
UBO_ADDRESS = Web3.to_checksum_address('0x2B3eCb0991AF0498ECE9135bcD04013d7993110c')
USDC_ADDRESS = Web3.to_checksum_address('0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174')
CCO2_ADDRESS = Web3.to_checksum_address('0x82B37070e43C1BA0EA9e2283285b674eF7f1D4E2')

# LP Pool Addresses
BCT_KLIMA_POOL = Web3.to_checksum_address('0x9803c7ae526049210a1725f7487af26fe2c24614')
Expand All @@ -33,6 +34,7 @@
MOSS_USDC_POOL = Web3.to_checksum_address('0x75a7360e330c338007CF482e2CDf0A06573763A1')
UBO_KLIMA_POOL = Web3.to_checksum_address('0x5400A05B8B45EaF9105315B4F2e31F806AB706dE')
USDC_NCT_POOL = Web3.to_checksum_address('0xdb995f975f1bfc3b2157495c47e4efb31196b2ca')
KLIMA_CCO2_POOL = Web3.to_checksum_address('0x4D2263FF85e334C1f1d04C6262F6c2580335a93C')

# Klima Protocol Contracts
STAKING_ADDRESS = Web3.to_checksum_address('0x25d28a24Ceb6F81015bB0b2007D795ACAc411b4d')
Expand Down
4 changes: 3 additions & 1 deletion src/contract_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def klima_usdc_price(web3):
)


def token_supply(web3, token_address, abi, decimals):
def token_supply(web3, token_address, abi, decimals=None):
'''
Compute the total supply of the specified ERC-20 token at `token_address` with `abi` and the correct `decimals`
'''
Expand All @@ -41,6 +41,8 @@ def token_supply(web3, token_address, abi, decimals):
)

try:
if decimals is None:
decimals = contract.functions.decimals().call()
total_supply = contract.functions.totalSupply().call() / 10**decimals
return total_supply
except Exception:
Expand Down

0 comments on commit 9a67519

Please sign in to comment.