2/5
## Introduction to the Remix Buy Me A Coffee Project Welcome back! In this section, we'll dive into building a classic and highly instructive smart contract: the "Buy Me A Coffee" contract. We'll be using the Remix IDE and the Vyper programming language. This project serves as an excellent vehicle for understanding fundamental smart contract development patterns. All the code for this section can be found in the dedicated GitHub repository: `github.com/Cyfrin/remix-buy-me-a-coffee-cu`. You can also find links and other resources within the Cyfrin Updraft course materials under "Course Resources". For convenience, the `README.md` file in the project repository contains a special link that will automatically open Remix with the `buy_me_a_coffee.vy` contract loaded, saving you the hassle of copy-pasting. ## Project Goals and Core Concepts The "Buy Me A Coffee" contract is a popular introductory project because it touches upon several essential concepts in a clear way. Our version will have the following core functionality: 1. **Receive Funds:** Users (funders) can send Ether (ETH) to the smart contract. 2. **Minimum Donation:** We'll enforce a minimum donation amount, but specified in *USD* (e.g., the equivalent of $50 in ETH). 3. **Price Feeds (Oracles):** To enforce the USD minimum, the contract will interact with an external price feed oracle. This oracle provides the current ETH-to-USD conversion rate, allowing the contract to access real-world data – a crucial concept in Web3 development. We'll use an interface (`AggregatorV3Interface`) to define how our contract communicates with the oracle. 4. **Track Funders:** The contract will keep a record of addresses that have sent funds. 5. **Owner Withdrawal:** Only the original deployer (the owner) of the contract can withdraw the accumulated ETH. Throughout this project, we will explore key Vyper and EVM concepts, including: * **`payable` functions:** Functions designed to receive ETH. * **State Variables:** Storing persistent data on the blockchain, such as the owner's address (`OWNER`), the minimum USD value (`MINIMUM_USD`), the price feed contract address (`price_feed`), a list of funders (`funders` using `DynArray`), and a mapping of addresses to the amounts they've funded (`address_to_amount_funded` using `HashMap`). * **Interfaces:** Defining blueprints for interacting with other deployed smart contracts (like the price feed oracle). * **Access Control:** Restricting certain actions (like withdrawing funds) to specific addresses (in this case, the `OWNER`). * **Reading Contract State:** Calling view functions to read stored data without creating a transaction. ## Contract Walkthrough and Demonstration Let's walk through the high-level functionality of the completed contract, as you might interact with it in Remix. 1. **Compilation:** The Vyper code is compiled using the Vyper compiler available within Remix. 2. **Deployment:** Using the "Deploy & Run Transactions" tab in Remix, the contract is deployed to a blockchain. * *Demonstration Note:* The video demonstration uses the **Sepolia Testnet** via MetaMask ("Injected Provider"). * *Course Recommendation:* Due to the difficulty of obtaining testnet ETH, **you will likely use the Tenderly Virtual Networks** provided through the course platform for deployment and testing, rather than Sepolia. 3. **Interaction (Initial State):** Once deployed, we can interact with the contract's public functions and state variables: * Reading `OWNER` shows the address that deployed the contract. * Reading `price_feed` shows the address of the configured Chainlink ETH/USD price feed oracle. * Reading `MINIMUM_USD` displays the required minimum donation value (e.g., `50000000000000000000`, representing $50 scaled up by 10^18). * Reading `funders` (e.g., via `get_funder(0)`) initially shows no funders. 4. **Funding:** A user sends funds by calling the `fund` function. * This function is marked `@payable`, allowing it to receive ETH. * In Remix, the `Value` field is used to specify the amount of ETH to send, entered in Wei (e.g., `20000000000000000` Wei equals 0.02 ETH). * When `fund` is called, the contract internally uses the `price_feed` to get the current ETH/USD rate, calculates the USD value of the sent `msg.value`, and uses an `assert` statement to ensure this value meets or exceeds `MINIMUM_USD`. If the check passes, the sender's address is added to the `funders` list, and their contribution is tracked in `address_to_amount_funded`. * The contract's ETH balance increases (visible in Remix and on a block explorer like Etherscan). * Calling `get_funder(0)` now returns the address of the user who just funded the contract. 5. **Withdrawal:** The contract owner can withdraw the accumulated funds. * The `withdraw` function contains an access control check (e.g., an internal `_only_owner` function verifying `msg.sender == OWNER`). * Only the owner can successfully call `withdraw`. * Executing `withdraw` transfers the entire ETH balance of the contract to the `OWNER`'s address using the `send()` function. * The contract's ETH balance returns to 0. ## Key Functionality Explained Let's reiterate the two primary actions: * **Funding (`fund`):** This `@payable` function is the entry point for users. It crucially integrates with the price feed oracle to convert the incoming `msg.value` (in ETH/Wei) into its equivalent USD value. It then validates this against the `MINIMUM_USD` requirement before recording the funder and the amount. * **Withdrawing (`withdraw`):** This function is protected by an access control mechanism, ensuring only the `OWNER` can execute it. Its purpose is to transfer the contract's entire collected ETH balance to the owner. It may also include logic to reset tracking variables if needed. ## Next Steps and Getting Help This project introduces concepts that build upon previous lessons and ventures into more advanced Vyper and EVM territory, particularly with interfaces and oracles. Understanding these fundamentals deeply is essential for building sophisticated decentralized applications. If you encounter difficulties or get confused at any point: * **Utilize Course Resources:** Refer back to the GitHub repository and the materials on the Cyfrin Updraft platform. * **Engage in Discussions:** Use the course's support channels or discussion forums to ask questions and interact with instructors and fellow students. * **Leverage AI Tools:** AI assistants can be helpful for explaining concepts or suggesting debugging approaches. Remember, even when using tools like AI to help generate code, a strong understanding of the underlying principles is crucial for identifying errors, debugging effectively, and building truly robust and secure smart contracts. Let's start building!
Welcome back! In this section, we'll dive into building a classic and highly instructive smart contract: the "Buy Me A Coffee" contract. We'll be using the Remix IDE and the Vyper programming language. This project serves as an excellent vehicle for understanding fundamental smart contract development patterns.
All the code for this section can be found in the dedicated GitHub repository: github.com/Cyfrin/remix-buy-me-a-coffee-cu
. You can also find links and other resources within the Cyfrin Updraft course materials under "Course Resources". For convenience, the README.md
file in the project repository contains a special link that will automatically open Remix with the buy_me_a_coffee.vy
contract loaded, saving you the hassle of copy-pasting.
The "Buy Me A Coffee" contract is a popular introductory project because it touches upon several essential concepts in a clear way. Our version will have the following core functionality:
Receive Funds: Users (funders) can send Ether (ETH) to the smart contract.
Minimum Donation: We'll enforce a minimum donation amount, but specified in USD (e.g., the equivalent of $50 in ETH).
Price Feeds (Oracles): To enforce the USD minimum, the contract will interact with an external price feed oracle. This oracle provides the current ETH-to-USD conversion rate, allowing the contract to access real-world data – a crucial concept in Web3 development. We'll use an interface (AggregatorV3Interface
) to define how our contract communicates with the oracle.
Track Funders: The contract will keep a record of addresses that have sent funds.
Owner Withdrawal: Only the original deployer (the owner) of the contract can withdraw the accumulated ETH.
Throughout this project, we will explore key Vyper and EVM concepts, including:
payable
functions: Functions designed to receive ETH.
State Variables: Storing persistent data on the blockchain, such as the owner's address (OWNER
), the minimum USD value (MINIMUM_USD
), the price feed contract address (price_feed
), a list of funders (funders
using DynArray
), and a mapping of addresses to the amounts they've funded (address_to_amount_funded
using HashMap
).
Interfaces: Defining blueprints for interacting with other deployed smart contracts (like the price feed oracle).
Access Control: Restricting certain actions (like withdrawing funds) to specific addresses (in this case, the OWNER
).
Reading Contract State: Calling view functions to read stored data without creating a transaction.
Let's walk through the high-level functionality of the completed contract, as you might interact with it in Remix.
Compilation: The Vyper code is compiled using the Vyper compiler available within Remix.
Deployment: Using the "Deploy & Run Transactions" tab in Remix, the contract is deployed to a blockchain.
Demonstration Note: The video demonstration uses the Sepolia Testnet via MetaMask ("Injected Provider").
Course Recommendation: Due to the difficulty of obtaining testnet ETH, you will likely use the Tenderly Virtual Networks provided through the course platform for deployment and testing, rather than Sepolia.
Interaction (Initial State): Once deployed, we can interact with the contract's public functions and state variables:
Reading OWNER
shows the address that deployed the contract.
Reading price_feed
shows the address of the configured Chainlink ETH/USD price feed oracle.
Reading MINIMUM_USD
displays the required minimum donation value (e.g., 50000000000000000000
, representing $50 scaled up by 10^18).
Reading funders
(e.g., via get_funder(0)
) initially shows no funders.
Funding: A user sends funds by calling the fund
function.
This function is marked @payable
, allowing it to receive ETH.
In Remix, the Value
field is used to specify the amount of ETH to send, entered in Wei (e.g., 20000000000000000
Wei equals 0.02 ETH).
When fund
is called, the contract internally uses the price_feed
to get the current ETH/USD rate, calculates the USD value of the sent msg.value
, and uses an assert
statement to ensure this value meets or exceeds MINIMUM_USD
. If the check passes, the sender's address is added to the funders
list, and their contribution is tracked in address_to_amount_funded
.
The contract's ETH balance increases (visible in Remix and on a block explorer like Etherscan).
Calling get_funder(0)
now returns the address of the user who just funded the contract.
Withdrawal: The contract owner can withdraw the accumulated funds.
The withdraw
function contains an access control check (e.g., an internal _only_owner
function verifying msg.sender == OWNER
).
Only the owner can successfully call withdraw
.
Executing withdraw
transfers the entire ETH balance of the contract to the OWNER
's address using the send()
function.
The contract's ETH balance returns to 0.
Let's reiterate the two primary actions:
Funding (fund
): This @payable
function is the entry point for users. It crucially integrates with the price feed oracle to convert the incoming msg.value
(in ETH/Wei) into its equivalent USD value. It then validates this against the MINIMUM_USD
requirement before recording the funder and the amount.
Withdrawing (withdraw
): This function is protected by an access control mechanism, ensuring only the OWNER
can execute it. Its purpose is to transfer the contract's entire collected ETH balance to the owner. It may also include logic to reset tracking variables if needed.
This project introduces concepts that build upon previous lessons and ventures into more advanced Vyper and EVM territory, particularly with interfaces and oracles. Understanding these fundamentals deeply is essential for building sophisticated decentralized applications.
If you encounter difficulties or get confused at any point:
Utilize Course Resources: Refer back to the GitHub repository and the materials on the Cyfrin Updraft platform.
Engage in Discussions: Use the course's support channels or discussion forums to ask questions and interact with instructors and fellow students.
Leverage AI Tools: AI assistants can be helpful for explaining concepts or suggesting debugging approaches.
Remember, even when using tools like AI to help generate code, a strong understanding of the underlying principles is crucial for identifying errors, debugging effectively, and building truly robust and secure smart contracts. Let's start building!
An instructive introduction to Introduction to the Remix Buy Me A Coffee Project - Build the classic "Buy Me A Coffee" smart contract using Vyper and Remix. Learn core concepts like price feeds for USD minimums, payable functions, state storage, and owner-only withdrawals.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on June 11, 2025