nucypher/tests/blockchain/eth/entities/deployers/test_deploy_preallocations.py

75 lines
2.9 KiB
Python

import os
import random
import pytest
from nucypher.blockchain.eth.constants import MIN_ALLOWED_LOCKED, MAX_MINTING_PERIODS
from nucypher.blockchain.eth.deployers import UserEscrowDeployer, UserEscrowProxyDeployer
@pytest.fixture(scope='function')
def user_escrow_proxy(three_agents):
token_agent, miner_agent, policy_agent = three_agents
testerchain = policy_agent.blockchain
deployer, alice, bob, *all_yall = testerchain.interface.w3.eth.accounts
escrow_proxy_deployer = UserEscrowProxyDeployer(deployer_address=deployer,
secret_hash=os.urandom(32))
_escrow_proxy_deployments_txhashes = escrow_proxy_deployer.deploy()
testerchain.time_travel(seconds=120)
yield escrow_proxy_deployer.contract_address
testerchain.interface.registry.clear()
testerchain.sever_connection()
@pytest.mark.slow()
def test_deploy_and_allocate(three_agents, user_escrow_proxy):
token_agent, miner_agent, policy_agent = three_agents
testerchain = policy_agent.blockchain
origin, alice, bob, *all_yall = testerchain.interface.w3.eth.accounts
deployments = dict()
allocation = MIN_ALLOWED_LOCKED * 1
number_of_deployments = 1
_last_deployment_address = None
for index in range(number_of_deployments):
escrow_deployer = UserEscrowDeployer(deployer_address=origin)
_deployment_txhashes = escrow_deployer.deploy()
# Ensure we have the correct assembly of address and abi
assert escrow_deployer.contract.address == escrow_deployer.contract.address
# assert escrow_deployer.contract_address == user_escrow_proxy # (address)
# Ensure each deployment is unique
if _last_deployment_address:
assert escrow_deployer.contract.address != _last_deployment_address
_last_deployment_address = escrow_deployer.contract.address
deployments[escrow_deployer.contract.address] = escrow_deployer
assert len(deployments) == number_of_deployments
# Let some time pass
testerchain.time_travel(hours=3)
assert token_agent.get_balance(address=origin) > 1
# Start allocating tokens
deposit_txhashes, approve_hashes = dict(), dict()
for address, deployer in deployments.items():
assert deployer.deployer_address == origin
deposit_txhash = deployer.initial_deposit(value=allocation, duration=MAX_MINTING_PERIODS)
receipt = testerchain.wait_for_receipt(txhash=deposit_txhash)
assert receipt['status'] == 1, "Transaction Rejected {}".format(deposit_txhash)
deposit_txhashes[address] = deposit_txhash
beneficiary = random.choice(all_yall)
assignment_txhash = deployer.assign_beneficiary(beneficiary)
receipt = testerchain.wait_for_receipt(txhash=assignment_txhash)
assert receipt['status'] == 1, "Transaction Rejected {}".format(assignment_txhash)
assert len(deposit_txhashes) == number_of_deployments == len(deployments)