Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api): add critical error for funder balance #3535

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/api/src/app/payments/add-earn.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { intraledgerPaymentSendWalletIdForBtcWallet } from "./send-intraledger"

import { getBalanceForWallet } from "@/app/wallets"

import { getRewardsConfig, OnboardingEarn } from "@/config"
import { IPMetadataAuthorizer } from "@/domain/accounts-ips/ip-metadata-authorizer"
import {
InvalidIpMetadataError,
InvalidQuizQuestionIdError,
MissingIPMetadataError,
NoBtcWalletExistsForAccountError,
NotEnoughBalanceForRewardError,
UnauthorizedIPError,
UnknownRepositoryError,
} from "@/domain/errors"
Expand All @@ -23,6 +26,24 @@ import {
import { AccountsIpsRepository } from "@/services/mongoose/accounts-ips"
import { checkedToAccountId } from "@/domain/accounts"

const FunderBalanceChecker = () => {
const check = ({
balance,
amountToSend,
}: {
balance: Satoshis
amountToSend: Satoshis
}): ValidationError | true => {
if (balance < amountToSend) {
return new NotEnoughBalanceForRewardError(JSON.stringify({ balance, amountToSend }))
}

return true
}

return { check }
}

export const addEarn = async ({
quizQuestionId: quizQuestionIdString,
accountId: accountIdRaw,
Expand Down Expand Up @@ -88,6 +109,15 @@ export const addEarn = async ({
const shouldGiveReward = await RewardsRepository(accountId).add(quizQuestionId)
if (shouldGiveReward instanceof Error) return shouldGiveReward

const funderBalance = await getBalanceForWallet({ walletId: funderWalletId })
if (funderBalance instanceof Error) return funderBalance

const sendCheck = FunderBalanceChecker().check({
balance: funderBalance as Satoshis,
amountToSend: amount,
})
if (sendCheck instanceof Error) return sendCheck

const payment = await intraledgerPaymentSendWalletIdForBtcWallet({
senderWalletId: funderWalletId,
recipientWalletId,
Expand Down
3 changes: 3 additions & 0 deletions core/api/src/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export class MissingIPMetadataError extends ValidationError {}
export class InvalidIpMetadataError extends ValidationError {
level = ErrorLevel.Critical
}
export class NotEnoughBalanceForRewardError extends ValidationError {
level = ErrorLevel.Critical
}

export class UnauthorizedIPForOnboardingError extends AuthorizationError {}

Expand Down
5 changes: 5 additions & 0 deletions core/api/src/graphql/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ export const mapError = (error: ApplicationError): CustomGraphQLError => {
message = "Reward for quiz question was already claimed."
return new ValidationInternalError({ message, logger: baseLogger })

case "NotEnoughBalanceForRewardError":
message =
"Rewards wallet temporarily depleted. Please contact support if problem persists."
return new ValidationInternalError({ message, logger: baseLogger })

case "SubOneCentSatAmountForUsdSelfSendError":
case "SubOneCentSatAmountForUsdReceiveError":
message = "Amount sent was too low for recipient's usd wallet."
Expand Down
Loading