2

What is the easiest way to connect with Chainlink so we can test our dApp locally. Do we have to run a local Chainlink node, or is there any workaround for getting the online data?

4

Using Mocks (best option for local testing)

A route every engineer should use if for testing locally is to use mocks. These are fake instances of chainlink nodes that respond with fake or "mock" data. You can see some instances of this in Aave or Hardhat-starter-kit repo. This way you can deploy to your testnet and just dummy the responses from your node.

Easiest option (not great for quickly iterating)

The easiest way to test is to deploy to a testnet. This will let you work with already built Chainlink nodes and aggregator contracts, so you don't have to build your own. And if you want to build your own, this is compatible too. This will also most closely resemble your mainnet environment, and every project will have to enter this stage at some point or another. However, there are some great solutions if you need to test and run locally. A full blog on testing Chainlink smart contracts is available if you're looking for more information.

Forking

Chainlink Price Feeds are the easiest to test locally, as you can just fork whatever network you're deploying on. In most packages (like truffle, hardhat, etc) have a forking feature. You can see this chainlink-hardhat repo for an example.

You won't be able to test any of the request model features this way, like the Chainlink VRF and Any API call.

Helper functions

The truffle box has some sophisticated pseudo-mocks that actually deploy a version of the chainlink nodes so you can make calls to these fake oracles. They are "pseudo-mocks" since they actually do deploy a chainlink oracle contract on-chain, they just don't make a request to an off-chain chainlink node.

Run your own Chainlink node, and connect it to your local chain

For those more ambitious and want to see the process end to end, you can run your own chainlink node, and just change the ETH_URL in your node to your local chain. You can learn how to run a chainlink node easily, just pay attention to the callouts that teach you how to run one locally.

1
  • 1
    Thank you @patrick-collins, very detailed answer. Also, the blog post helped a lot.
    – abullock
    Nov 19 '20 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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