wallet-core: handle suspended coins properly in refresh
This commit is contained in:
parent
97267e7d1f
commit
ffe6a95214
@ -67,6 +67,7 @@ import {
|
||||
EXCHANGE_COINS_LOCK,
|
||||
InternalWalletState,
|
||||
} from "../internal-wallet-state.js";
|
||||
import { assertUnreachable } from "../util/assertUnreachable.js";
|
||||
import {
|
||||
readSuccessResponseJsonOrThrow,
|
||||
readUnexpectedResponseDetails,
|
||||
@ -879,17 +880,30 @@ export async function createRefreshGroup(
|
||||
!!denom,
|
||||
"denomination for existing coin must be in database",
|
||||
);
|
||||
if (coin.status !== CoinStatus.Dormant) {
|
||||
coin.status = CoinStatus.Dormant;
|
||||
const coinAv = await tx.coinAvailability.get([
|
||||
coin.exchangeBaseUrl,
|
||||
coin.denomPubHash,
|
||||
coin.maxAge,
|
||||
]);
|
||||
checkDbInvariant(!!coinAv);
|
||||
checkDbInvariant(coinAv.freshCoinCount > 0);
|
||||
coinAv.freshCoinCount--;
|
||||
await tx.coinAvailability.put(coinAv);
|
||||
switch (coin.status) {
|
||||
case CoinStatus.Dormant:
|
||||
break;
|
||||
case CoinStatus.Fresh: {
|
||||
coin.status = CoinStatus.Dormant;
|
||||
const coinAv = await tx.coinAvailability.get([
|
||||
coin.exchangeBaseUrl,
|
||||
coin.denomPubHash,
|
||||
coin.maxAge,
|
||||
]);
|
||||
checkDbInvariant(!!coinAv);
|
||||
checkDbInvariant(coinAv.freshCoinCount > 0);
|
||||
coinAv.freshCoinCount--;
|
||||
await tx.coinAvailability.put(coinAv);
|
||||
break;
|
||||
}
|
||||
case CoinStatus.FreshSuspended: {
|
||||
// For suspended coins, we don't have to adjust coin
|
||||
// availability, as they are not counted as available.
|
||||
coin.status = CoinStatus.Dormant;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
assertUnreachable(coin.status);
|
||||
}
|
||||
const refreshAmount = coin.currentAmount;
|
||||
inputPerCoin.push(refreshAmount);
|
||||
|
@ -937,10 +937,7 @@ async function setCoinSuspended(
|
||||
if (c.status !== CoinStatus.Fresh) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
coinAvailability.freshCoinCount == null ||
|
||||
coinAvailability.freshCoinCount === 0
|
||||
) {
|
||||
if (coinAvailability.freshCoinCount === 0) {
|
||||
throw Error(
|
||||
`invalid coin count ${coinAvailability.freshCoinCount} in DB`,
|
||||
);
|
||||
@ -951,9 +948,6 @@ async function setCoinSuspended(
|
||||
if (c.status == CoinStatus.Dormant) {
|
||||
return;
|
||||
}
|
||||
if (coinAvailability.freshCoinCount == null) {
|
||||
coinAvailability.freshCoinCount = 0;
|
||||
}
|
||||
coinAvailability.freshCoinCount++;
|
||||
c.status = CoinStatus.Fresh;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user