package handler import ( "ripple/config" "ripple/state" ) func finalizeCreditor(amount uint64, creditor uint64, fee uint64, sealPenalty uint64, penaltyTicker uint64) uint64 { if creditor == 0 || amount == 0 { return 0 } if fee > creditor { fee = creditor } if sealPenalty >= amount { return fee } return ((creditor-fee)*(amount-sealPenalty) + fee*penaltyTicker) / amount } func finalizeCreditorIn(payment state.Payment, sealPenalty uint64, penaltyTicker uint64) uint64 { return finalizeCreditor(payment.Amount, payment.CreditorIn, payment.FeeIn, sealPenalty, penaltyTicker) } func finalizeCreditorOut(payment state.Payment, sealPenalty uint64, penaltyTicker uint64) uint64 { return finalizeCreditor(payment.Amount, payment.CreditorOut, payment.FeeOut, sealPenalty, penaltyTicker) } func cancelCreditor(amount uint64, creditor uint64, penaltyTicker uint64) uint64 { if penaltyTicker >= amount { return creditor } else { return creditor * penaltyTicker / amount } } func insufficientPenalty(amount, fee uint64, penaltyRate uint32) bool { return amount == 0 || penaltyRate < uint32(config.PenaltyRate.Seconds())/2 || float32(fee) / float32(amount) < config.FeeRate/2 } func calculateCreditorOut(bandwidthOut uint64, pf state.Pathfinding) uint64 { feeOut := pf.FeeOut() if bandwidthOut > pf.Amount+feeOut+pf.Tax { return pf.Amount+feeOut+pf.Tax } return bandwidthOut } func calculateCreditorIn(creditorPrev, bandwidthIn uint64, pf state.Pathfinding) (uint64, bool) { feeIn := pf.FeeIn() if creditorPrev >= pf.Amount+feeIn+pf.Tax { return 0, true } creditorIn := pf.Amount+feeIn+pf.Tax-creditorPrev if creditorIn > bandwidthIn { return 0, false } return creditorIn, true }