1313from pretix .base .models import OrderPayment
1414from pretix .base .models .event import Event
1515
16- from pretix_eth .network .tokens import IToken , all_token_and_network_ids_to_tokens , TOKEN_ABI
16+ from pretix_eth .network .tokens import (
17+ IToken ,
18+ all_token_and_network_ids_to_tokens ,
19+ TOKEN_ABI ,
20+ )
1721
1822logger = logging .getLogger (__name__ )
1923
@@ -36,7 +40,7 @@ def add_arguments(self, parser):
3640
3741 def handle (self , * args , ** options ):
3842 no_dry_run = options ["no_dry_run" ]
39- log_verbosity = int (options .get (' verbosity' , 0 ))
43+ log_verbosity = int (options .get (" verbosity" , 0 ))
4044
4145 with scope (organizer = None ):
4246 # todo change to events where pending payments are expected only?
@@ -55,14 +59,20 @@ def confirm_payments_for_event(self, event: Event, no_dry_run, log_verbosity=0):
5559 OrderPayment .PAYMENT_STATE_CREATED ,
5660 OrderPayment .PAYMENT_STATE_PENDING ,
5761 OrderPayment .PAYMENT_STATE_CANCELED ,
58- )
62+ ),
5963 )
6064 if log_verbosity > 0 :
61- logger .info (f" * Found { unconfirmed_order_payments .count ()} unconfirmed order payments" )
65+ logger .info (
66+ f" * Found { unconfirmed_order_payments .count ()} "
67+ f"unconfirmed order payments"
68+ )
6269
6370 for order_payment in unconfirmed_order_payments :
6471 if log_verbosity > 0 :
65- logger .info (f" * trying to confirm payment: { order_payment } (has { order_payment .signed_messages .all ().count ()} signed messages)" )
72+ logger .info (
73+ f" * trying to confirm payment: { order_payment } "
74+ f"(has { order_payment .signed_messages .all ().count ()} signed messages)"
75+ )
6676 # it is tempting to put .filter(invalid=False) here, but remember
6777 # there is still a chance that low-gas txs are mined later on.
6878 for signed_message in order_payment .signed_messages .all ():
@@ -72,7 +82,9 @@ def confirm_payments_for_event(self, event: Event, no_dry_run, log_verbosity=0):
7282 full_id = order_payment .full_id
7383
7484 info = order_payment .info_data
75- token : IToken = all_token_and_network_ids_to_tokens [info ["currency_type" ]]
85+ token : IToken = all_token_and_network_ids_to_tokens [
86+ info ["currency_type" ]
87+ ]
7688 expected_network_id = token .NETWORK_IDENTIFIER
7789 expected_network_rpc_url_key = f"{ expected_network_id } _RPC_URL"
7890
@@ -89,66 +101,106 @@ def confirm_payments_for_event(self, event: Event, no_dry_run, log_verbosity=0):
89101 # Get balance
90102 w3 = Web3 (load_provider_from_uri (network_rpc_url ))
91103 if log_verbosity > 0 :
92- logger .info (f" * Looking for a receip for a transaction with hash={ signed_message .transaction_hash } " )
104+ logger .info (
105+ f" * Looking for a receip for a transaction with "
106+ f"hash={ signed_message .transaction_hash } "
107+ )
93108 try :
94- receipt = w3 .eth .getTransactionReceipt (signed_message .transaction_hash )
109+ receipt = w3 .eth .getTransactionReceipt (
110+ signed_message .transaction_hash
111+ )
95112 except TransactionNotFound :
96113 if log_verbosity > 0 :
97- logger .info (f" * Transaction hash={ signed_message .transaction_hash } not found, skipping." )
98- if signed_message .age > 30 * 60 :
114+ logger .info (
115+ f" * Transaction"
116+ f" hash={ signed_message .transaction_hash } not found,"
117+ f" skipping."
118+ )
119+ if signed_message .age > 30 * 60 :
99120 signed_message .invalidate ()
100121 continue
101122
102123 if receipt .status == 0 :
103124 if log_verbosity > 0 :
104- logger .info (f" * Transaction hash={ signed_message .transaction_hash } was has status=0, invalidating." )
125+ logger .info (
126+ f" * Transaction hash={ signed_message .transaction_hash } "
127+ f" was has status=0, invalidating."
128+ )
105129 signed_message .invalidate ()
106130 continue
107131
108132 block_number = receipt .blockNumber
109133
110- if block_number is None or block_number + SAFETY_BLOCK_COUNT > w3 .eth .get_block_number ():
111- logger .warning (f" * Transfer found in a block that is too young, waiting until at least { SAFETY_BLOCK_COUNT } more blocks are confirmed." )
134+ if (
135+ block_number is None
136+ or block_number + SAFETY_BLOCK_COUNT > w3 .eth .get_block_number ()
137+ ):
138+ logger .warning (
139+ f" * Transfer found in a block that is too young, "
140+ f"waiting until at least { SAFETY_BLOCK_COUNT } more blocks are confirmed."
141+ )
112142 continue
113143
114144 if token .IS_NATIVE_ASSET :
115145 # ETH
116- payment_amount = w3 .eth .getTransaction (signed_message .transaction_hash ).value
146+ payment_amount = w3 .eth .getTransaction (
147+ signed_message .transaction_hash
148+ ).value
117149 receipt_reciever = receipt .to .lower ()
118- correct_recipient = receipt_reciever == signed_message .recipient_address .lower ()
150+ correct_recipient = (
151+ receipt_reciever == signed_message .recipient_address .lower ()
152+ )
119153
120154 else :
121155 # DAI
122- contract = w3 .eth .contract (address = token .ADDRESS , abi = TOKEN_ABI )
123- transaction_details = contract .events .Transfer ().processReceipt (receipt )[0 ].args
156+ contract = w3 .eth .contract (address = token .ADDRESS ,
157+ abi = TOKEN_ABI )
158+ transaction_details = (
159+ contract .events .Transfer ().processReceipt (receipt )[
160+ 0 ].args
161+ )
124162 payment_amount = transaction_details .value
125163 # check that the payment happened on the right contract address
126164 correct_contract = token .ADDRESS .lower () == receipt .to .lower ()
127- # take recipient address from the topics, not from the "from" field, as that's the contract address
128- receipt_reciever = receipt .logs [0 ].topics [2 ][12 :].hex ().lower ()
129- correct_recipient = correct_contract and (receipt_reciever == signed_message .recipient_address .lower ())
130-
165+ # take recipient address from the topics,
166+ # not from the "from" field, as that's the contract address
167+ receipt_reciever = receipt .logs [0 ].topics [2 ][
168+ 12 :].hex ().lower ()
169+ correct_recipient = correct_contract and (
170+ receipt_reciever == signed_message .recipient_address .lower ()
171+ )
131172
132- receipt_sender = getattr (receipt , ' from' ).lower ()
173+ receipt_sender = getattr (receipt , " from" ).lower ()
133174 correct_sender = receipt_sender == signed_message .sender_address .lower ()
134175
135176 if not (correct_sender and correct_recipient ):
136177 logger .warning (
137- f" * Transaction hash provided does not match correct sender and recipient"
178+ f" * Transaction hash provided does not match "
179+ f"correct sender and recipient"
138180 )
139181 if log_verbosity > 0 :
140- logger .info (f"receipt sender={ receipt_sender } , expected sender={ signed_message .sender_address .lower ()} " )
141- logger .info (f"receipt recipient={ receipt_reciever } , expected recipient={ signed_message .recipient_address .lower ()} " )
182+ logger .info (
183+ f"receipt sender={ receipt_sender } , "
184+ f"expected sender={ signed_message .sender_address .lower ()} "
185+ )
186+ logger .info (
187+ f"receipt recipient={ receipt_reciever } , "
188+ f"expected recipient={ signed_message .recipient_address .lower ()} "
189+ )
142190 continue
143191
144192 if payment_amount > 0 :
145- logger .info (f"Payments found for { full_id } at { signed_message .sender_address } :" )
193+ logger .info (
194+ f"Payments found for { full_id } at { signed_message .sender_address } :"
195+ )
146196 if payment_amount < expected_amount :
147197 logger .warning (
148- f" * Expected payment of at least { expected_amount } { token .TOKEN_SYMBOL } "
198+ f" * Expected payment of at least"
199+ f" { expected_amount } { token .TOKEN_SYMBOL } "
149200 )
150201 logger .warning (
151- f" * Given payment was { payment_amount } { token .TOKEN_SYMBOL } "
202+ f" * Given payment was"
203+ f" { payment_amount } { token .TOKEN_SYMBOL } "
152204 )
153205 logger .warning (f" * Skipping" ) # noqa: F541
154206 continue
@@ -159,6 +211,8 @@ def confirm_payments_for_event(self, event: Event, no_dry_run, log_verbosity=0):
159211 signed_message .is_confirmed = True
160212 signed_message .save ()
161213 else :
162- logger .info (f" * DRY RUN: Would confirm order payment { full_id } " )
214+ logger .info (
215+ f" * DRY RUN: Would confirm order payment { full_id } "
216+ )
163217 else :
164218 logger .info (f"No payments found for { full_id } " )
0 commit comments