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

Add CCO2 price feed bot based on KLIMA/CCO2 pool #72

Merged
merged 1 commit into from
Jan 22, 2024
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
DISCORD_BOT_TOKEN_KLIMA_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_KLIMA_PRICE }}
DISCORD_BOT_TOKEN_BCT_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_BCT_PRICE }}
DISCORD_BOT_TOKEN_MCO2_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_MCO2_PRICE }}
DISCORD_BOT_TOKEN_CCO2_PRICE: ${{ secrets.DISCORD_BOT_TOKEN_CCO2_PRICE }}
DISCORD_BOT_TOKEN_STAKING_REWARDS: ${{ secrets.DISCORD_BOT_TOKEN_STAKING_REWARDS }}
DISCORD_BOT_TOKEN_SUPPLY_CC: ${{ secrets.DISCORD_BOT_TOKEN_SUPPLY_CC }}
DISCORD_BOT_TOKEN_INDEX: ${{ secrets.DISCORD_BOT_TOKEN_INDEX }}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ These variables must also be defined, and will be used to replace variables in t
- `DISCORD_BOT_TOKEN_BCT_PRICE`
- `DISCORD_BOT_TOKEN_MCO2_PRICE`
- `DISCORD_BOT_TOKEN_STAKING_REWARDS`
- `DISCORD_BOT_TOKEN_CCO2_PRICE`

#### Digital Ocean App Platform

Expand Down
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:,.3f} 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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we can't always assume that there's a decimals function to call on an ERC20, but in this case it should be fine since the tokens we are tracking all have the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's why I made it optional

Either we can specify it manually, or if not specified manually we try calling from the decimals function

total_supply = contract.functions.totalSupply().call() / 10**decimals
return total_supply
except Exception:
Expand Down
Loading