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 coingecko support + update token DB #7676

Merged
merged 1 commit into from
Oct 14, 2020
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
60 changes: 58 additions & 2 deletions app/economy/management/commands/get_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import cryptocompare as cc
import requests
from dashboard.models import Bounty, Tip
from economy.models import ConversionRate
from economy.models import ConversionRate, Token
from grants.models import Contribution
from kudos.models import KudosTransfer
from perftools.models import JSONStore
Expand Down Expand Up @@ -136,6 +136,52 @@ def polo():
print(e)


def coingecko(source, tokens):

"""Handle pulling market data from Coingecko."""

now = timezone.now()

token_str = ''
for token in tokens:
token_str += (token.conversion_rate_id + ',')

url = f'https://api.coingecko.com/api/v3/simple/price?ids={token_str}&vs_currencies=usd,eth'

response = requests.get(url).json()

for token in tokens:
from_currency = token.symbol
conversion_rate_id = token.conversion_rate_id
conversion_rates = response.get(conversion_rate_id)

token.conversion_rate_id

# token -> ETH
to_amount = conversion_rates.get('eth')
ConversionRate.objects.create(
from_amount=1,
to_amount=to_amount,
source=source,
from_currency=from_currency,
to_currency='ETH'
)
print(f'Coingecko: {from_currency} => ETH : {to_amount}')

# token -> USDT
to_amount = conversion_rates.get('usd')
ConversionRate.objects.create(
from_amount=1,
to_amount=to_amount,
source=source,
from_currency=from_currency,
to_currency='USDT'
)
print(f'Coingecko: {from_currency} => USD : {to_amount}')

ConversionRate.objects.filter(source=source, created_on__lt=now).delete()
print(f'Deleted old coingecko conversion rates')

def refresh_bounties():
for bounty in Bounty.objects.all():
print(f'refreshed {bounty.pk}')
Expand Down Expand Up @@ -292,6 +338,8 @@ def handle(self, *args, **options):
"""Get the latest currency rates."""
stablecoins()

approved_tokens = Token.objects.filter(approved=True)

try:
print('ED')
etherdelta()
Expand All @@ -304,7 +352,6 @@ def handle(self, *args, **options):
except Exception as e:
print(e)


try:
print('uniswap')
uniswap()
Expand All @@ -320,6 +367,15 @@ def handle(self, *args, **options):
except Exception as e:
print(e)

try:
source = 'coingecko'
coingecko_tokens = approved_tokens.filter(conversion_rate_source=source)
if coingecko_tokens.count() > 0:
print(source)
coingecko(source, coingecko_tokens)
except Exception as e:
print(e)

try:
print('refresh')
refresh_bounties()
Expand Down
28 changes: 28 additions & 0 deletions app/economy/migrations/0005_auto_20201014_0916.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.2.4 on 2020-10-14 09:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('economy', '0004_txupdate'),
]

operations = [
migrations.AddField(
model_name='token',
name='conversion_rate_id',
field=models.CharField(blank=True, help_text='unique identifier used by conversion_rate_source', max_length=25, null=True),
),
migrations.AddField(
model_name='token',
name='conversion_rate_source',
field=models.CharField(blank=True, choices=[('cryptocompare', 'cryptocompare'), ('poloniex', 'poloniex'), ('uniswap', 'uniswap'), ('manual', 'manual'), ('coingecko', 'coingecko')], help_text='API source', max_length=25, null=True),
),
migrations.AlterField(
model_name='conversionrate',
name='source',
field=models.CharField(choices=[('cryptocompare', 'cryptocompare'), ('poloniex', 'poloniex'), ('uniswap', 'uniswap'), ('manual', 'manual'), ('coingecko', 'coingecko')], db_index=True, max_length=30),
),
]
12 changes: 12 additions & 0 deletions app/economy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class ConversionRate(SuperModel):
('poloniex', 'poloniex'),
('uniswap', 'uniswap'),
('manual', 'manual'),
('coingecko', 'coingecko'),
]
from_amount = models.FloatField()
to_amount = models.FloatField()
Expand Down Expand Up @@ -237,6 +238,14 @@ def process_callbacks(self):
class Token(SuperModel):
"""Define the Token model."""

CONVERSION_RATE_SOURCE = [
('cryptocompare', 'cryptocompare'),
('poloniex', 'poloniex'),
('uniswap', 'uniswap'),
('manual', 'manual'),
('coingecko', 'coingecko'),
]

address = models.CharField(max_length=255, db_index=True)
symbol = models.CharField(max_length=10, db_index=True)
network = models.CharField(max_length=25, db_index=True)
Expand All @@ -246,6 +255,9 @@ class Token(SuperModel):
network_id = models.IntegerField(default=1)
metadata = JSONField(null=True, default=dict, blank=True)
approved = models.BooleanField(default=True)
conversion_rate_id = models.CharField(max_length=25, blank=True, null=True, help_text="unique identifier used by conversion_rate_source")
conversion_rate_source = models.CharField(max_length=25, blank=True, null=True, help_text="API source", choices=CONVERSION_RATE_SOURCE)


def __str__(self):
"""Define the string representation of a conversion rate."""
Expand Down