Exemplo n.º 1
0
 def test_cosmos_overflow(self):
     """Analog for this test:
     https://github.com/cosmos/cosmos-sdk/blob/5a2e59ebb23d1d23546f1145d8814a457655935a/types/decimal_test.go#L334
     """
     a = Dec("51643150036226787134389711697696177267")
     b = Dec("-31798496660535729618459429845579852627")
     assert str(a + b) == "19844653375691057515930281852116324640.000000000000000000"
Exemplo n.º 2
0
 def from_data(cls, data: dict) -> PolicyConstraints:
     return cls(
         rate_min=Dec(data["rate_min"]),
         rate_max=Dec(data["rate_max"]),
         cap=Coin.from_data(data["cap"]),
         change_max=Dec(data["change_max"]),
     )
Exemplo n.º 3
0
    def test_clamp(self, default_tax_policy, default_tax_rate):
        # Analogous test:
        # https://github.com/terra-project/core/blob/develop/x/treasury/internal/types/constraint_test.go#L10
        tax_policy = default_tax_policy
        prev_rate = default_tax_rate

        # Case 1: try to update delta > change_max
        new_rate = prev_rate + tax_policy.change_max * 2
        clamped_rate = tax_policy.clamp(prev_rate, new_rate)
        assert prev_rate + tax_policy.change_max == clamped_rate

        # Case 2: try to update delta > change_max (negative)
        new_rate = prev_rate - tax_policy.change_max * 2
        clamped_rate = tax_policy.clamp(prev_rate, new_rate)
        assert prev_rate - tax_policy.change_max == clamped_rate

        # Case 3: try to update the new rate > min_rate
        prev_rate = tax_policy.rate_max
        new_rate = prev_rate + Dec.with_prec(1, 3)
        clamped_rate = tax_policy.clamp(prev_rate, new_rate)
        assert tax_policy.rate_max == clamped_rate

        # Case 4: try to update the new rate < minRate
        prev_rate = tax_policy.rate_min
        new_rate = tax_policy.rate_min - Dec.with_prec(1, 3)
        clamped_rate = tax_policy.clamp(prev_rate, new_rate)
        assert tax_policy.rate_min == clamped_rate
Exemplo n.º 4
0
    def test_str_regex(self):
        """`Dec` will only parse strings that match Decimal format: `/^(\-)?(\d+)(\.\d+)?\Z/`
        """
        bad_examples = [
            "a1.2",
            "..2",
            "0b00",
            "0xdeadb33f",
            "0.0.5",
            "(626)112-3322",
            "2.14E-14",
            "42.",
            ".420",
        ]
        for bad in bad_examples:
            with pytest.raises(ValueError):
                Dec(bad)

        good_examples = [
            "0.0",
            "42.0",
            "333333.213123923402394",
            "1127.10102392039129301293230921039",
            "13123891823912839123.239482934829348239489",
            "-239299920",
            "-0000.0",
        ]
        for good in good_examples:
            assert isinstance(Dec(good).i, int)
Exemplo n.º 5
0
def default_tax_policy():
    return PolicyConstraints(
        rate_min=Dec.with_prec(5, 4),
        rate_max=Dec.with_prec(1, 2),
        cap=Coin("usdr", 1),
        change_max=Dec.with_prec(25, 5),
    )
Exemplo n.º 6
0
 def test_constructor_copies(self, x):
     """Dec should copy another Dec in the constructor, and the resulting
     object should be equivalent but not share the same address in memory.
     """
     A = Dec(x)
     B = Dec(A)
     assert A == B
     assert A is not B
Exemplo n.º 7
0
 def params(
         self,
         key: Optional[str] = None) -> Union[ApiResponse, dict, Dec, bool]:
     res = self._api_get("/distribution/parameters")
     p = res
     p["community_tax"] = Dec(p["community_tax"])
     p["base_proposer_reward"] = Dec(p["base_proposer_reward"])
     p["bonus_proposer_reward"] = Dec(p["bonus_proposer_reward"])
     return project(res, p[key] if key else JiguBox(p))
Exemplo n.º 8
0
 def test_cosmos_mul(self):
     examples = [
         (Dec(-1), Dec(-1), Dec(1)),
         (Dec(3), Dec(7), Dec(21)),
         (Dec.with_prec(3333, 4), Dec.with_prec(333, 4), Dec.with_prec(1109889, 8),),
     ]
     for x in examples:
         assert x[0] * x[1] == x[2]
Exemplo n.º 9
0
 def test_equality_decimal(self):
     assert Dec(Decimal("1.45")) == Decimal("1.45")
     assert Dec(Decimal("2342.123232123")) == Decimal("2342.123232123")
     assert Dec(Decimal("2332423442.123234234232123")) == Decimal(
         "2332423442.123234234232123"
     )
     # 24, 18 digit decimal precision
     assert Dec(Decimal("423412312312312342123123.123234234232123123")) == Decimal(
         "423412312312312342123123.123234234232123123"
     )
     # disregard beyond 19 digits of precision
     assert Dec(Decimal("0.1232342342321231233")) == Decimal("0.1232342342321231233")
Exemplo n.º 10
0
 def params(self, key: Optional[str] = None) -> Union[ApiResponse, Dec, dict]:
     res = self._api_get("/market/parameters")
     p = JiguBox(res)
     p["pool_recovery_period"] = int(p["pool_recovery_period"])
     p["base_pool"] = Dec.deserialize(p["base_pool"])
     p["min_spread"] = Dec.deserialize(p["min_spread"])
     p["tobin_tax"] = Dec.deserialize(p["tobin_tax"])
     ill = p["illiquid_tobin_tax_list"]
     p["illiquid_tobin_tax_list"] = JiguBox({})
     for item in ill:
         p["illiquid_tobin_tax_list"][item["denom"]] = Dec(item["tax_rate"])
     return project(res, p[key] if key else p)
Exemplo n.º 11
0
 def test_cosmos_quo(self):
     examples = [
         (Dec(-1), Dec(-1), Dec(1)),
         (Dec(3), Dec(7), Dec.with_prec(428571428571428571, 18)),
         (
             Dec.with_prec(3333, 4),
             Dec.with_prec(333, 4),
             Dec("10.009009009009009009"),
         ),
     ]
     for x in examples:
         assert x[0] / x[1] == x[2]
Exemplo n.º 12
0
 def from_data(cls, data: dict) -> TaxRateUpdateProposal:
     data = data["value"]
     return cls(
         title=data["title"],
         description=data["description"],
         tax_rate=Dec(data["tax_rate"]),
     )
Exemplo n.º 13
0
 def from_data(cls, data: dict) -> RewardWeightUpdateProposal:
     data = data["value"]
     return cls(
         title=data["title"],
         description=data["description"],
         reward_weight=Dec(data["reward_weight"]),
     )
Exemplo n.º 14
0
 def test_mul_examples(self):
     # TODO: add better examples
     examples = [
         (Dec("6.3") * Dec("6.5"), Dec("40.95")),
         (Dec("27.37") * Dec("36.299"), Dec("993.50363")),
         (Dec("-5231.234232") * 5.5, Dec("-28771.788276")),
     ]
     for x in examples:
         assert x[0] == x[1]
Exemplo n.º 15
0
 def test_truncates_beyond_18_digits(self, x, y):
     """If Dec is provided a string representing a number with higher
     degree of precision than 18 digits, those extra digits should be truncated.
     """
     A = str(x) + "." + y * "7"
     r = str(Dec(A)).split(".")
     assert r[0] == str(x)
     assert len(r[1]) == 18
Exemplo n.º 16
0
    def clamp(self, prev_rate: Dec, new_rate: Dec):
        prev_rate = Dec(prev_rate)
        new_rate = Dec(new_rate)

        if new_rate < self.rate_min:
            new_rate = self.rate_min
        elif new_rate > self.rate_max:
            new_rate = self.rate_max

        delta = new_rate - prev_rate
        if new_rate > prev_rate:
            if delta > self.change_max:
                new_rate = prev_rate + self.change_max
        else:
            if abs(delta) > self.change_max:
                new_rate = prev_rate - self.change_max
        return new_rate
Exemplo n.º 17
0
    def test_make_coin(self, d1):
        coin = Coin(d1, "13929")
        assert coin.denom == d1
        assert coin.amount == 13929

        coin = Coin(d1, "0.006250000000000000")
        assert coin.denom == d1
        assert coin.amount == Dec("0.00625")
Exemplo n.º 18
0
 def test_converts_decimal_objects(self, x):
     """`Dec` should capture a compatible `Decimal` object and serialize
     to 18 digits of precision.
     """
     d = Dec(x)
     r = str(d).split(".")
     if x < 0:
         assert d.i < 0
     assert int(r[0]) == int(x)
     assert len(r[1]) == 18
Exemplo n.º 19
0
 def from_data(cls, data: dict) -> MsgEditValidator:
     data = data["value"]
     msd = int(data["min_self_delegation"]) if data["min_self_delegation"] else None
     cr = Dec(data["commission_rate"]) if data["commission_rate"] else None
     return cls(
         Description=data["Description"],
         address=data["address"],
         commission_rate=cr,
         min_self_delegation=msd,
     )
Exemplo n.º 20
0
 def test_serializes_18_digits(self):
     examples = [
         ("0.5", "0.500000000000000000"),
         ("0.00625", "0.006250000000000000"),
         ("3913.11", "3913.110000000000000000"),
         ("-23.11", "-23.110000000000000000"),
         ("3", "3.000000000000000000"),
         ("-3", "-3.000000000000000000"),
     ]
     for x in examples:
         assert str(Dec(x[0])) == x[1]
Exemplo n.º 21
0
 def test_simple_check(self):
     examples = [
         "138875042105.980753034749566779",
         "8447.423744387144096286",
         "3913.113789811986907029",
         "0.500000000000000000",
         "0.006250000000000000",
         "-23.128250000000000023",
         "242.000000000000028422",
         "-242.000000000000020422",
     ]
     for x in examples:
         assert str(Dec(x)) == x
Exemplo n.º 22
0
 def test_serializes_zero(self):
     Z = "0.000000000000000000"
     assert str(Dec(0)) == Z
     assert str(Dec("0")) == Z
     assert str(Dec("-0")) == Z
     assert str(Dec("-00")) == Z
     assert str(Dec("-00.0")) == Z
     assert str(Dec("-00.0000000000000000000232")) == Z
Exemplo n.º 23
0
 def __post_init__(self):
     if self.commission_rate is not None:
         self.commission_rate = Dec(self.commission_rate)
     if self.min_self_delegation is not None:
         self.min_self_delegation = int(self.min_self_delegation)
     self.address = validate_val_address(self.address)
Exemplo n.º 24
0
 def test_add(self, inc):
     assert inc + inc == Dec("0.000000000000000002")
     assert (inc + inc).i == 2
Exemplo n.º 25
0
 def test_sub_properties(self, x, y, zero):
     assert Dec(x) - zero == Dec(x)
     assert Dec(x) - Dec(y) == -Dec(y) + Dec(x)
     assert Dec(x) - Dec(x) == zero
     assert isinstance(Dec(x) - Dec(y), Dec)
     if not isinstance(y, str):
         assert Dec(x) - y == -y + Dec(x)
         assert isinstance(Dec(x) - y, Dec)
         assert isinstance(y - Dec(x), Dec)
     if isinstance(x, int) and isinstance(y, int):
         assert Dec(x) - Dec(y) == Dec(x - y)
Exemplo n.º 26
0
 def test_mul_properties(self, x, y, one):
     assume(x != y)
     assert Dec(x) * one == Dec(x)
     assert Dec(x) * one == one * Dec(x)
     assert Dec(x) * Dec(y) == Dec(y) * Dec(x)
     if not isinstance(x, str) and not isinstance(y, str):
         assert Dec(x) * y == y * Dec(x)
         assert isinstance(Dec(x) * y, Dec)
         assert isinstance(y * Dec(x), Dec)
     if isinstance(x, int) and isinstance(y, int):
         assert Dec(x) * Dec(y) == Dec(x * y)
Exemplo n.º 27
0
def inc():
    nd = Dec(0)
    nd.i = 1
    return nd
Exemplo n.º 28
0
def default_tax_rate():
    return Dec.with_prec(1, 3)
Exemplo n.º 29
0
 def tax_rate(self) -> Union[ApiResponse, Dec]:
     res = self._api_get("/treasury/tax_rate")  # tr
     return project(res, Dec.deserialize(res))
Exemplo n.º 30
0
 def reward_weight(self) -> Union[ApiResponse, Dec]:
     res = self._api_get("/treasury/reward_weight")  # rw
     return project(res, Dec.deserialize(res))