From 333570c0da9c7f54b6fa8420901ea217f75ea7f2 Mon Sep 17 00:00:00 2001 From: SAIKAT KARMAKAR Date: Tue, 10 Oct 2023 14:29:53 +0100 Subject: [PATCH] intermediate update 10 oct --- Notes.md | 67 +++++++++++++++++++ src/fds/fds_contract.py | 66 +----------------- src/fds/tests/conftest.py | 21 ++++++ src/fds/tests/unit_tests/test_fds_contract.py | 13 ++-- 4 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 Notes.md diff --git a/Notes.md b/Notes.md new file mode 100644 index 0000000..e9e3566 --- /dev/null +++ b/Notes.md @@ -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() +``` diff --git a/src/fds/fds_contract.py b/src/fds/fds_contract.py index b1edef3..38940a4 100644 --- a/src/fds/fds_contract.py +++ b/src/fds/fds_contract.py @@ -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__( @@ -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, diff --git a/src/fds/tests/conftest.py b/src/fds/tests/conftest.py index 782f529..473bede 100644 --- a/src/fds/tests/conftest.py +++ b/src/fds/tests/conftest.py @@ -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(): @@ -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) @@ -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 diff --git a/src/fds/tests/unit_tests/test_fds_contract.py b/src/fds/tests/unit_tests/test_fds_contract.py index 2a578db..07ff9fc 100644 --- a/src/fds/tests/unit_tests/test_fds_contract.py +++ b/src/fds/tests/unit_tests/test_fds_contract.py @@ -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": [], @@ -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