Skip to content

Commit

Permalink
10 oct update. fds_contract done with all tests passed. first draft o…
Browse files Browse the repository at this point in the history
…f fds_tx created
  • Loading branch information
Aviksaikat committed Oct 11, 2023
1 parent 333570c commit 22ecc5f
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 72 deletions.
1 change: 1 addition & 0 deletions .build/SolidityTestContract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"abi":[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"count","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"increment","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}],"ast":{"ast_type":"SourceUnit","children":[{"ast_type":"PragmaDirective","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":22,"start":32}},{"ast_type":"ContractDefinition","children":[{"ast_type":"VariableDeclaration","children":[{"ast_type":"ElementaryTypeName","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"address","src":{"jump_code":"","length":7,"start":92}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"owner","src":{"jump_code":"","length":20,"start":92}},{"ast_type":"VariableDeclaration","children":[{"ast_type":"ElementaryTypeName","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"uint256","src":{"jump_code":"","length":7,"start":118}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"count","src":{"jump_code":"","length":20,"start":118}},{"ast_type":"FunctionDefinition","children":[{"ast_type":"Block","children":[{"ast_type":"ExpressionStatement","children":[{"ast_type":"Assignment","children":[{"ast_type":"Identifier","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"owner","src":{"jump_code":"","length":5,"start":169}},{"ast_type":"MemberAccess","children":[{"ast_type":"Identifier","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"msg","src":{"jump_code":"","length":3,"start":177}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":10,"start":177}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":18,"start":169}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":18,"start":169}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":35,"start":159}},{"ast_type":"ParameterList","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":2,"start":156}},{"ast_type":"ParameterList","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","start":159}}],"classification":1,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"","src":{"jump_code":"","length":49,"start":145}},{"ast_type":"FunctionDefinition","children":[{"ast_type":"Block","children":[{"ast_type":"Return","children":[{"ast_type":"Identifier","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"count","src":{"jump_code":"","length":5,"start":269}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":12,"start":262}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":29,"start":252}},{"ast_type":"ParameterList","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":2,"start":217}},{"ast_type":"ParameterList","children":[{"ast_type":"VariableDeclaration","children":[{"ast_type":"ElementaryTypeName","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"uint256","src":{"jump_code":"","length":7,"start":243}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"","src":{"jump_code":"","length":7,"start":243}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":9,"start":242}}],"classification":1,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"getCount","src":{"jump_code":"","length":81,"start":200}},{"ast_type":"FunctionDefinition","children":[{"ast_type":"Block","children":[{"ast_type":"Return","children":[{"ast_type":"UnaryOperation","children":[{"ast_type":"Identifier","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"count","src":{"jump_code":"","length":5,"start":352}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":7,"start":352}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":14,"start":345}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":31,"start":335}},{"ast_type":"ParameterList","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":2,"start":305}},{"ast_type":"ParameterList","children":[{"ast_type":"VariableDeclaration","children":[{"ast_type":"ElementaryTypeName","children":[],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"uint256","src":{"jump_code":"","length":7,"start":326}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"","src":{"jump_code":"","length":7,"start":326}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":9,"start":325}}],"classification":1,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"increment","src":{"jump_code":"","length":79,"start":287}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"name":"SolidityTestContract","src":{"jump_code":"","length":312,"start":56}}],"classification":0,"col_offset":-1,"end_col_offset":-1,"end_lineno":-1,"lineno":-1,"src":{"jump_code":"","length":337,"start":32}},"contractName":"SolidityTestContract","deploymentBytecode":{"bytecode":"0x608060405234801561001057600080fd5b50600080546001600160a01b0319163317905561010e806100326000396000f3fe6080604052348015600f57600080fd5b506004361060465760003560e01c806306661abd14604b5780638da5cb5b146066578063a87d942c14608f578063d09de08a146096575b600080fd5b605360015481565b6040519081526020015b60405180910390f35b6000546078906001600160a01b031681565b6040516001600160a01b039091168152602001605d565b6001546053565b6053600180546000918260a78360b0565b91905055905090565b600060001982141560d157634e487b7160e01b600052601160045260246000fd5b506001019056fea264697066735822122001fe99b1c685802a63efc7614ec8b95700cde60ed68ecac2b2ce07be13549f2064736f6c63430008080033"},"devdoc":{"kind":"dev","methods":{},"version":1},"runtimeBytecode":{"bytecode":"0x6080604052348015600f57600080fd5b506004361060465760003560e01c806306661abd14604b5780638da5cb5b146066578063a87d942c14608f578063d09de08a146096575b600080fd5b605360015481565b6040519081526020015b60405180910390f35b6000546078906001600160a01b031681565b6040516001600160a01b039091168152602001605d565b6001546053565b6053600180546000918260a78360b0565b91905055905090565b600060001982141560d157634e487b7160e01b600052601160045260246000fd5b506001019056fea264697066735822122001fe99b1c685802a63efc7614ec8b95700cde60ed68ecac2b2ce07be13549f2064736f6c63430008080033"},"sourceId":"SolidityTestContract.sol","sourcemap":"56:312:0:-:0;;;145:49;;;;;;;;;-1:-1:-1;169:5:0;:18;;-1:-1:-1;;;;;;169:18:0;177:10;169:18;;;56:312;;;;;;","userdoc":{"kind":"user","methods":{},"version":1}}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none",
"docwriter.progress.trackClasses": true
"docwriter.progress.trackClasses": true,
"docify.commentService.programmingLanguage": "python"
}
19 changes: 19 additions & 0 deletions contracts/SolidityTestContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;

contract SolidityTestContract {
address public owner;
uint256 public count;

constructor() {
owner = msg.sender;
}

function getCount() external view returns (uint256) {
return count;
}

function increment() external returns (uint256) {
return count++;
}
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ name = "fds"
# "--import-mode=importlib",
#]
# NOTE: Prevents the ape plugin from activating on our tests
addopts = "-p no:ape_test"
# addopts = "-p no:ape_test"


[tool.black]
Expand Down
101 changes: 67 additions & 34 deletions src/fds/fds_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,93 @@
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
# import ape
from ape import Contract # , project
from ape.api.accounts import AccountAPI
from ape.contracts.base import ContractContainer, ContractInstance, ContractTransactionHandler
from ape.api.transactions import ReceiptAPI, TransactionAPI
from ape.contracts.base import ContractContainer, ContractInstance
from ape.types import AddressType
from ape.utils import BaseInterfaceModel
from ethpm_types import ABI, ContractType

from fds.utils.Exceptions import AccountNotFoundException, NotImplementedException
# * Fds modules
from fds.utils.Exceptions import AccountNotFoundException, ContractNotFoundException
from fds.utils.types import AbiType

# from ape.utils import BaseInterfaceModel
# from ethpm_types import ABI , ContractType

# from pandas import DataFrame



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

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

self.contract_address = address # type: ignore
self.abi = abi
return Contract(address=self.address, abi=self.abi)
self.contract = Contract(address=self.contract_address, abi=self.abi) # type: ignore
return self.contract

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
raise AccountNotFoundException("Account has not been set up yet.")

return self.account.deploy(contract, *args, publish, **kwargs)
self.contract = self.account.deploy(contract, *args, publish=publish, **kwargs)
self.contract_address = self.contract.address # type: ignore
return self.contract

def deploy_from_abi(
def call(
self,
address: AddressType,
abi: Optional[Union[List[ABI], Dict, str, Path]] = None,
**kwargs,
) -> ContractContainer:
txn: TransactionAPI,
send_everything: bool = False,
private: bool = False,
**signer_options,
) -> ReceiptAPI:
"""
Make a transaction call.
"""

if not self.contract_address or not self.contract_address == "0x":
raise ContractNotFoundException("Contract is not deployed yet.")
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)
raise AccountNotFoundException("Account has not been set up yet.")

return self.account.call(
txn=txn, send_everything=send_everything, private=private, **signer_options
)

# TODO: Decide whether we need this as ape has already a built in query method to get events
# def getPastEvents(
# self,
# *columns: List[str],
# event_name: str,
# start_block: int = 0,
# stop_block: Optional[int] = None,
# step: int = 1,
# engine_to_use: Optional[str] = None,
# ) -> DataFrame:
# # self.contract = contract
# # print(self.contract, self.contract_address, self.contract_address)
# # if not self.contract or not self.contract_address == "0x" or not self.contract_address:
# # raise ContractNotFoundException("Contract is not deployed yet.")

# self.events = getattr(self.contract, event_name).query(
# *columns,
# start_block=start_block,
# stop_block=stop_block,
# step_block=step,
# engine_to_use=engine_to_use,
# )

return self.account.deploy(self.contract, **kwargs)
# return self.events
42 changes: 42 additions & 0 deletions src/fds/fds_tx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from ape.api.accounts import AccountAPI
from ape.contracts.base import ContractContainer, ContractInstance
from ape.types import AddressType

from fds.fds_contract import FDSContract
from fds.utils.types import AbiType


class Tx:
def __init__(self, account: AccountAPI):
self.account = account
self.contract_address = None

def getContract(
self, account: AccountAPI, address: AddressType, abi: AbiType
) -> ContractInstance:
self.account = account
self.address = address
self.abi = abi

contract = FDSContract(self.account)
return contract.at(address=address, abi=self.abi)

def deployContract(
self, contract: ContractContainer, *args, publish: bool = False, **kwargs
) -> ContractInstance:
contract = FDSContract(self.account) # type: ignore

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

def syncNonce(self, account: AccountAPI) -> int:
"""
Sync the nonce with the account. This is used to prevent
accidental re - authenticating a user when they change their account.
@param account - The account to sync the nonce with. Must be an instance of AccountAPI.
@return The nonce
"""
self.account = account

return self.account.nonce
4 changes: 2 additions & 2 deletions src/fds/fds_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ def file_to_string(file):

buffer = utils.file_to_buffer("CHANGELOG.rst")
string = utils.file_to_string("CHANGELOG.rst")
print(string)
print(buffer)
# print(string)
# print(buffer)
93 changes: 80 additions & 13 deletions src/fds/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
from typing import cast
from pathlib import Path

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

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

TEST_CONTRACT_ADDRESS = "0x13370Df4d8fE698f2c186A18903f27e00a097331"
PROJECT_PATH = Path(__file__).parent
CONTRACTS_FOLDER = PROJECT_PATH / "data" / "contracts" / "abi"


abi = [
{"inputs": [], "stateMutability": "nonpayable", "type": "constructor"},
{
"inputs": [],
"name": "count",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
},
{
"inputs": [],
"name": "getCount",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "view",
"type": "function",
},
{
"inputs": [],
"name": "increment",
"outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}],
"stateMutability": "nonpayable",
"type": "function",
},
{
"inputs": [],
"name": "owner",
"outputs": [{"internalType": "address", "name": "", "type": "address"}],
"stateMutability": "view",
"type": "function",
},
]


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -137,15 +175,6 @@ def ethereum(networks):
return networks.ethereum


@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
def mock_provider(mock_web3, eth_tester_provider):
web3 = eth_tester_provider.web3
Expand All @@ -154,6 +183,44 @@ def mock_provider(mock_web3, eth_tester_provider):
eth_tester_provider._web3 = web3


@pytest.fixture()
def contract_instance(eth_tester_provider, solidity_contract_instance):
return solidity_contract_instance
@pytest.fixture(scope="session")
def get_contract_type():
def fn(name: str) -> ContractType:
return ContractType.parse_file(CONTRACTS_FOLDER / f"{name}.json")

return fn


@pytest.fixture(scope="session")
def solidity_contract_type(get_contract_type) -> ContractType:
return get_contract_type("SolidityTestContract")


@pytest.fixture(scope="session")
def solidity_contract_container(solidity_contract_type) -> ContractContainer:
return ContractContainer(contract_type=solidity_contract_type)


@pytest.fixture
def networks_connected_to_tester(eth_tester_provider):
return eth_tester_provider.network_manager


@pytest.fixture
def solidity_contract_instance(
owner, solidity_contract_container, networks_connected_to_tester
) -> ContractInstance:
return owner.deploy(solidity_contract_container)


@pytest.fixture
def ABI():
return abi


@pytest.fixture
def fdsContractDeploy(owner):
fdscontract = FDSContract(owner)
contract = fdscontract.deploy(ape.project.SolidityTestContract)

return contract
Loading

0 comments on commit 22ecc5f

Please sign in to comment.