Skip to content

Commit

Permalink
feat: patch overloaded methods (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored May 18, 2022
1 parent 7541d6e commit b712f23
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
1 change: 0 additions & 1 deletion dank_mids/brownie_patch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

from dank_mids.brownie_patch.contract import patch_contract

3 changes: 1 addition & 2 deletions dank_mids/brownie_patch/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
from types import MethodType
from typing import Any, Dict, Optional, Tuple

from web3 import Web3

from brownie._config import CONFIG
from brownie.convert import Wei
from brownie.exceptions import VirtualMachineError
from brownie.network.account import Account
from brownie.network.rpc import Rpc
from brownie.network.state import Chain
from brownie.network.transaction import TransactionReceipt
from web3 import Web3

rpc = Rpc()

Expand Down
5 changes: 2 additions & 3 deletions dank_mids/brownie_patch/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from typing import Coroutine, Dict, Tuple, Union

import eth_abi
from hexbytes import HexBytes
from web3 import Web3

from brownie.exceptions import VirtualMachineError
from brownie.network.contract import ContractCall, _get_tx
from brownie.project.compiler.solidity import SOLIDITY_ERROR_CODES
from hexbytes import HexBytes
from web3 import Web3


def _patch_call(call: ContractCall, w3: Web3) -> None:
Expand Down
23 changes: 15 additions & 8 deletions dank_mids/brownie_patch/contract.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@

from typing import Optional
from typing import Optional, Union

from brownie import Contract, network, web3
from brownie.network.contract import ContractCall, ContractTx, OverloadedMethod
from dank_mids.brownie_patch.call import _patch_call
from dank_mids.brownie_patch.overloaded import _patch_overloaded_method
from dank_mids.brownie_patch.tx import _patch_tx
from web3 import Web3

from brownie import Contract, network, web3
from brownie.network.contract import ContractCall, ContractTx
ContractMethod = Union[ContractCall, ContractTx, OverloadedMethod]

def _patch_if_method(method: ContractMethod, w3: Web3):
if isinstance(method, ContractCall) or isinstance(method, ContractTx):
_patch_call(method, w3)
# TODO implement this properly
#elif isinstance(method, ContractTx):
# _patch_tx(method, w3)
elif isinstance(method, OverloadedMethod):
_patch_overloaded_method(method, w3)


def patch_contract(contract: Contract, w3: Optional[Web3]) -> Contract:
if w3 is None and network.is_connected():
w3 = web3
assert w3 is not None, "You must make sure either brownie is connected or you pass in a Web3 instance."
for k, v in contract.__dict__.items():
if isinstance(v, ContractCall):
_patch_call(v, w3)
# TODO implement this properly
#elif isinstance(v, ContractTx):
# _patch_tx(v, w3)
_patch_if_method(v, w3)
return contract
31 changes: 31 additions & 0 deletions dank_mids/brownie_patch/overloaded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import functools
from types import MethodType
from typing import Coroutine, Dict, Tuple, Union

from brownie.network.contract import ContractCall, ContractTx, OverloadedMethod
from dank_mids.brownie_patch.call import _patch_call
from dank_mids.brownie_patch.tx import _patch_tx
from web3 import Web3


def _patch_overloaded_method(call: OverloadedMethod, w3: Web3) -> None:
@functools.wraps(call)
async def coroutine(
self,
*args: Tuple,
block_identifier: Union[int, str, bytes] = None,
override: Dict = None
) -> Coroutine:
fn = self._get_fn_from_args(args)
kwargs = {"block_identifier": block_identifier, "override": override}
kwargs = {k: v for k, v in kwargs.items() if v is not None}
return await fn.coroutine(*args, **kwargs)

for args, method in call.__dict__['methods'].items():
if isinstance(method, ContractCall) or isinstance(method, ContractTx):
_patch_call(method, w3)
# TODO implement this properly
#elif isinstance(call, ContractTx):
#_patch_tx(call, w3)

call.coroutine = MethodType(coroutine, call)
2 changes: 1 addition & 1 deletion dank_mids/brownie_patch/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Coroutine, Dict, Tuple, Union

from brownie.network.contract import ContractTx, _get_tx
from dank_mids.brownie_patch.account import _patch_account
from web3 import Web3

from dank_mids.brownie_patch.account import _patch_account

def _patch_tx(call: ContractTx, w3: Web3) -> None:

Expand Down
2 changes: 1 addition & 1 deletion tests/test_brownie_patch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from brownie import Contract
from dank_mids.brownie_patch import patch_contract
from dank_mids.brownie_patch.call import _patch_call
from dank_mids.brownie_patch.contractcall import _patch_call
from dank_mids.brownie_patch.tx import _patch_tx
from multicall.utils import await_awaitable

Expand Down

0 comments on commit b712f23

Please sign in to comment.