-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
9th oct update. building fds_contract
- Loading branch information
1 parent
06fdeed
commit 8f15248
Showing
5 changed files
with
174 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AccountNotFoundException(Exception): | ||
pass | ||
|
||
|
||
class NotImplementedException(Exception): | ||
pass |