Skip to content

Commit

Permalink
13 oct update. EnsRegistry class done. all tests added for test_fds_c…
Browse files Browse the repository at this point in the history
…ontract and passed
  • Loading branch information
Aviksaikat committed Oct 14, 2023
1 parent b89ef6e commit c460357
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 5 deletions.
57 changes: 53 additions & 4 deletions src/fds/contracts/ENSRegistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,59 @@
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 EnsRegistry contract
"""

from typing import Union

class ENSRegistry:
def __init__(self):
...
from ape import networks, project
from ape.api.accounts import AccountAPI
from ape.api.transactions import ReceiptAPI
from ape.types import AddressType


class EnsRegistry:
def __init__(self, account: AccountAPI, contract_address: Union[AddressType, None] = None):
self.account = account
self.contract_address = contract_address
self.contract = None
self.web3 = networks.provider._web3

if contract_address:
self.contract = project.ENSRegistry.at(self.contract_address)

# ? interacting with the contract methods
# * returns the address of the owner
def owner(self, node: int) -> str:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
return self.contract.owner(self.node) # type: ignore

def setResolver(self, node: int, address: AddressType) -> ReceiptAPI:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
return self.contract.setResolver(self.node, address, sender=self.account) # type: ignore

def getResolver(self, node: int) -> str:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
return self.contract.resolver(self.node) # type: ignore

def setOwner(self, node: int, address: AddressType) -> ReceiptAPI:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
return self.contract.setOwner(self.node, address, sender=self.account) # type: ignore

# * Returns the TTL of a node, and any records associated with it.
def getTTL(self, node: int) -> int:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
return self.contract.ttl(self.node) # type: ignore

def setTTL(self, node: int, ttl: int) -> ReceiptAPI:
node = node.to_bytes(32, byteorder="big") # type: ignore
self.node = f"0x{node.hex()}" # type: ignore
self.ttl = ttl
return self.contract.setTTL(self.node, self.ttl, sender=self.account) # type: ignore

# TODO: Add all other methods if required.
17 changes: 17 additions & 0 deletions src/fds/fds_ens2.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@
mailbox smart contracts
"""


from typing import Dict

from ape.api.accounts import AccountAPI

from fds.contracts.ENSRegistry import EnsRegistry

KEY_STRING = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" # noqa: 501


class FDSENS2:
def __init__(self, account: AccountAPI, config: Dict):
self.account = account
self.config = config

self.ensRegistry = EnsRegistry(self.account, self.config.get("subdomainRegistrarAddress"))
18 changes: 18 additions & 0 deletions src/fds/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import ape # type: ignore
import pytest
from ape import project
from ape.contracts.base import ContractContainer, ContractInstance
from ethpm_types import ContractType

from fds.contracts.ENSRegistry import EnsRegistry
from fds.fds_contract import FDSContract
from fds.fds_crypto import Crypto
from fds.fds_wallet import Wallet
Expand Down Expand Up @@ -229,3 +231,19 @@ def fdsContractDeploy(owner):
contract = fdscontract.deploy(ape.project.SolidityTestContract)

return contract


@pytest.fixture
def ensContract(owner):
contract = project.ENSRegistry.deploy(sender=owner)

return contract


@pytest.fixture
def ENS(owner, ensContract):
contract = ensContract

ENS = EnsRegistry(owner, contract.address)

return ENS
51 changes: 51 additions & 0 deletions src/fds/tests/unit_tests/test_ensregistry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
from ape.exceptions import ContractLogicError

from fds.contracts.ENSRegistry import EnsRegistry


def test_load_ensContract(owner, ENS):
assert ENS.owner(0) == owner.address


def test_set_resolver(owner, ENS):
ENS.setResolver(0, owner.address)

assert ENS.getResolver(0) == owner.address


def test_fail_setting_resolver(owner, ENS):
with pytest.raises(ContractLogicError):
ENS.setResolver(1, owner.address)


def test_set_owner(ENS, receiver):
ENS.setOwner(0, receiver.address)

assert ENS.owner(0) == receiver.address


def test_fail_setting_owner(owner, ensContract, receiver):
contract = ensContract

ENS = EnsRegistry(receiver, contract.address)

with pytest.raises(ContractLogicError):
ENS.setOwner(0, owner.address)


def test_set_n_get_ttl(ENS):
assert ENS.getTTL(0) == 0

ENS.setTTL(0, 420)

assert ENS.getTTL(0) == 420


def test_fail_setting_ttl(owner, receiver, ensContract):
contract = ensContract

ENS = EnsRegistry(owner, contract.address)

with pytest.raises(ContractLogicError):
ENS.setTTL(12, 1111)
2 changes: 1 addition & 1 deletion src/fds/tests/unit_tests/test_fds_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_deploy_contract(owner, fdsContractDeploy):
# fdscontract = FDSContract(owner)
# contract = fdscontract.deploy(project.SolidityTestContract)
contract = fdsContractDeploy
assert contract.address == "0xF2Df0b975c0C9eFa2f8CA0491C2d1685104d2488"
assert contract.address != "0x0000000000000000000000000000000000000000"
assert contract.balance == 0
assert contract.owner() == owner

Expand Down

0 comments on commit c460357

Please sign in to comment.