Skip to content

Commit

Permalink
feat: LedgerContract switched path to optional (fetchai#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Morales committed Jul 1, 2022
1 parent 327b3d6 commit f2cd0d2
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ test:

.PHONY: unit-test
unit-test:
coverage run -m pytest $(PYCOSM_TESTS_DIR) --doctest-modules --ignore $(PYCOSM_TESTS_DIR)/vulture_whitelist.py -m "not integtest"
coverage run -m pytest $(PYCOSM_TESTS_DIR) --doctest-modules --ignore $(PYCOSM_TESTS_DIR)/vulture_whitelist.py -m "not integration"

.PHONY: integration-test
integration-test:
coverage run -m pytest $(PYCOSM_TESTS_DIR) --doctest-modules --ignore $(PYCOSM_TESTS_DIR)/vulture_whitelist.py -m "integtest"
coverage run -m pytest $(PYCOSM_TESTS_DIR) --doctest-modules --ignore $(PYCOSM_TESTS_DIR)/vulture_whitelist.py -m "integration"

.PHONY: coverage-report
coverage-report:
Expand Down
32 changes: 25 additions & 7 deletions cosmpy/aerial/contract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,34 @@ def _generate_label(digest: bytes) -> str:

class LedgerContract:
def __init__(
self, path: str, client: LedgerClient, address: Optional[Address] = None
self,
path: Optional[str],
client: LedgerClient,
address: Optional[Address] = None,
digest: Optional[bytes] = None,
):
self._path = path
self._client = client
self._digest = _compute_digest(self._path)
self._code_id = self._find_contract_id_by_digest(self._digest)
self._address = address

# select the digest either by computing it from the provided contract or by the value specified by
# the user
self._digest: Optional[bytes] = digest
if path is not None:
self._digest = _compute_digest(str(self._path))

# attempt to lookup the code id from the network by digest
if self._digest is not None:
self._code_id = self._find_contract_id_by_digest(self._digest)
else:
self._code_id = None

@property
def path(self) -> str:
def path(self) -> Optional[str]:
return self._path

@property
def digest(self) -> bytes:
def digest(self) -> Optional[bytes]:
return self._digest

@property
Expand All @@ -81,6 +95,8 @@ def store(
gas_limit: Optional[int] = None,
memo: Optional[str] = None,
) -> int:
if self._path is None:
raise RuntimeError("Unable to upload code, no contract provided")

# build up the store transaction
tx = Transaction()
Expand All @@ -107,8 +123,10 @@ def instantiate(
admin_address: Optional[Address] = None,
funds: Optional[str] = None,
) -> Address:
# query the account information for the sender
label = label or _generate_label(self._digest)

assert self._digest is not None

label = label or _generate_label(bytes(self._digest))

# build up the store transaction
tx = Transaction()
Expand Down
4 changes: 2 additions & 2 deletions docs/oracles.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ORACLE_VALUE_DECIMALS = 5
Next, we create a wallet and ledger interface to interact with the latest stable testnet:
```python
wallet = LocalWallet(PrivateKey("T7w1yHq1QIcQiSqV27YSwk+i1i+Y4JMKhkpawCQIh6s="))
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
```

Create the `LedgerContract` object:
Expand Down Expand Up @@ -86,7 +86,7 @@ Now we'll write a script that deploys a contract that can request the oracle val
We again start by creating a wallet and ledger interface in a new terminal session:
```python
wallet = LocalWallet(PrivateKey("CI5AZQcr+FNl2usnSIQYpXsGWvBxKLRDkieUNIvMOV8="))
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
```

Set `ORACLE_CONTRACT_ADDRESS` to the address of the contract deployed in the previous script:
Expand Down
2 changes: 1 addition & 1 deletion docs/stake-optimizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ First, you need to define a network to work with.
from cosmpy.aerial.client import LedgerClient
from cosmpy.aerial.config import NetworkConfig

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
```

## Set and Query Variables
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_atomic_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def main():
alice = LocalWallet(PrivateKey("X2Tv0Ok3RN2yi9GhWjLUX7RIfX5go9Wu+fwoJlqK2Og="))
bob = LocalWallet(PrivateKey("p0h0sYImB4xGq3Zz+xfIrY4QR6CPqeNg8w6X3NUWLe4="))

client = LedgerClient(NetworkConfig.latest_stable_testnet())
client = LedgerClient(NetworkConfig.fetchai_stable_testnet())

# check to see if all the clients have enough funds
alice_balance = client.query_bank_balance(alice.address())
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_compounder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def main():
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

# get all the active validators on the network
validators = ledger.query_validators()
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_deploy_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def main():

wallet = LocalWallet(PrivateKey("X2Tv0Ok3RN2yi9GhWjLUX7RIfX5go9Wu+fwoJlqK2Og="))

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

contract = LedgerContract(args.contract_path, ledger, address=args.contract_address)
contract.deploy({}, wallet)
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def main():

wallet = LocalWallet(PrivateKey("T7w1yHq1QIcQiSqV27YSwk+i1i+Y4JMKhkpawCQIh6s="))

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

contract = LedgerContract(args.contract_path, ledger, address=args.contract_address)

Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_oracle_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def main():

wallet = LocalWallet(PrivateKey("CI5AZQcr+FNl2usnSIQYpXsGWvBxKLRDkieUNIvMOV8="))

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

contract = LedgerContract(args.contract_path, ledger, address=args.contract_address)

Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_send_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def main():
alice = LocalWallet(PrivateKey("X2Tv0Ok3RN2yi9GhWjLUX7RIfX5go9Wu+fwoJlqK2Og="))
bob = LocalWallet(PrivateKey("p0h0sYImB4xGq3Zz+xfIrY4QR6CPqeNg8w6X3NUWLe4="))

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

print(
f"Alice Address: {alice.address()} Balance: {ledger.query_bank_balance(alice.address())}"
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_stake_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def M(x, f, S, k, D):


def main():
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

# Set initial stake and desired stake period
initial_stake = 50000000000000000000
Expand Down
2 changes: 1 addition & 1 deletion examples/aerial_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _wait_for_tx(operation: str, tx: SubmittedTx):
def main():
alice = LocalWallet(PrivateKey("p0h0sYImB4xGq3Zz+xfIrY4QR6CPqeNg8w6X3NUWLe4="))

ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())

# get all the active validators on the network
validators = ledger.query_validators()
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta:__legacy__"
build-backend = "setuptools.build_meta:__legacy__"

[tool.pytest.ini_options]
markers = [
"integration: marks tests as integration (deselect with '-m \"not integration\"')",
]
30 changes: 28 additions & 2 deletions tests/integration/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
def test_contract():
"""Test simple contract deploy execute and query."""
wallet = LocalWallet.generate()
faucet_api = FaucetApi(NetworkConfig.latest_stable_testnet())
faucet_api = FaucetApi(NetworkConfig.fetchai_stable_testnet())
faucet_api.get_wealth(wallet.address())
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
contract = LedgerContract(CONTRACT_PATH, ledger)
contract_address = contract.deploy({}, wallet)
assert contract_address
Expand All @@ -53,5 +53,31 @@ def test_contract():
assert result["value"] == value


@pytest.mark.integration
def test_deployed_contract():
"""Test interaction with already deployed contract."""
wallet = LocalWallet.generate()
faucet_api = FaucetApi(NetworkConfig.fetchai_stable_testnet())
faucet_api.get_wealth(wallet.address())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
contract = LedgerContract(CONTRACT_PATH, ledger)
contract_address = contract.deploy({}, wallet)
assert contract_address

deployed_contract = LedgerContract(None, ledger, contract_address)

result = deployed_contract.query({"get": {"owner": str(wallet.address())}})

assert not result["exists"]
assert not result["value"]

value = "foobar"
deployed_contract.execute({"set": {"value": value}}, wallet).wait_to_complete()
result = deployed_contract.query({"get": {"owner": str(wallet.address())}})

assert result["exists"]
assert result["value"] == value


if __name__ == "__main__":
pytest.main([__file__])
4 changes: 2 additions & 2 deletions tests/integration/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
@pytest.mark.integration
def test_faucet_transaction_balance():
"""Test faucet claims, tx settled, balance check."""
ledger = LedgerClient(NetworkConfig.latest_stable_testnet())
faucet_api = FaucetApi(NetworkConfig.latest_stable_testnet())
ledger = LedgerClient(NetworkConfig.fetchai_stable_testnet())
faucet_api = FaucetApi(NetworkConfig.fetchai_stable_testnet())
wallet1 = LocalWallet.generate()
wallet2 = LocalWallet.generate()

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ skipsdist = True
skip_install = True
deps =
-rrequirements-dev.txt
commands = coverage run -m pytest tests --doctest-modules --ignore tests/vulture_whitelist.py
commands = coverage run -m pytest tests --doctest-modules --ignore tests/vulture_whitelist.py -m "integration"

[testenv:coverage-report]
skipsdist = True
Expand Down

0 comments on commit f2cd0d2

Please sign in to comment.