From c189bd221e834c7ef3bd72d24295bd4dfcb8b61c Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Thu, 3 Mar 2022 22:19:50 +0000 Subject: [PATCH 1/2] Add token to fiat rate in exporter --- pretix_eth/exporter.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pretix_eth/exporter.py b/pretix_eth/exporter.py index 7385191e..311af81b 100644 --- a/pretix_eth/exporter.py +++ b/pretix_eth/exporter.py @@ -10,6 +10,7 @@ from pretix_eth.models import WalletAddress import pytz +import json def date_to_string(time_zone, date): @@ -24,8 +25,14 @@ def payment_to_row(payment): completion_date = '' token = payment.info_data.get("currency_type", "") + # token = "DAI-L1". Get "DAI" (token_currency_name) so it can be used to get fiat_rate + token_currency_name = token.split("-")[0].strip() fiat_amount = payment.amount token_amount = payment.info_data.get("amount", "") + token_rates = json.loads(payment.payment_provider.settings.TOKEN_RATES) + # show the fiat to token price conversion + # set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc. + fiat_rate = token_rates.get(f"{token_currency_name}_RATE") wallet_address = WalletAddress.objects.filter(order_payment=payment).first() hex_wallet_address = wallet_address.hex_address if wallet_address else "" @@ -41,6 +48,7 @@ def payment_to_row(payment): fiat_amount, token_amount, token, + fiat_rate, hex_wallet_address, ] return row @@ -54,8 +62,14 @@ def refund_to_row(refund): completion_date = '' token = refund.info_data.get("currency_type", "") + # token = "DAI-L1". Get "DAI" (token_currency_name) so it can be used to get fiat_rate + token_currency_name = token.split("-")[0].strip() fiat_amount = refund.amount token_amount = refund.info_data.get("amount", "") + token_rates = json.loads(refund.payment_provider.settings.TOKEN_RATES) + # show the fiat to token price conversion + # set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc. + fiat_rate = token_rates.get(token_currency_name+"_RATE") wallet_address = WalletAddress.objects.filter(order_payment=refund.payment).first() hex_wallet_address = wallet_address.hex_address if wallet_address else "" @@ -71,6 +85,7 @@ def refund_to_row(refund): fiat_amount, token_amount, token, + fiat_rate, hex_wallet_address, ] return row @@ -82,7 +97,8 @@ class EthereumOrdersExporter(ListExporter): headers = ( 'Type', 'Event slug', 'Order', 'Payment ID', 'Creation date', - 'Completion date', 'Status', 'Amount', 'Token', 'Wallet address' + 'Completion date', 'Status', 'Fiat Amount', 'Token Amount', + 'Token Name', 'Token Rate in Fiat', 'Wallet address' ) @property From ee7d9a25b1ca7053e20a3472e83a7dd18a3197dc Mon Sep 17 00:00:00 2001 From: Rahul Kothari Date: Tue, 29 Mar 2022 21:19:41 +0100 Subject: [PATCH 2/2] add fiat to token rate when exporting eth orders --- pretix_eth/exporter.py | 25 +++++++++++-------- pretix_eth/payment.py | 3 +++ .../core/test_addresses_correctly_assigned.py | 7 +++++- tests/core/test_payment_and_refund_export.py | 10 +++++++- tests/core/test_refund.py | 3 ++- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/pretix_eth/exporter.py b/pretix_eth/exporter.py index 311af81b..803a582d 100644 --- a/pretix_eth/exporter.py +++ b/pretix_eth/exporter.py @@ -10,7 +10,6 @@ from pretix_eth.models import WalletAddress import pytz -import json def date_to_string(time_zone, date): @@ -29,10 +28,13 @@ def payment_to_row(payment): token_currency_name = token.split("-")[0].strip() fiat_amount = payment.amount token_amount = payment.info_data.get("amount", "") - token_rates = json.loads(payment.payment_provider.settings.TOKEN_RATES) - # show the fiat to token price conversion - # set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc. - fiat_rate = token_rates.get(f"{token_currency_name}_RATE") + + # Show fiat to token price conversion on exports. + # get token rates from order.info_data. But default to admin settings (if info not present) + token_rates = payment.info_data.get("token_rates", {}) + fiat_rate = token_rates.get( + f"{token_currency_name}_RATE", "Error fetching from order data" + ) wallet_address = WalletAddress.objects.filter(order_payment=payment).first() hex_wallet_address = wallet_address.hex_address if wallet_address else "" @@ -66,10 +68,13 @@ def refund_to_row(refund): token_currency_name = token.split("-")[0].strip() fiat_amount = refund.amount token_amount = refund.info_data.get("amount", "") - token_rates = json.loads(refund.payment_provider.settings.TOKEN_RATES) - # show the fiat to token price conversion - # set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc. - fiat_rate = token_rates.get(token_currency_name+"_RATE") + + # Show fiat to token price conversion on exports. + # get token rates from refund.info. But default to admin settings (if info not present) + token_rates = refund.info_data.get("token_rates", {}) + fiat_rate = token_rates.get( + f"{token_currency_name}_RATE", "Error fetching from order data" + ) wallet_address = WalletAddress.objects.filter(order_payment=refund.payment).first() hex_wallet_address = wallet_address.hex_address if wallet_address else "" @@ -97,7 +102,7 @@ class EthereumOrdersExporter(ListExporter): headers = ( 'Type', 'Event slug', 'Order', 'Payment ID', 'Creation date', - 'Completion date', 'Status', 'Fiat Amount', 'Token Amount', + 'Completion date', 'Status', 'Fiat Amount', 'Token Amount', 'Token Name', 'Token Rate in Fiat', 'Wallet address' ) diff --git a/pretix_eth/payment.py b/pretix_eth/payment.py index 57aa41f7..e213cf28 100644 --- a/pretix_eth/payment.py +++ b/pretix_eth/payment.py @@ -216,6 +216,7 @@ def _payment_is_valid_info(self, payment: OrderPayment) -> bool: "currency_type" in payment.info_data, "time" in payment.info_data, "amount" in payment.info_data, + "token_rates" in payment.info_data, ) ) @@ -224,6 +225,7 @@ def execute_payment(self, request: HttpRequest, payment: OrderPayment): "currency_type": request.session["payment_currency_type"], "time": request.session["payment_time"], "amount": request.session["payment_amount"], + "token_rates": self.get_token_rates_from_admin_settings(), } payment.save(update_fields=["info"]) @@ -301,6 +303,7 @@ def execute_refund(self, refund: OrderRefund): "currency_type": refund.payment.info_data["currency_type"], "amount": refund.payment.info_data["amount"], "wallet_address": wallet_queryset.first().hex_address, + "token_rates": self.get_token_rates_from_admin_settings(), } refund.save(update_fields=["info"]) diff --git a/tests/core/test_addresses_correctly_assigned.py b/tests/core/test_addresses_correctly_assigned.py index c2bac6eb..d2df602e 100644 --- a/tests/core/test_addresses_correctly_assigned.py +++ b/tests/core/test_addresses_correctly_assigned.py @@ -17,7 +17,12 @@ @pytest.fixture def get_request_and_payment(get_order_and_payment): def _create_request_and_payment(): - info_data = {"currency_type": "ETH - L1", "time": int(time.time()), "amount": 1} + info_data = { + "currency_type": "ETH - L1", + "time": int(time.time()), + "amount": 1, + "token_rates": {"ETH_RATE": 3000}, + } _, payment = get_order_and_payment(info_data=info_data) factory = RequestFactory() diff --git a/tests/core/test_payment_and_refund_export.py b/tests/core/test_payment_and_refund_export.py index 657de2f4..9938a54e 100644 --- a/tests/core/test_payment_and_refund_export.py +++ b/tests/core/test_payment_and_refund_export.py @@ -19,6 +19,10 @@ def test_headers_are_present(organizer, event, create_admin_client): "Creation date", "Completion date", "Status", + "Fiat Amount", + "Token Amount", + "Token Name", + "Token Rate in Fiat", "Amount", "Token", "Wallet address", @@ -51,7 +55,11 @@ def _create_payment_with_address( "amount": "100.0", "provider": "ethereum", }, - info_data={"currency_type": "ETH - L1", "amount": "100.0"}, + info_data={ + "currency_type": "ETH - L1", + "amount": "100.0", + "token_rates": {"ETH_RATE": 3000}, + }, hex_address="0x0000000000000000000000000000000000000000", ): diff --git a/tests/core/test_refund.py b/tests/core/test_refund.py index 595ddb65..3ea54daf 100644 --- a/tests/core/test_refund.py +++ b/tests/core/test_refund.py @@ -33,7 +33,7 @@ def test_refund_created( "state": OrderPayment.PAYMENT_STATE_CONFIRMED, "provider": "ethereum", }, - info_data={"amount": "100", "currency_type": "ETH - L1"}, + info_data={"amount": "100", "currency_type": "ETH - L1", "token_rates": {}}, ) WalletAddress.objects.create( @@ -56,6 +56,7 @@ def test_refund_created( "currency_type": "ETH - L1", "amount": "100", "wallet_address": "0x0000000000000000000000000000000000000001", + "token_rates": {}, }