def test_imbalance_penalty(): r""" Test an imbalance penalty by moving back and forth The imbalance fee looks like 20 | / | / 10 |\. / | \. / 0 | \/ --------------- 0 50 100 For each input, we first assume the channel is used to forward tokens to a payee, which moves the capacity from x1 to x2. The we assume the same amount is mediated in the opposite direction (moving from x2 to x1) and check that the calculated fee is the same as before just with the opposite sign. """ v_schedule = FeeScheduleState(imbalance_penalty=[ (TokenAmount(0), FeeAmount(10)), (TokenAmount(50), FeeAmount(0)), (TokenAmount(100), FeeAmount(20)), ]) for x1, amount, expected_fee_payee, expected_fee_payer in [ (0, 50, -6, 10), (50, 50, 12, -20), (0, 10, -2, 2), (10, 10, -2, 2), (0, 20, -5, 4), (40, 15, 0, 0), ]: x2 = x1 + amount assert v_schedule.fee_payee( balance=Balance(100 - x1), amount=PaymentWithFeeAmount( amount)) == FeeAmount(expected_fee_payee) assert v_schedule.fee_payer( balance=Balance(100 - x2), amount=PaymentWithFeeAmount( amount)) == FeeAmount(expected_fee_payer) with pytest.raises(UndefinedMediationFee): v_schedule.fee_payee(balance=Balance(0), amount=PaymentWithFeeAmount(1)) with pytest.raises(UndefinedMediationFee): v_schedule.fee_payer(balance=Balance(100), amount=PaymentWithFeeAmount(1))
def test_basic_fee(): flat_schedule = FeeScheduleState(flat=FeeAmount(2)) assert flat_schedule.fee_payer(PaymentWithFeeAmount(10), balance=Balance(0)) == FeeAmount(2) prop_schedule = FeeScheduleState( proportional=ProportionalFeeAmount(int(0.01e6))) assert prop_schedule.fee_payer(PaymentWithFeeAmount(40), balance=Balance(0)) == FeeAmount(0) assert prop_schedule.fee_payer(PaymentWithFeeAmount(60), balance=Balance(0)) == FeeAmount(1) assert prop_schedule.fee_payer(PaymentWithFeeAmount(1000), balance=Balance(0)) == FeeAmount(10) combined_schedule = FeeScheduleState(flat=FeeAmount(2), proportional=ProportionalFeeAmount( int(0.01e6))) assert combined_schedule.fee_payer(PaymentWithFeeAmount(60), balance=Balance(0)) == FeeAmount(3)