Skip to content

Commit

Permalink
chore: add critical error for funder balance
Browse files Browse the repository at this point in the history
  • Loading branch information
vindard committed Nov 16, 2023
1 parent 4000061 commit 4756dbf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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

0 comments on commit 4756dbf

Please sign in to comment.