• Stars
    star
    258
  • Rank 158,189 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An example smart contract utilizing Chainlink

Chainlink Truffle logo


Open in Gitpod

Chainlink Truffle Starter Kit

Implementation of the following 4 Chainlink features using the Truffle development environment:

Getting Started

It's recommended that you've gone through the Truffle getting started documentation before proceeding here.

Open in Gitpod

Requirements

  • git
    • You'll know you did it right if you can run git --version and you see a response like git version x.x.x
  • Nodejs
    • You'll know you've installed nodejs right if you can run:
      • node --versionand get an ouput like: vx.x.x
  • Yarn instead of npm
    • You'll know you've installed yarn right if you can run:
      • yarn --version And get an output like: x.x.x
      • You might need to install it with npm

If you're familiar with npx and npm instead of yarn, you can use npx for execution and npm for installing dependencies.

Quickstart

  1. 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

Then:

yarn

or

npm i
  1. You can now do stuff!

Run truffle test

This will run your JavaScript, TypeScript, and Solidity tests underneath the ./test folder. If the development network is not specified in your truffle-config, truffle test will automatically bring up and tear down a local network on localhost:8545.

Usage

If you run truffle --help you'll get an output of all the tasks you can run.

Deploying Contracts

In your truffle-config.js you'll see section like:

  networks: {

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 deploy a contract, you would call:

truffle migrate --network <NETWORK>

This will deploy your contracts to the network you specify. If you do not specify network, by default truffle migrate will deploy to the development network if it is defined in your truffle-config.

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 start a local network with all your contracts in it, run the following:

ganache -d

This will start ganache, a local blockchain with private keys, pre-funded wallets, and an endpoint to potentially add to an EVM wallet. -d specifies a deterministic wallet. You can see all other ways you can configure Ganache here.

In our truffle-config, we've defined the local ganache network. To migrate, simply call:

truffle migrate --network ganache

Using a Testnet or Live Network (like Mainnet or Polygon)

To interact with a live or test network, you will need ETH and LINK token by following these steps:

  1. Get some Sepolia Testnet ETH and LINK

Head over to the Chainlink faucets and get some ETH and LINK. Please follow the chainlink documentation if unfamiliar.

  1. Create VRF V2 subscription

Head over to VRF Subscription Page and create the new subscription. Save your subscription ID and place it in your helper-truffle-config.js under subId.

Truffle Dashboard

This is our recommended way of deploying your contracts. With Truffle dashboard, you can deploy contracts and sign transactions through MetaMask - so you never exposing your private key! To do so, run:

truffle dashboard

This should bring up the dashboard on localhost:24012, where you can connect your wallet to the network of your choice. Note that if you want to deploy to a testnet or mainnet, you'll need ETH to pay the gas fees. Then, if you want to migrate your contracts, simply call:

truffle migrate --network dashboard

You should then see prompts to sign the transactions on the dashboard view. Check out this video on how to use it with Truffle and HardHat here.

Environment configuration

In some cases, you might want your transactions to be automatically signed. In which case, you will need to provide your wallet's private key

  1. An RPC URL
  2. A private key
  3. ETH & LINK token (either testnet or real)

Let's look at an example of setting these up using the Sepolia testnet.

Sepolia 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 .env.example

IMPORTANT: MAKE SURE YOU'D DONT EXPOSE THE KEYS YOU PUT IN THIS .env FILE. 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.

  1. Set your SEPOLIA_RPC_URL environment variable.

You can get one for free from Infura, Alchemy, or Moralis. This is your connection to the blockchain.

  1. Set your PRIVATE_KEY environment variable.

This is your private key from your wallet, ie MetaMask. This is needed for deploying contracts to public networks.

WARNING WARNING WARNING

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 use Truffle Dashboard. Alternatively, 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!

.env example:

SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfasdfasdfasdf'
PRIVATE_KEY='abcdef'

bash example

export SEPOLIA_RPC_URL='https://sepolia.infura.io/v3/asdfasdfasdfasdf'
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.

Uncomment the sepolia network in your truffle-config. You should now be all setup! You can run any command and just pass the --network sepolia now!

To deploy all contracts:

truffle migrate --network sepolia

Test

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:

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 scripts to perform operations on each contract. Alternatively, if you want to interact with contracts on the fly, you can read about how to use truffle console or truffle develop here.

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 migrate --f 3 --to 3 --network <NETWORK>

After deployment, run the following:

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 migrate --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:

truffle exec scripts/requestAndReadRandomNumber.js --network <NETWORK>

Keepers

The Keepers Consumer contract has one script, to check the upkeep. After deployment, run the following:

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:

truffle run verify <CONTRACT> --network <NETWORK>

example:

truffle run verify PriceConsumerV3 --network sepolia

Contributing

Contributions are always welcome! Open a PR or an issue!

Thank You!

Resources

More Repositories

1

full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12,549
star
2

full-blockchain-solidity-course-py

Ultimate Solidity, Blockchain, and Smart Contract - Beginner to Expert Full Course | Python Edition
10,703
star
3

chainlink

node of the decentralized oracle network, bridging on and off-chain computation
Go
6,990
star
4

hardhat-starter-kit

A repo for boilerplate code for testing, deploying, and shipping chainlink solidity code.
JavaScript
1,203
star
5

chainlink-mix

Working with smart contracts with eth-brownie, python, and Chainlink.
Solidity
484
star
6

defi-minimal

This repo is dedicated to making minimal repos of existing defi primatives.
JavaScript
473
star
7

documentation

https://docs.chain.link The Chainlink developer documentation website for Smart Contract Devs and Node Operators
MDX
406
star
8

foundry-starter-kit

Solidity
331
star
9

external-adapters-js

Monorepo containing JavaScript implementation of external adapters
TypeScript
270
star
10

starter-kits

A repo of starter kits for working with various languages and frameworks across the smart contract ecosystem.
238
star
11

foundry-starter-kit-old

Solidity
204
star
12

smart-contract-examples

Example and sample projects
Solidity
188
star
13

solana-starter-kit

Example code for using Chainlink on Solana
JavaScript
172
star
14

chainlink-brownie-contracts

A repository for automatically using the latest chainlink repo from the core chainlink repo.
Solidity
168
star
15

chainlink-ruby

Easily connect your applications to blockchains
Ruby
157
star
16

Web3_tutorial_Chinese

Solidity
152
star
17

functions-hardhat-starter-kit

JavaScript
148
star
18

chainlink-polkadot

Go
139
star
19

LinkToken

LINK Token Contracts for the Chainlink Network
Solidity
132
star
20

ccip

Cross Chain Interoperability Protocol
Go
122
star
21

huff-starter-kit

A template repo to work with huff smart contracts and learn about low level EVM opcodes. Also Horse β™˜.
Solidity
101
star
22

apeworx-starter-kit

A repo dedicated to showing how to make smart contracts in a purely pythonic environment
Python
90
star
23

chainlink-solana

Go
81
star
24

blockchain-developer-hub

Dive into blockchain and smart contract development.
JavaScript
80
star
25

ccip-starter-kit-foundry

This project demonstrates a couple of basic Chainlink CCIP use cases
Solidity
75
star
26

chainlink-fullstack

Full stack starter project showcasing Chainlink products on Ethereum (EVM)
TypeScript
68
star
27

libocr

libocr consists of a Go library and a set of Solidity smart contracts that implement the Chainlink Offchain Reporting Protocol
Go
68
star
28

hardhat-chainlink

Integrates Chainlink into Hardhat projects. This package is currently in the BETA testing phase and is not recommended for production usage yet.
TypeScript
65
star
29

solidity-cborutils

A library for encoding CBOR in Solidity.
Solidity
64
star
30

dapptools-starter-kit

Solidity
63
star
31

ccip-cross-chain-nft

This project demonstrates how to mint an NFT on one blockchain from another blockchain using Chainlink CCIP
Solidity
62
star
32

ccip-read

TypeScript
60
star
33

chainlink-testing-framework

A testing framework for smart contracts and Chainlink nodes
Go
60
star
34

foundry-chainlink-toolkit

A plugin to use Chainlink products/services in Foundry
Solidity
59
star
35

chainlink-starknet

Go
58
star
36

ccip-starter-kit-hardhat

This project demonstrates a couple of basic Chainlink CCIP use cases
TypeScript
58
star
37

prediction-game

Rugby prediction game using Chainlink Functions, Automation, and CCIP.
TypeScript
56
star
38

chainlink-local

The Chainlink CCIP Local Simulator, visit documentation by clicking the link below:
Solidity
49
star
39

ccip-defi-lending

CCIP Launch Defi Demo
JavaScript
46
star
40

smart-contract-db

This repository is an archive. This project has been superseded by blockchain.education https://github.com/smartcontractkit/blockchain-developer-hub
TypeScript
44
star
41

external-initiator

Initiate Chainlink job runs from external sources
Go
39
star
42

chainlink-solana-demo

Showing how to deploy a Solana program using Chainlink Price Feeds
TypeScript
37
star
43

chainlink-automation-templates

Real-world sample projects using Chainlink Automation
Solidity
35
star
44

usechainlinkfunctions

TypeScript
31
star
45

functions-toolkit

An NPM package with collection of tools that can be used for working with Chainlink Functions.
TypeScript
26
star
46

near-protocol-contracts

Rust
26
star
47

chainlink-common

SDK for building Chainlink Services and Plugins
Go
25
star
48

chainlink-cosmos

Go
25
star
49

env-enc

TypeScript
25
star
50

ccip-cross-chain-name-service

This project is an educational example of how to create a minimal cross-chain name service using Chainlink CCIP
TypeScript
25
star
51

functions-deepdive-constellation2023

Deepdive Workshop For Constellation 2023 Hackathon
JavaScript
24
star
52

run-functions-dapp

Demo dApp using Chainlink Functions to bring web2 data on-chain.
TypeScript
24
star
53

wasp

Protocol-agnostic load testing library for Go
Go
24
star
54

devnet

An Ethereum development network with preset configs for easy testing.
Makefile
22
star
55

Blockchain-RPC-Exporter

Prometheus exporter for EVM compatible RPC endpoints.
Python
20
star
56

ccip-owner-contracts

A set of smart contracts used for administering Chainlink contracts, most notably CCIP
Solidity
18
star
57

chainlink-functions-demo-app

TypeScript
18
star
58

schemas

specification for offering and requesting off-blockchain services
17
star
59

paypal-adapter

TypeScript
17
star
60

chainlink-github-actions

Common action to run chainlink-testing-framework based tests in github ci
TypeScript
16
star
61

risk-management-network

Rust
16
star
62

wei_watchers

A service for push notifications of Ethereum logs and account balances.
Ruby
15
star
63

chain-selectors

Go
15
star
64

arbitrum-quickstart

JavaScript
14
star
65

ccip-tic-tac-toe

TypeScript
14
star
66

solc-api

minimal Solidity compiler service
JavaScript
14
star
67

chainlink-env

Chainlink k8s environment library
Go
14
star
68

datastreams-demo

Demo dApp using Chainlink Data Streams.
TypeScript
13
star
69

operator-ui

TypeScript
13
star
70

hello_chainlink

chainlink demos
JavaScript
12
star
71

chainlink-staking-v0.2-public-guide

12
star
72

wsrpc

A bi-directional Websockets RPC library
Go
12
star
73

quickstarts-historical-prices-api

TypeScript
12
star
74

functions-lens-eventbrite

Solidity
12
star
75

tdh2

An implementation of the TDH2 protocol by Shoup & Gennaro (paper link https://www.shoup.net/papers/thresh1.pdf)
Go
11
star
76

chainlink-RSK

Solidity
11
star
77

functions-the-graph-uniswap

Solidity
11
star
78

rwa-tokenization

TypeScript
11
star
79

chainlink-automation

Go
10
star
80

chainlink-data-streams

Go
10
star
81

workshop-distributed-news

Solidity
10
star
82

solana-prediction-game

JavaScript
10
star
83

builder

A docker build file for chainlink
Dockerfile
10
star
84

chainlink-vrf

OCR2VRF (in development, do not use in production)
Go
10
star
85

ccip-liquidation-protector

This project demonstrates how to automate preventing DeFi liquidations & optimizing interest rates on multiple blockchains using Chainlink CCIP, Chainlink Automation and Chainlink Functions
TypeScript
10
star
86

cairo-starter-kit

The boilerplate code for getting started with the usage of Chainlink services on Starknet
TypeScript
9
star
87

ea-framework-js

TypeScript
9
star
88

hardhat-network

Deploys a basic hardhat chain for testing
Dockerfile
9
star
89

bq-example-contract

JavaScript
8
star
90

.github

TypeScript
8
star
91

chainlink-node-compose

Docker Compose script for launching multiple chainlink nodes
Shell
8
star
92

substrate-adapter

Go
7
star
93

link-token-sidechain-evm

LINK Token On EVM Sidechains
Solidity
7
star
94

push-gha-metrics-action

Shell
7
star
95

DEVREL-collaborative-nft-tutorial

Solidity
7
star
96

blockchain-ecosystem-map

JavaScript
7
star
97

timelock-worker

Daemon to poll and execute scheduled transactions from a Timelock contract.
Go
7
star
98

chainlink-solhint-rules

CLL custom Solhint rules
JavaScript
7
star
99

functions-google-bigquery

Demonstration of accessing data from Google BigQuery via Chainlink Functions
JavaScript
6
star
100

chainlink-terra-feeds-demo

Showing how to deploy a Terra smart contract which utilizes Chainlink Data Feeds
Rust
6
star