1- from typing import (
2- Optional ,
3- Union ,
4- Any ,
5- List ,
6- Iterable ,
7- TypeVar ,
8- Type ,
9- Dict ,
10- )
1+ from __future__ import annotations
112
12- from eth_typing import (
13- URI ,
14- )
3+ from typing import Any , Iterable , Type , TypeVar
4+
5+ from eth_typing import URI
156from web3 import HTTPProvider
16- from web3 .types import (
17- RPCEndpoint ,
18- RPCResponse ,
19- )
7+ from web3 .types import RPCEndpoint , RPCResponse
208
219from ankr import types
2210from ankr .exceptions import AdvancedAPIException
2311
24-
25- TRequestPaginated = TypeVar ("TRequestPaginated" , bound = types .RequestPaginated )
26- TReplyPaginated = TypeVar ("TReplyPaginated" , bound = types .ReplyPaginated )
12+ TRequest = TypeVar ("TRequest" , bound = types .RPCModel )
13+ TReply = TypeVar ("TReply" , bound = types .RPCModel )
14+ TRequestPaginated = TypeVar ("TRequestPaginated" , bound = types .RPCRequestPaginated )
15+ TReplyPaginated = TypeVar ("TReplyPaginated" , bound = types .RPCReplyPaginated )
2716
2817
2918class AnkrProvider (HTTPProvider ):
3019 def __init__ (
3120 self ,
3221 api_key : str = "" ,
33- endpoint_uri : Optional [ Union [ URI , str ]] = None ,
34- request_kwargs : Optional [ Any ] = None ,
35- session : Optional [ Any ] = None ,
22+ endpoint_uri : URI | str | None = None ,
23+ request_kwargs : Any | None = None ,
24+ session : Any = None ,
3625 ) -> None :
3726 if endpoint_uri is None :
3827 endpoint_uri = "https://rpc.ankr.com/multichain/"
@@ -46,7 +35,18 @@ def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
4635 raise AdvancedAPIException ("returned no result" )
4736 return response
4837
49- def make_request_paginated (
38+ def call_method (
39+ self ,
40+ rpc : str ,
41+ request : TRequest ,
42+ reply_type : Type [TReply ],
43+ ) -> TReply :
44+ request_dict = request .dict (by_alias = True , exclude_none = True )
45+ response = self .make_request (RPCEndpoint (rpc ), request_dict )
46+ reply = reply_type (** response ["result" ])
47+ return reply
48+
49+ def call_method_paginated (
5050 self ,
5151 rpc : str ,
5252 request : TRequestPaginated ,
@@ -60,59 +60,4 @@ def make_request_paginated(
6060
6161 if reply .next_page_token :
6262 request .page_token = reply .next_page_token
63- yield from self .make_request_paginated (
64- RPCEndpoint (rpc ), request , reply_type
65- )
66-
67-
68- class AnkrAdvancedAPI :
69- def __init__ (
70- self ,
71- api_key : Optional [str ] = None ,
72- endpoint_uri : Optional [str ] = None ,
73- ) -> None :
74- self .provider = AnkrProvider (api_key or "" , endpoint_uri )
75-
76- def get_logs (
77- self ,
78- blockchain : types .BlockchainNames ,
79- from_block : Optional [types .BlockNumber ] = None ,
80- to_block : Optional [types .BlockNumber ] = None ,
81- address : Optional [types .AddressOrAddresses ] = None ,
82- topics : Optional [types .Topics ] = None ,
83- decode_logs : Optional [bool ] = None ,
84- ** kwargs : Any ,
85- ) -> Iterable [types .Log ]:
86- for reply in self .provider .make_request_paginated (
87- "ankr_getLogs" ,
88- types .GetLogsRequest (
89- blockchain = blockchain ,
90- from_block = from_block ,
91- to_block = to_block ,
92- address = address ,
93- topics = topics ,
94- decode_logs = decode_logs ,
95- ** kwargs ,
96- ),
97- types .GetLogsReply ,
98- ):
99- yield from reply .logs
100-
101- def get_nfts (
102- self ,
103- blockchain : types .BlockchainNames ,
104- wallet_address : str ,
105- filter : Optional [List [Dict [str , List [str ]]]] = None ,
106- ** kwargs : Any ,
107- ) -> Iterable [types .Nft ]:
108- for reply in self .provider .make_request_paginated (
109- "ankr_getNFTsByOwner" ,
110- types .GetNFTsByOwnerRequest (
111- blockchain = blockchain ,
112- wallet_address = wallet_address ,
113- filter = filter ,
114- ** kwargs ,
115- ),
116- types .GetNFTsByOwnerReply ,
117- ):
118- yield from reply .assets
63+ yield from self .call_method_paginated (RPCEndpoint (rpc ), request , reply_type )
0 commit comments