def test_can_create_advanced_collectible( get_keyhash, chainlink_fee, ): # Arrange if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip("Only for local testing") advanced_collectible = AdvancedCollectible.deploy( get_contract("vrf_coordinator").address, get_contract("link_token").address, get_keyhash, {"from": get_account()}, ) get_contract("link_token").transfer(advanced_collectible.address, chainlink_fee * 3, {"from": get_account()}) # Act transaction_receipt = advanced_collectible.createCollectible( "None", {"from": get_account()}) requestId = transaction_receipt.events["RequestedCollectible"]["requestId"] assert isinstance(transaction_receipt.txid, str) get_contract("vrf_coordinator").callBackWithRandomness( requestId, 777, advanced_collectible.address, {"from": get_account()}) # Assert assert advanced_collectible.tokenCounter() > 0 assert isinstance(advanced_collectible.tokenCounter(), int)
def test_can_create_advanced_collectible_integration( get_keyhash, chainlink_fee, ): # Arrange if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip("Only for integration testing") advanced_collectible = AdvancedCollectible.deploy( get_contract("vrf_coordinator").address, get_contract("link_token").address, get_keyhash, {"from": get_account()}, ) get_contract("link_token").transfer(advanced_collectible.address, chainlink_fee * 3, {"from": get_account()}) # Act advanced_collectible.createCollectible("None", {"from": get_account()}) # time.sleep(75) listen_for_event(advanced_collectible, "ReturnedCollectible", timeout=200, poll_interval=10) # Assert assert advanced_collectible.tokenCounter() > 0
def test_returns_random_number_testnet(get_keyhash, ): # Arrange if network.show_active() not in ["kovan", "rinkeby", "ropsten"]: pytest.skip("Only for testnet testing") vrf_consumer = VRFConsumer.deploy( get_keyhash, get_contract("vrf_coordinator").address, get_contract("link_token").address, config["networks"][network.show_active()]["fee"], {"from": get_account()}, ) tx = fund_with_link( vrf_consumer.address, amount=config["networks"][network.show_active()]["fee"]) tx.wait(1) # Act transaction_receipt = vrf_consumer.getRandomNumber({"from": get_account()}) assert isinstance(transaction_receipt.txid, str) transaction_receipt.wait(1) event_response = listen_for_event(vrf_consumer, "ReturnedRandomness") # Assert assert event_response.event is not None assert vrf_consumer.randomResult() > 0 assert isinstance(vrf_consumer.randomResult(), int)
def test_can_pick_winner_correctly(check_local_blockchain_envs, lottery_contract): # Arrange (by fixtures) account = get_account() lottery_contract.startLottery({"from": account}) lottery_contract.enter( {"from": account, "value": lottery_contract.getEntranceFee()} ) lottery_contract.enter( {"from": get_account(index=1), "value": lottery_contract.getEntranceFee()} ) lottery_contract.enter( {"from": get_account(index=2), "value": lottery_contract.getEntranceFee()} ) fund_with_link(lottery_contract) starting_balance_of_account = account.balance() balance_of_lottery = lottery_contract.balance() transaction = lottery_contract.endLottery({"from": account}) request_id = transaction.events["RequestedRandomness"]["requestId"] STATIC_RNG = 777 get_contract("vrf_coordinator").callBackWithRandomness( request_id, STATIC_RNG, lottery_contract.address, {"from": account} ) # 777 % 3 = 0 assert lottery_contract.recentWinner() == account assert lottery_contract.balance() == 0 assert account.balance() == starting_balance_of_account + balance_of_lottery
def test_can_get_latest_price(): # Arrange address = get_contract("eth_usd_price_feed").address # Act price_feed = PriceFeedConsumer.deploy(address, {"from": get_account()}) # Assert value = price_feed.getLatestPrice({"from": get_account()}) assert isinstance(value, int) assert value > 0
def test_can_create_simple_collectible(): if network.show_active() not in ["development"] or "fork" in network.show_active(): pytest.skip("Only for local testing") simple_collectible = SimpleCollectible.deploy( {"from": get_account(), "gas_price": chain.base_fee} ) simple_collectible.createCollectible( "None", {"from": get_account(), "gas_price": chain.base_fee} ) assert simple_collectible.ownerOf(0) == get_account()
def test_can_request_random_number(get_keyhash, chainlink_fee): # Arrange vrf_consumer = VRFConsumer.deploy( get_keyhash, get_contract("vrf_coordinator").address, get_contract("link_token").address, chainlink_fee, {"from": get_account()}, ) get_contract("link_token").transfer(vrf_consumer.address, chainlink_fee * 3, {"from": get_account()}) # Act requestId = vrf_consumer.getRandomNumber.call({"from": get_account()}) assert isinstance(requestId, convert.datatypes.HexString)
def enter_lottery(): account = get_account() lottery = Lottery[-1] value = lottery.getEntranceFee() + 100000000 tx = lottery.enter({"from": account, "value": value}) tx.wait(1) print("You entered the lottery!")
def test_cant_enter_unless_started(check_local_blockchain_envs, lottery_contract): # Arrange (by fixtures) # Act / Assert with pytest.raises(exceptions.VirtualMachineError): lottery_contract.enter( {"from": get_account(), "value": lottery_contract.getEntranceFee()} )
def main(): account = get_account() api_contract = APIConsumer[-1] tx = fund_with_link( api_contract.address, amount=config["networks"][network.show_active()]["fee"]) tx.wait(1) request_tx = api_contract.requestVolumeData({"from": account}) request_tx.wait(1)
def main(): account = get_account() vrf_contract = VRFConsumer[-1] tx = fund_with_link( vrf_contract.address, amount=config["networks"][network.show_active()]["fee"] ) tx.wait(1) vrf_contract.getRandomNumber({"from": account}) print("Requested!")
def main(): account = get_account() keeper_contract = Counter[-1] upkeepNeeded, performData = keeper_contract.checkUpkeep.call( # "0x000000000000000000000000d04647b7cb523bb9f26730e9b6de1174db7591ad", "", {"from": account}, ) print(f"The status of this upkeep is currently: {upkeepNeeded}") print(f"Here is the perform data: {performData}")
def test_can_end_lottery(check_local_blockchain_envs, lottery_contract): # Arrange (by fixtures) account = get_account() lottery_contract.startLottery({"from": account}) lottery_contract.enter( {"from": account, "value": lottery_contract.getEntranceFee()} ) fund_with_link(lottery_contract) lottery_contract.endLottery({"from": account}) assert lottery_contract.lottery_state() == 2
def test_can_start_and_enter_lottery(check_local_blockchain_envs, lottery_contract): # Arrange (by fixtures) account = get_account() lottery_contract.startLottery({"from": account}) # Act lottery_contract.enter( {"from": account, "value": lottery_contract.getEntranceFee()} ) # Assert assert lottery_contract.players(0) == account
def end_lottery(): account = get_account() lottery = Lottery[-1] # fund the contract # then end the lottery tx = fund_with_link(lottery.address) tx.wait(1) ending_transaction = lottery.endLottery({"from": account}) ending_transaction.wait(1) time.sleep(180) print(f"{lottery.recentWinner()} is the new winner!")
def test_can_call_check_upkeep(): # Arrange interval = 2 account = get_account() counter = Counter.deploy(interval, {"from": account}) upkeepNeeded, performData = counter.checkUpkeep.call( "", {"from": account}, ) assert isinstance(upkeepNeeded, bool) assert isinstance(performData, bytes)
def test_send_api_request_local( deploy_api_contract, chainlink_fee, get_data, ): # Arrange if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip("Only for local testing") api_contract = deploy_api_contract get_contract("link_token").transfer( api_contract.address, chainlink_fee * 2, {"from": get_account()} ) # Act transaction_receipt = api_contract.requestVolumeData({"from": get_account()}) requestId = transaction_receipt.events["ChainlinkRequested"]["id"] # Assert get_contract("oracle").fulfillOracleRequest( requestId, get_data, {"from": get_account()} ) assert isinstance(api_contract.volume(), int) assert api_contract.volume() > 0
def deploy_price_feed_consumer(): account = get_account() eth_usd_price_feed_address = get_contract("eth_usd_price_feed").address price_feed = PriceFeedConsumer.deploy( eth_usd_price_feed_address, {"from": account}, ) if (config["networks"][network.show_active()].get("verify", False)): price_feed.tx.wait(BLOCK_CONFIRMATIONS_FOR_VERIFICATION) PriceFeedConsumer.publish_source(price_feed) else: price_feed.tx.wait(1) print(f"The current price of ETH is {price_feed.getLatestPrice()}") return price_feed
def deploy_lottery(): account = get_account() lottery = Lottery.deploy( get_contract("eth_usd_price_feed").address, get_contract("vrf_coordinator").address, get_contract("link_token").address, config["networks"][network.show_active()]["fee"], config["networks"][network.show_active()]["keyhash"], {"from": account}, publish_source=config["networks"][network.show_active()].get( "verify", False), ) print("Deployed lottery!") return lottery
def test_returns_random_number_local(get_keyhash, chainlink_fee): # Arrange if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip("Only for local testing") vrf_consumer = VRFConsumer.deploy( get_keyhash, get_contract("vrf_coordinator").address, get_contract("link_token").address, chainlink_fee, {"from": get_account()}, ) get_contract("link_token").transfer(vrf_consumer.address, chainlink_fee * 3, {"from": get_account()}) # Act transaction_receipt = vrf_consumer.getRandomNumber({"from": get_account()}) # requestId = vrf_consumer.getRandomNumber.call({"from": get_account()}) requestId = transaction_receipt.return_value assert isinstance(transaction_receipt.txid, str) get_contract("vrf_coordinator").callBackWithRandomness( requestId, 777, vrf_consumer.address, {"from": get_account()}) # Assert assert vrf_consumer.randomResult() > 0 assert isinstance(vrf_consumer.randomResult(), int)
def deploy_api_contract(get_job_id, chainlink_fee): # Arrange / Act api_consumer = APIConsumer.deploy( get_contract("oracle").address, get_job_id, chainlink_fee, get_contract("link_token").address, {"from": get_account()}, ) block_confirmations=6 if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: block_confirmations=1 api_consumer.tx.wait(block_confirmations) # Assert assert api_consumer is not None return api_consumer
def test_can_pick_winner(check_local_blockchain_envs, lottery_contract): # Arrange (by fixtures) account = get_account() lottery_contract.startLottery({"from": account}) lottery_contract.enter({ "from": account, "value": lottery_contract.getEntranceFee() }) lottery_contract.enter({ "from": account, "value": lottery_contract.getEntranceFee() }) fund_with_link(lottery_contract) lottery_contract.endLottery({"from": account}) time.sleep(180) assert lottery_contract.recentWinner() == account assert lottery_contract.balance() == 0
def deploy_api_consumer(): jobId = config["networks"][network.show_active()]["jobId"] fee = config["networks"][network.show_active()]["fee"] account = get_account() oracle = get_contract("oracle").address link_token = get_contract("link_token").address api_consumer = APIConsumer.deploy( oracle, Web3.toHex(text=jobId), fee, link_token, {"from": account}, ) if (config["networks"][network.show_active()].get("verify", False)): api_consumer.tx.wait(BLOCK_CONFIRMATIONS_FOR_VERIFICATION) APIConsumer.publish_source(api_consumer) else: api_consumer.tx.wait(1) print(f"API Consumer deployed to {api_consumer.address}") return api_consumer
def depoly_vrf(): account = get_account() print(f"On network {network.show_active()}") keyhash = config["networks"][network.show_active()]["keyhash"] fee = config["networks"][network.show_active()]["fee"] vrf_coordinator = get_contract("vrf_coordinator") link_token = get_contract("link_token") vrf_consumer = VRFConsumer.deploy( keyhash, vrf_coordinator, link_token, fee, {"from": account}, ) if (config["networks"][network.show_active()].get("verify", False)): vrf_consumer.tx.wait(BLOCK_CONFIRMATIONS_FOR_VERIFICATION) VRFConsumer.publish_source(vrf_consumer) else: vrf_consumer.tx.wait(1)
def test_send_api_request_testnet(deploy_api_contract, chainlink_fee): # Arrange if network.show_active() not in ["kovan", "rinkeby", "mainnet"]: pytest.skip("Only for local testing") api_contract = deploy_api_contract if (config["networks"][network.show_active()].get("verify", False)): APIConsumer.publish_source(api_contract) tx = fund_with_link( api_contract.address, amount=chainlink_fee ) tx.wait(1) # Act transaction = api_contract.requestVolumeData({"from": get_account()}) transaction.wait(1) # Assert event_response = listen_for_event(api_contract, "DataFullfilled") assert event_response.event is not None assert isinstance(api_contract.volume(), int) assert api_contract.volume() > 0
def start_lottery(): account = get_account() lottery = Lottery[-1] starting_tx = lottery.startLottery({"from": account}) starting_tx.wait(1) print("The lottery is started!")