Added withdrawals and deposits
This commit is contained in:
parent
f3bb1ff79a
commit
3ffea8b46b
@ -8,3 +8,19 @@ class WalletNotFoundError(Exception):
|
||||
super().__init__(
|
||||
f"Wallet of a user with id {self.owner_id} was not found for the guild with id {self.guild_id}"
|
||||
)
|
||||
|
||||
|
||||
class WalletInsufficientFunds(Exception):
|
||||
"""Wallet's balance is not sufficient to perform the operation"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
wallet: "Wallet",
|
||||
amount: float,
|
||||
) -> None:
|
||||
self.wallet = wallet
|
||||
self.amount = amount
|
||||
|
||||
super().__init__(
|
||||
f"Wallet of a user with id {self.wallet.owner_id} for the guild with id {self.wallet.guild_id} does not have sufficient funds to perform the operation (balance: {self.wallet.balance}, requested: {self.amount})"
|
||||
)
|
||||
|
@ -8,6 +8,7 @@ from bson import ObjectId
|
||||
from pymongo.results import InsertOneResult
|
||||
|
||||
from classes.errors import WalletNotFoundError
|
||||
from classes.errors.wallet import WalletInsufficientFunds
|
||||
from modules.database import col_wallets
|
||||
|
||||
logger: Logger = logging.getLogger(__name__)
|
||||
@ -94,11 +95,20 @@ class Wallet:
|
||||
async def unfreeze(self) -> None:
|
||||
await self._set("is_frozen", False)
|
||||
|
||||
# TODO Write a dosctring
|
||||
# TODO Write a docstring
|
||||
async def deposit(self, amount: float) -> None:
|
||||
await self._set("balance", round(self.balance + amount, 2))
|
||||
|
||||
# TODO Add a check to prevent negative balances
|
||||
# TODO Write a dosctring
|
||||
async def withdraw(self, amount: float) -> None:
|
||||
# TODO Write a docstring
|
||||
async def withdraw(
|
||||
self,
|
||||
amount: float,
|
||||
allow_overdraft: bool = False,
|
||||
overdraft_limit: Optional[float] = None,
|
||||
) -> None:
|
||||
if amount > self.balance and (
|
||||
not allow_overdraft or (allow_overdraft and amount > overdraft_limit)
|
||||
):
|
||||
raise WalletInsufficientFunds(self, amount)
|
||||
|
||||
await self._set("balance", round(self.balance - amount, 2))
|
||||
|
Loading…
x
Reference in New Issue
Block a user