smartcontractkit / hardhat-starter-kit

A repo for boilerplate code for testing, deploying, and shipping chainlink solidity code.
MIT License
1.21k stars 491 forks source link

Wrong requestId in RandomNumberConsumer unit test #84

Closed cuckflong closed 2 years ago

cuckflong commented 2 years ago

The requestId from

const transaction = await randomNumberConsumer.getRandomNumber()
const transactionReceipt = await transaction.wait(1)
const requestId = transactionReceipt.events[0].topics[1]

is different to the requestId from the RandomNumberConsumer contract if you log and see them

function getRandomNumber() public returns (bytes32 requestId) {
    require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
    requestId = requestRandomness(keyHash, fee);
    console.logBytes32(requestId);
  }

image

rgottleber commented 2 years ago

Hi, It looks like you have code from RandomNumberConsumer_unit_test.js as well as RandomNumberConsumer.sol

If those are the files you are referencing, the difference in the values is because the first request id transactionReceipt.events[0].topics[1] is the id of the request to the RandomNumberConsumer contract while requestId = requestRandomness(keyHash, fee); is the id of the request to the VRF Coordinator contract.

Closing this for now, but feel free to follow up if more information would help.

cuckflong commented 2 years ago

Thanks for the response but I thought vrfCoordinatorMock.callBackWithRandomness is suppose to be called with the id of the request to the VRF Coordinator contract instead of id of the request to the RandomNumberConsumer contract

const transaction = await randomNumberConsumer.getRandomNumber()
const transactionReceipt = await transaction.wait(1)
const requestId = transactionReceipt.events[0].topics[1]
const randomValue = 777
await vrfCoordinatorMock.callBackWithRandomness(
    requestId,
    randomValue,
    randomNumberConsumer.address
 )

Such that when you try to do something like https://docs.chain.link/docs/chainlink-vrf-best-practices/v1/#having-multiple-vrf-requests-in-flight will work properly