Skip to content

Commit

Permalink
chore: address nits
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Sep 5, 2023
1 parent a859f20 commit 92bd690
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
43 changes: 14 additions & 29 deletions docs/userguides/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,20 @@ You can load contracts using their ABIs:
from ape import Contract

address = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
abi = '[{"inputs":[{"internalType":"contract MultiWrapper"...]'

contract = Contract(address, abi=abi)
# or
# path can be relative to project or absolute
abi_file = "abi.json"
contract = Contract(address, abi=abi_file)
# or
abi_as_object = [
{
"inputs": [{"internalType": "uint256", "name": "num", "type": "uint256"}],
"stateMutability": "nonpayable",
"type": "constructor",
},
{
"anonymous": False,
"inputs": [
{
"indexed": True,
"internalType": "address",
"name": "newAddress",
"type": "address",
}
],
"name": "AddressChange",
"type": "event",
}
]
contract = Contract(address, abi=abi_as_object)

# Using a JSON str:
contract = Contract(
address, abi='[{"name":"foo","type":"fallback", "stateMutability":"nonpayable"}]'
)

# Using a JSON file path:
contract = Contract(address, abi="abi.json")

# Using a Python dictionary from JSON:
contract = Contract(
address,
abi=[{"name":"foo","type":"fallback", "stateMutability":"nonpayable"}]
)
```

This will create the Contract instance from the given ABI.
Expand Down
11 changes: 2 additions & 9 deletions src/ape/managers/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,31 +1206,24 @@ def instance_at(
# if abi is type string then convert it to json object
if isinstance(abi, str):
try:
# convert the abi from string to list
list_abi = json.loads(abi)
# Create a new ContractType with the provided ABI
contract_type = ContractType(abi=list_abi)
except Exception as err:
if contract_type:
# If a default contract type was provided, don't error and use it.
logger.error(str(err))
else:
raise # Current exception
# if it's of type List[ABI] pass it directly to ContractType Class
elif isinstance(abi, list):
# Use the provided abi directly
contract_type = ContractType(abi=abi)
elif isinstance(abi, Path):
# Handle both absolute and relative paths
if not abi.is_absolute():
project_folder = Path.cwd()
abi = project_folder / abi
try:
with abi.open() as f:
json_str = f.read()
list_abi = json.loads(json_str)
# Create a new ContractType with the provided ABI from the file
contract_type = ContractType(abi=list_abi)
list_abi = json.loads(abi.read_text())
contract_type = ContractType(abi=list_abi)
except Exception as err:
if contract_type:
# If a default contract type was provided, don't error and use it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@
,"stateMutability":"pure","type":"function"},{"inputs":[]
,"name":"theAddress","outputs":[{"internalType":"address"
,"name":"","type":"address"}],"stateMutability":"view"
,"type":"function"}]
,"type":"function"}]

0 comments on commit 92bd690

Please sign in to comment.