Skip to content

Commit f37bdf4

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

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

pretix_eth/exporter.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ def payment_to_row(payment):
2929
token_currency_name = token.split("-")[0].strip()
3030
fiat_amount = payment.amount
3131
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")
32+
33+
# Show fiat to token price conversion on exports.
34+
# get token rates from order.info_data. But default to admin settings (if info not present)
35+
token_rates = payment.info_data.get("token_rates", {})
36+
fiat_rate = token_rates.get(
37+
f"{token_currency_name}_RATE", "Error fetching from order data"
38+
)
3639

3740
wallet_address = WalletAddress.objects.filter(order_payment=payment).first()
3841
hex_wallet_address = wallet_address.hex_address if wallet_address else ""
@@ -66,10 +69,13 @@ def refund_to_row(refund):
6669
token_currency_name = token.split("-")[0].strip()
6770
fiat_amount = refund.amount
6871
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")
72+
73+
# Show fiat to token price conversion on exports.
74+
# get token rates from refund.info. But default to admin settings (if info not present)
75+
token_rates = refund.info_data.get("token_rates", {})
76+
fiat_rate = token_rates.get(
77+
f"{token_currency_name}_RATE", "Error fetching from order data"
78+
)
7379

7480
wallet_address = WalletAddress.objects.filter(order_payment=refund.payment).first()
7581
hex_wallet_address = wallet_address.hex_address if wallet_address else ""

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)