-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
contracts.py
294 lines (251 loc) · 11.1 KB
/
contracts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import json
import logging
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Generic, Literal, NamedTuple, TypeVar, overload
from eth_typing.abi import Decodable
from web3 import Web3
from web3._utils.abi import get_abi_output_types
from web3.types import BlockIdentifier
from rotkehlchen.chain.ethereum.abi import decode_event_data_abi
from rotkehlchen.globaldb.handler import GlobalDBHandler
from rotkehlchen.logging import RotkehlchenLogsAdapter
from rotkehlchen.types import ChainID, ChecksumEvmAddress
if TYPE_CHECKING:
from rotkehlchen.chain.ethereum.types import ETHEREUM_KNOWN_ABI
from rotkehlchen.chain.evm.node_inquirer import EvmNodeInquirer
from rotkehlchen.chain.evm.structures import EvmTxReceiptLog
from rotkehlchen.chain.evm.types import WeightedNode
from rotkehlchen.chain.optimism.types import OPTIMISM_KNOWN_ABI
logger = logging.getLogger(__name__)
log = RotkehlchenLogsAdapter(logger)
WEB3 = Web3()
class EvmContract(NamedTuple):
address: ChecksumEvmAddress
abi: list[dict[str, Any]]
deployed_block: int
def call(
self,
node_inquirer: 'EvmNodeInquirer',
method_name: str,
arguments: list[Any] | None = None,
call_order: Sequence['WeightedNode'] | None = None,
block_identifier: BlockIdentifier = 'latest',
) -> Any:
return node_inquirer.call_contract(
contract_address=self.address,
abi=self.abi,
method_name=method_name,
arguments=arguments,
call_order=call_order,
block_identifier=block_identifier,
)
def get_logs_since_deployment(
self,
node_inquirer: 'EvmNodeInquirer',
event_name: str,
argument_filters: dict[str, Any],
to_block: int | Literal['latest'] = 'latest',
call_order: Sequence['WeightedNode'] | None = None,
) -> Any:
return node_inquirer.get_logs(
contract_address=self.address,
abi=self.abi,
event_name=event_name,
argument_filters=argument_filters,
from_block=self.deployed_block,
to_block=to_block,
call_order=call_order,
)
def get_logs(
self,
node_inquirer: 'EvmNodeInquirer',
event_name: str,
argument_filters: dict[str, Any],
from_block: int,
to_block: int | Literal['latest'] = 'latest',
call_order: Sequence['WeightedNode'] | None = None,
) -> Any:
return node_inquirer.get_logs(
contract_address=self.address,
abi=self.abi,
event_name=event_name,
argument_filters=argument_filters,
from_block=from_block,
to_block=to_block,
call_order=call_order,
)
def encode(self, method_name: str, arguments: list[Any] | None = None) -> str:
contract = WEB3.eth.contract(address=self.address, abi=self.abi)
return contract.encodeABI(method_name, args=arguments if arguments else [])
def decode(
self,
result: Decodable,
method_name: str,
arguments: list[Any] | None = None,
) -> tuple[Any, ...]:
contract = WEB3.eth.contract(address=self.address, abi=self.abi)
fn_abi = contract._find_matching_fn_abi(
fn_identifier=method_name,
args=arguments if arguments else [],
)
output_types = get_abi_output_types(fn_abi)
return WEB3.codec.decode(output_types, result)
def decode_event(
self,
tx_log: 'EvmTxReceiptLog',
event_name: str,
argument_names: Sequence[str] | None,
) -> tuple[list, list]:
"""Decodes an event by finding the event ABI in the given contract's abi
Perhaps we can have a faster version of this method where instead of name
and argument names we just give the index of event abi in the list if we know it
"""
contract = WEB3.eth.contract(address=self.address, abi=self.abi)
event_abi = contract._find_matching_event_abi(
event_name=event_name,
argument_names=argument_names,
)
return decode_event_data_abi(tx_log=tx_log, event_abi=event_abi) # type: ignore
T = TypeVar('T', bound='ChainID')
class EvmContracts(Generic[T]):
"""A class allowing to query contract data for an Evm Chain. addresses and ABIs.
Some very frequently used abis are saved as class attributes in order to avoid
multiple DB reads and json importing. Class attributes to not duplicate across all evm chains
"""
erc20_abi: list[dict[str, Any]]
erc721_abi: list[dict[str, Any]]
univ1lp_abi: list[dict[str, Any]]
def __init__(self, chain_id: T) -> None:
self.chain_id = chain_id
@classmethod
def initialize_common_abis(cls) -> None:
"""Initialize common abi class attributes. Should be called only once at initialization"""
cls.erc20_abi = cls.abi_or_none(name='ERC20_TOKEN', fallback_to_packaged_db=True) # type: ignore # abi should exist in the DB # noqa: E501, RUF100
cls.erc721_abi = cls.abi_or_none('ERC721_TOKEN', fallback_to_packaged_db=True) # type: ignore # abi should exist in the DB # noqa: E501, RUF100
cls.univ1lp_abi = cls.abi_or_none('UNIV1_LP', fallback_to_packaged_db=True) # type: ignore # abi should exist in the DB # noqa: E501, RUF100
def contract_by_address(
self,
address: ChecksumEvmAddress,
fallback_to_packaged_db: bool = True,
) -> EvmContract | None:
"""
Returns contract data by address if found. Can fall back to packaged global db if
not found in the normal global DB
"""
globaldb = GlobalDBHandler()
with globaldb.conn.read_ctx() as cursor:
bindings = (self.chain_id.serialize_for_db(), address)
result = cursor.execute(
'SELECT contract_abi.value, contract_data.deployed_block FROM '
'contract_data LEFT JOIN contract_abi ON contract_data.abi=contract_abi.id '
'WHERE contract_data.chain_id=? AND contract_data.address=?',
bindings,
).fetchone()
if result is not None:
return EvmContract(
address=address,
abi=json.loads(result[0]), # not handling json error -- assuming DB consistency # noqa: E501
deployed_block=result[1] if result[1] else 0,
)
if fallback_to_packaged_db is False:
return None
# Try to find the contract in the packaged db
with globaldb.packaged_db_conn().read_ctx() as packaged_cursor:
log.debug(f'Using packaged globaldb to get contract {address} information')
result = packaged_cursor.execute(
'SELECT contract_data.address, contract_data.chain_id, '
'contract_data.deployed_block, contract_abi.name, contract_abi.value FROM '
'contract_data LEFT JOIN contract_abi ON '
'contract_data.abi=contract_abi.id WHERE contract_data.chain_id=? AND '
'contract_data.address=?',
bindings,
).fetchone()
if result is None:
log.debug(f"Couldn't find contract {address} in the packaged globaldb")
return None
# Copy the contract to the global db
abi_id = globaldb.get_or_write_abi(
serialized_abi=result[4],
abi_name=result[3],
)
with globaldb.conn.write_ctx() as write_cursor:
write_cursor.execute(
'INSERT OR IGNORE INTO contract_data(address, chain_id, abi, deployed_block) '
'VALUES (?, ?, ?, ?)',
(result[0], result[1], abi_id, result[2]),
)
log.debug(f'Saved contract {address} in the globaldb')
return EvmContract(
address=address,
abi=json.loads(result[4]), # not handling json error -- assuming DB consistency
deployed_block=result[2] if result[2] else 0,
)
def contract(self, address: ChecksumEvmAddress) -> EvmContract:
"""Gets details of an evm contract from the global DB by address
Missing contract is a programming error and should never happen.
"""
contract = self.contract_by_address(address=address, fallback_to_packaged_db=True)
assert contract, f'No contract data for {address} found'
return contract
@classmethod
def abi_or_none(
cls,
name: str,
fallback_to_packaged_db: bool = False,
) -> list[dict[str, Any]] | None:
"""Gets abi of an evm contract from the abi json file and optionally falls back to
the packaged db if the abi is not found.
Returns None if missing
"""
globaldb = GlobalDBHandler()
with globaldb.conn.read_ctx() as cursor:
result = cursor.execute(
'SELECT value FROM contract_abi WHERE name=?',
(name,),
).fetchone()
if result is not None:
return json.loads(result[0])
if fallback_to_packaged_db is False:
return None
# Try to find the ABI in the packaged db
with globaldb.packaged_db_conn().read_ctx() as packaged_cursor:
log.debug(f'Using packaged globaldb to get abi {name=} information')
result = packaged_cursor.execute(
'SELECT value FROM contract_abi WHERE name=?',
(name,),
).fetchone()
if result is None:
return None
globaldb.get_or_write_abi(
serialized_abi=result[0],
abi_name=name,
)
return json.loads(result[0])
@overload
def abi(self: 'EvmContracts[Literal[ChainID.ETHEREUM]]', name: 'ETHEREUM_KNOWN_ABI') -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.OPTIMISM]]', name: 'OPTIMISM_KNOWN_ABI') -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.POLYGON_POS]]', name: Literal['']) -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.ARBITRUM_ONE]]', name: Literal['']) -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.BASE]]', name: Literal['']) -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.GNOSIS]]', name: Literal['']) -> list[dict[str, Any]]: # noqa: E501
...
@overload
def abi(self: 'EvmContracts[Literal[ChainID.SCROLL]]', name: Literal['']) -> list[dict[str, Any]]: # noqa: E501
...
def abi(self, name: str) -> list[dict[str, Any]]:
"""Gets abi of an evm contract from the abi json file
Missing abi is a programming error and should never happen
"""
abi = self.abi_or_none(name=name, fallback_to_packaged_db=True)
assert abi, f'No abi for {name} found'
return abi