Logo

dev-resources.site

for different kinds of informations.

Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity

Published at
11/25/2024
Categories
web3
solidity
Author
prince_ad7e7c212e5c5ecf40
Categories
2 categories in total
web3
open
solidity
open
Author
25 person written this
prince_ad7e7c212e5c5ecf40
open
Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity

If you're diving into Solidity development, particularly in the realm of DeFi, you'll often encounter patterns like approve and depositCollateral. These functions form the backbone of how ERC-20 tokens are transferred and managed within smart contracts. Let's break it down so you can master this concept!

What Are We Looking At?
Here's the code snippet we’ll analyze:

ERC20Mock(weth).approve(address(dsce), AMOUNT_COLLATERAL);
dsce.depositCollateral(weth, AMOUNT_COLLATERAL);
Enter fullscreen mode Exit fullscreen mode

Grants a smart contract permission to transfer a specific amount of tokens on behalf of the user.
Executes the actual transfer of those tokens into the smart contract as collateral.

First Step: approve
What Does It Do?
The approve function allows a user to authorize another address (typically a smart contract) to spend tokens on their behalf. This is part of the ERC-20 token standard and is essential for enabling interactions between users and DeFi protocols.

Breaking It Down:
solidity

ERC20Mock(weth).approve(address(dsce), AMOUNT_COLLATERAL);
Enter fullscreen mode Exit fullscreen mode

ERC20Mock(weth): Represents the token contract (mocked here for testing) deployed at the weth address.
.approve(address(dsce), AMOUNT_COLLATERAL): Authorizes the dsce contract to spend up to AMOUNT_COLLATERAL worth of weth tokens on the user’s behalf.

How It Works Behind the Scenes:
The ERC-20 token contract maintains an internal mapping called allowances, which tracks how much a spender is allowed to use. When approve is called, it updates this mapping:

allowances[msg.sender][spender] = amount;
Enter fullscreen mode Exit fullscreen mode

msg.sender: The user who is calling the function.
spender: The contract being authorized (here, dsce).
amount: The maximum tokens allowed for transfer.

Second Step: depositCollateral
What Does It Do?
The depositCollateral function moves the user’s tokens from their wallet into the smart contract. It’s the action step that follows the permission granted by approve.

dsce.depositCollateral(weth, AMOUNT_COLLATERAL);
Enter fullscreen mode Exit fullscreen mode

dsce: The contract where collateral will be deposited.
.depositCollateral(weth, AMOUNT_COLLATERAL): Tells the contract to pull AMOUNT_COLLATERAL worth of weth tokens from the user’s account.

How They Work Together
To better understand the flow, let’s visualize the sequence:

Before approve:

User Balance: 100 weth
Contract Balance: 0 weth
Allowance: 0

After approve:

User Balance: 100 weth
Contract Balance: 0 weth
Allowance: 10 weth (assuming AMOUNT_COLLATERAL = 10)
After depositCollateral:

User Balance: 90 weth
Contract Balance: 10 weth
Allowance: 0

Real-World Analogy

Imagine you’re storing gold coins in a locker. Here’s how the process maps to our code:

approve:
You tell the attendant, “You can take up to 10 coins from me.”
depositCollateral:
The attendant takes the 10 coins and puts them in the locker for safekeeping.

Key Takeaways
approve: Grants permission for token transfers.
depositCollateral: Executes the transfer and updates balances.
ERC-20 Standards: This process ensures security and interoperability in DeFi systems.

solidity Article's
30 articles in total
Favicon
Have You Fallen for a Phishing Scam? Let’s Talk About It 👀
Favicon
Solidity
Favicon
Why Every Crypto Developer Should Understand Tokenomics 🚀
Favicon
How we used the ERC-2535 Diamonds at Proof of Peacemaking Protocol
Favicon
🔐 Solidity Limitations, Solutions, Best Practices and Gas Optimization 🚀
Favicon
go调用solidity合约新方法
Favicon
⚖️ The Importance of Using ReentrancyGuard in Solidity Smart Contract
Favicon
Formal Verification: An Example
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
When to Use ERC-721 vs ERC-1155: Choosing the Right NFT Standard
Favicon
Solidity Pattern - Proxy Delegate and Decorator Patterns
Favicon
Solidity Patterns - CEI
Favicon
Foundry vs Hardhat (A story version)
Favicon
Energy NFT Marketplace
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
🚀 Getting Started with kritisi CLI: An AI-Driven Security Tool for Solidity
Favicon
Formal Verification: The Foundation of Ethereum Smart Contracts
Favicon
The Danger of Randomness in Smart Contracts and its solution
Favicon
What is Reentrancy?
Favicon
Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity
Favicon
Ethereum Transaction Calls and State Changes
Favicon
Creating a Toy Solidity compiler and running it in a Toy EVM
Favicon
Send Tokens in Bulk with Low Fees and Fast Delivery: The Ultimate Airdrop Tool for 2024
Favicon
🛡️ Why Using OpenZeppelin in Smart Contracts Is Essential
Favicon
The delegatecall Function in Solidity
Favicon
The delegatecall Function in Solidity
Favicon
A Walkthrough of Solidity Custom Errors
Favicon
How to write dynamic staking smart contract step by step in practice
Favicon
Mainnet Forking in Foundry
Favicon
Every Blockchain Developer Must Know About This Scam!

Featured ones: