Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to load contracts using ABIs [APE-1334] #1635

Merged
merged 18 commits into from
Sep 6, 2023
Merged

feat: option to load contracts using ABIs [APE-1334] #1635

merged 18 commits into from
Sep 6, 2023

Conversation

Aviksaikat
Copy link
Contributor

What I did

Added the option to load contracts using ABIs.

Example

contract = Contract(address, abi=abi)

fixes: #1615

How I did it

Added the option to pass abi in the instance_at method of ape.managers.chain.ContractCache Class.

How to verify it

  1. Clone this revision & install it using pip. Or use poetry to run a virtual enviroment.
  • Sample pyproject.toml for poetry
[tool.poetry]
name = "load_contracts_from_abi"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
readme = "README.md"
packages = [{include = "load_contracts_from_abi"}]

[tool.poetry.dependencies]
python = ">=3.10,<3.11"
eth-ape = "*"
ape-alchemy = "^0.6.2"
ape-solidity= "^0.6.7"
ape-foundry = "^0.6.14"
ape-etherscan = "*"
  1. Run poetry install && poetry shell
  2. Run pip install -e <path/to/this/revision>
  3. Run this code below
#!/usr/bin/python3
from ape import Contract


def main():

     abi = '[{"inputs":[{"internalType":"contract MultiWrapper","name":"_multiWrapper"
,"type":"address"},{"internalType":"contract IOracle[]","name":"existingOracles"
,"type":"address[]"},{"internalType":"enum OffchainOracle.OracleType[]","name":"oracleTypes"
,"type":"uint8[]"},{"internalType":"contract IERC20[]","name":"existingConnectors"
,"type":"address[]"},{"internalType":"contract IERC20","name":"wBase","type":"address"}]
,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false
,"internalType":"contract IERC20","name":"connector","type":"address"}],"name":"ConnectorAdded"
,"type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract
IERC20","name":"connector","type":"address"}],"name":"ConnectorRemoved","type":"event"}
,{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract MultiWrapper"
,"name":"multiWrapper","type":"address"}],"name":"MultiWrapperUpdated","type":"event"}
,{"anonymous":false,"inputs":[{"indexed":false,"internalType":"contract IOracle"
,"name":"oracle","type":"address"},{"indexed":false,"internalType":"enum OffchainOracle.OracleType"
,"name":"oracleType","type":"uint8"}],"name":"OracleAdded","type":"event"},{"anonymous":false
,"inputs":[{"indexed":false,"internalType":"contract IOracle","name":"oracle","type":"address"}
,{"indexed":false,"internalType":"enum OffchainOracle.OracleType","name":"oracleType"
,"type":"uint8"}],"name":"OracleRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true
,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true
,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred"
,"type":"event"},{"inputs":[{"internalType":"contract IERC20","name":"connector"
,"type":"address"}],"name":"addConnector","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[{"internalType":"contract IOracle","name":"oracle"
,"type":"address"},{"internalType":"enum OffchainOracle.OracleType","name":"oracleKind"
,"type":"uint8"}],"name":"addOracle","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[],"name":"connectors","outputs":[{"internalType":"contract
IERC20[]","name":"allConnectors","type":"address[]"}],"stateMutability":"view","type":"function"}
,{"inputs":[{"internalType":"contract IERC20","name":"srcToken","type":"address"}
,{"internalType":"contract IERC20","name":"dstToken","type":"address"},{"internalType":"bool"
,"name":"useWrappers","type":"bool"}],"name":"getRate","outputs":[{"internalType":"uint256"
,"name":"weightedRate","type":"uint256"}],"stateMutability":"view","type":"function"}
,{"inputs":[{"internalType":"contract IERC20","name":"srcToken","type":"address"}
,{"internalType":"bool","name":"useSrcWrappers","type":"bool"}],"name":"getRateToEth"
,"outputs":[{"internalType":"uint256","name":"weightedRate","type":"uint256"}],"stateMutability":"view"
,"type":"function"},{"inputs":[],"name":"multiWrapper","outputs":[{"internalType":"contract
MultiWrapper","name":"","type":"address"}],"stateMutability":"view","type":"function"}
,{"inputs":[],"name":"oracles","outputs":[{"internalType":"contract IOracle[]","name":"allOracles"
,"type":"address[]"},{"internalType":"enum OffchainOracle.OracleType[]","name":"oracleTypes"
,"type":"uint8[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner"
,"outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view"
,"type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"connector"
,"type":"address"}],"name":"removeConnector","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[{"internalType":"contract IOracle","name":"oracle"
,"type":"address"},{"internalType":"enum OffchainOracle.OracleType","name":"oracleKind"
,"type":"uint8"}],"name":"removeOracle","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[{"internalType":"contract MultiWrapper","name":"_multiWrapper"
,"type":"address"}],"name":"setMultiWrapper","outputs":[],"stateMutability":"nonpayable"
,"type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}]
,"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]' 
    address = "0x07D91f5fb9Bf7798734C3f606dB065549F6893bb"
    contract = Contract(address, abi=abi)

    assert contract.address == address

Checklist

  • All changes are completed
  • New test cases have been added
  • Documentation has been updated

@vany365 vany365 changed the title feat: added option to load contracts using ABIs feat: added option to load contracts using ABIs [APE-1334] Aug 29, 2023
@Aviksaikat Aviksaikat changed the title feat: added option to load contracts using ABIs [APE-1334] feat: option to load contracts using ABIs [APE-1334] Sep 1, 2023
antazoey
antazoey previously approved these changes Sep 5, 2023
Copy link
Member

@antazoey antazoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have like a hundred nits but the PR looks pretty good!

Do you mind tightening it up some by:

  1. Avoiding ... in the example
  2. Removing some of the superfluous comments
  3. Using abi_list = json.loads(abi.read_text()) instead of the with open block

docs/userguides/contracts.md Outdated Show resolved Hide resolved
docs/userguides/contracts.md Outdated Show resolved Hide resolved
docs/userguides/contracts.md Outdated Show resolved Hide resolved
docs/userguides/contracts.md Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
@antazoey
Copy link
Member

antazoey commented Sep 5, 2023

@Aviksaikat is busy and said they dont mind if i address the nits

src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
Copy link
Contributor

@NotPeopling2day NotPeopling2day left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@Aviksaikat
Copy link
Contributor Author

Wow!!, the refactoring looks leet. You really cleaned up the mess I created. I will try my best to follow in your footsteps in future PRs.

@antazoey antazoey merged commit 26d22cc into ApeWorX:main Sep 6, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to load contracts from abi [APE-1306]
4 participants