mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 17:48:15 +00:00
store: credit late and overpaid crypto by share
This commit is contained in:
1 parent
64ce21fdff
commit
1519df2db8
9 files changed
+310
-100
No files matched your search
@@ -1,16 +1,18 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE NamedFieldPuns #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TupleSections #-}
|
||||
|
||||
-- | The credit of a closed order's late money to its balance: only confirmed money not yet credited, crypto at the lower
|
||||
-- of the invoice's rate and the current one. The rate is read outside any transaction, so the credit runs after the
|
||||
-- settlement that recorded the money commits, and again from the poller's pass for whatever is still not credited.
|
||||
-- | The credit of a name invoice's money to its order's balance: a closed order's late money, or the extra of an invoice paid above what
|
||||
-- it asked. Only confirmed money not yet credited, crypto as its share of the invoice's amount at the lower of the invoice's rate and
|
||||
-- the current one. The rate is read outside any transaction, so the credit runs after the settlement that recorded the money commits,
|
||||
-- and again from the poller's pass for whatever is still not credited.
|
||||
module StoreService.Names.LateCredits
|
||||
( CreditOutcome (..),
|
||||
LateMoney (..),
|
||||
minCurrentRateFraction,
|
||||
creditLateInvoice,
|
||||
lateCreditOf,
|
||||
lateMoneyOf,
|
||||
)
|
||||
where
|
||||
|
||||
@@ -22,7 +24,7 @@ import Data.Time.Clock (UTCTime)
|
||||
import Simplex.Chat.PaymentService.Types (CryptoCurrency, InvoiceId)
|
||||
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
|
||||
import StoreService.Names.Store (LateCredit (..), LateCreditState (..), LateSupport (..), NamePhase (..), creditLatePayment, lateCreditState, setLateSupport)
|
||||
import StoreService.Providers (ProviderError (..), Rate (..), unitDecimals)
|
||||
import StoreService.Providers (ProviderError (..), Rate (..))
|
||||
import StoreService.Store.Invoices (lockInvoice)
|
||||
import Text.Read (readMaybe)
|
||||
|
||||
@@ -30,12 +32,16 @@ import Text.Read (readMaybe)
|
||||
minCurrentRateFraction :: Rational
|
||||
minCurrentRateFraction = 1 % 2
|
||||
|
||||
-- | CONoInvoiceRate: crypto due with no invoice rate recorded to credit it at. CORefused: the credit's number was taken meanwhile,
|
||||
-- which the invoice's lock rules out.
|
||||
-- | A closed order's late money, or the extra crypto of an invoice that paid its order.
|
||||
data LateMoney = LMLate | LMOverpaid
|
||||
deriving (Eq, Show)
|
||||
|
||||
-- | CONoInvoiceFigures: crypto due with no invoice rate or invoiced units recorded to credit it at. CORefused: the credit's number was
|
||||
-- taken meanwhile, which the invoice's lock rules out.
|
||||
data CreditOutcome
|
||||
= CONothingDue
|
||||
| COCredited LateCredit
|
||||
| CONoInvoiceRate
|
||||
| COCredited LateMoney LateCredit
|
||||
| CONoInvoiceFigures
|
||||
| CORateFailed ProviderError
|
||||
| CORateFloor Text Rate
|
||||
| CORefused
|
||||
@@ -46,9 +52,9 @@ creditLateInvoice st readRate invId now =
|
||||
withTransaction st (`lateCreditState` invId) >>= \case
|
||||
Just s | creditable s && moneyDue s -> case lsCoin s of
|
||||
Nothing -> credit Nothing
|
||||
Just coin -> case invoiceRate s of
|
||||
Nothing -> pure CONoInvoiceRate
|
||||
Just (invoiceText, invoiceValue) ->
|
||||
Just coin -> case invoiceFigures s of
|
||||
Nothing -> pure CONoInvoiceFigures
|
||||
Just (invoiceText, invoiceValue, _) ->
|
||||
readRate coin >>= \case
|
||||
Left e -> pure $ CORateFailed e
|
||||
Right rate
|
||||
@@ -62,33 +68,60 @@ creditLateInvoice st readRate invId now =
|
||||
lateCreditState db invId >>= \case
|
||||
Just s | creditable s, Just c <- lateCreditOf s rate -> do
|
||||
applied <- creditLatePayment db c now
|
||||
pure $ if applied then COCredited c else CORefused
|
||||
pure $ if applied then COCredited (lateMoneyOf s) c else CORefused
|
||||
_ -> pure CONothingDue
|
||||
|
||||
-- | A closed order's money, unless a rate below the floor sent it to support for good, so a later recovery cannot credit money refunded by hand.
|
||||
-- | An order its invoice paid gets credits only for the crypto paid above what the invoice asked.
|
||||
lateMoneyOf :: LateCreditState -> LateMoney
|
||||
lateMoneyOf LateCreditState {lsPhase} = if lsPhase == PUnpaid then LMLate else LMOverpaid
|
||||
|
||||
-- | A closed order's money, or an overpaid order's crypto, unless a rate below the floor sent it to support for good, so a later
|
||||
-- recovery cannot credit money refunded by hand.
|
||||
creditable :: LateCreditState -> Bool
|
||||
creditable LateCreditState {lsPhase, lsLive, lsSupport} = lsPhase == PUnpaid && not lsLive && lsSupport /= Just LSRateFloor
|
||||
creditable s@LateCreditState {lsLive, lsCoin, lsSupport} =
|
||||
lsSupport /= Just LSRateFloor && case lateMoneyOf s of
|
||||
LMLate -> not lsLive
|
||||
LMOverpaid -> lsCoin /= Nothing
|
||||
|
||||
moneyDue :: LateCreditState -> Bool
|
||||
moneyDue s@LateCreditState {lsConfirmedCents, lsCreditedCents} = case lsCoin s of
|
||||
Nothing -> lsConfirmedCents > lsCreditedCents
|
||||
moneyDue s = case lsCoin s of
|
||||
Nothing -> centsDue s > 0
|
||||
Just _ -> unitsDue s > 0
|
||||
|
||||
-- | Card money is never paid above its invoice, so only a closed order's counts.
|
||||
centsDue :: LateCreditState -> Int
|
||||
centsDue s@LateCreditState {lsConfirmedCents, lsCreditedCents} = case lateMoneyOf s of
|
||||
LMLate -> lsConfirmedCents - lsCreditedCents
|
||||
LMOverpaid -> 0
|
||||
|
||||
-- | All of a closed order's confirmed units, and only those above the invoiced units of an overpaid one, less what was credited.
|
||||
unitsDue :: LateCreditState -> Integer
|
||||
unitsDue LateCreditState {lsConfirmedUnits, lsCreditedUnits} = maybe 0 toInteger lsConfirmedUnits - toInteger lsCreditedUnits
|
||||
unitsDue s@LateCreditState {lsConfirmedUnits, lsInvoicedUnits, lsCreditedUnits} = creditableUnits - toInteger lsCreditedUnits
|
||||
where
|
||||
confirmed = maybe 0 toInteger lsConfirmedUnits
|
||||
creditableUnits = case lateMoneyOf s of
|
||||
LMLate -> confirmed
|
||||
LMOverpaid -> maybe 0 (max 0 . (confirmed -) . toInteger) lsInvoicedUnits
|
||||
|
||||
invoiceRate :: LateCreditState -> Maybe (Text, Scientific)
|
||||
invoiceRate LateCreditState {lsInvoiceRate} = lsInvoiceRate >>= \t -> (t,) <$> readMaybe (T.unpack t)
|
||||
-- | The invoice's rate as written and as a decimal, and its invoiced units, above 0.
|
||||
invoiceFigures :: LateCreditState -> Maybe (Text, Scientific, Integer)
|
||||
invoiceFigures LateCreditState {lsInvoiceRate, lsInvoicedUnits} = do
|
||||
t <- lsInvoiceRate
|
||||
v <- readMaybe (T.unpack t)
|
||||
invoiced <- toInteger <$> lsInvoicedUnits
|
||||
if invoiced > 0 then Just (t, v, invoiced) else Nothing
|
||||
|
||||
-- | The next credit of the money not yet credited: card cents as they are, crypto units at the lower rate, rounded down to the cent.
|
||||
-- | The next credit of the money not yet credited: card cents as they are, crypto units as their share of the invoice's amount at the
|
||||
-- lower rate, rounded down to the cent.
|
||||
lateCreditOf :: LateCreditState -> Maybe Rate -> Maybe LateCredit
|
||||
lateCreditOf s@LateCreditState {lsInvoice, lsRegistration, lsCredits, lsConfirmedCents, lsCreditedCents} rate = case (lsCoin s, rate, invoiceRate s) of
|
||||
lateCreditOf s@LateCreditState {lsInvoice, lsRegistration, lsCredits, lsInvoiceAmount} rate = case (lsCoin s, rate, invoiceFigures s) of
|
||||
(Nothing, _, _)
|
||||
| lsConfirmedCents > lsCreditedCents -> Just (credit Nothing Nothing Nothing Nothing (lsConfirmedCents - lsCreditedCents))
|
||||
(Just coin, Just Rate {rateText, rateValue}, Just (invoiceText, invoiceValue))
|
||||
| centsDue s > 0 -> Just (credit Nothing Nothing Nothing Nothing (centsDue s))
|
||||
(Just _, Just Rate {rateText, rateValue}, Just (invoiceText, invoiceValue, invoiced))
|
||||
| unitsDue s > 0 ->
|
||||
let (usedText, usedValue) = if rateValue < invoiceValue then (rateText, rateValue) else (invoiceText, invoiceValue)
|
||||
cents = floor (fromInteger (unitsDue s) * toRational usedValue * 100 / 10 ^ unitDecimals coin)
|
||||
share = toInteger lsInvoiceAmount * unitsDue s % invoiced
|
||||
cents = floor (share * toRational usedValue / toRational invoiceValue)
|
||||
in Just (credit (Just $ fromInteger $ unitsDue s) (Just invoiceText) (Just rateText) (Just usedText) cents)
|
||||
_ -> Nothing
|
||||
where
|
||||
|
||||
@@ -301,6 +301,8 @@ data LateCreditState = LateCreditState
|
||||
lsReceivedUnits :: Maybe Int64,
|
||||
lsConfirmedUnits :: Maybe Int64,
|
||||
lsInvoiceRate :: Maybe Text,
|
||||
lsInvoiceAmount :: Int,
|
||||
lsInvoicedUnits :: Maybe Int64,
|
||||
lsCreditedUnits :: Int64,
|
||||
lsCreditedCents :: Int,
|
||||
lsCredits :: Int,
|
||||
@@ -977,6 +979,15 @@ lateFrom =
|
||||
closedUnpaid :: Query
|
||||
closedUnpaid = " r.phase = ? AND r.live = 0"
|
||||
|
||||
-- | Its order paid by it, with crypto received above what it asked; takes the unpaid phase. The last term matches the partial index of
|
||||
-- overpaid payments, so the poller's pass reads only those.
|
||||
overpaidOrder :: Query
|
||||
overpaidOrder = " r.phase <> ? AND" <> cryptoInvoice <> " AND p.received_units > p.invoiced_units"
|
||||
|
||||
-- | Confirmed crypto above what the invoice asked, not all credited.
|
||||
uncreditedExtra :: Query
|
||||
uncreditedExtra = " p.confirmed_units - p.invoiced_units >" <> creditedTotal "units"
|
||||
|
||||
cardInvoice :: Query
|
||||
cardInvoice = " i.payment_crypto_currency IS NULL"
|
||||
|
||||
@@ -1013,14 +1024,23 @@ uncreditedMoney =
|
||||
-- Confirmed money waiting comes first, so its alert never asks for a refund by hand.
|
||||
pendingCase :: Query
|
||||
pendingCase =
|
||||
" CASE WHEN" <> uncreditedMoney <> " THEN " <> literal LPUncredited
|
||||
<> " WHEN COALESCE(p.amount, 0) = 0 AND (p.crypto_paid IS NOT NULL OR p.paid_in_full = 1) THEN " <> literal LPConfirming
|
||||
<> " WHEN" <> cardInvoice <> " AND COALESCE(p.amount, 0) > p.confirmed_amount THEN " <> literal LPUnconfirmed
|
||||
<> " WHEN p.received_units > COALESCE(p.confirmed_units, 0) THEN " <> literal LPUnconfirmed
|
||||
<> " WHEN" <> cryptoInvoice <> " AND p.received_units IS NULL AND COALESCE(p.amount, 0) > 0 THEN " <> literal LPUncounted
|
||||
" CASE WHEN" <> uncreditedMoney <> " THEN " <> pendingLiteral LPUncredited
|
||||
<> " WHEN COALESCE(p.amount, 0) = 0 AND (p.crypto_paid IS NOT NULL OR p.paid_in_full = 1) THEN " <> pendingLiteral LPConfirming
|
||||
<> " WHEN" <> cardInvoice <> " AND COALESCE(p.amount, 0) > p.confirmed_amount THEN " <> pendingLiteral LPUnconfirmed
|
||||
<> " WHEN p.received_units > COALESCE(p.confirmed_units, 0) THEN " <> pendingLiteral LPUnconfirmed
|
||||
<> " WHEN" <> cryptoInvoice <> " AND p.received_units IS NULL AND COALESCE(p.amount, 0) > 0 THEN " <> pendingLiteral LPUncounted
|
||||
<> " END"
|
||||
where
|
||||
literal p = fromString $ "'" <> T.unpack (textEncode p) <> "'"
|
||||
|
||||
-- | Why an overpaid invoice's extra is not all credited: confirmed extra not credited yet, or received units above the confirmed ones,
|
||||
-- which overpaidOrder already holds above the invoiced ones.
|
||||
overpaidPendingCase :: Query
|
||||
overpaidPendingCase =
|
||||
" CASE WHEN" <> uncreditedExtra <> " THEN " <> pendingLiteral LPUncredited
|
||||
<> " WHEN p.received_units > COALESCE(p.confirmed_units, 0) THEN " <> pendingLiteral LPUnconfirmed
|
||||
<> " END"
|
||||
|
||||
pendingLiteral :: LatePending -> Query
|
||||
pendingLiteral p = fromString $ "'" <> T.unpack (textEncode p) <> "'"
|
||||
|
||||
-- | What an alert names as received: units for crypto, and cents for card or for crypto that could not be counted in units.
|
||||
receivedFigure :: Query
|
||||
@@ -1033,17 +1053,17 @@ lateColumns =
|
||||
<> creditedTotal "units"
|
||||
<> ","
|
||||
<> creditedTotal "cents"
|
||||
<> ", (SELECT COUNT(*) FROM sx_badge_service_name_late_credits c WHERE c.invoice_id = ni.invoice_id), p.created_at"
|
||||
<> ", (SELECT COUNT(*) FROM sx_badge_service_name_late_credits c WHERE c.invoice_id = ni.invoice_id), p.created_at, i.amount, p.invoiced_units"
|
||||
|
||||
type LateRow = (Text, Text, NamePhase, BoolInt, Maybe Text, Maybe LateSupport, Int, Int, Maybe Int64, Maybe Int64) :. (Maybe Text, Int64, Int, Int, UTCTime)
|
||||
type LateRow = (Text, Text, NamePhase, BoolInt, Maybe Text, Maybe LateSupport, Int, Int, Maybe Int64, Maybe Int64) :. (Maybe Text, Int64, Int, Int, UTCTime, Int, Maybe Int64)
|
||||
|
||||
toLateState :: LateRow -> Either Text LateCreditState
|
||||
toLateState
|
||||
( (inv, reg, lsPhase, BI lsLive, coin_, lsSupport, lsReceivedCents, lsConfirmedCents, lsReceivedUnits, lsConfirmedUnits)
|
||||
:. (lsInvoiceRate, lsCreditedUnits, lsCreditedCents, lsCredits, lsPaymentCreatedAt)
|
||||
:. (lsInvoiceRate, lsCreditedUnits, lsCreditedCents, lsCredits, lsPaymentCreatedAt, lsInvoiceAmount, lsInvoicedUnits)
|
||||
) = do
|
||||
lsCoin <- traverse (\c -> note ("invoices.payment_crypto_currency: " <> c) (textToCryptoCurrency c)) coin_
|
||||
pure LateCreditState {lsInvoice = InvoiceId inv, lsRegistration = RegistrationId reg, lsPhase, lsLive, lsCoin, lsSupport, lsReceivedCents, lsConfirmedCents, lsReceivedUnits, lsConfirmedUnits, lsInvoiceRate, lsCreditedUnits, lsCreditedCents, lsCredits, lsPaymentCreatedAt}
|
||||
pure LateCreditState {lsInvoice = InvoiceId inv, lsRegistration = RegistrationId reg, lsPhase, lsLive, lsCoin, lsSupport, lsReceivedCents, lsConfirmedCents, lsReceivedUnits, lsConfirmedUnits, lsInvoiceRate, lsInvoiceAmount, lsInvoicedUnits, lsCreditedUnits, lsCreditedCents, lsCredits, lsPaymentCreatedAt}
|
||||
|
||||
-- | Nothing without a payment row.
|
||||
lateCreditState :: DB.Connection -> InvoiceId -> IO (Maybe LateCreditState)
|
||||
@@ -1087,38 +1107,43 @@ lateWait db (InvoiceId invId) =
|
||||
(Just LPConfirming, Nothing) -> Just LWConfirming
|
||||
(Just LPUnconfirmed, Nothing) -> Just LWConfirming
|
||||
|
||||
-- | Closed orders' invoices with confirmed money not all credited, for the poller's credit pass; none a rate below the floor sent to support.
|
||||
-- | Closed orders' invoices with confirmed money not all credited, then overpaid invoices with confirmed extra not all credited, for the
|
||||
-- poller's credit pass; none a rate below the floor sent to support.
|
||||
uncreditedLateInvoices :: DB.Connection -> IO [InvoiceId]
|
||||
uncreditedLateInvoices db =
|
||||
map (InvoiceId . fromOnly)
|
||||
<$> DB.query
|
||||
db
|
||||
("SELECT ni.invoice_id" <> lateFrom <> " WHERE" <> closedUnpaid <> " AND" <> uncreditedMoney <> " AND (ni.late_support IS NULL OR ni.late_support <> ?) ORDER BY p.created_at, ni.invoice_id")
|
||||
(PUnpaid, LSRateFloor)
|
||||
|
||||
-- | Pending money whose payment row was created before the cutoff, never alerted, or alerted for less money than now received.
|
||||
lateAlertDue :: DB.Connection -> UTCTime -> IO [LateAlert]
|
||||
lateAlertDue db cutoff =
|
||||
DB.query
|
||||
db
|
||||
( lateColumns
|
||||
<> ","
|
||||
<> pendingCase
|
||||
<> ","
|
||||
<> receivedFigure
|
||||
<> ", ni.late_alerted_at, ni.late_alerted_units"
|
||||
<> lateFrom
|
||||
<> " WHERE"
|
||||
<> closedUnpaid
|
||||
<> " AND p.created_at < ? AND"
|
||||
<> pendingCase
|
||||
<> " IS NOT NULL AND (ni.late_alerted_at IS NULL OR"
|
||||
<> receivedFigure
|
||||
<> " > COALESCE(ni.late_alerted_units, -1)) ORDER BY p.created_at, ni.invoice_id"
|
||||
)
|
||||
(PUnpaid, truncateToSecond cutoff)
|
||||
>>= decodeAll toAlert
|
||||
uncreditedLateInvoices db = (<>) <$> uncredited closedUnpaid uncreditedMoney <*> uncredited overpaidOrder uncreditedExtra
|
||||
where
|
||||
uncredited order money =
|
||||
map (InvoiceId . fromOnly)
|
||||
<$> DB.query
|
||||
db
|
||||
("SELECT ni.invoice_id" <> lateFrom <> " WHERE" <> order <> " AND" <> money <> " AND (ni.late_support IS NULL OR ni.late_support <> ?) ORDER BY p.created_at, ni.invoice_id")
|
||||
(PUnpaid, LSRateFloor)
|
||||
|
||||
-- | Pending money of closed orders, then of overpaid invoices, whose payment row was created before the cutoff, never alerted, or alerted
|
||||
-- for less money than now received.
|
||||
lateAlertDue :: DB.Connection -> UTCTime -> IO [LateAlert]
|
||||
lateAlertDue db cutoff = (<>) <$> alertsDue closedUnpaid pendingCase <*> alertsDue overpaidOrder overpaidPendingCase
|
||||
where
|
||||
alertsDue order pending =
|
||||
DB.query
|
||||
db
|
||||
( lateColumns
|
||||
<> ","
|
||||
<> pending
|
||||
<> ","
|
||||
<> receivedFigure
|
||||
<> ", ni.late_alerted_at, ni.late_alerted_units"
|
||||
<> lateFrom
|
||||
<> " WHERE"
|
||||
<> order
|
||||
<> " AND p.created_at < ? AND"
|
||||
<> pending
|
||||
<> " IS NOT NULL AND (ni.late_alerted_at IS NULL OR"
|
||||
<> receivedFigure
|
||||
<> " > COALESCE(ni.late_alerted_units, -1)) ORDER BY p.created_at, ni.invoice_id"
|
||||
)
|
||||
(PUnpaid, truncateToSecond cutoff)
|
||||
>>= decodeAll toAlert
|
||||
toAlert :: LateRow :. (Maybe LatePending, Maybe Int64, Maybe UTCTime, Maybe Int64) -> Either Text LateAlert
|
||||
toAlert (row :. (pending_, laReceived, laAlertedAt, laAlertedReceived)) = case pending_ of
|
||||
Just laPending -> (\laState -> LateAlert {laState, laPending, laReceived, laAlertedAt, laAlertedReceived}) <$> toLateState row
|
||||
|
||||
@@ -34,7 +34,7 @@ module StoreService.Poller
|
||||
where
|
||||
|
||||
import StoreService.Config (PollConfig (..))
|
||||
import StoreService.Names.LateCredits (CreditOutcome (..), creditLateInvoice)
|
||||
import StoreService.Names.LateCredits (CreditOutcome (..), LateMoney (..), creditLateInvoice, lateMoneyOf)
|
||||
import StoreService.Names.Phases (Phases, RegistrationId (..))
|
||||
import StoreService.Names.Store (CleanupKept (..), LateAlert (..), LateCredit (..), LateCreditState (..), LatePending (..), LateSupport (..), claimLateAlert, deleteUnpaidRegistration, lateAlertDue, latePending, uncreditedLateInvoices, unpaidCleanupCandidates)
|
||||
import StoreService.Orders (decide, expireOverdue, settleOrder)
|
||||
@@ -332,7 +332,8 @@ creditDue PollerEnv {peCreditedAt, pePoll = PollConfig {pIdleSeconds}} now = ato
|
||||
when due $ writeTVar peCreditedAt (Just now)
|
||||
pure due
|
||||
|
||||
-- | Credits every closed order's confirmed money not all credited, so a crash or a failed rate read after a settlement loses nothing.
|
||||
-- | Credits every closed order's confirmed money, and every overpaid invoice's confirmed extra, not all credited, so a crash or a failed
|
||||
-- rate read after a settlement loses nothing.
|
||||
creditPass :: PollerEnv -> UTCTime -> IO ()
|
||||
creditPass env@PollerEnv {peStore, peProviders} now = do
|
||||
due <- creditDue env now
|
||||
@@ -366,13 +367,13 @@ creditLate env@PollerEnv {peStore} p now invId@(InvoiceId invText) = do
|
||||
outcome <- creditLateInvoice peStore (cachedRate env p) invId now
|
||||
case outcome of
|
||||
CONothingDue -> pure ()
|
||||
COCredited LateCredit {lcRegistration = RegistrationId rid, lcUnits, lcInvoiceRate, lcCurrentRate, lcRateUsed, lcCents} ->
|
||||
COCredited money LateCredit {lcRegistration = RegistrationId rid, lcUnits, lcInvoiceRate, lcCurrentRate, lcRateUsed, lcCents} ->
|
||||
logInfo $
|
||||
"names: invoice " <> invText <> " credited " <> tshow lcCents <> " cents to the balance of registration " <> rid
|
||||
"names: invoice " <> invText <> credited money <> tshow lcCents <> " cents" <> extra money <> " to the balance of registration " <> rid
|
||||
<> maybe "" (\units -> " for " <> tshow units <> " units at " <> fromMaybe "?" lcRateUsed <> " (invoice " <> fromMaybe "?" lcInvoiceRate <> ", current " <> fromMaybe "?" lcCurrentRate <> ")") lcUnits
|
||||
CONoInvoiceRate -> do
|
||||
due <- dueToWarn env now ("no invoice rate: " <> invText)
|
||||
when due $ logError ("names: invoice " <> invText <> " has confirmed late money, but no invoice rate was recorded to credit it at; check it at the provider")
|
||||
CONoInvoiceFigures -> do
|
||||
due <- dueToWarn env now ("no invoice figures: " <> invText)
|
||||
when due $ logError ("names: invoice " <> invText <> " has confirmed late money, but no invoice rate or invoiced units were recorded to credit it at; check it at the provider")
|
||||
CORateFailed (ProviderError e) -> do
|
||||
due <- dueToWarn env now ("rate read failed: " <> tshow (pProvider p))
|
||||
when due $ logWarn ("names: late credits wait, as the current rate cannot be read; each pass tries again: " <> e)
|
||||
@@ -381,6 +382,13 @@ creditLate env@PollerEnv {peStore} p now invId@(InvoiceId invText) = do
|
||||
when due $ logError ("names: invoice " <> invText <> " is not credited: the current rate " <> rateText <> " is below half its rate " <> invoiceRate <> "; it is never credited by itself, and support refunds it by hand")
|
||||
CORefused -> logError ("names: the credit of invoice " <> invText <> " was refused, its credit number taken meanwhile; the next pass tries again")
|
||||
pure outcome
|
||||
where
|
||||
credited = \case
|
||||
LMLate -> " credited "
|
||||
LMOverpaid -> " was overpaid; credited "
|
||||
extra = \case
|
||||
LMLate -> ""
|
||||
LMOverpaid -> " of the extra"
|
||||
|
||||
-- | Money not yet confirmed waits for the settlement that confirms it.
|
||||
pendingLate :: PollerEnv -> InvoiceId -> IO ()
|
||||
@@ -425,12 +433,15 @@ lateReason env@PollerEnv {peStore, peProviders} LateAlert {laPending, laState =
|
||||
uncounted = "an amount BTCPay reported with more decimals than its unit; check it at the provider"
|
||||
|
||||
lateAlertText :: LateAlert -> Text -> Text
|
||||
lateAlertText LateAlert {laState = LateCreditState {lsInvoice = InvoiceId invText, lsRegistration = RegistrationId rid, lsCoin, lsReceivedCents, lsConfirmedCents, lsCreditedCents, lsReceivedUnits, lsConfirmedUnits, lsCreditedUnits}} reason =
|
||||
"names: invoice " <> invText <> " of order " <> rid <> ": " <> figures <> "; " <> reason
|
||||
lateAlertText LateAlert {laState = s@LateCreditState {lsInvoice = InvoiceId invText, lsRegistration = RegistrationId rid, lsCoin, lsReceivedCents, lsConfirmedCents, lsCreditedCents, lsReceivedUnits, lsConfirmedUnits, lsInvoicedUnits, lsCreditedUnits}} reason =
|
||||
"names: invoice " <> invText <> " of order " <> rid <> overpaid <> ": " <> figures <> "; " <> reason
|
||||
where
|
||||
(overpaid, invoiced) = case (lateMoneyOf s, lsCoin, lsInvoicedUnits) of
|
||||
(LMOverpaid, Just coin, Just units) -> (", overpaid", tshow units <> " " <> unitName coin <> " invoiced, ")
|
||||
_ -> ("", "")
|
||||
figures = case (lsCoin, lsReceivedUnits) of
|
||||
(Nothing, _) -> amounts "cents" (toInteger lsReceivedCents) (toInteger lsConfirmedCents) (toInteger lsCreditedCents)
|
||||
(Just coin, Just received) -> amounts (unitName coin) (toInteger received) (maybe 0 toInteger lsConfirmedUnits) (toInteger lsCreditedUnits)
|
||||
(Just coin, Just received) -> invoiced <> amounts (unitName coin) (toInteger received) (maybe 0 toInteger lsConfirmedUnits) (toInteger lsCreditedUnits)
|
||||
(Just _, Nothing) -> tshow lsReceivedCents <> " cents received at the invoice's rate, not counted in units"
|
||||
amounts unit received confirmed credited =
|
||||
tshow received <> " " <> unit <> " received, " <> tshow confirmed <> " " <> unit <> " confirmed, " <> tshow credited <> " " <> unit <> " credited"
|
||||
|
||||
@@ -417,7 +417,9 @@ chainText chain = T.intercalate "; " $ zipWith linkText chain (map (Just . lkReg
|
||||
RPInvoice method (InvoiceId invId) ->
|
||||
let by = " by " <> methodText method <> " on invoice " <> invId
|
||||
late = sum $ map lcCents lateCredits
|
||||
in [dollars cents <> by | cents > 0] <> [dollars late <> " late" <> by | late > 0]
|
||||
-- an order its invoice paid is credited only the extra of an overpayment
|
||||
kind = if cents > 0 then " overpaid" else " late"
|
||||
in [dollars cents <> by | cents > 0] <> [dollars late <> kind <> by | late > 0]
|
||||
RPCode _ -> ["a prepaid code"]
|
||||
RPBalance -> []
|
||||
fromBalance cents = \case
|
||||
|
||||
@@ -357,6 +357,7 @@ namesRefundChatTests = describe "StoreService names: operator refund" $ do
|
||||
it "refuses a balance held by an order whose invoice holds money, before its expiry and past it" testRefundFundedHolder
|
||||
it "lists every order of a chain of three that the balance came through" testRefundChain
|
||||
it "stops the chain of a late-paid difference order at itself, its cents marked late" testRefundLateChain
|
||||
it "marks the extra credited to an order its invoice paid as overpaid" testRefundOverpaidChain
|
||||
it "is not a group command" $ \_ -> groupCmdAction GROwner "/refund s 10000" `shouldBe` IgnoreMsg
|
||||
|
||||
refundRaw :: HasCallStack => ChatController -> Text -> IO (Either Text Text)
|
||||
@@ -474,6 +475,16 @@ testRefundLateChain ps = withStoreService ps $ \_ _ cc -> do
|
||||
refundRaw cc "inv-d 2400" `shouldReturn` Right ("Recorded a refund of $24.00 for order d. Refund it now, by: " <> paidBy <> ".")
|
||||
Balances.figures st "s" t0 `shouldReturn` Just (15000, 15000, 0, Nothing)
|
||||
|
||||
testRefundOverpaidChain :: HasCallStack => TestParams -> IO ()
|
||||
testRefundOverpaidChain ps = withStoreService ps $ \_ _ cc -> do
|
||||
let st = chatStore cc
|
||||
paidBy = "order o: $200.00 by btc on invoice inv-o, $24.00 overpaid by btc on invoice inv-o"
|
||||
Balances.placeOrderRow st "o" False 20000 t0
|
||||
queued <- reg st "o"
|
||||
write st queued {nrPhase = PQueued} `shouldReturn` Written (nrVersion queued + 1)
|
||||
Balances.creditLate st (Balances.lateCredit "o" 1 (Just 40000) 2400) `shouldReturn` True
|
||||
refundRaw cc "inv-o" `shouldReturn` Right ("Order o: balance $24.00 (credited $24.00, refunded $0.00). Paid by: " <> paidBy <> ". To record its refund: //refund inv-o 2400")
|
||||
|
||||
data TurnHolder a = TurnHolder {thAction :: Async a, thRelease :: MVar ()}
|
||||
|
||||
-- | Runs the action until its body is inside the turn, where the body waits for the release.
|
||||
|
||||
@@ -629,6 +629,8 @@ testLateScans = withServiceStore $ \st -> do
|
||||
lsReceivedUnits = Just 300000,
|
||||
lsConfirmedUnits = Just 100000,
|
||||
lsInvoiceRate = Just "62500",
|
||||
lsInvoiceAmount = 20000,
|
||||
lsInvoicedUnits = Nothing,
|
||||
lsCreditedUnits = 100000,
|
||||
lsCreditedCents = 1500,
|
||||
lsCredits = 1,
|
||||
|
||||
@@ -79,6 +79,14 @@ namesLateCreditTests = describe "StoreService names: balance life" $ do
|
||||
it "credits an invoice past the read window after a hinted read" testHintPastWindow
|
||||
it "credits Stripe's succeeded amount without reading a rate" testStripeCredited
|
||||
it "credits late XMR in piconero when the plugin shows a confirmation, and alerts it to support when it shows none" testXmrLate
|
||||
describe "price share" $ do
|
||||
it "credits late XMR as its share of the price, so BTCPay's network fee is never credited" testPriceShareXmr
|
||||
it "credits a whole late BTC payment with BTCPay's added fee at the price, and an overpaid one the share of all it received" testPriceShareBtc
|
||||
it "credits nothing for an invoice with no invoiced units recorded, logging it as an error" testNoInvoicedUnits
|
||||
describe "overpayment" $ do
|
||||
it "credits the extra of an invoice overpaid in time to its order's balance, logged, once, and nothing for an exact or card payment" testOverpaidInTime
|
||||
it "credits the extra at the next pass when its credit step could not read the rate" testOverpaidByPass
|
||||
it "alerts the extra of an overpaid HighSpeed invoice to support, naming the invoiced units, and nothing for an exact one" testOverpaidHighSpeed
|
||||
describe "balances with late credits" $ do
|
||||
it "keeps a credit made while the balance is held, the held spend settling" testCreditWhileHeld
|
||||
it "refuses a covered spend that read the amount before a credit" testSpendBeforeCredit
|
||||
@@ -131,6 +139,7 @@ btcPaid units confirmed =
|
||||
rcvCrypto = Just (T.pack $ show units),
|
||||
rcvDue = Nothing,
|
||||
rcvUnits = Just units,
|
||||
rcvInvoicedUnits = Just lateUnits,
|
||||
rcvRate = Just "62500",
|
||||
rcvConfirmed = Confirmed {cfCents = CurrencyAmount (fromIntegral $ satCents confirmed), cfUnits = Just confirmed},
|
||||
rcvUncreditable = Nothing
|
||||
@@ -143,6 +152,7 @@ cardPaid cents confirmed =
|
||||
rcvCrypto = Nothing,
|
||||
rcvDue = Nothing,
|
||||
rcvUnits = Nothing,
|
||||
rcvInvoicedUnits = Nothing,
|
||||
rcvRate = Nothing,
|
||||
rcvConfirmed = Confirmed {cfCents = CurrencyAmount (fromIntegral confirmed), cfUnits = Nothing},
|
||||
rcvUncreditable = Nothing
|
||||
@@ -151,6 +161,10 @@ cardPaid cents confirmed =
|
||||
nothingPaid :: Received
|
||||
nothingPaid = cardPaid 0 0
|
||||
|
||||
-- | Bitcoin received and confirmed on an invoice that asked the given satoshi.
|
||||
btcPaidOf :: Int64 -> Int64 -> Int64 -> Received
|
||||
btcPaidOf invoiced units confirmed = (btcPaid units confirmed) {rcvInvoicedUnits = Just invoiced}
|
||||
|
||||
-- | 320000 satoshi: 20000 cents at the invoice's rate, 19200 at the stub's current rate of 60000.5.
|
||||
lateUnits :: Int64
|
||||
lateUnits = 320000
|
||||
@@ -233,7 +247,7 @@ testHeldSettled :: IO ()
|
||||
testHeldSettled = withLate $ \l@Late {lStore = st} -> do
|
||||
source st "s" 10000
|
||||
holder st "h" "s" 10000 (at 60) `shouldReturn` Right ()
|
||||
settleAt l "h" (SigSettled (btcPaid 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
settleAt l "h" (SigSettled (btcPaidOf 480000 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
NameRegistration {nrPhase, nrLive, nrBalanceUsed} <- reg st "h"
|
||||
(nrPhase, nrLive, nrBalanceUsed) `shouldBe` (PQueued, True, 10000)
|
||||
figures st "s" (at 10) `shouldReturn` Just (0, 10000, 0, Nothing)
|
||||
@@ -243,8 +257,8 @@ testFundedThenSettledLate :: IO ()
|
||||
testFundedThenSettledLate = withLate $ \l@Late {lStore = st} -> do
|
||||
source st "s" 10000
|
||||
holder st "h" "s" 10000 (at 60) `shouldReturn` Right ()
|
||||
settleAt l "h" (SigFunded (btcPaid 480000 0) PaidInFull) (at 50) `shouldReturn` Right ISOpen
|
||||
settleAt l "h" (SigSettled (btcPaid 480000 480000) (at 120)) (at 120) `shouldReturn` Right ISPaid
|
||||
settleAt l "h" (SigFunded (btcPaidOf 480000 480000 0) PaidInFull) (at 50) `shouldReturn` Right ISOpen
|
||||
settleAt l "h" (SigSettled (btcPaidOf 480000 480000 480000) (at 120)) (at 120) `shouldReturn` Right ISPaid
|
||||
nrPhase <$> reg st "h" `shouldReturn` PQueued
|
||||
figures st "s" (at 120) `shouldReturn` Just (0, 10000, 0, Nothing)
|
||||
|
||||
@@ -253,7 +267,7 @@ testHoldNotSpent = withLate $ \l@Late {lStore = st} -> do
|
||||
source st "unspentsrc" 10000
|
||||
holder st "unspent" "unspentsrc" 10000 (at 60) `shouldReturn` Right ()
|
||||
withTransaction st $ \db -> DB.execute db "UPDATE sx_badge_service_name_balances SET reserved_by = NULL WHERE registration_id = ?" (Only ("unspentsrc" :: Text))
|
||||
logs <- capturingStderr $ settleAt l "unspent" (SigSettled (btcPaid 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
logs <- capturingStderr $ settleAt l "unspent" (SigSettled (btcPaidOf 480000 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
logs `shouldSatisfy` logged "ERROR" "queued registration unspent as paid, but the balance of registration unspentsrc it held could not be spent"
|
||||
nrPhase <$> reg st "unspent" `shouldReturn` PQueued
|
||||
balanceOf st "unspentsrc" `shouldReturn` Just (10000, 10000, 0)
|
||||
@@ -282,7 +296,7 @@ testLiveOrderExistsHolder = withLate $ \l@Late {lStore = st} -> do
|
||||
holder st "a" "s" 10000 (at 60) `shouldReturn` Right ()
|
||||
meetsLiveOrder st "a"
|
||||
setRate l (Left $ ProviderError "rates down")
|
||||
void $ readSignal l "a" (SigSettled (btcPaid lateUnits lateUnits) (at 10))
|
||||
void $ readSignal l "a" (SigSettled (btcPaidOf 480000 lateUnits lateUnits) (at 10))
|
||||
figures st "s" t0 `shouldReturn` Just (10000, 10000, 0, Nothing)
|
||||
balanceOf st "a" `shouldReturn` Nothing
|
||||
setRate l (Right stubRate)
|
||||
@@ -296,7 +310,7 @@ testSettlementBeforeRefund = withLate $ \l@Late {lStore = st} -> do
|
||||
source st "s" 10000
|
||||
holder st "h" "s" 10000 (at 60) `shouldReturn` Right ()
|
||||
recordRefund turns st "s" 10000 (at 5) >>= (`shouldSatisfy` \case Left (RefundHeld _) -> True; _ -> False)
|
||||
settleAt l "h" (SigSettled (btcPaid 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
settleAt l "h" (SigSettled (btcPaidOf 480000 480000 480000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
recordRefund turns st "s" 10000 (at 11) >>= (`shouldSatisfy` \case Left _ -> True; Right _ -> False)
|
||||
balanceOf st "s" `shouldReturn` Just (0, 10000, 0)
|
||||
|
||||
@@ -308,7 +322,7 @@ testLateCredited = withLate $ \l@Late {lStore = st} -> do
|
||||
holder st "credited" "paid" 10000 (at 60) `shouldReturn` Right ()
|
||||
settleAt l "credited" (SigClosed nothingPaid PaidInPart) (at 61) `shouldReturn` Right ISExpired
|
||||
figures st "paid" t0 `shouldReturn` Just (10000, 10000, 0, Nothing)
|
||||
logs <- readSignal l "credited" (SigClosed (btcPaid lateUnits lateUnits) PaidInPart)
|
||||
logs <- readSignal l "credited" (SigClosed (btcPaidOf 480000 lateUnits lateUnits) PaidInPart)
|
||||
logs `shouldSatisfy` logged "INFO" "invoice inv-credited credited 19200 cents to the balance of registration credited for 320000 units at 60000.5 (invoice 62500, current 60000.5)"
|
||||
logs `shouldNotSatisfy` logged "ERROR" "inv-credited"
|
||||
balanceOf st "credited" `shouldReturn` Just (19200, 19200, 0)
|
||||
@@ -458,7 +472,7 @@ testRateProblemsNamed = withLate $ \l@Late {lStore = st} -> do
|
||||
settleAt l "norate" (SigClosed (btcPaid lateUnits lateUnits) {rcvRate = Nothing} PaidInPart) (at 70) `shouldReturn` Right ISExpired
|
||||
logs <- (<>) <$> passAt l (at 71) <*> passAt l (at 72)
|
||||
count "invoice inv-noprovider has late money to credit, but no provider" logs `shouldBe` 1
|
||||
logs `shouldSatisfy` logged "ERROR" "invoice inv-norate has confirmed late money, but no invoice rate was recorded to credit it at"
|
||||
logs `shouldSatisfy` logged "ERROR" "invoice inv-norate has confirmed late money, but no invoice rate or invoiced units were recorded to credit it at"
|
||||
|
||||
testOneRateReadPerPass :: IO ()
|
||||
testOneRateReadPerPass = withLate $ \l@Late {lStore = st} -> do
|
||||
@@ -550,6 +564,7 @@ xmrPaid uncreditable =
|
||||
rcvCrypto = Just "0.32095000",
|
||||
rcvDue = Nothing,
|
||||
rcvUnits = Just xmrUnits,
|
||||
rcvInvoicedUnits = Just xmrUnits,
|
||||
rcvRate = Just "168.25",
|
||||
rcvConfirmed = maybe (Confirmed {cfCents = CurrencyAmount 5400, cfUnits = Just xmrUnits}) (const Confirmed {cfCents = CurrencyAmount 0, cfUnits = Just 0}) uncreditable,
|
||||
rcvUncreditable = uncreditable
|
||||
@@ -560,7 +575,7 @@ xmrPaid uncreditable =
|
||||
testXmrLate :: IO ()
|
||||
testXmrLate = withLate $ \l@Late {lStore = st} -> do
|
||||
mapM_ (\r -> closedOrder l r False) ["xmrshown", "xmrnone"]
|
||||
withTransaction st $ \db -> DB.execute_ db "UPDATE sx_badge_service_invoices SET payment_crypto_currency = 'xmr' WHERE invoice_id IN ('inv-xmrshown', 'inv-xmrnone')"
|
||||
withTransaction st $ \db -> DB.execute_ db "UPDATE sx_badge_service_invoices SET payment_crypto_currency = 'xmr', amount = 5400 WHERE invoice_id IN ('inv-xmrshown', 'inv-xmrnone')"
|
||||
setRate l (Right $ rateOf "150" 150)
|
||||
void $ readSignal l "xmrshown" (SigSettled (xmrPaid Nothing) (at 70))
|
||||
creditsOf st "xmrshown" `shouldReturn` [(Just 320950000000, Just "168.25", Just "150", Just "150", 4814)]
|
||||
@@ -571,6 +586,90 @@ testXmrLate = withLate $ \l@Late {lStore = st} -> do
|
||||
alertsAt l (Right stubRate) (pastWindow now 60)
|
||||
>>= (`shouldSatisfy` logged "ERROR" "invoice inv-xmrnone of order xmrnone: 320950000000 piconero received, 0 piconero confirmed, 0 piconero credited; the Monero plugin shows no confirmation of this money, so it is never credited by itself; refund by hand only a transaction confirmed on chain")
|
||||
|
||||
-- price share
|
||||
|
||||
-- | A closed unpaid order of the given cents whose invoice asked crypto of the given coin.
|
||||
closedOrderOf :: Late -> Text -> Text -> Int -> IO ()
|
||||
closedOrderOf l@Late {lStore} r coin cents = do
|
||||
placeOrderRow lStore r False cents (at 60)
|
||||
withTransaction lStore $ \db -> DB.execute db "UPDATE sx_badge_service_invoices SET payment_crypto_currency = ? WHERE invoice_id = ?" (coin, "inv-" <> r)
|
||||
settleAt l r (SigClosed nothingPaid PaidInPart) (at 61) `shouldReturn` Right ISExpired
|
||||
|
||||
-- | The review's $100 Monero invoice: 0.50211283 XMR at a rate of 200, 0.00211283 of it BTCPay's network fee.
|
||||
testPriceShareXmr :: IO ()
|
||||
testPriceShareXmr = withLate $ \l@Late {lStore = st} -> do
|
||||
closedOrderOf l "xmr" "xmr" 10000
|
||||
setRate l (Right $ rateOf "200" 200)
|
||||
let piconero = 502112830000
|
||||
paid = Received {rcvAmount = CurrencyAmount 10042, rcvCrypto = Just "0.50211283", rcvDue = Just "0", rcvUnits = Just piconero, rcvInvoicedUnits = Just piconero, rcvRate = Just "200", rcvConfirmed = Confirmed {cfCents = CurrencyAmount 10042, cfUnits = Just piconero}, rcvUncreditable = Nothing}
|
||||
void $ readSignal l "xmr" (SigClosed paid PaidInFull)
|
||||
creditsOf st "xmr" `shouldReturn` [(Just piconero, Just "200", Just "200", Just "200", 10000)]
|
||||
balanceOf st "xmr" `shouldReturn` Just (10000, 10000, 0)
|
||||
|
||||
testPriceShareBtc :: IO ()
|
||||
testPriceShareBtc = withLate $ \l@Late {lStore = st} -> do
|
||||
closedOrder l "fee" False
|
||||
closedOrder l "over" False
|
||||
setRate l (Right $ rateOf "62500" 62500)
|
||||
void $ readSignal l "fee" (SigClosed (btcPaidOf 330000 330000 330000) PaidInFull)
|
||||
creditsOf st "fee" `shouldReturn` [(Just 330000, Just "62500", Just "62500", Just "62500", 20000)]
|
||||
setRate l (Right stubRate)
|
||||
void $ readSignal l "over" (SigClosed (btcPaidOf lateUnits 480000 480000) PaidInFull)
|
||||
creditsOf st "over" `shouldReturn` [(Just 480000, Just "62500", Just "60000.5", Just "60000.5", 28800)]
|
||||
balanceOf st "over" `shouldReturn` Just (28800, 28800, 0)
|
||||
|
||||
testNoInvoicedUnits :: IO ()
|
||||
testNoInvoicedUnits = withLate $ \l@Late {lStore = st} -> do
|
||||
closedOrder l "noinvoiced" False
|
||||
settleAt l "noinvoiced" (SigClosed (btcPaid lateUnits lateUnits) {rcvInvoicedUnits = Nothing} PaidInFull) (at 70) `shouldReturn` Right ISExpired
|
||||
logs <- passAt l (at 71)
|
||||
logs `shouldSatisfy` logged "ERROR" "invoice inv-noinvoiced has confirmed late money, but no invoice rate or invoiced units were recorded to credit it at"
|
||||
balanceOf st "noinvoiced" `shouldReturn` Nothing
|
||||
|
||||
-- overpayment
|
||||
|
||||
-- | 480000 satoshi on an invoice of 320000: the extra 160000 is 10000 cents of the price, 9600 at the stub's current rate.
|
||||
testOverpaidInTime :: IO ()
|
||||
testOverpaidInTime = withLate $ \l@Late {lStore = st} -> do
|
||||
mapM_ (\(r, card) -> placeOrderRow st r card 20000 (at 60)) [("over", False), ("exact", False), ("card", True)]
|
||||
logs <- readSignal l "over" (SigSettled (btcPaidOf lateUnits 480000 480000) (at 10))
|
||||
logs `shouldSatisfy` logged "INFO" "invoice inv-over was overpaid; credited 9600 cents of the extra to the balance of registration over for 160000 units at 60000.5 (invoice 62500, current 60000.5)"
|
||||
nrPhase <$> reg st "over" `shouldReturn` PQueued
|
||||
creditsOf st "over" `shouldReturn` [(Just 160000, Just "62500", Just "60000.5", Just "60000.5", 9600)]
|
||||
balanceOf st "over" `shouldReturn` Just (9600, 9600, 0)
|
||||
void $ passAt l (at 20)
|
||||
void $ readSignal l "over" (SigSettled (btcPaidOf lateUnits 480000 480000) (at 10))
|
||||
length <$> creditsOf st "over" `shouldReturn` 1
|
||||
void $ readSignal l "exact" (SigSettled (btcPaidOf lateUnits lateUnits lateUnits) (at 10))
|
||||
void $ readSignal l "card" (SigSettled (cardPaid 20000 20000) (at 10))
|
||||
void $ passAt l (at 21)
|
||||
mapM (fmap nrPhase . reg st) ["exact", "card"] `shouldReturn` [PQueued, PQueued]
|
||||
mapM (balanceOf st) ["exact", "card"] `shouldReturn` [Nothing, Nothing]
|
||||
|
||||
testOverpaidByPass :: IO ()
|
||||
testOverpaidByPass = withLate $ \l@Late {lStore = st} -> do
|
||||
placeOrderRow st "over" False 20000 (at 60)
|
||||
setRate l (Left $ ProviderError "rates down")
|
||||
void $ readSignal l "over" (SigSettled (btcPaidOf lateUnits 480000 480000) (at 10))
|
||||
balanceOf st "over" `shouldReturn` Nothing
|
||||
withTransaction st uncreditedLateInvoices `shouldReturn` [invOf "over"]
|
||||
setRate l (Right stubRate)
|
||||
void $ passAt l (at 20)
|
||||
balanceOf st "over" `shouldReturn` Just (9600, 9600, 0)
|
||||
withTransaction st uncreditedLateInvoices `shouldReturn` []
|
||||
|
||||
testOverpaidHighSpeed :: IO ()
|
||||
testOverpaidHighSpeed = withLate $ \l@Late {lStore = st} -> do
|
||||
mapM_ (\r -> placeOrderRow st r False 20000 (at 60)) ["hsover", "hsexact"]
|
||||
let highSpeed units = (btcPaidOf lateUnits units 0) {rcvUncreditable = Just UCHighSpeed}
|
||||
settleAt l "hsover" (SigSettled (highSpeed 480000) (at 70)) (at 70) `shouldReturn` Right ISPaid
|
||||
settleAt l "hsexact" (SigSettled (highSpeed lateUnits) (at 70)) (at 70) `shouldReturn` Right ISPaid
|
||||
void $ passAt l (at 71)
|
||||
mapM (balanceOf st) ["hsover", "hsexact"] `shouldReturn` [Nothing, Nothing]
|
||||
logs <- alertsAt l (Right stubRate) (pastWindow (at 70) 60)
|
||||
logs `shouldSatisfy` logged "ERROR" "invoice inv-hsover of order hsover, overpaid: 320000 satoshi invoiced, 480000 satoshi received, 0 satoshi confirmed, 0 satoshi credited; the invoice was made under HighSpeed"
|
||||
logs `shouldNotSatisfy` T.isInfixOf "inv-hsexact"
|
||||
|
||||
-- balances with late credits
|
||||
|
||||
testCreditWhileHeld :: IO ()
|
||||
@@ -580,7 +679,7 @@ testCreditWhileHeld = withLate $ \l@Late {lStore = st} -> do
|
||||
holder st "h" "x" 19200 (at 60) `shouldReturn` Right ()
|
||||
void $ readSignal l "x" (SigClosed (btcPaid 400000 400000) PaidInPart)
|
||||
figures st "x" (at 10) `shouldReturn` Just (24000, 24000, 0, Just BalanceHold {bhHolder = RegistrationId "h", bhUntil = at 60, bhOverdue = False, bhFunded = False})
|
||||
settleAt l "h" (SigSettled (btcPaid 330000 330000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
settleAt l "h" (SigSettled (btcPaidOf 332800 330000 330000) (at 10)) (at 10) `shouldReturn` Right ISPaid
|
||||
figures st "x" (at 10) `shouldReturn` Just (4800, 24000, 0, Nothing)
|
||||
invariantHolds st "x"
|
||||
|
||||
@@ -714,7 +813,7 @@ testConcurrentCredits = withServiceStores $ \open -> do
|
||||
let readRate :: CryptoCurrency -> IO (Either ProviderError Rate)
|
||||
readRate _ = Right stubRate <$ meet arrived
|
||||
(a, b) <- concurrently (creditLateInvoice st readRate (invOf r) (at 80)) (creditLateInvoice other readRate (invOf r) (at 80))
|
||||
(n, length [() | COCredited _ <- [a, b]], length [() | CONothingDue <- [a, b]]) `shouldBe` (n, 1, 1)
|
||||
(n, length [() | COCredited _ _ <- [a, b]], length [() | CONothingDue <- [a, b]]) `shouldBe` (n, 1, 1)
|
||||
length <$> creditsOf st r `shouldReturn` 1
|
||||
balanceOf st r `shouldReturn` Just (19200, 19200, 0)
|
||||
refused <- forM [1 .. rounds] $ \n -> do
|
||||
@@ -749,7 +848,7 @@ testCreditAgainstSettlement = withServiceStores $ \open -> do
|
||||
raise = meet arrived >> settleOrder other otherWaiters Nothing (invOf r) (SigClosed (btcPaid lateUnits lateUnits) PaidInPart) (at 80)
|
||||
(credited, raised) <- concurrently (creditLateInvoice st readRate (invOf r) (at 80)) raise
|
||||
(n, raised) `shouldBe` (n, Right ISExpired)
|
||||
credited `shouldSatisfy` \case COCredited _ -> True; _ -> False
|
||||
credited `shouldSatisfy` \case COCredited _ _ -> True; _ -> False
|
||||
creditLateInvoice st (const $ pure $ Right stubRate) (invOf r) (at 81) >>= (`shouldSatisfy` (/= CORefused))
|
||||
units <- map (\(u, _, _, _, _) -> u) <$> creditsOf st r
|
||||
(n, sum (map (fromMaybe 0) units)) `shouldBe` (n, lateUnits)
|
||||
|
||||
@@ -702,14 +702,15 @@ reported fake poller Ctx {cClient} event ref state = do
|
||||
statusOf <$> webRequestBody cClient "POST" "/webhooks/btcpay" (webhookSigHeader (bWebhookSecret $ fbConfig fake) body) body `shouldReturn` 200
|
||||
drainHints poller
|
||||
|
||||
-- | Paid in full and settled: $100.00 at the fixtures' rate of 108000.12 dollars a bitcoin.
|
||||
-- | Paid in full and settled: $100.00 at the fixtures' rate of 108000.12 dollars a bitcoin, as the invoice asked.
|
||||
settledState :: [JT.Pair]
|
||||
settledState = ["status" J..= ("Settled" :: Text), "paymentMethodPaid" J..= ("0.00092593" :: Text)]
|
||||
settledState = ["status" J..= ("Settled" :: Text), "amount" J..= ("0.00092593" :: Text), "paymentMethodPaid" J..= ("0.00092593" :: Text)]
|
||||
|
||||
-- | $54.00 confirmed after the invoice expired, credited as 5400 cents at that rate.
|
||||
-- | 0.0005 bitcoin confirmed after the $100.00 invoice of 0.00092593 expired, credited as its share of the price, 5399 cents.
|
||||
latePaid :: [JT.Pair]
|
||||
latePaid =
|
||||
[ "additionalStatus" J..= ("PaidLate" :: Text),
|
||||
[ "additionalStatus" J..= ("PaidPartial" :: Text),
|
||||
"amount" J..= ("0.00092593" :: Text),
|
||||
"paymentMethodPaid" J..= lateBtc,
|
||||
"payments" J..= [J.object ["id" J..= ("late-tx" :: Text), "receivedDate" J..= (1700003600 :: Int), "value" J..= lateBtc, "fee" J..= ("0.00000000" :: Text), "status" J..= ("Settled" :: Text)]]
|
||||
]
|
||||
@@ -768,7 +769,7 @@ testSpendEndToEnd ps = withBTCPaySpend ps $ \cc fake poller ctx@Ctx {cClock} ->
|
||||
(\v -> (field "latePayment" v, field "balance" v)) <$> viewOf ctx first `shouldReturn` (Just $ J.object ["waiting" J..= ("crediting" :: Text)], Just J.Null)
|
||||
setRates fake ratesUp
|
||||
void $ capturingStderr $ forgetRates poller >> getCurrentTime >>= creditPass poller
|
||||
(\v -> (field "latePayment" v, field "balance" v)) <$> viewOf ctx first `shouldReturn` (Just J.Null, balanceObject 5400 0)
|
||||
(\v -> (field "latePayment" v, field "balance" v)) <$> viewOf ctx first `shouldReturn` (Just J.Null, balanceObject 5399 0)
|
||||
balanceView ctx src `shouldReturn` balanceObject 10000 0
|
||||
-- dynamis again with a new commitment: BTCPay settles the difference, and the poller queues the order and spends the hold
|
||||
(200, again) <- spend ctx src (dynamisAgain 3)
|
||||
|
||||
@@ -49,6 +49,7 @@ namesStoreTests = describe "StoreService names: store" $ do
|
||||
it "turns a code_issued order taken at price 0 and not live at its next version, dropping issued_code_id with its index and foreign key, which the down migration restores" testDropTakenCodes
|
||||
it "makes taken orders not live and gives a taken paid order a balance of its price, none to a code order or a price of 0, and rolls back the balance tables and columns" testNameBalancesMigration
|
||||
it "gives the orders stored before it empty agreed terms, and rolls the column back keeping them" testNameTermsMigration
|
||||
it "adds the payments' invoiced units with the index of overpaid payments, and rolls both back keeping the payments" testInvoicedUnitsMigration
|
||||
describe "order hash" $ do
|
||||
it "is equal for the same order fields, and differs when any one field differs" testOrderHashFields
|
||||
it "keeps field boundaries, so moving a byte between two fields changes it" testOrderHashBoundaries
|
||||
@@ -163,7 +164,7 @@ namesTables = map ("sx_badge_service_name_" <>) ["prepaid_codes", "registrations
|
||||
|
||||
-- | Added to the badge tables' payments by the names migrations.
|
||||
balancePaymentColumns :: [Text]
|
||||
balancePaymentColumns = ["confirmed_amount", "received_units", "confirmed_units", "invoice_rate"]
|
||||
balancePaymentColumns = ["confirmed_amount", "received_units", "confirmed_units", "invoice_rate", "invoiced_units"]
|
||||
|
||||
badgeTables :: [Text]
|
||||
badgeTables = map ("sx_badge_service_" <>) ["invoices", "payments", "badge_prices", "badge_offers", "badge_purchases", "badge_ledger", "badge_issuances", "badge_codes", "badge_code_invoices", "group"]
|
||||
@@ -190,7 +191,7 @@ testNamesDownUp :: IO ()
|
||||
testNamesDownUp = withServiceStore $ \st -> do
|
||||
let names = dropWhile ((/= "20261013_names") . name) storeServiceSchemaMigrations
|
||||
badgeColumns = mapM (fmap sort . columnsOf st) badgeTables
|
||||
map name names `shouldBe` ["20261013_names", "20261022_drop_taken_codes", "20261022_name_balances", "20261022_name_late_wait", "20261023_name_terms"]
|
||||
map name names `shouldBe` ["20261013_names", "20261022_drop_taken_codes", "20261022_name_balances", "20261022_name_late_wait", "20261023_name_terms", "20261027_name_invoiced_units"]
|
||||
badgeBefore <- badgeColumns
|
||||
code <- issue st 1 usable
|
||||
placeCode st "r1" ownerA (order "alicebakery" 1) code `shouldReturn` Right ()
|
||||
@@ -233,6 +234,10 @@ testDropTakenCodes = withServiceStore $ \st -> do
|
||||
testNameBalancesMigration :: IO ()
|
||||
testNameBalancesMigration = withServiceStore $ \st -> do
|
||||
let balances = [m | m@Migration {name = "20261022_name_balances"} <- storeServiceSchemaMigrations]
|
||||
-- its index reads received_units, which the balances' down migration drops
|
||||
invoicedUnits = [m | m@Migration {name = "20261027_name_invoiced_units"} <- storeServiceSchemaMigrations]
|
||||
down = MTRDown (mapMaybe toDownMigration (balances <> invoicedUnits))
|
||||
up = MTRUp (balances <> invoicedUnits)
|
||||
applied :: IO (Bool, Bool, Bool)
|
||||
applied = (,,) <$> (not . null <$> columnsOf st "sx_badge_service_name_balances") <*> (elem "balance_source" <$> columnsOf st "sx_badge_service_name_registrations") <*> (elem "confirmed_units" <$> columnsOf st "sx_badge_service_payments")
|
||||
live :: Text -> IO Bool
|
||||
@@ -242,7 +247,7 @@ testNameBalancesMigration = withServiceStore $ \st -> do
|
||||
balanceOf :: Text -> IO (Maybe (Int, Int, Int, Maybe BalanceHold))
|
||||
balanceOf rid = withTransaction st $ \db -> fmap (\Balance {blAmount, blCredited, blRefunded, blHold} -> (blAmount, blCredited, blRefunded, blHold)) <$> getBalance db (RegistrationId rid) t0
|
||||
length balances `shouldBe` 1
|
||||
runMigrations st $ MTRDown (mapMaybe toDownMigration balances)
|
||||
runMigrations st down
|
||||
applied `shouldReturn` (False, False, False)
|
||||
code <- issue st 1 usable
|
||||
withTransaction st $ \db -> do
|
||||
@@ -253,14 +258,14 @@ testNameBalancesMigration = withServiceStore $ \st -> do
|
||||
insertOldRegistration db "converted" 3 "taken" 0 Nothing (Just "inv-converted")
|
||||
insertInvoiceRow db "inv-queued" (at 60) "paid"
|
||||
insertOldRegistration db "queued" 4 "queued" price Nothing (Just "inv-queued")
|
||||
runMigrations st $ MTRUp balances
|
||||
runMigrations st up
|
||||
applied `shouldReturn` (True, True, True)
|
||||
mapM live ["paid", "code", "converted", "queued"] `shouldReturn` [False, False, False, True]
|
||||
mapM balanceOf ["paid", "code", "converted", "queued"] `shouldReturn` [Just (price, price, 0, Nothing), Nothing, Nothing, Nothing]
|
||||
runMigrations st $ MTRDown (mapMaybe toDownMigration balances)
|
||||
runMigrations st down
|
||||
applied `shouldReturn` (False, False, False)
|
||||
mapM live ["paid", "queued"] `shouldReturn` [False, True]
|
||||
runMigrations st $ MTRUp balances
|
||||
runMigrations st up
|
||||
balanceOf "paid" `shouldReturn` Just (price, price, 0, Nothing)
|
||||
|
||||
testNameTermsMigration :: IO ()
|
||||
@@ -288,6 +293,27 @@ testNameTermsMigration = withServiceStore $ \st -> do
|
||||
runMigrations st $ MTRUp terms
|
||||
agreedTerms `shouldReturn` [("paid", "")]
|
||||
|
||||
testInvoicedUnitsMigration :: IO ()
|
||||
testInvoicedUnitsMigration = withServiceStore $ \st -> do
|
||||
let invoiced = [m | m@Migration {name = "20261027_name_invoiced_units"} <- storeServiceSchemaMigrations]
|
||||
applied :: IO (Bool, Bool)
|
||||
applied = (,) <$> (elem "invoiced_units" <$> columnsOf st "sx_badge_service_payments") <*> indexExists st "sx_badge_service_idx_payments_overpaid"
|
||||
payments :: IO [(Text, Maybe Int64)]
|
||||
payments = withTransaction st $ \db -> DB.query_ db "SELECT invoice_id, received_units FROM sx_badge_service_payments"
|
||||
length invoiced `shouldBe` 1
|
||||
applied `shouldReturn` (True, True)
|
||||
runMigrations st $ MTRDown (mapMaybe toDownMigration invoiced)
|
||||
applied `shouldReturn` (False, False)
|
||||
withTransaction st $ \db -> do
|
||||
insertInvoiceRow db "inv-paid" (at 60) "paid"
|
||||
insertPayment db "inv-paid"
|
||||
stored <- payments
|
||||
runMigrations st $ MTRUp invoiced
|
||||
applied `shouldReturn` (True, True)
|
||||
runMigrations st $ MTRDown (mapMaybe toDownMigration invoiced)
|
||||
applied `shouldReturn` (False, False)
|
||||
payments `shouldReturn` stored
|
||||
|
||||
-- | A registration row as the code before the balances wrote it, paid by the invoice or the code given.
|
||||
insertOldRegistration :: DB.Connection -> Text -> Word8 -> Text -> Int -> Maybe Int64 -> Maybe Text -> IO ()
|
||||
insertOldRegistration db rid n phase cents codeId invId = do
|
||||
|
||||
Reference in new issue
Block a user