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

Add fiat export #211

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion pretix_eth/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,18 @@ 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", "")

# 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 ""

Expand All @@ -41,6 +50,7 @@ def payment_to_row(payment):
fiat_amount,
token_amount,
token,
fiat_rate,
hex_wallet_address,
]
return row
Expand All @@ -54,9 +64,18 @@ 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", "")

# 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 ""

Expand All @@ -71,6 +90,7 @@ def refund_to_row(refund):
fiat_amount,
token_amount,
token,
fiat_rate,
hex_wallet_address,
]
return row
Expand All @@ -82,7 +102,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
Expand Down
3 changes: 3 additions & 0 deletions pretix_eth/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)

Expand All @@ -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"])

Expand Down Expand Up @@ -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"])
7 changes: 6 additions & 1 deletion tests/core/test_addresses_correctly_assigned.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion tests/core/test_payment_and_refund_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
):

Expand Down
3 changes: 2 additions & 1 deletion tests/core/test_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -56,6 +56,7 @@ def test_refund_created(
"currency_type": "ETH - L1",
"amount": "100",
"wallet_address": "0x0000000000000000000000000000000000000001",
"token_rates": {},
}


Expand Down