A repo dedicated to showing how to make smart contracts in a purely pythonic environment
Note: This repo has been recently updated to use Sepolia.
Apeworx (Vyper) Starter Kit
This is a repo to work with and use Chainlink smart contracts in a python, apeworx & vyper environment. If you're brand new to Chainlink, check out the beginner walk-through in remix to learn the basics.
It shows how to use these frameworks and languages as well as the following Chainlink features:
Table of Contents
- Requirements - Quickstart - Deploying Contracts - Price Feed Consumer - Keepers Consumer - VRFv2 Consumer - Deploying to Local, Adhoc, Mainnet, and Testnets - Importing an account - Deploy to a local or adhoc network - Deploy to a mainnet or test network - Interacting with Contracts - Price Feed Consumer - VRF Consumer - Keeper Consumer - Contributing - ResourcesGetting Started
It's recommended that you've gone through the apeworx getting started documentation before proceeding here.
Requirements
- You'll know you did it right if you can rungit --version and you see a response like git version x.x.x
- You'll know you've installed python right if you can run:
- python --version or python3 --version and get an output like: Python x.x.x
- pipx is different from pip
- You may have to close and re-open your terminal
- You'll know you've installed it right if you can run:
- pipx --version and see something like x.x.x.x
- We recommend using pipx but you can follow the ape documentation for other installation methods.
- You'll know you've done it right if you run ape --version and see an output like x.x.x
Quickstart
- Clone repo and install dependencies
git clone https://github.com/smartcontractkit/apeworx-starter-kit
cd apeworx-starter-kit
ape plugins install alchemy vyper
- You're ready to go!
ape test
Usage
If you run ape --help you'll get an output of all the tasks you can run.
Deploying Contracts
The following will deploy your contracts to a temporary ape test network. 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.
After your script completes, the network deletes itself.
Price Feed Consumer
ape run scripts/deploypricefeed_consumer.py
Keepers Consumer
ape run scripts/deploykeepersconsumer.py
VRFv2 Consumer
ape run scripts/deployvrfconsumer.py
Deploying to Local, Adhoc, Mainnet, and Testnets
In order to deploy to a local, adhoc, mainnet, or testnet, you'll need to first create accounts. For the scripts we currently have, it'll default to the "default" account. If you'd like to have the scripts point to a different account, go to helperfunctions.py and change the getaccount function to look for your account instead of default.
Ape doesn't support .env files or keeping your private keys in plaintext, which means it's harder for you to release your private key to the world!
Importing an account
To import an account into ape, run the following:
<pre><code class="lang-">ape accounts import default</code></pre>
Where default will be the name of your account. Ape will then prompt you for your private key and password, and encrypt it on your computer. The only way to use this key moving forward will be to decrypt the key with your password.
Deploy to a local or adhoc network
Ape doesn't come with a built-in local network like hardhat or ganache, so we will have to use our own. Ape also prefers users to build plugins for working additional networks, you can find a list of the plugins on their github.
We recommend using Foundry's Anvil as your local network.
- Install Foundry / Anvil
and get an output like anvil 0.1.0 (f016135 2022-07-04T00:15:02.655418Z)
- Start up anvil
Run:
<pre><code class="lang-">anvil</code></pre>
You'll see an output with many private keys.
If you'd like to use this as your main "default" account, run the following:
<pre><code class="lang-">ape accounts delete default</code></pre>
And then, re-import your private key from anvil by following the importing an account guide
- Run your script
> Note: This will only work since the chain Id is 31337 for anvil! For working with non-local networks, please see Deploy to a mainnet or testnet
<pre><code class="lang-">ape run scripts/deploypricefeed_consumer.py --network http://127.0.0.1:8545</code></pre>
You'll be prompted for your password.
Deploy to a mainnet or test network
- Import an account
Please see import an account. And be sure your account has plenty of testnet or mainnet tokens if working on live network. See this faucet for testnet tokens.
- Set your RPC_URL
Since we are working with Alchemy, create an environment variable called WEB3ALCHEMYPROJECTID or WEB3ALCHEMYAPI_KEY. If using a linux or mac environment, you can set it by running:
<pre><code class="lang-">export WEB3ALCHEMYPROJECTID=MYAPI_TOKEN</code></pre>
> Note: At this time, it's really tricky to change networks with Ape. If you want to use another network ape doesn't have a plugin for, you can use an adhoc network as shown above.
- Update your
helper_config.py
If you're using a network not covered in helper_config.py be sure to add it.
- Run your script!
<pre><code class="lang-">ape run scripts/deploypricefeed_consumer.py --network ethereum:sepolia:alchemy</code></pre>
Interacting with Contracts
To interact with contracts, we recommend using the console.
<pre><code class="lang-">ape console --network ethereum:sepolia:alchemy</code></pre>
Or, you can follow along and run the scripts to see the end-to-end functionality.
Price Feed Consumer
- Deploy the contract
<pre><code class="lang-">ape run scripts/deploypricefeed_consumer.py --network ethereum:sepolia:alchemy</code></pre>
- Read it
<pre><code class="lang-">ape run scripts/readpricefeed.py --network ethereum:sepolia:alchemy</code></pre>
VRF Consumer
- Create a subscription and fund it with LINK.
You can do that with the script, or by going to the UI at vrf.chain.link
<pre><code class="lang-">ape run scripts/create_subscription.py --network ethereum:sepolia:alchemy</code></pre>
- Update your
helper_config.py` with your subscription Id.
- Deploy vrf consumer
ape run scripts/deployvrfconsumer.py --network ethereum:sepolia:alchemy
- Request a random number and wait for a response
ape run scripts/requestandread_randomness.py --network ethereum:sepolia:alchemy
Keeper Consumer
- Deploy the contract
ape run scripts/deploykeepersconsumer.py --network ethereum:sepolia:alchemy
- Register your upkeep on the Keepers UI
- Watch for your counter to automatically start going up!
ape run scripts/read_counter.py --network ethereum:sepolia:alchemy
And it should be updated!
Miscellaneous
- Testing and forking are a bit tricky at the moment.
Contributing
Contributions are always welcome! Open a PR or an issue!
Thank You!