예제 #1
0
def test_fee_estimate(flat_fee, prop_fee_cli, max_lin_imbalance_fee,
                      target_amount, expected_fee):
    """ Tests the backwards fee calculation. """
    capacity = TA(10_000)

    prop_fee = ppm_fee_per_channel(ProportionalFeeAmount(prop_fee_cli))
    imbalance_fee = None
    if max_lin_imbalance_fee > 0:
        # This created a simple asymmetric imbalance fee
        imbalance_fee = [(0, 0), (capacity, 0),
                         (2 * capacity, max_lin_imbalance_fee)]

    tn = TokenNetworkForTests(
        channels=[
            dict(participant1=1, participant2=2),
            dict(participant1=2, participant2=3)
        ],
        default_capacity=capacity,
    )

    tn.set_fee(2,
               1,
               flat=flat_fee,
               proportional=prop_fee,
               imbalance_penalty=imbalance_fee)
    tn.set_fee(2,
               3,
               flat=flat_fee,
               proportional=prop_fee,
               imbalance_penalty=imbalance_fee)
    assert tn.estimate_fee(1, 3, value=PA(target_amount)) == expected_fee
예제 #2
0
 def estimate_fee(self, initator: int, target: int, value=PA(10), max_paths=1):
     result = self.get_paths(
         source=a(initator),
         target=a(target),
         value=value,
         max_paths=max_paths,
         address_to_reachability=self.address_to_reachability,
     )
     if not result:
         return None
     return result[0]["estimated_fee"]
 def estimate_fee(self,
                  initator: int,
                  target: int,
                  value=PA(100),
                  max_paths=1):
     paths = self.get_paths(
         source=a(initator),
         target=a(target),
         value=value,
         max_paths=max_paths,
         reachability_state=self.reachability_state,
     )
     if not paths:
         return None
     return paths[0].estimated_fee
예제 #4
0
def test_fees_in_routing():
    tn = TokenNetworkForTests(
        channels=[dict(participant1=1, participant2=2), dict(participant1=2, participant2=3)]
    )

    # Make sure that routing works and the default fees are zero
    result = tn.get_paths(
        source=a(1),
        target=a(3),
        value=PA(10),
        max_paths=1,
        address_to_reachability=tn.address_to_reachability,
    )
    assert len(result) == 1
    assert [PrettyBytes(decode_hex(node)) for node in result[0]["path"]] == [a(1), a(2), a(3)]
    assert result[0]["estimated_fee"] == 0

    # Fees for the initiator are ignored
    tn.set_fee(1, 2, flat=FA(1))
    assert tn.estimate_fee(1, 3) == 0

    # Node 2 demands fees for incoming transfers
    tn.set_fee(2, 1, flat=FA(1))
    assert tn.estimate_fee(1, 3) == 1

    # Node 2 demands fees for outgoing transfers
    tn.set_fee(2, 3, flat=FA(1))
    assert tn.estimate_fee(1, 3) == 2

    # Same fee in the opposite direction
    assert tn.estimate_fee(3, 1) == 2

    # Reset fees to zero
    tn.set_fee(1, 2)
    tn.set_fee(2, 1)
    tn.set_fee(2, 3)

    # Now let's try imbalance fees
    tn.set_fee(2, 3, imbalance_penalty=[(TA(0), FA(0)), (TA(200), FA(200))])
    assert tn.estimate_fee(1, 3) == 10
    assert tn.estimate_fee(3, 1) == -10

    # When the range covered by the imbalance_penalty does include the
    # necessary balance values, the route should be considered invalid.
    tn.set_fee(2, 3, imbalance_penalty=[(TA(0), FA(0)), (TA(80), FA(200))])
    assert tn.estimate_fee(1, 3) is None
예제 #5
0
def test_compounding_fees():
    """ The transferred amount needs to include the fees for all mediators.
    Earlier mediators will apply the proportional fee not only on the payment
    amount, but also on the fees for later mediators.
    """
    tn = TokenNetworkForTests(
        channels=[
            dict(participant1=1, participant2=2),
            dict(participant1=2, participant2=3),
            dict(participant1=3, participant2=4),
        ]
    )
    tn.set_fee(2, 3, proportional=1e6)  # this is a 100% fee
    tn.set_fee(3, 4, proportional=1e6)
    assert tn.estimate_fee(1, 4, value=PA(1)) == (
        1  # fee for node 3
        + 2  # fee for node 2, which mediates 1 token for the payment and 1 for node 3's fees
    )