From 6d2aa5cb53f08ad4d272fa500598409a78994112 Mon Sep 17 00:00:00 2001 From: Graham Dixon Date: Thu, 11 Mar 2021 20:33:28 +0000 Subject: [PATCH 1/3] Fixes tests to allow travis to complete and adds network as a param fed to token_by_name() --- app/dashboard/models.py | 2 +- app/dashboard/tests/test_users_list.py | 7 +++++++ app/dashboard/tokens.py | 4 ++-- .../management/commands/test_assemble_leaderboards.py | 6 ++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/dashboard/models.py b/app/dashboard/models.py index 190eb9be829..4672edd7ab0 100644 --- a/app/dashboard/models.py +++ b/app/dashboard/models.py @@ -1492,7 +1492,7 @@ def value_in_usdt_then(self): # TODO: DRY def get_natural_value(self): - token = token_by_name(self.token_name) + token = token_by_name(self.token_name, self.bounty.network) decimals = token['decimals'] amount = self.payout_amount if self.payout_amount else 0 return float(amount) / 10**decimals diff --git a/app/dashboard/tests/test_users_list.py b/app/dashboard/tests/test_users_list.py index 8dae3d04f24..6926d389bba 100644 --- a/app/dashboard/tests/test_users_list.py +++ b/app/dashboard/tests/test_users_list.py @@ -38,6 +38,7 @@ def setup_bounties(): title='foo', value_in_token=3, token_name='USDT', + network="rinkeby", web3_created=timezone.now() - timedelta(days=7), github_url='https://github.com/oogetyboogety/gitcointestproject/issues/28', token_address='0x0', @@ -60,6 +61,8 @@ def setup_bounties(): fulfiller_address='0x0000000000000000000000000000000000000000', accepted=True, bounty=Bounty.objects.first(), + token_name="USDT", + payout_amount=1.5, profile=User.objects.filter(username='user1').first().profile ) @@ -67,6 +70,8 @@ def setup_bounties(): fulfiller_address='0x0000000000000000000000000000000000000000', accepted=True, bounty=Bounty.objects.last(), + token_name="USDT", + payout_amount=1.5, profile=User.objects.last().profile ) @@ -74,6 +79,8 @@ def setup_bounties(): class UsersListTest(TestCase): """Define tests for the user list.""" + fixtures = ['tokens.json'] + def setUp(self): self.request = RequestFactory() self.current_user = User.objects.create( diff --git a/app/dashboard/tokens.py b/app/dashboard/tokens.py index b98f9edbdaa..1aa9758e0db 100644 --- a/app/dashboard/tokens.py +++ b/app/dashboard/tokens.py @@ -29,8 +29,8 @@ def addr_to_token(addr, network='mainnet'): return False -def token_by_name(name): - for token in get_tokens(): +def token_by_name(name, network='mainnet'): + for token in get_tokens(network): if token['name'].lower() == name.lower(): return token return False diff --git a/app/marketing/tests/management/commands/test_assemble_leaderboards.py b/app/marketing/tests/management/commands/test_assemble_leaderboards.py index c1d3f9398c8..3c136d4ef44 100644 --- a/app/marketing/tests/management/commands/test_assemble_leaderboards.py +++ b/app/marketing/tests/management/commands/test_assemble_leaderboards.py @@ -85,7 +85,7 @@ def setUp(self): raw_data={}, idx_status='submitted', current_bounty=True, - network='mainnet', + network='rinkeby', metadata={"issueKeywords": "Python, Shell"}, ) @@ -94,6 +94,8 @@ def setUp(self): bounty=self.bounty, accepted=True, profile=self.bounty_earner_profile, + token_name="USDT", + payout_amount=3, ) self.tip_value = 7 @@ -127,7 +129,7 @@ def setUp(self): username=self.tip_earner_handle, from_username=self.tip_payer_handle, github_url='https://github.com/gitcoinco/web', - network='mainnet', + network='rinkeby', expires_date=datetime.now(UTC) + timedelta(days=1), tokenAddress='0x0000000000000000000000000000000000000000', txid='123', From 3c3614c2d6198206dd017bfc5487880f4e9723a8 Mon Sep 17 00:00:00 2001 From: Graham Dixon Date: Fri, 12 Mar 2021 06:59:33 +0000 Subject: [PATCH 2/3] Switches double quotes for single quotes --- app/dashboard/tests/test_users_list.py | 6 +++--- .../tests/management/commands/test_assemble_leaderboards.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/dashboard/tests/test_users_list.py b/app/dashboard/tests/test_users_list.py index 6926d389bba..a26185e46e1 100644 --- a/app/dashboard/tests/test_users_list.py +++ b/app/dashboard/tests/test_users_list.py @@ -38,7 +38,7 @@ def setup_bounties(): title='foo', value_in_token=3, token_name='USDT', - network="rinkeby", + network='rinkeby', web3_created=timezone.now() - timedelta(days=7), github_url='https://github.com/oogetyboogety/gitcointestproject/issues/28', token_address='0x0', @@ -61,7 +61,7 @@ def setup_bounties(): fulfiller_address='0x0000000000000000000000000000000000000000', accepted=True, bounty=Bounty.objects.first(), - token_name="USDT", + token_name='USDT', payout_amount=1.5, profile=User.objects.filter(username='user1').first().profile ) @@ -70,7 +70,7 @@ def setup_bounties(): fulfiller_address='0x0000000000000000000000000000000000000000', accepted=True, bounty=Bounty.objects.last(), - token_name="USDT", + token_name='USDT', payout_amount=1.5, profile=User.objects.last().profile ) diff --git a/app/marketing/tests/management/commands/test_assemble_leaderboards.py b/app/marketing/tests/management/commands/test_assemble_leaderboards.py index 3c136d4ef44..597cf45c0f7 100644 --- a/app/marketing/tests/management/commands/test_assemble_leaderboards.py +++ b/app/marketing/tests/management/commands/test_assemble_leaderboards.py @@ -94,7 +94,7 @@ def setUp(self): bounty=self.bounty, accepted=True, profile=self.bounty_earner_profile, - token_name="USDT", + token_name='USDT', payout_amount=3, ) From 732a0f4708487a3e971e06c2d1843de1c08d0c05 Mon Sep 17 00:00:00 2001 From: Graham Dixon Date: Fri, 12 Mar 2021 15:55:11 +0000 Subject: [PATCH 3/3] Wraps value_in_usdt_at_time() in a try catch to handle missing payout_amount's --- app/dashboard/models.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/dashboard/models.py b/app/dashboard/models.py index 4672edd7ab0..47c3180fd17 100644 --- a/app/dashboard/models.py +++ b/app/dashboard/models.py @@ -1501,21 +1501,22 @@ def get_natural_value(self): def value_true(self): return self.get_natural_value() - def value_in_usdt_at_time(self, at_time): - decimals = 10 ** 18 - if self.token_name in ['USDT', 'USDC']: - return float(self.payout_amount / 10 ** 6) - if self.token_name in settings.STABLE_COINS: - return float(self.payout_amount / 10 ** 18) try: - return round(float(convert_amount(self.value_true, self.token_name, 'USDT', at_time)), 2) - except ConversionRateNotFoundError: + if self.token_name in ['USDT', 'USDC']: + return float(self.payout_amount / 10 ** 6) + if self.token_name in settings.STABLE_COINS: + return float(self.payout_amount / 10 ** 18) try: - in_eth = round(float(convert_amount(self.value_true, self.token_name, 'ETH', at_time)), 2) - return round(float(convert_amount(in_eth, 'USDT', 'USDT', at_time)), 2) + return round(float(convert_amount(self.value_true, self.token_name, 'USDT', at_time)), 2) except ConversionRateNotFoundError: - return None + try: + in_eth = round(float(convert_amount(self.value_true, self.token_name, 'ETH', at_time)), 2) + return round(float(convert_amount(in_eth, 'USDT', 'USDT', at_time)), 2) + except ConversionRateNotFoundError: + return None + except: + return None @property def token_value_in_usdt_now(self):