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

Fixes tests and adds network as a param to token_by_name() #8549

Merged
merged 3 commits into from
Mar 12, 2021
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
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