@@ -31,61 +31,67 @@ def handle_offchain_lookup(
31
31
offchain_lookup_payload : Dict [str , Any ],
32
32
transaction : TxParams ,
33
33
) -> bytes :
34
- formatted_sender = to_hex_if_bytes (offchain_lookup_payload [' sender' ]).lower ()
35
- formatted_data = to_hex_if_bytes (offchain_lookup_payload [' callData' ]).lower ()
34
+ formatted_sender = to_hex_if_bytes (offchain_lookup_payload [" sender" ]).lower ()
35
+ formatted_data = to_hex_if_bytes (offchain_lookup_payload [" callData" ]).lower ()
36
36
37
- if formatted_sender != to_hex_if_bytes (transaction ['to' ]).lower ():
37
+ if formatted_sender != to_hex_if_bytes (transaction ["to" ]).lower ():
38
38
raise ValidationError (
39
- ' Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does '
40
- ' not equal `to` address in transaction.'
39
+ " Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does "
40
+ " not equal `to` address in transaction."
41
41
)
42
42
43
- for url in offchain_lookup_payload [' urls' ]:
43
+ for url in offchain_lookup_payload [" urls" ]:
44
44
formatted_url = URI (
45
45
str (url )
46
- .replace (' {sender}' , str (formatted_sender ))
47
- .replace (' {data}' , str (formatted_data ))
46
+ .replace (" {sender}" , str (formatted_sender ))
47
+ .replace (" {data}" , str (formatted_data ))
48
48
)
49
49
50
50
try :
51
- if ' {data}' in url and ' {sender}' in url :
51
+ if " {data}" in url and " {sender}" in url :
52
52
response = get_response_from_get_request (formatted_url )
53
- elif '{sender}' in url :
54
- response = get_response_from_post_request (formatted_url , data = {
55
- "data" : formatted_data ,
56
- "sender" : formatted_sender ,
57
- })
53
+ elif "{sender}" in url :
54
+ response = get_response_from_post_request (
55
+ formatted_url ,
56
+ data = {
57
+ "data" : formatted_data ,
58
+ "sender" : formatted_sender ,
59
+ },
60
+ )
58
61
else :
59
- raise ValidationError (' url not formatted properly.' )
62
+ raise ValidationError (" url not formatted properly." )
60
63
except Exception :
61
64
continue # try next url if timeout or issues making the request
62
65
63
- if 400 <= response .status_code <= 499 : # if request returns 400 error, raise exception
66
+ if (
67
+ 400 <= response .status_code <= 499
68
+ ): # if request returns 400 error, raise exception
64
69
response .raise_for_status ()
65
70
if not 200 <= response .status_code <= 299 : # if not 400 error, try next url
66
71
continue
67
72
68
73
result = response .json ()
69
74
70
- if ' data' not in result .keys ():
75
+ if " data" not in result .keys ():
71
76
raise ValidationError (
72
77
"Improperly formatted response for offchain lookup HTTP request - missing 'data' "
73
78
"field."
74
79
)
75
80
76
- encoded_data_with_function_selector = b'' .join ([
77
- # 4-byte callback function selector
78
- to_bytes_if_hex (offchain_lookup_payload ['callbackFunction' ]),
79
-
80
- # encode the `data` from the result and the `extraData` as bytes
81
- encode_abi (
82
- ['bytes' , 'bytes' ],
83
- [
84
- to_bytes_if_hex (result ['data' ]),
85
- to_bytes_if_hex (offchain_lookup_payload ['extraData' ]),
86
- ]
87
- )
88
- ])
81
+ encoded_data_with_function_selector = b"" .join (
82
+ [
83
+ # 4-byte callback function selector
84
+ to_bytes_if_hex (offchain_lookup_payload ["callbackFunction" ]),
85
+ # encode the `data` from the result and the `extraData` as bytes
86
+ encode_abi (
87
+ ["bytes" , "bytes" ],
88
+ [
89
+ to_bytes_if_hex (result ["data" ]),
90
+ to_bytes_if_hex (offchain_lookup_payload ["extraData" ]),
91
+ ],
92
+ ),
93
+ ]
94
+ )
89
95
90
96
return encoded_data_with_function_selector
91
97
raise MultipleFailedRequests ("Offchain lookup failed for supplied urls." )
0 commit comments