<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: prince</title>
    <description>The latest articles on DEV Community by prince (@prince_ad7e7c212e5c5ecf40).</description>
    <link>https://dev.to/prince_ad7e7c212e5c5ecf40</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2399233%2F75c90062-ea47-40b0-8844-ca695262f7db.png</url>
      <title>DEV Community: prince</title>
      <link>https://dev.to/prince_ad7e7c212e5c5ecf40</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prince_ad7e7c212e5c5ecf40"/>
    <language>en</language>
    <item>
      <title>Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity</title>
      <dc:creator>prince</dc:creator>
      <pubDate>Mon, 25 Nov 2024 12:25:25 +0000</pubDate>
      <link>https://dev.to/prince_ad7e7c212e5c5ecf40/understanding-approve-and-depositcollateral-the-core-of-erc-20-token-transfers-in-solidity-2jcc</link>
      <guid>https://dev.to/prince_ad7e7c212e5c5ecf40/understanding-approve-and-depositcollateral-the-core-of-erc-20-token-transfers-in-solidity-2jcc</guid>
      <description>&lt;p&gt;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!&lt;/p&gt;

&lt;p&gt;What Are We Looking At?&lt;br&gt;
Here's the code snippet we’ll analyze:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERC20Mock(weth).approve(address(dsce), AMOUNT_COLLATERAL);
dsce.depositCollateral(weth, AMOUNT_COLLATERAL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Grants a smart contract permission to transfer a specific amount of tokens on behalf of the user.&lt;br&gt;
Executes the actual transfer of those tokens into the smart contract as collateral.&lt;/p&gt;

&lt;p&gt;First Step: approve&lt;br&gt;
What Does It Do?&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Breaking It Down:&lt;br&gt;
solidity&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERC20Mock(weth).approve(address(dsce), AMOUNT_COLLATERAL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;How It Works Behind the Scenes:&lt;br&gt;
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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allowances[msg.sender][spender] = amount;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;msg.sender: The user who is calling the function.&lt;br&gt;
spender: The contract being authorized (here, dsce).&lt;br&gt;
amount: The maximum tokens allowed for transfer.&lt;/p&gt;

&lt;p&gt;Second Step: depositCollateral&lt;br&gt;
What Does It Do?&lt;br&gt;
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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dsce.depositCollateral(weth, AMOUNT_COLLATERAL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;How They Work Together&lt;br&gt;
To better understand the flow, let’s visualize the sequence:&lt;/p&gt;

&lt;p&gt;Before approve:&lt;/p&gt;

&lt;p&gt;User Balance: 100 weth&lt;br&gt;
Contract Balance: 0 weth&lt;br&gt;
Allowance: 0&lt;/p&gt;

&lt;p&gt;After approve:&lt;/p&gt;

&lt;p&gt;User Balance: 100 weth&lt;br&gt;
Contract Balance: 0 weth&lt;br&gt;
Allowance: 10 weth (assuming AMOUNT_COLLATERAL = 10)&lt;br&gt;
After depositCollateral:&lt;/p&gt;

&lt;p&gt;User Balance: 90 weth&lt;br&gt;
Contract Balance: 10 weth&lt;br&gt;
Allowance: 0&lt;/p&gt;

&lt;p&gt;Real-World Analogy&lt;/p&gt;

&lt;p&gt;Imagine you’re storing gold coins in a locker. Here’s how the process maps to our code:&lt;/p&gt;

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

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

</description>
      <category>web3</category>
      <category>solidity</category>
    </item>
    <item>
      <title>Why Multiply and Divide by 1𝑒18 in Solidity?</title>
      <dc:creator>prince</dc:creator>
      <pubDate>Fri, 22 Nov 2024 09:49:43 +0000</pubDate>
      <link>https://dev.to/prince_ad7e7c212e5c5ecf40/why-multiply-and-divide-by-118-in-solidity-53mp</link>
      <guid>https://dev.to/prince_ad7e7c212e5c5ecf40/why-multiply-and-divide-by-118-in-solidity-53mp</guid>
      <description>&lt;p&gt;When developing smart contracts for decentralized finance (DeFi), precision handling is crucial. A common pattern you’ll encounter is multiplying and then dividing by 1e18 in calculations involving token prices, amounts, and values. At first glance, this may seem redundant—but it serves a critical purpose.&lt;/p&gt;

&lt;p&gt;-Ethereum and DeFi systems heavily rely on consistent precision across calculations: &lt;br&gt;
Ethereum’s 18 Decimal Standard: Most tokens, including ERC20 tokens, use 18 decimal places for their values.&lt;br&gt;
 For example, 1 ETH is represented as 1 × 10 ^18 . This standard ensures consistency across token operations. &lt;/p&gt;

&lt;p&gt;-Chainlink Price Feed Precision: Chainlink, a popular oracle for fetching token prices, provides values with 8 decimal places (e.g., 1000 × 10 ^8 ).&lt;br&gt;
 This mismatch in precision needs to be addressed when integrating Chainlink data with Ethereum-based systems.&lt;/p&gt;

&lt;p&gt;The Problem: Mismatched Precisions Imagine you want to calculate the USD value of 1 ETH: Token Price from Chainlink = 1000 × 10^8&lt;br&gt;
 Token Amount = 1×10 ^18.&lt;br&gt;
 If you directly multiply these values: &lt;/p&gt;

&lt;p&gt;USD Value = 1000 × 10^8 * 10^18&lt;/p&gt;

&lt;p&gt;The result has 26 decimal places, far exceeding Ethereum’s standard of 18. Using this value in other parts of your contract—such as calculating collateralization or health factors—can lead to inconsistencies and errors.&lt;/p&gt;

&lt;p&gt;The Solution: &lt;br&gt;
1.Multiply the price by 1e10 to bring Chainlink's 8-decimal precision up to 18-decimal precision.&lt;/p&gt;

&lt;p&gt;-Adjust price precision: 1000 &lt;em&gt;10^8 * 10^10 = 1000&lt;/em&gt; 10^18&lt;/p&gt;

&lt;p&gt;2.Multiply the result by the token amount&lt;/p&gt;

&lt;p&gt;-Multiply by the token amount of 1ETH:&lt;br&gt;
(1000*10^18) *(1*10^18) = 1000 * 10^36&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Divide by 1e18 to normalize the final value back to 18 decimals&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note 10e8 = 10^8 0r 10 raised to the power of 8.&lt;br&gt;
     10e18 = 10^18 or 10 raised to the power of 18.&lt;/p&gt;

&lt;p&gt;An Example Let’s calculate the USD value of 1 ETH at $1,000: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inputs:
-Price =  1000 × 10^8   (from Chainlink) 
-Amount =  1 × 10^18   (1 ETH).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step-by-Step Calculation:  &lt;/p&gt;

&lt;p&gt;-Adjust price precision: 1000 &lt;em&gt;10^8 * 10^10 = 1000&lt;/em&gt; 10^18&lt;br&gt;
-Multiply by the amount:(1000*10^18) *(1*10^18) = 1000 * &lt;br&gt;
   10^36&lt;br&gt;
-Normalize precision: &lt;br&gt;
    USD Value = 1000 * 10^36 / 10^18 = 1000 * 10^18&lt;/p&gt;

&lt;p&gt;1000 * 10^18 represents $1,000 in 18-decimal precision.&lt;br&gt;
NOTE:&lt;br&gt;
Without dividing by 1e18, the result would have 36  decimals, making it incompatible with other values that adhere to Ethereum’s  18-decimal standard.&lt;/p&gt;

</description>
      <category>defi</category>
      <category>web3</category>
      <category>ethereum</category>
      <category>cryptocurrency</category>
    </item>
  </channel>
</rss>
