eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.63k stars 551 forks source link

Challenges working with proxy contracts #619

Open iamdefinitelyahuman opened 4 years ago

iamdefinitelyahuman commented 4 years ago

Overview

When dealing with proxy contracts, it's sometimes required to have two different ProjectContract objects pointing at the same address. Currently this fails because "contract already exists".

Specification

TBD - there must be an elegant solution to this though.

VitalyV1337 commented 3 years ago

Any updates on this?

aekasitt commented 2 years ago

I made a workaround for this;

from brownie import Proxy, Impl
from brownie.exceptions import ContractExists
from brownie.network.contract import ProjectContract, ContractContainer
from brownie.project.main import get_loaded_projects, Project

def wrap_proxy(contract_container: ContractContainer = Impl, contract_name: str = 'Impl') -> Impl:
  impl: Impl   = Impl.at('0x...')  # Deplyed Impl
  proxy: Proxy = Proxy.at('0x...') # Deployed Proxy
  impl_proxy: Impl
  try:
    impl_proxy = Impl.at(proxy.address)
  except ContractExists:
    project: Project = get_loaded_projects()[0]
    impl_proxy       = ProjectContract(project, build={'abi': contract_container.abi, 'contractName': contract_name}, address=proxy.address)
  return impl_proxy

See tests at this repo