package state func finalizeCreditor(creditorAmount, creditorFee, amount, sealPenalty, penaltyTicker uint64) uint64 { return (creditorAmount * (amount - sealPenalty) + creditorFee * penaltyTicker) / amount } func finalizeCreditorIn(payment Payment, penaltyTicker uint64) uint64 { var amountIn uint64 if payment.CreditorIn > payment.FeeIn { amountIn = payment.CreditorIn - payment.FeeIn } feeIn := payment.CreditorIn - amountIn return finalizeCreditor(amountIn, feeIn, payment.Amount, payment.SealPenalty(penaltyTicker), penaltyTicker) } func finalizeCreditorOut(payment Payment, penaltyTicker uint64) uint64 { var feeOut uint64 if payment.CreditorOut > payment.Amount { feeOut = payment.CreditorOut-payment.Amount } amountOut := payment.CreditorOut-feeOut return finalizeCreditor(amountOut, feeOut, payment.Amount, payment.SealPenalty(penaltyTicker), penaltyTicker) } func (s *Storage) MustFinalizeIncoming(payment Payment, paymentID [32]byte, penaltyTicker uint64) { s.MustFinalizeIncomingWithAmount(payment.Incoming, paymentID, finalizeCreditorIn(payment, penaltyTicker), payment.GetTaxRate()) } func (s *Storage) MustFinalizeOutgoing(payment Payment, paymentID [32]byte, penaltyTicker uint64) { s.MustFinalizePayment(payment.Outgoing, paymentID, -int64(finalizeCreditorOut(payment, penaltyTicker))) } func cancelCreditor(amount, creditor, penaltyTicker uint64) uint64 { if penaltyTicker >= amount { return creditor } else { return creditor * penaltyTicker / amount } } func (s *Storage) MustCancelIncoming(payment Payment, paymentID [32]byte, penaltyTicker uint64) { s.MustFinalizeIncomingWithAmount(payment.Incoming, paymentID, cancelCreditor(payment.Amount, payment.CreditorIn, penaltyTicker), payment.GetTaxRate()) } func (s *Storage) MustCancelOutgoing(payment Payment, paymentID [32]byte, penaltyTicker uint64) { s.MustFinalizePayment(payment.Outgoing, paymentID, -int64(cancelCreditor(payment.Amount, payment.CreditorOut, penaltyTicker))) }