Skip to content

Commit e31a0a9

Browse files
committed
Linter complaints
1 parent 54c97b0 commit e31a0a9

10 files changed

Lines changed: 174 additions & 113 deletions

File tree

pretix_eth/management/commands/confirm_payments.py

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
from pretix.base.models import OrderPayment
1414
from 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

1822
logger = 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}")

pretix_eth/migrations/0002_auto_20220529_2332.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('pretixbase', '0208_auto_20220214_1632'),
11-
('pretix_eth', '0001_initial'),
10+
("pretixbase", "0208_auto_20220214_1632"),
11+
("pretix_eth", "0001_initial"),
1212
]
1313

1414
operations = [
1515
migrations.CreateModel(
16-
name='SignedMessage',
16+
name="SignedMessage",
1717
fields=[
18-
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
19-
('signature', models.CharField(max_length=132)),
20-
('raw_message', models.CharField(max_length=256)),
21-
('sender_address', models.CharField(max_length=42)),
22-
('recipient_address', models.CharField(max_length=42)),
23-
('chain_id', models.SmallIntegerField()),
24-
('order_payment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='signed_messages', to='pretixbase.orderpayment')),
18+
(
19+
"id",
20+
models.AutoField(
21+
auto_created=True, primary_key=True, serialize=False
22+
),
23+
),
24+
("signature", models.CharField(max_length=132)),
25+
("raw_message", models.CharField(max_length=256)),
26+
("sender_address", models.CharField(max_length=42)),
27+
("recipient_address", models.CharField(max_length=42)),
28+
("chain_id", models.SmallIntegerField()),
29+
(
30+
"order_payment",
31+
models.ForeignKey(
32+
on_delete=django.db.models.deletion.CASCADE,
33+
related_name="signed_messages",
34+
to="pretixbase.orderpayment",
35+
),
36+
),
2537
],
2638
),
2739
migrations.DeleteModel(
28-
name='WalletAddress',
40+
name="WalletAddress",
2941
),
3042
]

pretix_eth/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class SignedMessage(models.Model):
1313
sender_address = models.CharField(max_length=42)
1414
recipient_address = models.CharField(max_length=42)
1515
chain_id = models.IntegerField()
16-
order_payment = models.ForeignKey(to=OrderPayment, on_delete=models.CASCADE, related_name='signed_messages')
16+
order_payment = models.ForeignKey(
17+
to=OrderPayment,
18+
on_delete=models.CASCADE,
19+
related_name='signed_messages',
20+
)
1721
transaction_hash = models.CharField(max_length=66, null=True)
1822
invalid = models.BooleanField(default=False)
1923
created_at = models.DateTimeField(editable=False, null=True)

pretix_eth/network/tokens.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,40 @@
2626
},
2727
{
2828
"constant": False,
29-
"inputs": [{"name": "_to", "type": "address"}, {"name": "_value", "type": "uint256"}],
29+
"inputs": [
30+
{"name": "_to", "type": "address"},
31+
{"name": "_value", "type": "uint256"},
32+
],
3033
"name": "transfer",
3134
"outputs": [{"name": "", "type": "bool"}],
32-
"type": "function"
35+
"type": "function",
3336
},
3437
# Event
3538
{
36-
"anonymous": False,
37-
"inputs": [
38-
{
39-
"indexed": True,
40-
"internalType": "address",
41-
"name": "from",
42-
"type": "address"
43-
},
44-
{
45-
"indexed": True,
46-
"internalType": "address",
47-
"name": "to",
48-
"type": "address"
49-
},
50-
{
51-
"indexed": False,
52-
"internalType": "uint256",
53-
"name": "value",
54-
"type": "uint256"
55-
}
56-
],
57-
"name": "Transfer",
58-
"type": "event"
59-
}
39+
"anonymous": False,
40+
"inputs": [
41+
{
42+
"indexed": True,
43+
"internalType": "address",
44+
"name": "from",
45+
"type": "address",
46+
},
47+
{
48+
"indexed": True,
49+
"internalType": "address",
50+
"name": "to",
51+
"type": "address",
52+
},
53+
{
54+
"indexed": False,
55+
"internalType": "uint256",
56+
"name": "value",
57+
"type": "uint256",
58+
},
59+
],
60+
"name": "Transfer",
61+
"type": "event",
62+
},
6063
]
6164

6265

@@ -163,13 +166,13 @@ def get_balance_of_address(self, hex_wallet_address, rpc_url):
163166

164167
def get_transaction_link(self, transaction_hash: Optional[str]) -> Optional[
165168
str]:
166-
return '{base}/tx/{hash}'.format(
169+
return "{base}/tx/{hash}".format(
167170
base=self.EIP3091_EXPLORER_URL,
168171
hash=transaction_hash,
169172
)
170173

171174
def get_address_link(self, address: Optional[str]) -> Optional[str]:
172-
return '{base}/address/{address}'.format(
175+
return "{base}/address/{address}".format(
173176
base=self.EIP3091_EXPLORER_URL,
174177
address=address,
175178
)

pretix_eth/payment.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from eth_utils import from_wei
2020

21-
#from .models import WalletAddress
2221
from .network.tokens import (
2322
IToken,
2423
registry,
@@ -215,7 +214,8 @@ def payment_prepare(self, request: HttpRequest, payment: OrderPayment):
215214
return False
216215

217216
def payment_is_valid_session(self, request):
218-
# Note: payment_currency_type check already done in token_verbose_name_to_token_network_id()
217+
# Note: payment_currency_type check already done
218+
# in token_verbose_name_to_token_network_id()
219219
return all(
220220
(
221221
"payment_currency_type" in request.session,
@@ -225,7 +225,8 @@ def payment_is_valid_session(self, request):
225225
)
226226

227227
def _payment_is_valid_info(self, payment: OrderPayment) -> bool:
228-
# Note: payment_currency_type check already done in token_verbose_name_to_token_network_id()
228+
# Note: payment_currency_type check already done
229+
# in token_verbose_name_to_token_network_id()
229230
return all(
230231
(
231232
"currency_type" in payment.info_data,
@@ -302,7 +303,8 @@ def payment_control_render(self, request: HttpRequest, payment: OrderPayment):
302303

303304
hex_wallet_address = self.get_receiving_address()
304305

305-
# display all submitted transaction hashes along with their respective sendr and recipient addresses
306+
# display all submitted transaction hashes along with
307+
# their respective sendr and recipient addresses
306308
last_signed_message: SignedMessage = payment.signed_messages.last()
307309

308310
if last_signed_message is not None:
@@ -343,4 +345,4 @@ def payment_partial_refund_supported(self, payment: OrderPayment):
343345
return self.payment_refund_supported(payment)
344346

345347
def execute_refund(self, refund: OrderRefund):
346-
raise Exception("Refunds are disabled for this payment provider for private key security reasons.")
348+
raise Exception("Refunds are disabled for this payment provider.")

0 commit comments

Comments
 (0)