2

Github repo located here: https://github.com/supsam89/digiwear_nft_drop

The command I'm having problems with is brownie run scripts/deploy.py

I am trying to deploy a drop/lottery contract that picks multiple winners randomly based on entrants (then will eventually transfer an NFT to them).

When I get to end the drop/lottery function it either takes too long (gas intensive) or doesn't work. After performing time.sleep(60) the state remains as "Calculating winners".

The function I believe is causing the problem in NFTDrop.sol:

    function endDrop() public onlyOwner {
        drop_state = DROP_STATE.CALCULATING_WINNERS;
        bytes32 requestId = requestRandomness(keyhash, fee);
        emit RequestedRandomness(requestId);
    }

    function fulfillRandomness(bytes32 _requestId, uint256 _randomness) internal override {
        require(
            drop_state == DROP_STATE.CALCULATING_WINNERS,
            "Not calculating winners yet!"
        );
        require(_randomness > 0, "randomness-not-found");
        uint256[] memory expandedValues = expand(_randomness, tokenAmount);
        for (uint256 i = 0; i < tokenAmount; i++) {
            uint256 indexOfWinner = expandedValues[i] % entrants.length;
            recentWinner = entrants[indexOfWinner];
            winners.push(recentWinner);
        }
        drop_state = DROP_STATE.CLOSED;
        randomness = _randomness;
    }

    function expand(uint256 _randomness, uint256 _numberOfTokens) public pure returns (uint256[] memory expandedValues) {
        expandedValues = new uint256[](_numberOfTokens);
        for (uint256 i = 0; i < _numberOfTokens; i++) {
            expandedValues[i] = uint256(keccak256(abi.encode(_randomness, i)));
        }
        return expandedValues;
    }

Any help to the problem or advise on how to code this better would be greatly appreciated!

Thanks in advance.

1 Answer 1

1

Per the Chainlink docs.

Maximum Gas for Callback If your fulfillRandomness function uses more than 200k gas, the transaction will fail.

Your fulfillRandomness function takes too much gas, you'll have to shorten it.

1
  • 2
    Got it, moved logic into endDrop instead and working. Many thanks, also for the amazing tutorial that has catapulted my progress :D
    – supsam89
    Oct 13, 2021 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.