1/5
We learned about encoding, which is how we can represent various data types in hex and bytes, such as strings, numbers, and concatenated strings. We then looked at how the EVM uses encoding to build function calls, such as `transfer(to: address, amount: uint256)`, which is the function signature of a function we've used before. We can decode this using the built-in `method_id` function, which returns the function selector. This function selector will look like this: ```python return method_id("transfer(address,uint256)", output_type=bytes4) ``` The first four bytes of the encoded function call will be the function selector, which tells the contract which function to call. We then saw how we can use `raw_call` to call a function directly. We can combine a function signature with parameter values and create the raw hex data we need to make a function call. In the below example, we can use `raw_call` to call the transfer function. ```python def call_function_directly(address_to_call: address, new_amount: uint256, update_address: address): success: bool = False response: Bytes[32] = b"" success, response = raw_call( address_to_call, abi_encode( update_address, new_amount, method_id="transfer(address,uint256)" ), max_outsize=32, revert_on_failure=False ) assert success return response ``` The `abi_encode` function will encode the arguments with the `method_id` and return raw hex data. We also saw how we can import interfaces to call functions within the imported contract. For example, we can call the `transfer` function within an imported contract called `myInterface`. ```python from interfaces import myInterface myInterface(address).transfer(to_call: address, amount: uint256) ``` We can also use raw hex data to make calls to other contracts without using the ABI or interface. This is a little bit of a deeper dive into EVM function calls, but we'll practice more in the future.
A technical deep dive into raw calls in Viper and Solidity. The lesson explores the concept of function selectors, method IDs, and how to directly interact with smart contracts without relying on the ABI or interfaces.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
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 February 4, 2025
Duration: 2h 21min
Duration: 1h 58min
Duration: 2h 55min
Duration: 1h 55min
Duration: 46min
Course Overview
About the course
How to build a DeFi stablecoin and customized NFT
How to deploy your smart contract on ZKsync with Moccasin
Advanced testing techniques like stateful and stateless Python fuzzing
How to write algorithmic trading scripts in Python
Hashing signatures, proxies, delegate calls, upgradable contracts, random numbers, and more!
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 February 4, 2025