A truly random selected lottery smart contract. Players are able to enter the smart contract using their Etheruem rinkeby wallets. The smart contract will randomly select a winner and pay out the prize to the winner.
Please install or have installed the following:
- 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
- Download the mix and install dependancies.
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
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 rinkeby ETH and LINK. You can get LINK and ETH into your wallet by using the rinkeby faucets located here. If you're new to this, watch this video.
You can add your environment variables to the .env
file:
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 everytime you open a new terminal, or learn how to set them easier)
DO NOT SEND YOUR PRIVATE KEY WITH FUNDS IN IT ONTO GITHUB
Add your account by doing the following:
brownie accounts new <some_name_you_decide>
You'll be prompted to add your private key, and a password.
Then, in your code, you'll want to use load
instead of add when getting an account.
account = accounts.load("some_name_you_decide")
Then you'll want to add your RPC_URL to the network of choice. For example:
brownie networks modify rinkeby host=https://your_url_here
If the network you want doesn't already exist, see the below section
Otherwise, you can build, test, and deploy on your local environment.
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.
brownie run scripts/deploy_lottery.py
You will need LINK and ETH in your wallet to run this script. Visit the rinkeby faucet to get LINK and ETH.
There are 2 types of tests in this project.
- unit tests, which run on a local blockchain.
- integration tests, which run on a testnet
To run the unit tests:
brownie test
integration tests:
brownie test --network <network>
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.
brownie test
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
Rinkeby is currently supported
brownie test --network rinkeby
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
pip install black
pip install autoflake
autoflake --in-place --remove-unused-variables --remove-all-unused-imports -r .
black .
To get started with Brownie:
- Chainlink Documentation
- Check out the Chainlink documentation to get started from any level of smart contract engineering.
- Check out the other Brownie mixes that can be used as a starting point for your own contracts. They also provide example code to help you get started.
- "Getting Started with Brownie" is a good tutorial to help you familiarize yourself with Brownie.
- For more in-depth information, read the Brownie documentation.
Any questions? Join our Discord