Skip to content

Commit

Permalink
intermediate update 10 oct
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Oct 10, 2023
1 parent a91cd3f commit 333570c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 70 deletions.
67 changes: 67 additions & 0 deletions Notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
- `fds_contract.py`

```py
class FDSContract(AccountAPI):
"""
FDSContract class
constructor:
account: account to sign and send transactions
abi: abi of the contract. can be of type list, ABI, Dictionary or file containing the abi in json format
bytecode: hex format
"""

account: AccountAPI = None
raw_address: AddressType

def __init__(
self,
account: AccountAPI = None,
):
super().__init__()
self.account = account

@property
def contract(
self, address: AddressType, abi: Optional[Union[List[ABI], Dict, str, Path]] = None
) -> ContractInstance:
"""
to use the methods of the Contract class of ape.
i.e. to use something like contract = Contract("0xdead") which will make a contract container to work with
"""
self.address = address
self.abi = abi
if self.abi:
return self.chain_manager.contracts.instance_at(self.address, abi=self.abi)

return self.chain_manager.contracts.instance_at(self.address)

def Contract(self, address: AddressType) -> ContractInstance:
self.address = address

return self.contract(self.address)

def at(self, address: AddressType, abi: Optional[Union[List[ABI], Dict, str, Path]] = None):
self.address = address
self.abi = abi

return self.contract(self.address, self.abi)

def deploy(
self, contract: ContractContainer, *args, publish: bool = False, **kwargs
) -> ContractInstance:
if not self.account:
raise AccountNotFoundException("Account hash not been set up yet.")

return super().deploy(contract, *args, publish, **kwargs)

# * For ape AccountAPI class
@property
def address(self) -> AddressType:
self.raw_address # TODO: implement this

def sign_message(self):
raise NotImplementedException()

def sign_transaction(self):
raise NotImplementedException()
```
66 changes: 1 addition & 65 deletions src/fds/fds_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,70 +33,6 @@

from fds.utils.Exceptions import AccountNotFoundException, NotImplementedException

# class FDSContract(AccountAPI):
# """
# FDSContract class
# constructor:
# account: account to sign and send transactions
# abi: abi of the contract. can be of type list, ABI, Dictionary or file containing the abi in json format
# bytecode: hex format
# """

# account: AccountAPI = None
# raw_address: AddressType

# def __init__(
# self,
# account: AccountAPI = None,
# ):
# super().__init__()
# self.account = account

# @property
# def contract(
# self, address: AddressType, abi: Optional[Union[List[ABI], Dict, str, Path]] = None
# ) -> ContractInstance:
# """
# to use the methods of the Contract class of ape.
# i.e. to use something like contract = Contract("0xdead") which will make a contract container to work with
# """
# self.address = address
# self.abi = abi
# if self.abi:
# return self.chain_manager.contracts.instance_at(self.address, abi=self.abi)

# return self.chain_manager.contracts.instance_at(self.address)

# def Contract(self, address: AddressType) -> ContractInstance:
# self.address = address

# return self.contract(self.address)

# def at(self, address: AddressType, abi: Optional[Union[List[ABI], Dict, str, Path]] = None):
# self.address = address
# self.abi = abi

# return self.contract(self.address, self.abi)

# def deploy(
# self, contract: ContractContainer, *args, publish: bool = False, **kwargs
# ) -> ContractInstance:
# if not self.account:
# raise AccountNotFoundException("Account hash not been set up yet.")

# return super().deploy(contract, *args, publish, **kwargs)

# # * For ape AccountAPI class
# @property
# def address(self) -> AddressType:
# self.raw_address # TODO: implement this

# def sign_message(self):
# raise NotImplementedException()

# def sign_transaction(self):
# raise NotImplementedException()


class FDSContract:
def __init__(
Expand All @@ -123,7 +59,7 @@ def deploy(
raise AccountNotFoundException("Account hash not been set up yet.")
return

return super().deploy(contract, *args, publish, **kwargs)
return self.account.deploy(contract, *args, publish, **kwargs)

def deploy_from_abi(
self,
Expand Down
21 changes: 21 additions & 0 deletions src/fds/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from typing import cast

import ape # type: ignore
import pytest

from fds.fds_crypto import Crypto
from fds.fds_wallet import Wallet

TEST_CONTRACT_ADDRESS = "0x13370Df4d8fE698f2c186A18903f27e00a097331"


@pytest.fixture(scope="session")
def address():
return TEST_CONTRACT_ADDRESS


@pytest.fixture(autouse=True)
def crypto_instance():
Expand All @@ -23,6 +32,13 @@ def wallet_instance(test_wallet):


# * Ape fixtures
@pytest.fixture(autouse=True)
def eth_tester_provider(ethereum):
if not ape.networks.active_provider or ape.networks.provider.name != "test":
with ethereum.local.use_provider("test") as provider:
yield provider
else:
yield ape.networks.provider


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -136,3 +152,8 @@ def mock_provider(mock_web3, eth_tester_provider):
eth_tester_provider._web3 = mock_web3
yield eth_tester_provider
eth_tester_provider._web3 = web3


@pytest.fixture()
def contract_instance(eth_tester_provider, solidity_contract_instance):
return solidity_contract_instance
13 changes: 8 additions & 5 deletions src/fds/tests/unit_tests/test_fds_contract.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import pytest
from ape import accounts

from fds.fds_contract import FDSContract


def test_load_contract_from_abi(owner):
@pytest.mark.use_network("ethereum:local:test")
def test_load_contract_from_abi(owner, address):
abi = [
{
"inputs": [],
Expand All @@ -21,10 +23,11 @@ def test_load_contract_from_abi(owner):
},
]

account = owner
address = account.address
# address = "0x13370Df4d8fE698f2c186A18903f27e00a097331" # random address
fdscontract = FDSContract(owner)
loaded_contract = fdscontract.at(address=address, abi=abi)

print(dir(loaded_contract))
print(type(loaded_contract))
assert loaded_contract.address == address

# * test contract method
assert loaded_contract.getCount() == 0

0 comments on commit 333570c

Please sign in to comment.