2019-03-17 17:57:10 +00:00
|
|
|
from decimal import InvalidOperation, Decimal
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
import pytest
|
2019-03-14 00:39:56 +00:00
|
|
|
from web3 import Web3
|
|
|
|
|
2019-04-16 00:59:45 +00:00
|
|
|
from nucypher.blockchain.economics import TokenEconomics
|
2019-03-18 19:15:57 +00:00
|
|
|
from nucypher.blockchain.eth.token import NU, Stake
|
2019-03-14 00:39:56 +00:00
|
|
|
from nucypher.utilities.sandbox.constants import INSECURE_DEVELOPMENT_PASSWORD
|
|
|
|
|
|
|
|
|
2019-03-20 03:38:29 +00:00
|
|
|
def test_NU(token_economics):
|
2019-03-14 00:39:56 +00:00
|
|
|
|
|
|
|
# Starting Small
|
2019-03-20 03:38:29 +00:00
|
|
|
min_allowed_locked = NU(token_economics.minimum_allowed_locked, 'NuNit')
|
|
|
|
assert token_economics.minimum_allowed_locked == int(min_allowed_locked.to_nunits())
|
2019-03-14 00:39:56 +00:00
|
|
|
|
2019-03-20 03:38:29 +00:00
|
|
|
min_NU_locked = int(str(token_economics.minimum_allowed_locked)[0:-18])
|
2019-03-14 00:39:56 +00:00
|
|
|
expected = NU(min_NU_locked, 'NU')
|
|
|
|
assert min_allowed_locked == expected
|
|
|
|
|
|
|
|
# Starting Big
|
|
|
|
min_allowed_locked = NU(min_NU_locked, 'NU')
|
2019-03-20 03:38:29 +00:00
|
|
|
assert token_economics.minimum_allowed_locked == int(min_allowed_locked)
|
|
|
|
assert token_economics.minimum_allowed_locked == int(min_allowed_locked.to_nunits())
|
2019-03-14 00:39:56 +00:00
|
|
|
assert str(min_allowed_locked) == '15000 NU'
|
|
|
|
|
2019-03-17 17:57:10 +00:00
|
|
|
# Alternate construction
|
|
|
|
assert NU(1, 'NU') == NU('1.0', 'NU') == NU(1.0, 'NU')
|
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
# Arithmetic
|
|
|
|
|
2019-03-17 17:57:10 +00:00
|
|
|
# NUs
|
2019-03-15 15:11:06 +00:00
|
|
|
one_nu = NU(1, 'NU')
|
|
|
|
zero_nu = NU(0, 'NU')
|
2019-03-15 16:55:45 +00:00
|
|
|
one_hundred_nu = NU(100, 'NU')
|
|
|
|
two_hundred_nu = NU(200, 'NU')
|
|
|
|
three_hundred_nu = NU(300, 'NU')
|
|
|
|
|
2019-03-17 17:57:10 +00:00
|
|
|
# Nits
|
2019-03-18 19:28:25 +00:00
|
|
|
one_nu_wei = NU(1, 'NuNit')
|
|
|
|
three_nu_wei = NU(3, 'NuNit')
|
2019-03-18 19:16:36 +00:00
|
|
|
assert three_nu_wei.to_tokens() == Decimal('3E-18')
|
2019-03-17 17:57:10 +00:00
|
|
|
assert one_nu_wei.to_tokens() == Decimal('1E-18')
|
2019-03-15 16:55:45 +00:00
|
|
|
|
|
|
|
# Base Operations
|
|
|
|
assert one_hundred_nu < two_hundred_nu < three_hundred_nu
|
|
|
|
assert one_hundred_nu <= two_hundred_nu <= three_hundred_nu
|
|
|
|
|
|
|
|
assert three_hundred_nu > two_hundred_nu > one_hundred_nu
|
|
|
|
assert three_hundred_nu >= two_hundred_nu >= one_hundred_nu
|
|
|
|
|
|
|
|
assert (one_hundred_nu + two_hundred_nu) == three_hundred_nu
|
|
|
|
assert (three_hundred_nu - two_hundred_nu) == one_hundred_nu
|
|
|
|
|
|
|
|
difference = one_nu - one_nu_wei
|
|
|
|
assert not difference == zero_nu
|
|
|
|
|
|
|
|
actual = float(difference.to_tokens())
|
|
|
|
expected = 0.999999999999999999
|
|
|
|
assert actual == expected
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-18 19:28:25 +00:00
|
|
|
# 3.14 NU is 3_140_000_000_000_000_000 NuNit
|
2019-03-15 15:11:06 +00:00
|
|
|
pi_nuweis = NU(3.14, 'NU')
|
2019-03-18 19:28:25 +00:00
|
|
|
assert NU('3.14', 'NU') == pi_nuweis.to_nunits() == NU(3_140_000_000_000_000_000, 'NuNit')
|
2019-03-17 17:57:10 +00:00
|
|
|
|
|
|
|
# Mixed type operations
|
|
|
|
difference = NU('3.14159265', 'NU') - NU(1.1, 'NU')
|
|
|
|
assert difference == NU('2.04159265', 'NU')
|
|
|
|
|
|
|
|
result = difference + one_nu_wei
|
2019-03-18 19:28:25 +00:00
|
|
|
assert result == NU(2041592650000000001, 'NuNit')
|
2019-03-17 17:57:10 +00:00
|
|
|
|
2019-05-29 13:53:23 +00:00
|
|
|
# Similar to stake read + metadata operations in Staker
|
2019-03-17 17:57:10 +00:00
|
|
|
collection = [one_hundred_nu, two_hundred_nu, three_hundred_nu]
|
2019-03-18 19:28:25 +00:00
|
|
|
assert sum(collection) == NU('600', 'NU') == NU(600, 'NU') == NU(600.0, 'NU') == NU(600e+18, 'NuNit')
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
#
|
2019-03-18 19:16:36 +00:00
|
|
|
# Fractional Inputs
|
2019-03-15 16:55:45 +00:00
|
|
|
#
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-18 19:28:25 +00:00
|
|
|
# A decimal amount of NuNit (i.e., a fraction of a NuNit)
|
|
|
|
pi_nuweis = NU('3.14', 'NuNit')
|
2019-03-18 19:16:36 +00:00
|
|
|
assert pi_nuweis == three_nu_wei # Floor
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-18 19:28:25 +00:00
|
|
|
# A decimal amount of NU, which amounts to NuNit with decimals
|
2019-03-18 19:16:36 +00:00
|
|
|
pi_nus = NU('3.14159265358979323846', 'NU')
|
2019-03-18 19:28:25 +00:00
|
|
|
assert pi_nus == NU(3141592653589793238, 'NuNit') # Floor
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
# Positive Infinity
|
2019-03-18 19:16:36 +00:00
|
|
|
with pytest.raises(NU.InvalidAmount):
|
2019-03-15 16:55:45 +00:00
|
|
|
_inf = NU(float('infinity'), 'NU')
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
# Negative Infinity
|
2019-03-18 19:16:36 +00:00
|
|
|
with pytest.raises(NU.InvalidAmount):
|
2019-03-15 16:55:45 +00:00
|
|
|
_neg_inf = NU(float('-infinity'), 'NU')
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-15 16:55:45 +00:00
|
|
|
# Not a Number
|
|
|
|
with pytest.raises(InvalidOperation):
|
|
|
|
_nan = NU(float('NaN'), 'NU')
|
2019-03-15 15:11:06 +00:00
|
|
|
|
2019-03-14 00:39:56 +00:00
|
|
|
|
2019-05-29 13:58:04 +00:00
|
|
|
def test_stake(testerchain, agency):
|
2019-08-15 03:29:24 +00:00
|
|
|
token_agent, staking_agent, _policy_agent = agency
|
2019-03-14 00:39:56 +00:00
|
|
|
|
|
|
|
class FakeUrsula:
|
2019-05-29 14:15:18 +00:00
|
|
|
token_agent, staking_agent, _policy_agent = agency
|
2019-04-16 00:59:45 +00:00
|
|
|
|
2019-03-14 00:39:56 +00:00
|
|
|
burner_wallet = Web3().eth.account.create(INSECURE_DEVELOPMENT_PASSWORD)
|
2019-06-04 15:46:26 +00:00
|
|
|
checksum_address = burner_wallet.address
|
2019-05-29 14:15:18 +00:00
|
|
|
staking_agent = staking_agent
|
2019-04-16 00:59:45 +00:00
|
|
|
token_agent = token_agent
|
|
|
|
blockchain = testerchain
|
|
|
|
economics = TokenEconomics()
|
2019-03-14 00:39:56 +00:00
|
|
|
|
|
|
|
ursula = FakeUrsula()
|
2019-06-13 07:20:48 +00:00
|
|
|
stake = Stake(checksum_address=ursula.checksum_address,
|
2019-08-12 07:15:43 +00:00
|
|
|
first_locked_period=1,
|
|
|
|
last_locked_period=100,
|
2019-03-15 02:51:22 +00:00
|
|
|
value=NU(100, 'NU'),
|
2019-08-15 03:29:24 +00:00
|
|
|
index=0,
|
|
|
|
staking_agent=staking_agent)
|
2019-03-15 02:51:22 +00:00
|
|
|
|
2019-03-14 00:39:56 +00:00
|
|
|
assert stake.value, 'NU' == NU(100, 'NU')
|
|
|
|
|
|
|
|
assert isinstance(stake.time_remaining(), int) # seconds
|
|
|
|
slang_remaining = stake.time_remaining(slang=True) # words
|
|
|
|
assert isinstance(slang_remaining, str)
|
|
|
|
|
|
|
|
|
2019-06-11 08:43:44 +00:00
|
|
|
def test_stake_integration(stakers):
|
|
|
|
staker = list(stakers)[1]
|
|
|
|
stakes = staker.stakes
|
2019-03-14 00:39:56 +00:00
|
|
|
assert stakes
|
|
|
|
|
|
|
|
stake = stakes[0]
|
2019-06-13 23:39:59 +00:00
|
|
|
stake.sync()
|
|
|
|
|
2019-06-11 08:43:44 +00:00
|
|
|
blockchain_stakes = staker.staking_agent.get_all_stakes(staker_address=staker.checksum_address)
|
2019-03-14 00:39:56 +00:00
|
|
|
|
2019-08-15 03:29:24 +00:00
|
|
|
stake_info = (stake.first_locked_period, stake.last_locked_period, int(stake.value))
|
2019-03-14 00:39:56 +00:00
|
|
|
published_stake_info = list(blockchain_stakes)[0]
|
2019-06-11 08:43:44 +00:00
|
|
|
assert stake_info == published_stake_info
|
|
|
|
assert stake_info == stake.to_stake_info()
|