Skip to content

Commit 76664d7

Browse files
authored
feat: support new pectra endpoitns (#23)
1 parent a5a38d5 commit 76664d7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/test_async_beacon.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,35 @@ async def test_get_validator_balances(mocker: MockerFixture):
129129
mocked_fn.assert_called_with(
130130
f"http://127.0.0.1:8545/eth/v1/beacon/states/{state_id}/validator_balances", timeout=10.0, params={"id": ["0", "1"]}
131131
)
132+
133+
134+
@pytest.mark.asyncio()
135+
async def test_get_pending_consolidations(mocker: MockerFixture):
136+
response_json = {"data": ["consolidation1", "consolidation2"]}
137+
mocked_response = mocker.patch("web3_utils.async_beacon.AsyncBeacon._get_pending_consolidations", return_value=response_json)
138+
async_beacon = AsyncBeacon("http://127.0.0.1:8545", logger=logging.getLogger(), retry_stop=None)
139+
response = await async_beacon.get_pending_consolidations()
140+
assert response == response_json
141+
mocked_response.assert_called_once_with("head")
142+
143+
144+
@pytest.mark.asyncio()
145+
async def test_get_pending_deposits(mocker: MockerFixture):
146+
response_json = {"data": ["deposit1", "deposit2"]}
147+
mocked_response = mocker.patch("web3_utils.async_beacon.AsyncBeacon._get_pending_deposits", return_value=response_json)
148+
async_beacon = AsyncBeacon("http://127.0.0.1:8545", logger=logging.getLogger(), retry_stop=None)
149+
response = await async_beacon.get_pending_deposits()
150+
assert response == response_json
151+
mocked_response.assert_called_once_with("head")
152+
153+
154+
@pytest.mark.asyncio()
155+
async def test_get_pending_partial_withdrawals(mocker: MockerFixture):
156+
response_json = {"data": ["withdrawal1", "withdrawal2"]}
157+
mocked_response = mocker.patch(
158+
"web3_utils.async_beacon.AsyncBeacon._get_pending_partial_withdrawals", return_value=response_json
159+
)
160+
async_beacon = AsyncBeacon("http://127.0.0.1:8545", logger=logging.getLogger(), retry_stop=None)
161+
response = await async_beacon.get_pending_partial_withdrawals()
162+
assert response == response_json
163+
mocked_response.assert_called_once_with("head")

web3_utils/async_beacon.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ async def get_validators(self, state_id: str = "head", ids: List[str] = None, st
5959
async def get_validator_balances(self, state_id: str = "head", indexes: List[str] = None) -> Dict[str, Any]:
6060
return await self._run_as_async(self._get_validator_balances, state_id, indexes)
6161

62+
async def get_pending_consolidations(self, state_id: str = "head"):
63+
return await self._run_as_async(self._get_pending_consolidations, state_id)
64+
65+
async def get_pending_deposits(self, state_id: str = "head"):
66+
return await self._run_as_async(self._get_pending_deposits, state_id)
67+
68+
async def get_pending_partial_withdrawals(self, state_id: str = "head"):
69+
return await self._run_as_async(self._get_pending_partial_withdrawals, state_id)
70+
6271
def _get_validator(self, pubkey: str, state_id: str = "head"):
6372
try:
6473
return super().get_validator(pubkey, state_id)
@@ -86,6 +95,18 @@ def _get_validator_balances(self, state_id: str = "head", indexes: List[str] = N
8695

8796
return self._make_get_request_with_params(endpoint, params)
8897

98+
def _get_pending_consolidations(self, state_id: str = "head"):
99+
endpoint = f"/eth/v1/beacon/states/{state_id}/pending_consolidations"
100+
return self._make_get_request_with_params(endpoint, params=None)
101+
102+
def _get_pending_deposits(self, state_id: str = "head"):
103+
endpoint = f"/eth/v1/beacon/states/{state_id}/pending_deposits"
104+
return self._make_get_request_with_params(endpoint, params=None)
105+
106+
def _get_pending_partial_withdrawals(self, state_id: str = "head"):
107+
endpoint = f"/eth/v1/beacon/states/{state_id}/pending_partial_withdrawals"
108+
return self._make_get_request_with_params(endpoint, params=None)
109+
89110
def _make_get_request_with_params(self, endpoint: str, params: Any) -> Dict[str, Any]:
90111
uri = URI(self.base_url + endpoint)
91112
return json_make_get_request(uri, timeout=self.request_timeout, params=params)

0 commit comments

Comments
 (0)