Ejemplo n.º 1
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"]),
     )
Ejemplo n.º 2
0
    def clamp(self, prev_rate: Dec, new_rate: Dec) -> Dec:
        """Simulates the effect of the policy contraint.

        Args:
            prev_rate (Dec): previous rate
            new_rate (Dec): new rate

        Returns:
            Dec: result of clamp (constrained change)
        """
        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
Ejemplo n.º 3
0
    async def inflation(self) -> Dec:
        """Fetches the current inflation.

        Returns:
            Dec: inflation
        """
        return Dec(await self._c._get("/minting/inflation"))
Ejemplo n.º 4
0
    async def annual_provisions(self) -> Dec:
        """Fetches the annual provisions.

        Returns:
            Dec: annual provisions
        """
        return Dec(await self._c._get("/minting/annual-provisions"))
Ejemplo n.º 5
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
Ejemplo n.º 6
0
    async def tax_rate(self) -> Dec:
        """Fetches the current tax rate.

        Returns:
            Dec: tax rate
        """
        res = await self._c._get("/treasury/tax_rate")
        return Dec(res)
Ejemplo n.º 7
0
    def terra_pool_delta(self) -> Dec:
        """Fetches the Terra pool delta.

        Returns:
            Dec: Terra pool delta
        """
        res = self._c._get("/market/terra_pool_delta")
        return Dec(res)
Ejemplo n.º 8
0
    async def reward_weight(self) -> Dec:
        """Fetches the current reward rate.

        Returns:
            Dec: reward weight
        """
        res = await self._c._get("/treasury/reward_weight")
        return Dec(res)
Ejemplo n.º 9
0
def test_arithmetic():

    zero = Coin("uluna", 0)
    coin = Coin("uluna", 1000)
    coin2 = Coin("uluna", 2000)
    coin3 = Coin("ukrw", 2000)

    # addition
    sum = coin.add(coin2)
    dec_sum = coin.add(0.1)
    sum2 = coin + coin2

    assert coin.add(zero).amount == coin.amount
    assert sum.amount == 3000
    assert sum.denom == "uluna"
    assert sum2.amount == 3000
    assert coin.add(1500) == Coin("uluna", 2500)
    assert dec_sum.is_dec_coin()
    assert not dec_sum.is_int_coin()
    assert dec_sum.amount == Dec(1000.1)
    with pytest.raises(ArithmeticError) as e:
        coin.add(coin3)

    # subtraction
    diff = coin2.sub(coin)
    diff2 = coin2 - coin.amount
    diff3 = coin2 - coin
    assert diff.denom == "uluna"
    assert diff.amount == 1000
    assert diff2.amount == 1000
    assert diff3.amount == 1000
    with pytest.raises(ArithmeticError) as e:
        coin2.sub(coin3)

    # multiplication
    product = coin.mul(3.1233)
    product2 = coin * 3.1233
    assert product.denom == "uluna"
    assert product.amount == 3123.3
    assert product2.amount == 3123.3

    # division
    quotient = coin.div(5)
    quotient2 = coin / 5
    quotient3 = coin / 5.0
    quotient4 = coin // 3
    assert quotient.denom == "uluna"
    assert quotient.amount == 200
    assert quotient2.amount == 200 and quotient2.is_int_coin()
    assert quotient3.amount == 200 and quotient3.is_dec_coin()
    assert quotient4.amount == 333 and quotient4.is_int_coin()

    # modulo
    rem = coin.mod(43)
    assert rem.denom == "uluna"
    assert rem.amount == coin.amount % 43
Ejemplo n.º 10
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,
     )
Ejemplo n.º 11
0
def test_18_digits_of_precision():
    examples = [
        ["0.5", "0.500000000000000000"],
        ["0.00625", "0.006250000000000000"],
        ["3913.11", "3913.110000000000000000"],
        ["-23.11", "-23.110000000000000000"],
        ["-3", "-3.000000000000000000"],
        ["-3.0000000000000000001", "-3.000000000000000000"],
    ]

    for (example, expected) in examples:
        assert str(Dec(example)) == expected
Ejemplo n.º 12
0
def test_deserializes():
    examples = [
        "138875042105.980753034749566779",
        "8447.423744387144096286",
        "3913.113789811986907029",
        "0.500000000000000000",
        "0.006250000000000000",
        "-23.128250000000000023",
        "242.000000000000028422",
        "-242.000000000000020422",
    ]

    for example in examples:
        assert str(Dec(example)) == example
Ejemplo n.º 13
0
def test_zero():
    zero = Dec("0")
    assert str(zero) == "0.000000000000000000"
Ejemplo n.º 14
0
 async def reward_weight(self) -> Dec:
     res = await self._c._get(f"/treasury/reward_weight")
     return Dec(res)
Ejemplo n.º 15
0
 async def tax_rate(self) -> Dec:
     res = await self._c._get(f"/treasury/tax_rate")
     return Dec(res)
Ejemplo n.º 16
0
 async def terra_pool_delta(self) -> Dec:
     res = await self._c._get(f"/market/terra_pool_delta")
     return Dec(res.get("result"))
Ejemplo n.º 17
0
 async def inflation(self) -> Dec:
     return Dec(await self._c._get(f"/minting/inflation"))
Ejemplo n.º 18
0
def test_add_sub_resolution():
    zero = Dec("0")
    unit = Dec("0.000000000000000001")
    assert zero.add(unit) == unit
    assert zero.sub(unit) == unit.mul(-1)
Ejemplo n.º 19
0
 async def annual_provisions(self) -> Dec:
     return Dec(await self._c._get(f"/minting/annual-provisions"))
Ejemplo n.º 20
0
    for (example, expected) in examples:
        assert str(Dec(example)) == expected


def test_add_sub_resolution():
    zero = Dec("0")
    unit = Dec("0.000000000000000001")
    assert zero.add(unit) == unit
    assert zero.sub(unit) == unit.mul(-1)


@pytest.mark.parametrize(
    "d1,d2,mul,quo,add,sub",
    [
        [Dec(0), Dec(1), Dec(0),
         Dec(0), Dec(1), Dec(-1)],
        [Dec(0), Dec(-1), Dec(-0),
         Dec(0), Dec(-1), Dec(1)],
        [
            Dec(3),
            Dec(7),
            Dec(21),
            Dec.with_prec("428571428571428571", 18),
            Dec(10),
            Dec(-4),
        ],
        [Dec(2), Dec(4),
         Dec(8), Dec.with_prec(5, 1),
         Dec(6), Dec(-2)],
        [Dec(100), Dec(100),