Skip to content

Commit

Permalink
feat: abivalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Feb 1, 2024
1 parent 52b4582 commit 44d2659
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
14 changes: 1 addition & 13 deletions ape_etherscan/explorer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import json
from json.decoder import JSONDecodeError
from typing import Optional

from ape.api import ExplorerAPI, PluginConfig
from ape.contracts import ContractInstance
from ape.exceptions import ProviderNotConnectedError
from ape.logging import logger
from ape.types import AddressType, ContractType
from ethpm_types.source import Source

Expand Down Expand Up @@ -67,16 +64,7 @@ def _get_source_code(self, address: AddressType) -> SourceCodeResponse:

def get_contract_type(self, address: AddressType) -> Optional[ContractType]:
source_code = self._get_source_code(address)
if not (abi_string := source_code.abi):
return None

try:
abi = json.loads(abi_string)
except JSONDecodeError as err:
logger.error(f"Error with contract ABI: {err}")
return None

contract_type = ContractType(abi=abi, contractName=source_code.name)
contract_type = ContractType(abi=source_code.abi, contractName=source_code.name)
if source_code.name == "Vyper_contract" and "symbol" in contract_type.view_methods:
try:
contract = ContractInstance(address, contract_type)
Expand Down
7 changes: 6 additions & 1 deletion ape_etherscan/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EtherscanInstance:


class SourceCodeResponse(BaseModel):
abi: str = Field("", alias="ABI")
abi: List = Field([], alias="ABI")
name: str = Field("unknown", alias="ContractName")
source_code: str = Field("", alias="SourceCode")
compiler_version: str = Field("", alias="CompilerVersion")
Expand All @@ -38,6 +38,11 @@ class SourceCodeResponse(BaseModel):
def validate_bools(cls, value):
return bool(int(value))

@field_validator("abi", mode="before")
@classmethod
def validate_abi(cls, value):
return json.loads(value)


@dataclass
class ContractCreationResponse:
Expand Down

0 comments on commit 44d2659

Please sign in to comment.