mirror of
https://github.com/yzernik/squeaknode.git
synced 2026-08-13 12:33:25 +02:00
Simplify get payment summary in controller (#2159)
This commit is contained in:
parent
61e9ed67cb
commit
80a484ef3b
6 changed files with 98 additions and 40 deletions
|
|
@ -28,14 +28,13 @@ from proto import squeak_admin_pb2
|
|||
from squeaknode.admin.profile_image_util import bytes_to_base64_string
|
||||
from squeaknode.admin.profile_image_util import load_default_profile_image
|
||||
from squeaknode.core.download_result import DownloadResult
|
||||
from squeaknode.core.payment_summary import PaymentSummary
|
||||
from squeaknode.core.peer_address import Network
|
||||
from squeaknode.core.peer_address import PeerAddress
|
||||
from squeaknode.core.received_offer import ReceivedOffer
|
||||
from squeaknode.core.received_payment import ReceivedPayment
|
||||
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
|
||||
from squeaknode.core.sent_offer import SentOffer
|
||||
from squeaknode.core.sent_payment import SentPayment
|
||||
from squeaknode.core.sent_payment_summary import SentPaymentSummary
|
||||
from squeaknode.core.squeak_entry import SqueakEntry
|
||||
from squeaknode.core.squeak_peer import SqueakPeer
|
||||
from squeaknode.core.squeak_profile import SqueakProfile
|
||||
|
|
@ -149,14 +148,13 @@ def received_payment_to_message(received_payment: ReceivedPayment) -> squeak_adm
|
|||
|
||||
|
||||
def payment_summary_to_message(
|
||||
received_payment_summary: ReceivedPaymentSummary,
|
||||
sent_payment_summary: SentPaymentSummary,
|
||||
payment_summary: PaymentSummary,
|
||||
) -> squeak_admin_pb2.PaymentSummary:
|
||||
return squeak_admin_pb2.PaymentSummary(
|
||||
num_received_payments=received_payment_summary.num_received_payments,
|
||||
num_sent_payments=sent_payment_summary.num_sent_payments,
|
||||
amount_earned_msat=received_payment_summary.total_amount_received_msat,
|
||||
amount_spent_msat=sent_payment_summary.total_amount_sent_msat,
|
||||
num_received_payments=payment_summary.received_payment_summary.num_received_payments,
|
||||
num_sent_payments=payment_summary.sent_payment_summary.num_sent_payments,
|
||||
amount_earned_msat=payment_summary.received_payment_summary.total_amount_received_msat,
|
||||
amount_spent_msat=payment_summary.sent_payment_summary.total_amount_sent_msat,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -942,11 +942,9 @@ class SqueakAdminServerHandler(object):
|
|||
|
||||
def handle_get_payment_summary(self, request):
|
||||
logger.info("Handle get payment summary")
|
||||
received_payment_summary = self.squeak_controller.get_received_payment_summary()
|
||||
sent_payment_summary = self.squeak_controller.get_sent_payment_summary()
|
||||
payment_summary = self.squeak_controller.get_payment_summary()
|
||||
payment_summary_msg = payment_summary_to_message(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
payment_summary,
|
||||
)
|
||||
return squeak_admin_pb2.GetPaymentSummaryReply(
|
||||
payment_summary=payment_summary_msg,
|
||||
|
|
@ -958,13 +956,10 @@ class SqueakAdminServerHandler(object):
|
|||
logger.info("Handle get payment summary for squeak hash: {}".format(
|
||||
squeak_hash_str,
|
||||
))
|
||||
received_payment_summary = self.squeak_controller.get_received_payment_summary_for_squeak(
|
||||
squeak_hash)
|
||||
sent_payment_summary = self.squeak_controller.get_sent_payment_summary_for_squeak(
|
||||
payment_summary = self.squeak_controller.get_payment_summary_for_squeak(
|
||||
squeak_hash)
|
||||
payment_summary_msg = payment_summary_to_message(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
payment_summary,
|
||||
)
|
||||
return squeak_admin_pb2.GetPaymentSummaryForSqueakReply(
|
||||
payment_summary=payment_summary_msg,
|
||||
|
|
@ -976,13 +971,10 @@ class SqueakAdminServerHandler(object):
|
|||
public_key_hex,
|
||||
))
|
||||
public_key = SqueakPublicKey.from_bytes(bytes.fromhex(public_key_hex))
|
||||
received_payment_summary = self.squeak_controller.get_received_payment_summary_for_pubkey(
|
||||
public_key)
|
||||
sent_payment_summary = self.squeak_controller.get_sent_payment_summary_for_pubkey(
|
||||
payment_summary = self.squeak_controller.get_payment_summary_for_pubkey(
|
||||
public_key)
|
||||
payment_summary_msg = payment_summary_to_message(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
payment_summary,
|
||||
)
|
||||
return squeak_admin_pb2.GetPaymentSummaryForPubkeyReply(
|
||||
payment_summary=payment_summary_msg,
|
||||
|
|
|
|||
31
squeaknode/core/payment_summary.py
Normal file
31
squeaknode/core/payment_summary.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2020 Jonathan Zernik
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
from typing import NamedTuple
|
||||
|
||||
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
|
||||
from squeaknode.core.sent_payment_summary import SentPaymentSummary
|
||||
|
||||
|
||||
class PaymentSummary(NamedTuple):
|
||||
"""Represents information about payments sent and received."""
|
||||
sent_payment_summary: SentPaymentSummary
|
||||
received_payment_summary: ReceivedPaymentSummary
|
||||
|
|
@ -31,13 +31,12 @@ from squeak.core.keys import SqueakPublicKey
|
|||
from squeaknode.core.download_result import DownloadResult
|
||||
from squeaknode.core.lightning_address import LightningAddressHostPort
|
||||
from squeaknode.core.offer import Offer
|
||||
from squeaknode.core.payment_summary import PaymentSummary
|
||||
from squeaknode.core.peer_address import Network
|
||||
from squeaknode.core.peer_address import PeerAddress
|
||||
from squeaknode.core.received_offer import ReceivedOffer
|
||||
from squeaknode.core.received_payment import ReceivedPayment
|
||||
from squeaknode.core.received_payment_summary import ReceivedPaymentSummary
|
||||
from squeaknode.core.sent_payment import SentPayment
|
||||
from squeaknode.core.sent_payment_summary import SentPaymentSummary
|
||||
from squeaknode.core.squeak_entry import SqueakEntry
|
||||
from squeaknode.core.squeak_peer import SqueakPeer
|
||||
from squeaknode.core.squeak_profile import SqueakProfile
|
||||
|
|
@ -370,23 +369,51 @@ class SqueakController:
|
|||
last_entry,
|
||||
)
|
||||
|
||||
def get_received_payment_summary(self) -> ReceivedPaymentSummary:
|
||||
return self.squeak_store.get_received_payment_summary()
|
||||
def get_payment_summary(self) -> PaymentSummary:
|
||||
received_payment_summary = self.squeak_store.get_received_payment_summary()
|
||||
sent_payment_summary = self.squeak_store.get_sent_payment_summary()
|
||||
return PaymentSummary(
|
||||
sent_payment_summary=sent_payment_summary,
|
||||
received_payment_summary=received_payment_summary,
|
||||
)
|
||||
|
||||
def get_received_payment_summary_for_squeak(self, squeak_hash: bytes) -> ReceivedPaymentSummary:
|
||||
return self.squeak_store.get_received_payment_summary_for_squeak(squeak_hash)
|
||||
def get_payment_summary_for_squeak(self, squeak_hash: bytes) -> PaymentSummary:
|
||||
received_payment_summary = self.squeak_store.get_received_payment_summary_for_squeak(
|
||||
squeak_hash)
|
||||
sent_payment_summary = self.squeak_store.get_sent_payment_summary_for_squeak(
|
||||
squeak_hash)
|
||||
return PaymentSummary(
|
||||
sent_payment_summary=sent_payment_summary,
|
||||
received_payment_summary=received_payment_summary,
|
||||
)
|
||||
|
||||
def get_received_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> ReceivedPaymentSummary:
|
||||
return self.squeak_store.get_received_payment_summary_for_pubkey(pubkey)
|
||||
def get_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> PaymentSummary:
|
||||
received_payment_summary = self.squeak_store.get_received_payment_summary_for_pubkey(
|
||||
pubkey)
|
||||
sent_payment_summary = self.squeak_store.get_sent_payment_summary_for_pubkey(
|
||||
pubkey)
|
||||
return PaymentSummary(
|
||||
sent_payment_summary=sent_payment_summary,
|
||||
received_payment_summary=received_payment_summary,
|
||||
)
|
||||
|
||||
def get_sent_payment_summary(self) -> SentPaymentSummary:
|
||||
return self.squeak_store.get_sent_payment_summary()
|
||||
# def get_received_payment_summary(self) -> ReceivedPaymentSummary:
|
||||
# return self.squeak_store.get_received_payment_summary()
|
||||
|
||||
def get_sent_payment_summary_for_squeak(self, squeak_hash: bytes) -> SentPaymentSummary:
|
||||
return self.squeak_store.get_sent_payment_summary_for_squeak(squeak_hash)
|
||||
# def get_received_payment_summary_for_squeak(self, squeak_hash: bytes) -> ReceivedPaymentSummary:
|
||||
# return self.squeak_store.get_received_payment_summary_for_squeak(squeak_hash)
|
||||
|
||||
def get_sent_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> SentPaymentSummary:
|
||||
return self.squeak_store.get_sent_payment_summary_for_pubkey(pubkey)
|
||||
# def get_received_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> ReceivedPaymentSummary:
|
||||
# return self.squeak_store.get_received_payment_summary_for_pubkey(pubkey)
|
||||
|
||||
# def get_sent_payment_summary(self) -> SentPaymentSummary:
|
||||
# return self.squeak_store.get_sent_payment_summary()
|
||||
|
||||
# def get_sent_payment_summary_for_squeak(self, squeak_hash: bytes) -> SentPaymentSummary:
|
||||
# return self.squeak_store.get_sent_payment_summary_for_squeak(squeak_hash)
|
||||
|
||||
# def get_sent_payment_summary_for_pubkey(self, pubkey: SqueakPublicKey) -> SentPaymentSummary:
|
||||
# return self.squeak_store.get_sent_payment_summary_for_pubkey(pubkey)
|
||||
|
||||
def reprocess_received_payments(self) -> None:
|
||||
self.squeak_store.clear_received_payment_settle_indices()
|
||||
|
|
|
|||
|
|
@ -131,13 +131,11 @@ def test_message_to_sent_payment(sent_payment, sent_payment_msg):
|
|||
|
||||
|
||||
def test_payment_summary_to_message(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
payment_summary,
|
||||
payment_summary_msg,
|
||||
):
|
||||
msg = payment_summary_to_message(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
payment_summary,
|
||||
)
|
||||
|
||||
assert msg == payment_summary_msg
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ from squeaknode.bitcoin.block_info import BlockInfo
|
|||
from squeaknode.core.download_result import DownloadResult
|
||||
from squeaknode.core.lightning_address import LightningAddressHostPort
|
||||
from squeaknode.core.offer import Offer
|
||||
from squeaknode.core.payment_summary import PaymentSummary
|
||||
from squeaknode.core.peer_address import Network
|
||||
from squeaknode.core.peer_address import PeerAddress
|
||||
from squeaknode.core.received_offer import ReceivedOffer
|
||||
|
|
@ -556,6 +557,17 @@ def sent_payment_summary(
|
|||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def payment_summary(
|
||||
received_payment_summary,
|
||||
sent_payment_summary,
|
||||
):
|
||||
yield PaymentSummary(
|
||||
sent_payment_summary=sent_payment_summary,
|
||||
received_payment_summary=received_payment_summary,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def download_result():
|
||||
yield DownloadResult(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue