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

chore: Cleanup of ethereum Django app #2126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions src/ethereum/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@

class EthereumConfig(AppConfig):
name = 'ethereum'

# TODO: Read contract addresses from s3
18 changes: 17 additions & 1 deletion src/ethereum/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.conf import settings
from web3 import Web3

from ethereum.utils import decimal_to_token_amount
from utils.aws import create_client
from utils.web3_utils import web3_provider

Expand Down Expand Up @@ -103,6 +102,23 @@
return decimal_to_token_amount(total, denomination), str(total)


def decimal_to_token_amount(value, denomination):
if type(value) is not Decimal:
raise TypeError('`value` must be of type Decimal')

Check warning on line 107 in src/ethereum/lib.py

View check run for this annotation

Codecov / codecov/patch

src/ethereum/lib.py#L107

Added line #L107 was not covered by tests

value_string = str(value)

integer_string = value_string.split('.')[0]
integer_pad_width = len(integer_string) + denomination
integer_padded = integer_string.ljust(integer_pad_width, '0')
integer_part = int(integer_padded)

decimal_padded = value_string.split('.')[1].ljust(denomination, '0')
decimal_part = int(decimal_padded)

return integer_part + decimal_part


def get_nonce(w3, account):
return w3.eth.get_transaction_count(account)

Expand Down
Empty file.
81 changes: 79 additions & 2 deletions src/ethereum/tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from unittest.mock import Mock, call, patch
from decimal import Decimal

from django.conf import settings
from django.test import TestCase, override_settings

from ethereum.lib import convert_reputation_amount_to_token_amount, get_private_key
from ethereum.lib import convert_reputation_amount_to_token_amount, decimal_to_token_amount, get_private_key


class EthereumLibTests(TestCase):
def setUp(self):
self.token_ticker = "RSC"
self.denomination = 18

def test_convert_reputation_amount_to_token_amount(self):
rep = 1
Expand Down Expand Up @@ -72,3 +73,79 @@ def test_get_private_key(self, mock_provider, mock_create_client):
"mock_keystore", "mock_password"
)
self.assertEqual(actual, "mock_private_key")

def test_decimal_to_token_amount_with_floats(self):
result_a = self.get_result('0.0')
self.assertEqual(result_a, 0)
result_b = self.get_result('0.01')
self.assertEqual(result_b, 10000000000000000)
c = '0.001'
result_c = self.get_result(c)
self.assertEqual(result_c, 1000000000000000)
d = '1.0'
result_d = self.get_result(d)
self.assertEqual(result_d, 1000000000000000000)
e = '1.00'
result_e = self.get_result(e)
self.assertEqual(result_e, 1000000000000000000)
f = '1.000'
result_f = self.get_result(f)
self.assertEqual(result_f, 1000000000000000000)
g = '1.001'
result_g = self.get_result(g)
self.assertEqual(result_g, 1001000000000000000)
h = '1.01'
result_h = self.get_result(h)
self.assertEqual(result_h, 1010000000000000000)
i = '1.1'
result_i = self.get_result(i)
self.assertEqual(result_i, 1100000000000000000)
j = '1.10'
result_j = self.get_result(j)
self.assertEqual(result_j, 1100000000000000000)
k = '10.0'
result_k = self.get_result(k)
self.assertEqual(result_k, 10000000000000000000)
m = '11.0'
result_m = self.get_result(m)
self.assertEqual(result_m, 11000000000000000000)
n = '11.01'
result_n = self.get_result(n)
self.assertEqual(result_n, 11010000000000000000)

def test_decimal_to_token_amount_does_NOT_round(self):
o = '111.5'
result_o = self.get_result(o)
self.assertEqual(result_o, 111500000000000000000)
p = '111.56'
result_p = self.get_result(p)
self.assertEqual(result_p, 111560000000000000000)
q = '111.04'
result_q = self.get_result(q)
self.assertEqual(result_q, 111040000000000000000)
r = '115.045'
result_r = self.get_result(r)
self.assertEqual(result_r, 115045000000000000000)

def test_decimal_to_token_amount_with_large_integer_part(self):
s = '1150.045'
result_s = self.get_result(s)
self.assertEqual(result_s, 1150045000000000000000)
t = '0011506.0'
result_t = self.get_result(t)
self.assertEqual(result_t, 11506000000000000000000)
u = '864213579.0000'
result_u = self.get_result(u)
self.assertEqual(result_u, 864213579000000000000000000)

def test_decimal_to_token_amount_with_large_decimal_part(self):
w = '9999999999.999999999999999999'
result_w = self.get_result(w)
self.assertEqual(result_w, 9999999999999999999999999999)
x = '000.9876543210'
result_x = self.get_result(x)
self.assertEqual(result_x, 987654321000000000)

def get_result(self, amount_string):
value = Decimal(amount_string)
return decimal_to_token_amount(value, self.denomination)
88 changes: 0 additions & 88 deletions src/ethereum/tests/test_utils.py

This file was deleted.

58 changes: 0 additions & 58 deletions src/ethereum/utils.py

This file was deleted.

1 change: 0 additions & 1 deletion src/reputation/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from web3 import Web3

import ethereum.lib
import ethereum.utils
from ethereum.lib import RSC_CONTRACT_ADDRESS, execute_erc20_transfer, get_private_key
from mailing_list.lib import base_email_context
from reputation.models import Withdrawal
Expand Down
Loading