Skip to content

Commit ee7d9a2

Browse files
committed
add fiat to token rate when exporting eth orders
1 parent c189bd2 commit ee7d9a2

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

pretix_eth/exporter.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pretix_eth.models import WalletAddress
1111

1212
import pytz
13-
import json
1413

1514

1615
def date_to_string(time_zone, date):
@@ -29,10 +28,13 @@ def payment_to_row(payment):
2928
token_currency_name = token.split("-")[0].strip()
3029
fiat_amount = payment.amount
3130
token_amount = payment.info_data.get("amount", "")
32-
token_rates = json.loads(payment.payment_provider.settings.TOKEN_RATES)
33-
# show the fiat to token price conversion
34-
# set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc.
35-
fiat_rate = token_rates.get(f"{token_currency_name}_RATE")
31+
32+
# Show fiat to token price conversion on exports.
33+
# get token rates from order.info_data. But default to admin settings (if info not present)
34+
token_rates = payment.info_data.get("token_rates", {})
35+
fiat_rate = token_rates.get(
36+
f"{token_currency_name}_RATE", "Error fetching from order data"
37+
)
3638

3739
wallet_address = WalletAddress.objects.filter(order_payment=payment).first()
3840
hex_wallet_address = wallet_address.hex_address if wallet_address else ""
@@ -66,10 +68,13 @@ def refund_to_row(refund):
6668
token_currency_name = token.split("-")[0].strip()
6769
fiat_amount = refund.amount
6870
token_amount = refund.info_data.get("amount", "")
69-
token_rates = json.loads(refund.payment_provider.settings.TOKEN_RATES)
70-
# show the fiat to token price conversion
71-
# set by event admin at the time of the order. eg fiat_rate=1$ or 4000$ etc.
72-
fiat_rate = token_rates.get(token_currency_name+"_RATE")
71+
72+
# Show fiat to token price conversion on exports.
73+
# get token rates from refund.info. But default to admin settings (if info not present)
74+
token_rates = refund.info_data.get("token_rates", {})
75+
fiat_rate = token_rates.get(
76+
f"{token_currency_name}_RATE", "Error fetching from order data"
77+
)
7378

7479
wallet_address = WalletAddress.objects.filter(order_payment=refund.payment).first()
7580
hex_wallet_address = wallet_address.hex_address if wallet_address else ""
@@ -97,7 +102,7 @@ class EthereumOrdersExporter(ListExporter):
97102

98103
headers = (
99104
'Type', 'Event slug', 'Order', 'Payment ID', 'Creation date',
100-
'Completion date', 'Status', 'Fiat Amount', 'Token Amount',
105+
'Completion date', 'Status', 'Fiat Amount', 'Token Amount',
101106
'Token Name', 'Token Rate in Fiat', 'Wallet address'
102107
)
103108

pretix_eth/payment.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def _payment_is_valid_info(self, payment: OrderPayment) -> bool:
216216
"currency_type" in payment.info_data,
217217
"time" in payment.info_data,
218218
"amount" in payment.info_data,
219+
"token_rates" in payment.info_data,
219220
)
220221
)
221222

@@ -224,6 +225,7 @@ def execute_payment(self, request: HttpRequest, payment: OrderPayment):
224225
"currency_type": request.session["payment_currency_type"],
225226
"time": request.session["payment_time"],
226227
"amount": request.session["payment_amount"],
228+
"token_rates": self.get_token_rates_from_admin_settings(),
227229
}
228230
payment.save(update_fields=["info"])
229231

@@ -301,6 +303,7 @@ def execute_refund(self, refund: OrderRefund):
301303
"currency_type": refund.payment.info_data["currency_type"],
302304
"amount": refund.payment.info_data["amount"],
303305
"wallet_address": wallet_queryset.first().hex_address,
306+
"token_rates": self.get_token_rates_from_admin_settings(),
304307
}
305308

306309
refund.save(update_fields=["info"])

tests/core/test_addresses_correctly_assigned.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
@pytest.fixture
1818
def get_request_and_payment(get_order_and_payment):
1919
def _create_request_and_payment():
20-
info_data = {"currency_type": "ETH - L1", "time": int(time.time()), "amount": 1}
20+
info_data = {
21+
"currency_type": "ETH - L1",
22+
"time": int(time.time()),
23+
"amount": 1,
24+
"token_rates": {"ETH_RATE": 3000},
25+
}
2126
_, payment = get_order_and_payment(info_data=info_data)
2227

2328
factory = RequestFactory()

tests/core/test_payment_and_refund_export.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def test_headers_are_present(organizer, event, create_admin_client):
1919
"Creation date",
2020
"Completion date",
2121
"Status",
22+
"Fiat Amount",
23+
"Token Amount",
24+
"Token Name",
25+
"Token Rate in Fiat",
2226
"Amount",
2327
"Token",
2428
"Wallet address",
@@ -51,7 +55,11 @@ def _create_payment_with_address(
5155
"amount": "100.0",
5256
"provider": "ethereum",
5357
},
54-
info_data={"currency_type": "ETH - L1", "amount": "100.0"},
58+
info_data={
59+
"currency_type": "ETH - L1",
60+
"amount": "100.0",
61+
"token_rates": {"ETH_RATE": 3000},
62+
},
5563
hex_address="0x0000000000000000000000000000000000000000",
5664
):
5765

tests/core/test_refund.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_refund_created(
3333
"state": OrderPayment.PAYMENT_STATE_CONFIRMED,
3434
"provider": "ethereum",
3535
},
36-
info_data={"amount": "100", "currency_type": "ETH - L1"},
36+
info_data={"amount": "100", "currency_type": "ETH - L1", "token_rates": {}},
3737
)
3838

3939
WalletAddress.objects.create(
@@ -56,6 +56,7 @@ def test_refund_created(
5656
"currency_type": "ETH - L1",
5757
"amount": "100",
5858
"wallet_address": "0x0000000000000000000000000000000000000001",
59+
"token_rates": {},
5960
}
6061

6162

0 commit comments

Comments
 (0)