|
1 | 1 | from typing import (
|
2 | 2 | TYPE_CHECKING,
|
3 |
| - Any, |
4 |
| - Dict, |
5 | 3 | Optional,
|
6 | 4 | cast,
|
7 | 5 | )
|
8 | 6 |
|
9 |
| -from eth_abi import ( |
10 |
| - encode_abi, |
11 |
| -) |
12 |
| -from eth_typing import ( |
13 |
| - URI, |
14 |
| -) |
15 |
| - |
16 |
| -from web3._utils.request import ( |
17 |
| - async_get_json_from_client_response, |
18 |
| - async_get_response_from_get_request, |
19 |
| - async_get_response_from_post_request, |
20 |
| -) |
21 |
| -from web3._utils.type_conversion_utils import ( |
22 |
| - to_bytes_if_hex, |
23 |
| - to_hex_if_bytes, |
24 |
| -) |
25 |
| -from web3.exceptions import ( |
26 |
| - ValidationError, |
27 |
| -) |
28 | 7 | from web3.types import (
|
29 | 8 | BlockIdentifier,
|
30 | 9 | TxParams,
|
@@ -61,67 +40,3 @@ async def get_buffered_gas_estimate(
|
61 | 40 | )
|
62 | 41 |
|
63 | 42 | return min(gas_limit, gas_estimate + gas_buffer)
|
64 |
| - |
65 |
| - |
66 |
| -async def async_handle_offchain_lookup( |
67 |
| - offchain_lookup_payload: Dict[str, Any], |
68 |
| - transaction: TxParams, |
69 |
| -) -> bytes: |
70 |
| - formatted_sender = to_hex_if_bytes(offchain_lookup_payload['sender']).lower() |
71 |
| - formatted_data = to_hex_if_bytes(offchain_lookup_payload['callData']).lower() |
72 |
| - |
73 |
| - if formatted_sender != to_hex_if_bytes(transaction['to']).lower(): |
74 |
| - raise ValidationError( |
75 |
| - 'Cannot handle OffchainLookup raised inside nested call. Returned `sender` ' |
76 |
| - 'value does not equal `to` address in transaction.' |
77 |
| - ) |
78 |
| - |
79 |
| - for url in offchain_lookup_payload['urls']: |
80 |
| - formatted_url = URI( |
81 |
| - str(url) |
82 |
| - .replace('{sender}', str(formatted_sender)) |
83 |
| - .replace('{data}', str(formatted_data)) |
84 |
| - ) |
85 |
| - |
86 |
| - try: |
87 |
| - if '{data}' in url and '{sender}' in url: |
88 |
| - response = await async_get_response_from_get_request(formatted_url) |
89 |
| - elif '{sender}' in url: |
90 |
| - response = await async_get_response_from_post_request(formatted_url, data={ |
91 |
| - "data": formatted_data, |
92 |
| - "sender": formatted_sender |
93 |
| - }) |
94 |
| - else: |
95 |
| - raise ValidationError('url not formatted properly.') |
96 |
| - except Exception: |
97 |
| - continue # try next url if timeout or issues making the request |
98 |
| - |
99 |
| - if 400 <= response.status <= 499: # if request returns 400 error, raise exception |
100 |
| - response.raise_for_status() |
101 |
| - if not 200 <= response.status <= 299: # if not 400 error, try next url |
102 |
| - continue |
103 |
| - |
104 |
| - result = await async_get_json_from_client_response(response) |
105 |
| - |
106 |
| - if 'data' not in result.keys(): |
107 |
| - raise ValidationError( |
108 |
| - "Improperly formatted response for offchain lookup HTTP request - missing 'data' " |
109 |
| - "field." |
110 |
| - ) |
111 |
| - |
112 |
| - encoded_data_with_function_selector = b''.join([ |
113 |
| - # 4-byte callback function selector |
114 |
| - to_bytes_if_hex(offchain_lookup_payload['callbackFunction']), |
115 |
| - |
116 |
| - # encode the `data` from the result and the `extraData` as bytes |
117 |
| - encode_abi( |
118 |
| - ['bytes', 'bytes'], |
119 |
| - [ |
120 |
| - to_bytes_if_hex(result['data']), |
121 |
| - to_bytes_if_hex(offchain_lookup_payload['extraData']), |
122 |
| - ] |
123 |
| - ) |
124 |
| - ]) |
125 |
| - |
126 |
| - return encoded_data_with_function_selector |
127 |
| - raise Exception("Offchain lookup failed for supplied urls.") |
0 commit comments