Skip to content

Commit

Permalink
9th oct update. building fds_contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviksaikat committed Oct 10, 2023
1 parent 06fdeed commit 8f15248
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
"python.formatting.provider": "none",
"docwriter.progress.trackClasses": true
}
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ build-backend = "poetry.core.masonry.api"
[tool.pytest.project]
name = "fds"

# [tool.pytest.ini_options]
[tool.pytest.ini_options]
# addopts = [
# "--import-mode=importlib",
# ]
#]
# NOTE: Prevents the ape plugin from activating on our tests
addopts = "-p no:ape_test"


[tool.black]
Expand Down
146 changes: 132 additions & 14 deletions src/fds/fds_contract.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,141 @@
"""
Copyright 2023 The FairDataSociety Authors
This file is part of the FairDataSociety library.
Copyright 2023 The FairDataSociety Authors
This file is part of the FairDataSociety library.
The FairDataSociety library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The FairDataSociety library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The FairDataSociety library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
The FairDataSociety library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the FairDataSociety library. If not, see <http:www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with the FairDataSociety library. If not, see <http:www.gnu.org/licenses/>.
handles crypto
handles Contract interactions
"""

from pathlib import Path
# from ape.managers.chain import instance_at
from typing import Dict, List, Optional, Union

import ape
# * ape imports
from ape import Contract, project
from ape.api.accounts import AccountAPI
from ape.contracts.base import ContractContainer, ContractInstance, ContractTransactionHandler
from ape.types import AddressType
from ape.utils import BaseInterfaceModel
from ethpm_types import ABI, ContractType

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:
pass
def __init__(
self,
account: AccountAPI = None,
):
super().__init__()
self.account = account

def at(
self, address: AddressType, abi: Optional[Union[List[ABI], Dict, str, Path]] = None
) -> ContractInstance:
if not self.account:
raise AccountNotFoundException("Account has not been set yet")
return
self.address = address
self.abi = abi
return Contract(address=self.adddress, abi=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

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

def deploy_from_abi(
self,
address: AddressType,
abi: Optional[Union[List[ABI], Dict, str, Path]] = None,
**kwargs,
) -> ContractContainer:
if not self.account:
raise AccountNotFoundException("Account has not been set yet")
return
self.address = address
self.abi = abi
self.contract = Contract(address=self.address, abi=self.abi)

return self.account.deploy(self.contract, **kwargs)
30 changes: 30 additions & 0 deletions src/fds/tests/unit_tests/test_fds_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from ape import accounts

from fds.fds_contract import FDSContract


def test_load_contract_from_abi():
abi = [
{
"inputs": [],
"name": "getCount",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
},
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
]

address = "0xabcdababababababababababababab"

fdscontract = FDSContract()
loaded_contract = fdscontract.at(address=address, abi=abi)

print(dir(loaded_contract))
print(type(loaded_contract))
6 changes: 6 additions & 0 deletions src/fds/utils/Exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AccountNotFoundException(Exception):
pass


class NotImplementedException(Exception):
pass

0 comments on commit 8f15248

Please sign in to comment.