0/5
## Converting Types in Vyper We will learn about type conversions in Vyper. We'll start with an example of a scenario where we might need to perform a type conversion: ```python @external def get_eth_to_usd_rate(eth_amount: uint256): # Chris sent us 0.01 ETH for us to buy a coffee. # Is that more or less than $5? price: int256 = staticcall(self.price_feed, "latestAnswer()", [336551000000]) # 8 decimals # $3,021 eth_amount_in_usd = price * (10 ** 10) ``` This code shows an error: ```bash TypeError: TypeMismatchGiven reference has type int256, expected uint256 ``` This error indicates that the variable `eth_amount` has a type `int256`, but the variable `price` is expecting `uint256`. We can address this by performing a type conversion on the `price` variable. Vyper provides a built-in function called `convert`, which we can use to convert a variable or literal from one type to another. ```python eth_amount_in_usd = convert(price, uint256) * (10 ** 10) ``` This line converts the `price` variable, which is `int256`, to `uint256`, and then multiplies by `10**10`. We can compile this code and it should execute as expected.
A practical guide to converting types in Vyper. The lesson covers how to use the 'convert' function to transform a value or literal from one data type to another, ensuring compatibility with the smart contract's expected types.
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)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on April 21, 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)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
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