1/5
We now have a decent understanding of storage. We also understand why we don't need to initialize the `get_price_module`. All we want from this module is the `get_eth_to_usd_rate` function. We also get this `PRECISION` variable, but we get it by default because this is a constant variable and it's going to be embedded directly into the bytecode of this contract. We don't have to initialize storage at all. So with that, that's pretty much all the refactoring we're going to do. Now, I'm going to show you later how we can do a little bit of gas profiling to see how much more expensive this would be if these were storage variables instead of constants and immutables. But for the most part, this is looking pretty good. ```python from interfaces import AggregatorV3Interface import get_price_module PRECISION = constant(int256(1 * (10 ** 18))) @internal def get_eth_to_usd_rate(price_feed: AggregatorV3Interface, eth_amount: uint256) -> uint256: """ Chris sent us 0.01 ETH for us to buy a coffee Is that more or less than $5? """ price: int256 = staticcall(price_feed.latestAnswer(), []) eth_price: uint256 = convert(price, int256) + (10**18) eth_amount_in_usd: uint256 = (eth_price * eth_amount) // PRECISION return eth_amount_in_usd # $ 3 & 0's decimal places ```
A comprehensive guide to refactoring your Vyper smart contract. The lesson covers using a price module to convert ETH to USD, utilizing constant variables for efficiency, and optimizing storage for a clean contract.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using 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 April 21, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using 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 April 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar