diff --git a/brownie-config.yaml b/brownie-config.yaml index 837027b..3a86443 100644 --- a/brownie-config.yaml +++ b/brownie-config.yaml @@ -4,12 +4,12 @@ reports: exclude_contracts: - SafeMath dependencies: - - smartcontractkit/chainlink-brownie-contracts@1.0.2 + - smartcontractkit/chainlink-brownie-contracts@1.1.1 - OpenZeppelin/openzeppelin-contracts@3.4.0 compiler: solc: remappings: - - '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.0.2' + - '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1' - '@openzeppelin=OpenZeppelin/openzeppelin-contracts@3.4.0' # automatically fetch contract sources from Etherscan autofetch_sources: True @@ -17,6 +17,11 @@ dotenv: .env # set a custom mnemonic for the development network networks: default: development + development: + keyhash: '0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4' + fee: 100000000000000000 + jobId: '29fa9aa13bf1468788b7cc4a500a45b8' + verify: False ganache: verify: False mainnet-fork: diff --git a/contracts/Lottery.sol b/contracts/Lottery.sol index ccf538c..3d7f2e6 100644 --- a/contracts/Lottery.sol +++ b/contracts/Lottery.sol @@ -8,7 +8,11 @@ import "@openzeppelin/contracts/access/Ownable.sol"; contract Lottery is VRFConsumerBase, Ownable { using SafeMathChainlink for uint256; AggregatorV3Interface internal ethUsdPriceFeed; - enum LOTTERY_STATE { OPEN, CLOSED, CALCULATING_WINNER } + enum LOTTERY_STATE { + OPEN, + CLOSED, + CALCULATING_WINNER + } LOTTERY_STATE public lottery_state; uint256 public lotteryId; address payable[] public players; @@ -20,64 +24,83 @@ contract Lottery is VRFConsumerBase, Ownable { uint256 public randomness; event RequestedRandomness(bytes32 requestId); - constructor(address ethUsdPriceFeedAddress, address _vrfCoordinator, address _link, bytes32 _keyHash) + constructor( + address ethUsdPriceFeedAddress, + address _vrfCoordinator, + address _link, + bytes32 _keyHash + ) + public VRFConsumerBase( _vrfCoordinator, // VRF Coordinator - _link // LINK Token - ) public - { + _link // LINK Token + ) + { ethUsdPriceFeed = AggregatorV3Interface(ethUsdPriceFeedAddress); lotteryId = 1; lottery_state = LOTTERY_STATE.CLOSED; keyHash = _keyHash; - usdEntryFee = 50; + usdEntryFee = 50 * (10**18); } function enter() public payable { require(msg.value >= getEntranceFee(), "Not enough ETH to enter!"); require(lottery_state == LOTTERY_STATE.OPEN); players.push(msg.sender); - } + } - function getEntranceFee() public view returns(uint256){ - uint256 precision = 1 * 10 ** 36; + function getEntranceFee() public view returns (uint256) { + uint256 precision = 1 * 10**18; uint256 price = getLatestEthUsdPrice(); // 2000_000000000000000000 - // uint256 costToEnter = usdEntryFee * 10 ** 18 / price; This would be 0.25 - uint256 costToEnter = usdEntryFee * precision / price; + uint256 costToEnter = (usdEntryFee * precision) / price; return costToEnter; } function getLatestEthUsdPrice() public view returns (uint256) { - (,int price,,,) = ethUsdPriceFeed.latestRoundData(); + (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData(); price = price * 10000000000; return uint256(price); } - + function startLottery() public onlyOwner { - require(lottery_state == LOTTERY_STATE.CLOSED, "can't start a new lottery yet"); + require( + lottery_state == LOTTERY_STATE.CLOSED, + "can't start a new lottery yet" + ); lottery_state = LOTTERY_STATE.OPEN; randomness = 0; } - function endLottery(uint256 userProvidedSeed) public onlyOwner { - require(lottery_state == LOTTERY_STATE.OPEN, "Can't end a lottery that hasnt started!"); - lottery_state = LOTTERY_STATE.CALCULATING_WINNER; - pickWinner(userProvidedSeed); + function endLottery() public onlyOwner { + require( + lottery_state == LOTTERY_STATE.OPEN, + "Can't end a lottery that hasnt started!" + ); + lottery_state = LOTTERY_STATE.CALCULATING_WINNER; + pickWinner(); } - - function pickWinner(uint256 userProvidedSeed) private returns (bytes32){ + function pickWinner() private returns (bytes32) { // DONT do something like this: // uint(keccak256(abi.encodePacked(nonce, msg.sender, block.difficulty, block.timestamp))) % players.length; - require(lottery_state == LOTTERY_STATE.CALCULATING_WINNER, "You aren't at that stage yet!"); - bytes32 requestId = requestRandomness(keyHash, fee, userProvidedSeed); + require( + lottery_state == LOTTERY_STATE.CALCULATING_WINNER, + "You aren't at that stage yet!" + ); + bytes32 requestId = requestRandomness(keyHash, fee); emit RequestedRandomness(requestId); } - - function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override { - require(lottery_state == LOTTERY_STATE.CALCULATING_WINNER, "You aren't at that stage yet!"); - require(randomness > 0, "random-not-found"); - uint256 index = randomness % players.length; + + function fulfillRandomness(bytes32 _requestId, uint256 _randomness) + internal + override + { + require( + lottery_state == LOTTERY_STATE.CALCULATING_WINNER, + "You aren't at that stage yet!" + ); + require(_randomness > 0, "random-not-found"); + uint256 index = _randomness % players.length; recentWinner = players[index]; players[index].transfer(address(this).balance); players = new address payable[](0); @@ -88,8 +111,8 @@ contract Lottery is VRFConsumerBase, Ownable { function get_players() public view returns (address payable[] memory) { return players; } - - function get_pot() public view returns(uint256){ + + function get_pot() public view returns (uint256) { return address(this).balance; } } diff --git a/scripts/0_deploy_mocks.py b/scripts/0_deploy_mocks.py deleted file mode 100644 index 52fdf97..0000000 --- a/scripts/0_deploy_mocks.py +++ /dev/null @@ -1,37 +0,0 @@ -from brownie import ( - LinkToken, - MockOracle, - MockV3Aggregator, - VRFCoordinatorMock, - network, -) -from scripts.helpful_scripts import ( - NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS, - get_account, -) - -DECIMALS = 8 -# This is 2,000 -INITIAL_VALUE = 200000000000 - - -def main(): - if network.show_active() in NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS: - print(f"The active network is {network.show_active()}") - print("Deploying Mocks...") - account = get_account() - print("Deploying Mock Link Token...") - link_token = LinkToken.deploy({"from": account}) - print("Deploying Mock Price Feed...") - mock_price_feed = MockV3Aggregator.deploy( - DECIMALS, INITIAL_VALUE, {"from": account} - ) - print("Deploying Mock VRFCoordinator...") - mock_vrf_coordinator = VRFCoordinatorMock.deploy( - link_token.address, {"from": account} - ) - print("Deploying Mock Oracle...") - mock_oracle = MockOracle.deploy(link_token.address, {"from": account}) - print("Mocks Deployed!") - else: - print("Only deploy mocks to a non-forked local network!") diff --git a/scripts/1_deploy_lottery.py b/scripts/1_deploy_lottery.py deleted file mode 100644 index cda930a..0000000 --- a/scripts/1_deploy_lottery.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/python3 -from brownie import Lottery, accounts, config, network - - -def main(): - account = accounts.add(config["wallets"]["from_key"]) - print(network.show_active()) - verify = ( - config["networks"][network.show_active()]["verify"] - if config["networks"][network.show_active()].get("verify") - else False - ) - return Lottery.deploy( - config["networks"][network.show_active()]["eth_usd_price_feed"], - config["networks"][network.show_active()]["vrf_coordinator"], - config["networks"][network.show_active()]["link_token"], - config["networks"][network.show_active()]["keyhash"], - {"from": account}, - publish_source=verify, - ) diff --git a/scripts/2_start_lottery.py b/scripts/2_start_lottery.py deleted file mode 100644 index 5d85a33..0000000 --- a/scripts/2_start_lottery.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 -from brownie import Lottery, accounts, config -from scripts.helpful_scripts import fund_with_link - -lottery_enum = {0: "OPEN", 1: "CLOSED", 2: "CALCULATING_WINNER"} - - -def main(): - account = accounts.add(config["wallets"]["from_key"]) - lottery = Lottery[len(Lottery) - 1] - transaction = fund_with_link(lottery.address) - transaction.wait(1) - transaction_2 = lottery.startLottery({"from": account}) - transaction_2.wait(1) - print(f"The lottery is currently {lottery_enum[lottery.lottery_state()]}") diff --git a/scripts/3_enter_lottery.py b/scripts/3_enter_lottery.py deleted file mode 100644 index 52e77f2..0000000 --- a/scripts/3_enter_lottery.py +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/python3 -from brownie import Lottery, accounts, config - - -def main(): - account = accounts.add(config["wallets"]["from_key"]) - lottery = Lottery[len(Lottery) - 1] - value = lottery.getEntranceFee() + 100000000 - transaction_2 = lottery.enter({"from": account, "value": value}) - transaction_2.wait(1) - print(f"{account} has entered the lottery!") diff --git a/scripts/4_end_lottery.py b/scripts/4_end_lottery.py deleted file mode 100644 index 2243ba3..0000000 --- a/scripts/4_end_lottery.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/python3 -from brownie import Lottery, accounts, config -import time - -STATIC_SEED = 123 - - -def main(): - account = accounts.add(config["wallets"]["from_key"]) - lottery = Lottery[len(Lottery) - 1] - print(f"The previous winner is {lottery.recentWinner()}, let's get a new one!") - transaction = lottery.endLottery(STATIC_SEED, {"from": account}) - transaction.wait(1) - time.sleep(60) - print(f"{lottery.recentWinner()} is the new winner!") diff --git a/scripts/deploy_lottery.py b/scripts/deploy_lottery.py new file mode 100644 index 0000000..6a0ac9a --- /dev/null +++ b/scripts/deploy_lottery.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 +from brownie import Lottery, accounts, config, network +from scripts.helpful_scripts import get_contract, get_account, fund_with_link +import time + +lottery_enum = {0: "OPEN", 1: "CLOSED", 2: "CALCULATING_WINNER"} + + +def deploy_lottery(): + account = get_account() + # level up get_account for all 3 though + print(network.show_active()) + return Lottery.deploy( + get_contract("eth_usd_price_feed").address, + get_contract("vrf_coordinator").address, + get_contract("link_token").address, + config["networks"][network.show_active()]["keyhash"], + {"from": account}, + publish_source=config["networks"][network.show_active()].get("verify", False), + ) + + +def start_lottery(): + account = get_account() + lottery = Lottery[-1] + transaction = fund_with_link(lottery.address) + transaction.wait(1) + transaction_2 = lottery.startLottery({"from": account}) + transaction_2.wait(1) + print(f"The lottery is currently {lottery_enum[lottery.lottery_state()]}") + + +def enter_lottery(): + account = get_account() + lottery = Lottery[-1] + value = lottery.getEntranceFee() + 100000000 + transaction_2 = lottery.enter({"from": account, "value": value}) + transaction_2.wait(1) + print(f"{account} has entered the lottery!") + + +def end_lottery(): + account = get_account() + lottery = Lottery[-1] + print(f"The previous winner is {lottery.recentWinner()}, let's get a new one!") + transaction = lottery.endLottery({"from": account}) + transaction.wait(1) + time.sleep(60) + print(f"{lottery.recentWinner()} is the new winner!") + + +def main(): + deploy_lottery() + # start_lottery() + # enter_lottery() + # end_lottery() diff --git a/scripts/deploy_mocks.py b/scripts/deploy_mocks.py new file mode 100644 index 0000000..e9c367e --- /dev/null +++ b/scripts/deploy_mocks.py @@ -0,0 +1,5 @@ +from scripts.helpful_scripts import deploy_mocks + + +def main(): + deploy_mocks() diff --git a/scripts/helpful_scripts.py b/scripts/helpful_scripts.py index a84f0fb..9df1cec 100644 --- a/scripts/helpful_scripts.py +++ b/scripts/helpful_scripts.py @@ -1,4 +1,14 @@ -from brownie import network, accounts, config, interface +from brownie import ( + network, + accounts, + config, + interface, + LinkToken, + MockV3Aggregator, + VRFCoordinatorMock, + MockOracle, + Contract, +) NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["hardhat", "development", "ganache"] LOCAL_BLOCKCHAIN_ENVIRONMENTS = NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS + [ @@ -7,38 +17,93 @@ "matic-fork", ] +contract_to_mock = { + "link_token": LinkToken, + "eth_usd_price_feed": MockV3Aggregator, + "vrf_coordinator": VRFCoordinatorMock, + "oracle": MockOracle, +} -def get_account(index=None): + +def get_account(index=None, id=None): if index: return accounts[index] if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: return accounts[0] - if network.show_active() in config["networks"]: - account = accounts.add(config["wallets"]["from_key"]) - return account - return None + if id: + return accounts.load(id) + return accounts.add(config["wallets"]["from_key"]) def fund_with_link( contract_address, account=None, link_token=None, amount=1000000000000000000 ): account = account if account else get_account() - link_token = ( - link_token - if link_token - else config["networks"][network.show_active()]["link_token"] - ) - tx = interface.LinkTokenInterface(link_token).transfer( - contract_address, amount, {"from": account} - ) - print("Funded {}".format(contract_address.address)) + link_token = link_token if link_token else get_contract("link_token") + tx = link_token.transfer(contract_address, amount, {"from": account}) + print("Funded {}".format(contract_address)) return tx -def get_verify_status(): - verify = ( - config["networks"][network.show_active()]["verify"] - if config["networks"][network.show_active()].get("verify") - else False +def get_contract(contract_name): + """If you want to use this function, go to the brownie config and add a new entry for + the contract that you want to be able to 'get'. Then add an entry in the in the variable 'contract_to_mock'. + You'll see examples like the 'link_token'. + This script will then either: + - Get a address from the config + - Or deploy a mock to use for a network that doesn't have it + + Args: + contract_name (string): This is the name that is refered to in the + brownie config and 'contract_to_mock' variable. + + Returns: + brownie.network.contract.ProjectContract: The most recently deployed + Contract of the type specificed by the dictonary. This could be either + a mock or the 'real' contract on a live network. + """ + contract_type = contract_to_mock[contract_name] + if network.show_active() in NON_FORKED_LOCAL_BLOCKCHAIN_ENVIRONMENTS: + if len(contract_type) <= 0: + deploy_mocks() + contract = contract_type[-1] + else: + try: + contract_address = config["networks"][network.show_active()][contract_name] + contract = Contract.from_abi( + contract_type._name, contract_address, contract_type.abi + ) + except KeyError: + print( + f"{network.show_active()} address not found, perhaps you should add it to the config or deploy mocks?" + ) + print( + f"brownie run scripts/deploy_mocks.py --network {network.show_active()}" + ) + return contract + + +def deploy_mocks(decimals=8, initial_value=200000000000): + """ + Use this script if you want to deploy mocks to a testnet + """ + print(f"The active network is {network.show_active()}") + print("Deploying Mocks...") + account = get_account() + print("Deploying Mock Link Token...") + link_token = LinkToken.deploy({"from": account}) + print("Deploying Mock Price Feed...") + mock_price_feed = MockV3Aggregator.deploy( + decimals, initial_value, {"from": account} ) - return verify + print(f"Deployed to {mock_price_feed.address}") + print("Deploying Mock VRFCoordinator...") + mock_vrf_coordinator = VRFCoordinatorMock.deploy( + link_token.address, {"from": account} + ) + print(f"Deployed to {mock_vrf_coordinator.address}") + + print("Deploying Mock Oracle...") + mock_oracle = MockOracle.deploy(link_token.address, {"from": account}) + print(f"Deployed to {mock_oracle.address}") + print("Mocks Deployed!") diff --git a/tests/conftest.py b/tests/conftest.py index 5127981..0473fee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,62 +15,6 @@ ) -@pytest.fixture -def get_eth_usd_price_feed_address(): - if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - if len(MockV3Aggregator) == 0: - mock_price_feed = MockV3Aggregator.deploy( - 8, 200000000000, {"from": accounts[0]} - ) - return mock_price_feed.address - else: - return MockV3Aggregator[len(MockV3Aggregator) - 1].address - if network.show_active() in config["networks"]: - return config["networks"][network.show_active()]["eth_usd_price_feed"] - else: - pytest.skip("Invalid network specified ") - return - - -@pytest.fixture(scope="module") -def get_link_token(): - if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - if len(LinkToken) == 0: - link_token = LinkToken.deploy({"from": get_account()}) - return link_token - else: - return LinkToken[len(LinkToken) - 1] - if network.show_active() in config["networks"]: - return Contract.from_abi( - "link_token", - config["networks"][network.show_active()]["link_token"], - LinkToken.abi, - ) - else: - pytest.skip("Invalid network/link token specified ") - - -@pytest.fixture -def get_vrf_coordinator(get_link_token): - if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - if len(VRFCoordinatorMock) == 0: - mock_vrf_coordinator = VRFCoordinatorMock.deploy( - get_link_token.address, {"from": get_account()} - ) - return mock_vrf_coordinator - else: - return VRFCoordinatorMock[len(VRFCoordinatorMock) - 1] - if network.show_active() in config["networks"]: - vrf_coordinator = Contract.from_abi( - "vrf_coordinator", - config["networks"][network.show_active()]["vrf_coordinator"], - VRFCoordinatorMock.abi, - ) - return vrf_coordinator - else: - pytest.skip("Invalid network specified") - - @pytest.fixture def get_keyhash(): if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: @@ -101,32 +45,6 @@ def get_data(): return 100 -@pytest.fixture -def get_oracle(get_link_token): - if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - if len(MockOracle) == 0: - mock_oracle = MockOracle.deploy( - get_link_token.address, {"from": get_account()} - ) - return mock_oracle - else: - return MockOracle[len(MockOracle) - 1] - if network.show_active() in config["networks"]: - mock_oracle = Contract.from_abi( - "mock_oracle", - config["networks"][network.show_active()]["oracle"], - MockOracle.abi, - ) - return mock_oracle - else: - pytest.skip("Invalid network specified") - - -@pytest.fixture -def dev_account(): - return accounts[0] - - @pytest.fixture def node_account(): return accounts[1] diff --git a/tests/test_lottery_integration.py b/tests/test_lottery_integration.py index 041ad79..9fe3bb5 100644 --- a/tests/test_lottery_integration.py +++ b/tests/test_lottery_integration.py @@ -2,46 +2,28 @@ import pytest from brownie import Lottery, network -from scripts.helpful_scripts import get_account, LOCAL_BLOCKCHAIN_ENVIRONMENTS +from scripts.helpful_scripts import ( + get_account, + get_contract, + LOCAL_BLOCKCHAIN_ENVIRONMENTS, +) +from scripts.deploy_lottery import deploy_lottery -STATIC_SEED = 123 STATIC_RNG = 777 -@pytest.fixture -def deploy_lottery_contract( - get_eth_usd_price_feed_address, - get_vrf_coordinator, - get_link_token, - get_keyhash, -): +def test_can_pick_winner(chainlink_fee): if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - # Arrange / Act - lottery = Lottery.deploy( - get_eth_usd_price_feed_address, - get_vrf_coordinator.address, - get_link_token.address, - get_keyhash, - {"from": get_account()}, - ) - # Assert - assert lottery is not None - return lottery - - -def test_can_pick_winner( - deploy_lottery_contract, get_link_token, chainlink_fee, get_vrf_coordinator -): - if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() account = get_account() lottery.startLottery({"from": account}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) - get_link_token.transfer(lottery.address, chainlink_fee * 2, {"from": account}) - transaction_receipt = lottery.endLottery(STATIC_SEED, {"from": account}) + get_contract("link_token").transfer( + lottery.address, chainlink_fee * 2, {"from": account} + ) + transaction_receipt = lottery.endLottery({"from": account}) transaction_receipt.events["RequestedRandomness"]["requestId"] time.sleep(60) assert lottery.recentWinner() == account diff --git a/tests/test_lottery_unit.py b/tests/test_lottery_unit.py index d8ce0c5..aac1a7b 100644 --- a/tests/test_lottery_unit.py +++ b/tests/test_lottery_unit.py @@ -1,58 +1,42 @@ +from scripts.deploy_lottery import deploy_lottery import pytest from brownie import Lottery, exceptions, network -from scripts.helpful_scripts import get_account, LOCAL_BLOCKCHAIN_ENVIRONMENTS +from scripts.helpful_scripts import ( + get_account, + get_contract, + LOCAL_BLOCKCHAIN_ENVIRONMENTS, +) -STATIC_SEED = 123 STATIC_RNG = 777 -@pytest.fixture -def deploy_lottery_contract( - get_eth_usd_price_feed_address, - get_vrf_coordinator, - get_link_token, - get_keyhash, -): +def test_get_entrance_fee(): if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - # Arrange / Act - lottery = Lottery.deploy( - get_eth_usd_price_feed_address, - get_vrf_coordinator.address, - get_link_token.address, - get_keyhash, - {"from": get_account()}, - ) - # Assert - assert lottery is not None - return lottery - - -def test_get_entrance_fee(deploy_lottery_contract): - if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: - pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() eth_usd_price = lottery.getLatestEthUsdPrice() - price = lottery.getEntranceFee() + entrance_fee = lottery.getEntranceFee() # 2000 is the inital of the eth / usd feed # usdEntryFee is 50 # so we do 2000/1 is 50/x = 0.025 ETH == $50 assert eth_usd_price == 2000000000000000000000 - assert price == 25000000000000000 + assert entrance_fee == 25000000000000000 -def test_cant_enter_lottery_unless_started(deploy_lottery_contract): +# 2500000 00000000 0000000000 +# 2000 00 000000000000000000 +def test_cant_enter_lottery_unless_started(): if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() with pytest.raises(exceptions.VirtualMachineError): lottery.enter({"from": get_account(), "value": lottery.getEntranceFee()}) -def test_can_start_and_enter_lottery(deploy_lottery_contract): +def test_can_start_and_enter_lottery(): if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() account = get_account() lottery.startLottery({"from": account}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) @@ -60,33 +44,35 @@ def test_can_start_and_enter_lottery(deploy_lottery_contract): assert lottery.players(1) == account -def test_can_end_lottery(deploy_lottery_contract, get_link_token, chainlink_fee): +def test_can_end_lottery(chainlink_fee): if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() account = get_account() lottery.startLottery({"from": account}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) - get_link_token.transfer(lottery.address, chainlink_fee * 2, {"from": account}) - transaction_receipt = lottery.endLottery(STATIC_SEED, {"from": account}) + get_contract("link_token").transfer( + lottery.address, chainlink_fee * 2, {"from": account} + ) + transaction_receipt = lottery.endLottery({"from": account}) assert transaction_receipt.events["RequestedRandomness"]["requestId"] is not None -def test_can_pick_winner( - deploy_lottery_contract, get_link_token, chainlink_fee, get_vrf_coordinator -): +def test_can_pick_winner(chainlink_fee): if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS: pytest.skip() - lottery = deploy_lottery_contract + lottery = deploy_lottery() account = get_account() lottery.startLottery({"from": account}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) lottery.enter({"from": account, "value": lottery.getEntranceFee()}) - get_link_token.transfer(lottery.address, chainlink_fee * 2, {"from": account}) - transaction_receipt = lottery.endLottery(STATIC_SEED, {"from": account}) + get_contract("link_token").transfer( + lottery.address, chainlink_fee * 2, {"from": account} + ) + transaction_receipt = lottery.endLottery({"from": account}) requestId = transaction_receipt.events["RequestedRandomness"]["requestId"] - get_vrf_coordinator.callBackWithRandomness( + get_contract("vrf_coordinator").callBackWithRandomness( requestId, STATIC_RNG, lottery.address, {"from": account} ) assert lottery.recentWinner() == account