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_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 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_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 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_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 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_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 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 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 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)