smartcontractkit / full-blockchain-solidity-course-py

Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition
MIT License
10.67k stars 2.89k forks source link

Lesson 7: endLottery TEST what about the other accounts? #1803

Open emilrueh opened 1 year ago

emilrueh commented 1 year ago

(Around the 08:10:00 mark in the tutorial video.)

Hi, wondering how we could test whether the other accounts pass as recentWinner when setting our STATIC_RNG to 778.

Right now we use 777 as the random value which with 3 players equals to index 0 therefore our standard account. I also want to test if another index can be the winner or only our standard account.

Right now it is unclear if the test is indeed doing what we want and not only passing the standard account if selected as winner, right?


I tried below but but that throws a TypeError: 'Account' object is not subscriptable so I might just not know how to get the other added players index:

STATIC_RNG = 778
assert lottery.recentWinner() == account[1]

I also tried to use lottery.players to get a players address but that does not work.


The full code of that test:


def test_can_pick_winner_correctly():

    # ARRANGE
    if network.show_active() not in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        pytest.skip()
    lottery = deploy_lottery()
    account = get_account()

    # ACT
    lottery.startLottery({"from": account})

    lottery.enterLottery({"from": account, "value": lottery.getEntranceFee()})
    lottery.enterLottery(
        {"from": get_account(index=1), "value": lottery.getEntranceFee()}
    )
    lottery.enterLottery(
        {"from": get_account(index=2), "value": lottery.getEntranceFee()}
    )

    fund_with_link(lottery)

    starting_balance_of_account = account.balance()
    balance_of_lottery = lottery.balance()

    transaction = lottery.end({"from": account})
    request_id = transaction.events["RequestedRandomness"]["requestId"]
    STATIC_RNG = 777
    get_contract("vrf_coordinator").callBackWithRandomness(
        request_id, STATIC_RNG, lottery.address, {"from": account}
    )
    print(lottery.players)  # just a test to see how to use it to get a player

    # ASSERT
    assert lottery.recentWinner() == account
    assert lottery.balance() == 0
    assert account.balance() == starting_balance_of_account + balance_of_lottery