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

Monetary Exchange #48

Merged
merged 7 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
File renamed without changes.
57 changes: 57 additions & 0 deletions src/main/py/exchange/offer_announcement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
"""
Module to manage exchange announcement functionality.

OfferAnnouncement class allows us to create a exchange announcement
"""


class OfferAnnouncement:
"""
Class to manage exchange announcements
"""

def __init__(
self,
artwork_id: str,
price: int,
exchange_type: str,
originator_public_key: str = "",
):
"""
Initializes an instance of the OfferAnnouncement class.
"""

self.artwork_id = artwork_id
self.originator_public_key = originator_public_key
self.price = price
self.exchange_type = exchange_type
self.deadline_reached = False

def get_artwork_id(self):
"""
Returns the artwork to be traded.
"""

return self.artwork_id

def get_originator_public_key(self):
"""
Returns the public key of the originator of the exchange announcement.
"""

return self.originator_public_key

def get_price(self):
"""
Returns the purchase price for the artwork.
"""

return self.price

def get_exchange_type(self):
"""
Returns the exchange type for the announcmnet (sale or trade).
"""

return self.exchange_type
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@

class OfferResponse:
"""
Class to manage trade responses
Class to manage exchange responses
"""

def __init__(
self,
trade_id: str,
exchange_id: str,
artwork_id: str,
price: int,
originator_public_key: str = "",
):
"""
Initializes an instance of the OfferResponse class.
- trade_id (str): The trade id to respond to.
"""
self.trade_id = trade_id

self.exchange_id = exchange_id
self.artwork_id = artwork_id
self.price = price
self.originator_public_key = originator_public_key

def get_trade_id(self):
def get_exchange_id(self):
"""
Returns the trade id to respond to.
Returns the exchange id to respond to.
"""

return self.trade_id
return self.exchange_id

def get_originator_public_key(self):
"""
Returns the public key of the originator of the trade response.
Returns the public key of the originator of the exchange response.
"""

return self.originator_public_key
Expand All @@ -45,3 +47,10 @@ def get_artwork_id(self):
"""

return self.artwork_id

def get_price(self):
"""
Returns the price from the OfferAnnouncement.
"""

return self.price
45 changes: 28 additions & 17 deletions src/main/py/peer/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
Module to manage peer inventory functionality.

The Inventory class keeps track of our commissions, owned artworks, and artworks pending trade.
The Inventory class keeps track of our commissions, owned artworks, and artworks pending exchange.
"""
import random
from commission.artwork import Artwork
Expand All @@ -15,9 +15,9 @@ def __init__(self) -> None:
"""Initializes an instance of the Inventory class"""
self.commissions = {}
self.owned_artworks = {}
self.pending_trades = {}
self.artworks_pending_trade = set()
self.completed_trades = set()
self.pending_exchanges = {}
self.artworks_pending_exchange = set()
self.completed_exchanges = set()
self.commission_canvases = {}

def add_commission(self, artwork: Artwork):
Expand All @@ -40,17 +40,18 @@ def add_owned_artwork(self, artwork: Artwork):

self.owned_artworks[artwork.key] = artwork

def add_pending_trade(self, trade_key, trade_offer):
def add_pending_exchange(self, exchange_key, exchange_offer):
"""
Adds an artwork pending trade to the inventory.
Adds an artwork pending exchange to the inventory.

Params:
- trade_key (bytes): The key of the artwork to add to the inventory.
- trade_offer (OfferAnnouncement | OfferResponse): The trade offer to add to the inventory.
- exchange_key (bytes): The key of the artwork to add to the inventory.
- exchange_offer (OfferAnnouncement | OfferResponse): The exchange offer
to add to the inventory.
"""

self.artworks_pending_trade.add(trade_offer.artwork_id)
self.pending_trades[trade_key] = trade_offer
self.artworks_pending_exchange.add(exchange_offer.artwork_id)
self.pending_exchanges[exchange_key] = exchange_offer

def remove_commission(self, artwork: Artwork):
"""
Expand All @@ -74,16 +75,16 @@ def remove_owned_artwork(self, artwork: Artwork):
if artwork.key in self.owned_artworks:
del self.owned_artworks[artwork.key]

def remove_pending_trade(self, trade_key):
def remove_pending_exchange(self, exchange_key):
"""
Removes an artwork pending trade from the inventory.
Removes an artwork pending exchange from the inventory.

Params:
- artwork (Artwork): The artwork to remove from the inventory.
"""

if trade_key in self.pending_trades:
del self.pending_trades[trade_key]
if exchange_key in self.pending_exchanges:
del self.pending_exchanges[exchange_key]

def get_commission(self, key: bytes):
"""
Expand Down Expand Up @@ -119,17 +120,27 @@ def is_owned_artwork(self, key: bytes):

return key in self.owned_artworks

def get_artwork_to_trade(self):
def get_artwork_to_exchange(self):
"""
Gets a random artwork to trade from the inventory.
Gets a random artwork to exchange from the inventory.
"""

available_artworks = [
artwork
for artwork in self.owned_artworks.values()
if artwork.key not in self.artworks_pending_trade
if artwork.key not in self.artworks_pending_exchange
]
if len(available_artworks) > 0:
random_artwork = random.choice(available_artworks)
return random_artwork
return None

def get_artwork_by_id(self, artwork_id: bytes):
"""
Returns an owned artwork from the inventory by its ID.
"""

for artwork in self.owned_artworks.values():
if artwork.id == artwork_id:
return artwork
raise KeyError(f"No owned artwork found for ID: {artwork_id}")
Loading