2

On the hardhat starter kit, the unit test checks if you can make an api request. But it doesn't actually fulfill the request. In the tasks, it requests volume data but it isn't shown how to fulfill the request.

From looking at the code for the mockOracle I found this function.

const r = await mockOracle.fulfillOracleRequest(requestId, data)

I dont have any idea how to make the data be what I want it to be. For example when I pass the following data it returns the huge seamingly random number 24516769870864860957297836120308737325622166553046088662895407649136392011776.

const data = ethers.utils.formatBytes32String("64")

How do I mock the api response with the data I pass to fulfillOracleRequest? Also, because I couldn't find any examples of this in the chainlink starter kit, is this even the right way to be doing this on the local hardhat network?

1 Answers1

1

The fulfillOracleRequest function in the MockOracle contract takes a bytes32 param for the response, so you need to encode it properly. This is how you can mock a response as part of the APIConsumer Unit Test:

let mockResponse = '777'
const responseBytes = ethers.utils.formatBytes32String(mockResponse)
const r = await mockOracle.fulfillOracleRequest(requestId, responseBytes)

To answer your second question, yes this is the correct thing to do when working with local chains that aren't integrated to Chainlink oracles. There is actually an open issue to implement these for the unit tests. Feel free to take it on if you want!

  • Thank you for the response! I tried the code but when I `console.log(responseBytes)` I am getting 0x0000000000000000000000000000000000000000000000000000000000000000. I requested the volume after but I got `BigNumber { value: "0" }` from the contract. I also tried the 777 as a string. The response bytes are 0x3737370...0 and the BigNumber value is 24974762856691535979668219084573578457621369693330343344309437594103489495040. – Holland Pleskac Dec 21 '21 at 18:43
  • I think the string is meant to be in quotes '777' actually, I'll update my answer. As for your weird result, hmmm that's unexpected. It's a bytes field we're sending, but the consuming contract ends up taking a uint. Maybe check out how the official tests interact with the MockOracle contract and try mimic it. https://github.com/smartcontractkit/chainlink/blob/develop/contracts/test/v0.6/ChainlinkClient.test.ts – Harry Papacharissiou Dec 23 '21 at 11:37