• Stars
    star
    484
  • Rank 90,873 (Top 2 %)
  • Language
    Solidity
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Working with smart contracts with eth-brownie, python, and Chainlink.

NOTE: The new default is Sepolia.

chainlink-mix

NOTE: This has been recently updated for better compatibility with local blockchains. Check out the scripts to learn more.


Chainlink Brownie logo


Average time to resolve an issue Percentage of issues still open

This is a repo to work with and use Chainlink smart contracts in a python environment. If you're brand new to Chainlink, check out the beginner walk-through in remix to learn the basics.

You can also check out the more advanced Chainlink tutorials there as well.

Prerequisites

Please install or have installed the following:

Installation

  1. Install Brownie, if you haven't already. Here is a simple way to install brownie.
python3 -m pip install --user pipx
python3 -m pipx ensurepath
# restart your terminal
pipx install eth-brownie

Or, if that doesn't work, via pip

pip install eth-brownie
  1. Download the mix and install dependencies.
brownie bake chainlink-mix
cd chainlink-mix
pip install -r requirements.txt

This will open up a new Chainlink project. Or, you can clone from source:

git clone https://github.com/PatrickAlphaC/chainlink-mix
cd chainlink-mix

Testnet Development

If you want to be able to deploy to testnets, do the following.

Set your WEB3_INFURA_PROJECT_ID, and PRIVATE_KEY environment variables.

You can get a WEB3_INFURA_PROJECT_ID by getting a free trial of Infura. At the moment, it does need to be infura with brownie. If you get lost, you can follow this guide to getting a project key. You can find your PRIVATE_KEY from your ethereum wallet like metamask.

You'll also need testnet ETH and LINK. You can get LINK and ETH into your wallet by using the faucets located here. If you're new to this, watch this video.. Look at the sepolia section for those specific testnet faucets.

You can add your environment variables to a .env file. You can use the .env.exmple as a template, just fill in the values and rename it to '.env'. Then, uncomment the line # dotenv: .env in brownie-config.yaml

Here is what your .env should look like:

export WEB3_INFURA_PROJECT_ID=<PROJECT_ID>
export PRIVATE_KEY=<PRIVATE_KEY>

AND THEN RUN source .env TO ACTIVATE THE ENV VARIABLES (You'll need to do this every time you open a new terminal, or learn how to set them easier)

WARNING WARNING WARNING

DO NOT SEND YOUR PRIVATE KEY WITH FUNDS IN IT ONTO GITHUB

Otherwise, you can build, test, and deploy on your local environment.

Local Development

For local testing install ganache-cli

npm install -g ganache-cli

or

yarn add global ganache-cli

All the scripts are designed to work locally or on a testnet. You can add a ganache-cli or ganache UI chain like so:

brownie networks add Ethereum ganache host=http://localhost:8545 chainid=1337

And update the brownie config accordingly. There is a deploy_mocks script that will launch and deploy mock Oracles, VRFCoordinators, Link Tokens, and Price Feeds on a Local Blockchain.

Running Scripts and Deployment

This mix provides a simple template for working with Chainlink Smart Contracts. The easiest way to start is to fork the mainnet chain to a local ganache chain. This will allow you to deploy local smart contracts to interact with the Chainlink Price Feeds.

NOTE: It's highly encouraged that you work with a local chain before testing on a testnet. You'll be a much faster developer!

If Sepolia network is not added to brownie by default, run:

brownie networks add Ethereum sepolia host=https://sepolia.infura.io/v3/$WEB3_INFURA_PROJECT_ID chainid=11155111 explorer=https://sepolia.etherscan.io/

Chainlink Price Feeds

This will deploy a smart contract to Sepolia and then read you the latest price via Chainlink Price Feeds.

brownie run scripts/price_feed_scripts/01_deploy_price_consumer_v3.py --network sepolia
brownie run scripts/price_feed_scripts/02_read_price_feed.py --network sepolia

Or, you can use ENS

brownie run scripts/price_feed_scripts/02_read_price_feed_with_ens.py --network sepolia

Otherwise, you can fork mainnet and use that in a local ganache development environment.

brownie console --network mainnet-fork
>>> price_feeds = PriceFeedConsumer.deploy('0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419', {'from': accounts[0]})
.
.
>>> latest_price = price_feeds.getLatestPrice()
>>> latest_price
59169208540

You can also use ENS to get prices. See the ens price feed script for more information.

Chainlink VRF

This will deploy a smart contract to Sepolia and get a Random number via Chainlink VRF.

If you haven't created and funded a subscription on vrf.chain.link you can do so on the UI, or by running:

brownie run scripts/vrf_scripts/create_subscription.py --network sepolia

Before running the next scripts. Running 01_deploy_vrf will also add your consumer contract to the registry.

brownie run scripts/vrf_scripts/01_deploy_vrf_consumer.py --network sepolia
brownie run scripts/vrf_scripts/02_request_randomness.py --network sepolia
brownie run scripts/vrf_scripts/03_read_random_number.py --network sepolia

Chainlink API Call

This will deploy a smart contract to Sepolia and then make an API call via Chainlink API Call.

brownie run scripts/chainlink_api_scripts/01_deploy_api_consumer.py --network sepolia
brownie run scripts/chainlink_api_scripts/02_request_api.py --network sepolia
brownie run scripts/chainlink_api_scripts/03_read_data.py --network sepolia

Chainlink Automation Deployment

This is just to show you how to deploy the Automation Compatible contracts, you can learn more about registering & using them in the Automate Contracts section of the Chainlink documentation.

brownie run scripts/automation_scripts/01_deploy_automation_counter.py --network sepolia
brownie run scripts/automation_scripts/02_check_upkeep.py --network sepolia

Local Development

For local development, you might want to deploy mocks. You can run the script to deploy mocks. Depending on your setup, it might make sense to not deploy mocks if you're looking to fork a mainnet. It all depends on what you're looking to do though. Right now, the scripts automatically deploy a mock so they can run.

Testing

brownie test

For more information on effective testing with Chainlink, check out Testing Smart Contracts

Tests are really robust here! They work for local development and testnets. There are a few key differences between the testnets and the local networks. We utilize mocks so we can work with fake oracles on our testnets.

There is a test_unnecessary folder, which is a good exercise for learning some of the nitty-gritty of smart contract development. It's overkill, so pytest will skip them intentionally. It also has a test_samples folder, which shows an example Chainlink API call transaction receipt.

To test development / local

brownie test

To test mainnet-fork

This will test the same way as local testing, but you will need a connection to a mainnet blockchain (like with the infura environment variable.)

brownie test --network mainnet-fork

To test a testnet

Sepolia is currently supported. Please check the Chainlink docs for which products are supported on which chains.

brownie test --network sepolia

Adding additional Chains

If the blockchain is EVM Compatible, adding new chains can be accomplished by something like:

brownie networks add Ethereum binance-smart-chain host=https://bsc-dataseed1.binance.org chainid=56

or, for a fork:

brownie networks add development binance-fork cmd=ganache-cli host=http://127.0.0.1 fork=https://bsc-dataseed1.binance.org accounts=10 mnemonic=brownie port=8545

Linting

pip install black
pip install autoflake
autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r .
black .

If you're using vscode and the solidity extension, you can create a folder called .vscode at the root folder of this project, and create a file called settings.json, and add the following content:

{
  "solidity.remappings": [
    "@chainlink/=[YOUR_HOME_DIR]/.brownie/packages/smartcontractkit/[email protected]",
    "@openzeppelin/=[YOUR_HOME_DIR]/.brownie/packages/OpenZeppelin/[email protected]"
  ]
}

This will quiet the linting errors it gives you.

Resources

To get started with Brownie:

Any questions? Join our Discord

License

This project is licensed under the MIT license.

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

defi-minimal

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

documentation

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

foundry-starter-kit

Solidity
331
star
8

external-adapters-js

Monorepo containing JavaScript implementation of external adapters
TypeScript
270
star
9

truffle-starter-kit

An example smart contract utilizing Chainlink
JavaScript
258
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