- Chainlink Truffle Starter Kit
- Getting Started
- It's recommended that you've gone through the Truffle getting started documentation before proceeding here.
- Deploying Contracts
- Run a Local Network
- Using a Testnet or Live Network (like Mainnet or Polygon)
- Interacting with Deployed Contracts
- Chainlink Price Feeds
- Request & Receive Data
- VRF Get a random number
- Verify on Etherscan
- Thank You!
Chainlink Truffle Starter Kit¶
It's recommended that you've gone through the Truffle getting started documentation before proceeding here.¶
- You'll know you did it right if you can run
git --versionand you see a response like
git version x.x.x
- You'll know you've installed nodejs right if you can run:
node --versionand get an ouput like:
- Yarn instead of
- You'll know you've installed yarn right if you can run:
yarn --versionAnd get an output like:
- You might need to install it with npm
If you're familiar with
yarn, you can use
npxfor execution and
npmfor installing dependencies.
- Clone and install dependencies
After installing all the requirements, run the following:
git clone https://github.com/smartcontractkit/truffle-starter-kit/ cd truffle-starter-kit
- You can now do stuff!
yarn truffle test
If you run
yarn Truffle --help you'll get an output of all the tasks you can run.
yarn truffle deploy --network <NETWORK>
This will deploy your contracts to the network you specify. Additionally, if on a local network, it will deploy mock Chainlink contracts for you to interact with. If you'd like to interact with your deployed contracts, skip down to Interacting with Deployed Contracts.
Run a Local Network¶
One of the best ways to test and interact with smart contracts is with a local network. To run a local network with all your contracts in it, run the following:
You'll get a local blockchain, private keys, contracts deployed (from the
deploy folder scripts), and an endpoint to potentially add to an EVM wallet.
In a new terminal, you can then deploy using:
yarn truffle deploy --network ganache
Using a Testnet or Live Network (like Mainnet or Polygon)¶
truffle-config.js you'll see section like:
This section of the file is where you define which networks you want to interact with. You can read more about that whole file in the Truffle documentation.
To interact with a live or test network, you'll need:
- An rpc URL
- A Private Key
- ETH & LINK token (either testnet or real)
Let's look at an example of setting these up using the Goerli testnet.
Goerli Ethereum Testnet Setup¶
First, we will need to set environment variables. We can do so by setting them in our
.env file (create it if it's not there). You can also read more about environment variables from the linked twilio blog. You'll find a sample of what this file will look like in
IMPORTANT: MAKE SURE YOU'D DONT EXPOSE THE KEYS YOU PUT IN THIS
.envFILE. By that, I mean don't push them to a public repo, and please try to keep them keys you use in development not associated with any real funds.
- Set your
- Set your
This is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks.
When developing, it's best practice to use a Metamask that isn't associated with any real money. A good way to do this is to make a new browser profile (on Chrome, Brave, Firefox, etc) and install Metamask on that brower, and never send this wallet money.
Don't commit and push any changes to .env files that may contain sensitive information, such as a private key! If this information reaches a public GitHub repository, someone can use it to check if you have any Mainnet funds in that wallet address, and steal them!
export GOERLI_RPC_URL='www.infura.io/asdfadsfafdadf' export PRIVATE_KEY='abcdef'
For other networks like mainnet and polygon, you can use different environment variables for your RPC URL and your private key. See the
truffle-config.js to learn more.
- Get some Goerli Testnet ETH and LINK
- Create VRF V2 subscription
Head over to VRF Subscription Page and create the new subscription. Save your subscription ID and place it in your
- Running commands
You should now be all setup! You can run any command and just pass the
--network goerli now!
To deploy contracts:
yarn truffle deploy --network goerli
Tests are located in the test directory, and are split between unit tests and staging/testnet tests. Unit tests should only be run on local environments, and staging tests should only run on live environments.
To run unit tests:
yarn truffle test
Interacting with Deployed Contracts¶
After deploying your contracts, the deployment output will give you the contract addresses as they are deployed. You can then use these contract addresses in conjunction with Truffle tasks to perform operations on each contract.
Chainlink Price Feeds¶
The Price Feeds consumer contract has one script, to read the latest price of a specified price feed contract.
You can deploy just the price feed consumer with:
truffle deploy --f 3 --to 3 --network <NETWORK>
After deployment, run the following:
yarn truffle exec scripts/readPriceConsumer.js --network <NETWORK>
Request & Receive Data¶
The API Consumer contract has one script, to request data from the API and wait for a response.
You can deploy just the API Consumer with:
truffle deploy --f 4 --to 4 --network <NETWORK>
After deployment, run the following:
yarn truffle exec scripts/requestAndReadAPI.js --network <NETWORK>
VRF Get a random number¶
The VRF Consumer contract has one script, to request a random number and wait for a response.
You can deploy just the VRF Consumer with:
truffle deploy --f 5 --to 5 --network <NETWORK>
After deployment, you'll need to add your contract address to your subscription. Head over to vrf.chain.link and add your consumer.
Then, run the following:
yarn truffle exec scripts/requestAndReadRandomNumber.js --network <NETWORK>
The Keepers Consumer contract has one script, to check the upkeep. After deployment, run the following:
yarn truffle exec scripts/checkUpkeep.js --network <NETWORK>
To see everything in action, you'll want to set up a consumer at keepers.chain.link.
Verify on Etherscan¶
You'll need an
ETHERSCAN_API_KEY environment variable. You can get one from the Etherscan API site.. If you have it set, your deploy script will try to verify them by default, but if you want to verify any manually, you can run:
yarn truffle run verify <CONTRACT> --network <NETWORK>
yarn truffle run verify PriceConsumerV3 --network goerli
Contributions are always welcome! Open a PR or an issue!