Skip to content

Commit

Permalink
Fixes tests and adds network as a param to token_by_name() (#8549)
Browse files Browse the repository at this point in the history
* Fixes tests to allow travis to complete and adds network as a param fed to token_by_name()

* Switches double quotes for single quotes

* Wraps value_in_usdt_at_time() in a try catch to handle missing payout_amount's
  • Loading branch information
gdixon authored Mar 12, 2021
1 parent efc260e commit 2a9b8ab
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
25 changes: 13 additions & 12 deletions app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions app/dashboard/tests/test_users_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -60,20 +61,26 @@ 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
)

BountyFulfillment.objects.create(
fulfiller_address='0x0000000000000000000000000000000000000000',
accepted=True,
bounty=Bounty.objects.last(),
token_name='USDT',
payout_amount=1.5,
profile=User.objects.last().profile
)


class UsersListTest(TestCase):
"""Define tests for the user list."""

fixtures = ['tokens.json']

def setUp(self):
self.request = RequestFactory()
self.current_user = User.objects.create(
Expand Down
4 changes: 2 additions & 2 deletions app/dashboard/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def setUp(self):
raw_data={},
idx_status='submitted',
current_bounty=True,
network='mainnet',
network='rinkeby',
metadata={"issueKeywords": "Python, Shell"},
)

Expand All @@ -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
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 2a9b8ab

Please sign in to comment.