<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kundan Kumar Kushwaha on Medium]]></title>
        <description><![CDATA[Stories by Kundan Kumar Kushwaha on Medium]]></description>
        <link>https://medium.com/@kundankushwaha.er?source=rss-9393fd3531b5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*VCifWUPPmsRWibW0tynwbw.jpeg</url>
            <title>Stories by Kundan Kumar Kushwaha on Medium</title>
            <link>https://medium.com/@kundankushwaha.er?source=rss-9393fd3531b5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 20:27:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kundankushwaha.er/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[#3. Truster — Damn Vulnerable DeFi]]></title>
            <link>https://coinsbench.com/3-truster-damn-vulnerable-defi-2eacfacd6550?source=rss-9393fd3531b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/2eacfacd6550</guid>
            <category><![CDATA[damn-vulnerable-defi]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[solidity]]></category>
            <dc:creator><![CDATA[Kundan Kumar Kushwaha]]></dc:creator>
            <pubDate>Wed, 28 Jun 2023 11:42:41 GMT</pubDate>
            <atom:updated>2023-06-30T11:29:38.914Z</atom:updated>
            <content:encoded><![CDATA[<h3>#3. Truster — Damn Vulnerable DeFi</h3><blockquote>More and more lending pools are offering flash loans. In this case, a new pool has launched that is offering flash loans of DVT tokens for free.</blockquote><blockquote>The pool holds 1 million DVT tokens. You have nothing.</blockquote><blockquote>To pass this challenge, take all tokens out of the pool. If possible, in a single transaction.</blockquote><blockquote><em>Challenge Link: </em>https://www.damnvulnerabledefi.xyz/challenges/truster/</blockquote><h4>Flash Loan Pool Contract</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0535f551f8662cf58c6534d733e8dc2e/href">https://medium.com/media/0535f551f8662cf58c6534d733e8dc2e/href</a></iframe><p>Let’s Break the <strong><em>flashloan </em></strong>function of <strong><em>TrusterLenderPool.sol</em></strong></p><p>Line 29: <strong><em>baclanceBefore : </em></strong>balance before flash loan.</p><p>Line 31: transfers token to borrower.</p><p>Line 32: <strong><em>target.functionCall(data). </em></strong>what the hell is this??</p><p>Line 34: checks for balance before flash loan is equals to after flash loan or not.</p><h4><strong><em>What the hell is target.functionCall(data)?</em></strong></h4><p><strong><em>functionCall</em></strong> function comes from library <strong><em>Address.sol</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7hm8wUrfsaqK-TkE76-sfQ.png" /></figure><p><strong><em>functionCall </em></strong>calls the another overloaded <strong><em>functionCall</em></strong> function with three arguments target, data, errorMessage, which again calls the <strong><em>functionCallWithValue </em></strong>(the one which<strong><em> </em></strong>overloaded with 4 arguments).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jWat_lnD1XDlasP0R1LI3Q.png" /></figure><p><strong><em>functionCallWithValue(target,data,value,errorMessage) </em></strong>Let the users to call any function at any target contract on behalf of <strong><em>TrusterLenderPool</em></strong> contract.</p><p>It means using <strong><em>target.functionCall(data) </em></strong>user can call any function of target contract specified in <strong><em>data </em></strong>without any restrictions.</p><p>Using this loophole in code we can get approval to spend all the tokens of pool contract using <strong><em>functionCall </em></strong>with <strong><em>data </em></strong>to call ERC20 <strong><em>approve</em></strong> function and then we can transfer them to our account.</p><p>To steel all tokens in one transaction we can create a contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*N1UurWOdFqkbjm-JOcPZ2g.png" /></figure><p>we can encoded arguments with function signature “<strong><em>approve(address,uint256)” </em></strong>to approve all tokens in pool contract to this contract address and then using this contract we can transferred those tokens into our account.</p><blockquote>To learn more about ABI specification you can read <a href="https://docs.soliditylang.org/en/latest/abi-spec.html">https://docs.soliditylang.org/en/v0.8.13/abi-spec.html#</a></blockquote><p>writing <strong><em>truster.challange.js</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DWrqQ4XwiKKTa_covO8pEQ.png" /></figure><p>Deploy <strong><em>AttackTruster</em></strong> contract and call <strong><em>attack</em></strong> function.</p><h4>Learning Takeaways</h4><p>Having feature to call any function on behalf of contract without access control is always vulnerable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2eacfacd6550" width="1" height="1" alt=""><hr><p><a href="https://coinsbench.com/3-truster-damn-vulnerable-defi-2eacfacd6550">#3. Truster — Damn Vulnerable DeFi</a> was originally published in <a href="https://coinsbench.com/">CoinsBench</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#2. Naive Receiver — Damn Vulnerable DeFi]]></title>
            <link>https://coinsbench.com/2-naive-receiver-damn-vulnerable-defi-5970ac42e353?source=rss-9393fd3531b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/5970ac42e353</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[damn-vulnerable-defi]]></category>
            <category><![CDATA[web3-security]]></category>
            <dc:creator><![CDATA[Kundan Kumar Kushwaha]]></dc:creator>
            <pubDate>Wed, 21 Jun 2023 07:14:54 GMT</pubDate>
            <atom:updated>2023-06-23T14:39:43.756Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>#2. Naive Receiver — Damn Vulnerable DeFi</strong></h3><p>It was an easy challenge when you compare these contracts with the contracts in <strong><em>Unstoppable</em></strong> challenge. You can figure out where the problem is.</p><blockquote>There’s a pool with 1000 ETH in balance, offering flash loans. It has a fixed fee of 1 ETH.</blockquote><blockquote>A user has deployed a contract with 10 ETH in balance. It is capable of interacting with the pool and receiving flash loans of ETH.</blockquote><blockquote>Take all ETH out of the user’s contract. If possible, in a single transaction.</blockquote><h4>Let’s first look at how a user will use it?</h4><p>There are two contracts one is <strong><em>NaiveReceiverLenderPool.sol</em></strong> a pool contract that provides flash loans and another is <strong><em>FlashLoanReceiver.sol</em></strong> a contract which receives the flash loan.</p><p>To receive flash loan in <strong><em>FlashLoanReceiver.sol </em></strong>contract, user will call the <strong><em>flashLoan</em></strong> function inside <strong><em>NaiveReceiverLenderPool.sol.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*c28QXJuHEU-Xiy9j1iuD3A.png" /></figure><p><strong><em>flashLoan</em></strong> function takes receiver contract address, token, amount, data as argument then</p><ol><li>It checks for the currency is supported or not.</li><li>Before transferring ETH, it stores the balance of the contract so that it can validate that receiver has paid the fee or not by comparing after and before balance.</li><li>After transferring ETH, it calls <strong><em>onFlashLoan </em></strong>with arguments <strong><em>msg.sender, token, ammount, fee, data </em></strong>function of receiver’s contract so that receiver will pay back along with the fee of 1 ether.</li><li>In final step it checks for receiver has paid loan money along with the 1 ether of fee or not.</li></ol><p>Now let’s look inside <strong><em>onFlashLoan </em></strong>function of <strong><em>FlashLoanReceiver.sol </em></strong>contract<strong><em> </em></strong>which is called step number 3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*VA2cxxvC8wYoIQCJUk2l7w.png" /></figure><p>Look at arguments : first argument in which <strong><em>msg.sender</em></strong> is passed from lender pool contract and is nowhere used inside <strong><em>onFlashLoan</em></strong> function(a fishy smell is coming from here).</p><p>What is <strong><em>msg.sender?</em></strong></p><p>In lender pool contract <strong><em>msg.sender</em></strong> is the address of the caller of <strong><em>flashLoan</em></strong> function, does it means anyone can be caller?</p><p>Let’s look at the steps involved in <strong><em>onFlashLoan.</em></strong></p><ol><li>First it checks for the caller of <strong><em>onFlashLoan</em></strong> is same as pool contract or not.</li><li>Then it check for currency is supported or not i.e. ETH or not.</li><li>Calculates the amount to be repaid i.e. <strong><em>amount + fee.</em></strong></li><li>Then calls a dummy function <strong><em>_executeActionDuringFlashLoan</em></strong>.</li><li>Finally it pays back the flash loan amount with fee.</li></ol><p>Nowhere is it checking that the caller of <strong><em>flashLoan </em></strong>function is owner of the receiver contract or not, it means YES, anyone can call <strong><em>flashLoan</em></strong> function with the this receiver contract address as receiver and 1 ETH will be deducted as fee.</p><h4>Solution</h4><p>An attacker will call <strong><em>falshLoan</em></strong> function 10 time with and all ETH will go out of the user’s contract.</p><p><strong>Simple Solution</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*xkjV1hpNqBi123WkiF5z2w.png" /></figure><p><strong>In single Transaction</strong></p><p>Make a smart contract with a function <strong><em>attack</em></strong> write loops inside that function then deploy and invoke that function.</p><p>The whole thing will happen in single transaction.</p><h4>Takeaway Learning.</h4><p>Always specify access control to sensitive functions where transaction is happening.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5970ac42e353" width="1" height="1" alt=""><hr><p><a href="https://coinsbench.com/2-naive-receiver-damn-vulnerable-defi-5970ac42e353">#2. Naive Receiver — Damn Vulnerable DeFi</a> was originally published in <a href="https://coinsbench.com/">CoinsBench</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Damn Vulnerable DeFi Challenge.]]></title>
            <link>https://coinsbench.com/introducing-damn-vulnerable-defi-challenge-120e7d025248?source=rss-9393fd3531b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/120e7d025248</guid>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Kundan Kumar Kushwaha]]></dc:creator>
            <pubDate>Sun, 18 Jun 2023 14:04:36 GMT</pubDate>
            <atom:updated>2023-06-26T16:21:18.718Z</atom:updated>
            <content:encoded><![CDATA[<p>A few months ago I started learning Solidity and jumped into learning Smart Contract Auditing. I solved all <a href="https://ethernaut.openzeppelin.com/">Ethernaut Challenges</a> from there I gained a lot knowledge on how EVM works, what is ERC20 (Standard Token) and ERC721 (NFT), how re-entrance attack happens, how to make a updatable contract using proxy and ERC 1967 (Proxy Storage Slot) and many more things. Solving these challenges made me to look deeper into the Smart Contract at the level of bytecodes. But then also I haven’t progressed as much as I could because of my inconsistency, so this time I have decided to learn in public.</p><p>I am starting a new security challenge series <a href="https://www.damnvulnerabledefi.xyz/">Damn Vulnerable DeFi</a></p><h4>Getting Start with Damn Vulnerable DeFi</h4><ol><li>Here is the website: <a href="https://www.damnvulnerabledefi.xyz/">https://www.damnvulnerabledefi.xyz/</a></li><li>Clone this Repository: <a href="https://github.com/tinchoabbate/damn-vulnerable-defi/tree/v3.0.0">https://github.com/tinchoabbate/damn-vulnerable-defi/tree/v3.0.0</a></li><li>Make sure, checkout to the latest version, the current latest version is v3.0.0</li><li>Install dependencies with <strong><em>yarn</em></strong> or <strong><em>npm install</em></strong></li><li>In the<strong><em> contracts</em></strong> folder, there is the folder for each challenge.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*VXfetF6kdSTXM13waOZpGg.png" /></figure><p>6. In the <em>test</em> folder inside each challenge there is a file named <strong><em>*.challenge.j</em></strong>s this is where you will write your solutions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*ZeC7jIJbT3n_TVEIn-ZopA.png" /></figure><p>Look at the comment — /*CODE YOUR SOLUTION HERE*/</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*I2tVw6trBtJ_cYHoTWQBBA.png" /></figure><p>8. Run the solutions with <strong><em>yarn run challenge-name</em></strong>. If the test executed successfully, you’ve passed!</p><h4>The Tools You Need to Install</h4><ol><li>Have <strong>VS CODE</strong>.</li><li>Install the<strong> solidity</strong> plugin by <em>Juan Blanco.</em></li><li>Install the <strong>Solidity Visual Developer</strong> plugin by <em>tintinweb.</em></li><li>Install <strong>ganache</strong> with <em>npm i -g ganache (To run local blockchain network).</em></li><li>Install the <strong>Ethereum Remix </strong>plugin by <em>Remix Project. (This will help in compiling, deploying, and playing with contracts).</em></li></ol><h4>How to Compile and Deploy Contracts using Remix Plugin</h4><ol><li>Click on Remix Plugin Icon &gt; Under the remix tab, click on the <strong>Compiler</strong>&gt; Select the contract you want to compile.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5S76XOGu_BUXznLPRU0kYw.png" /></figure><p>2. To deploy contracts, click on the <strong>Run and Deploy</strong></p><p>A dropdown menu will appear, click on the <strong>Activate</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PABzDi-y1Z6iM3STSC5MqA.png" /></figure><p>The <strong>Run &amp; Deploy</strong> Tab is going to open. To connect it with the local blockchain network, we have installed <strong><em>ganache.</em></strong></p><p>To run the local blockchain, open the terminal and type <strong><em>ganache.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X2plCg0FC0tT2P1Y-jW8AQ.png" /></figure><p>Enter the <em>network url</em> click on <strong><em>Connect.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*ePfdOrpGczjohdztgqrOxw.png" /></figure><p>Once it is connected you’re ready. Now you can compile, deploy and interact with contracts with ease using remix UI.</p><p>Now try the 1st challenge, google to learn as much as you can, and Here is my solution to the 1st challenge.</p><blockquote><a href="https://medium.com/@kundankushwaha.er/1-unstoppable-damn-vulnerable-defi-7651c96f35d2">#1. Unstoppable Solution — Damn Vulnerable DeFi</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=120e7d025248" width="1" height="1" alt=""><hr><p><a href="https://coinsbench.com/introducing-damn-vulnerable-defi-challenge-120e7d025248">Introducing Damn Vulnerable DeFi Challenge.</a> was originally published in <a href="https://coinsbench.com/">CoinsBench</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[#1. Unstoppable — Damn Vulnerable DeFi]]></title>
            <link>https://coinsbench.com/1-unstoppable-damn-vulnerable-defi-7651c96f35d2?source=rss-9393fd3531b5------2</link>
            <guid isPermaLink="false">https://medium.com/p/7651c96f35d2</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Kundan Kumar Kushwaha]]></dc:creator>
            <pubDate>Sun, 18 Jun 2023 14:01:07 GMT</pubDate>
            <atom:updated>2023-06-21T08:28:29.073Z</atom:updated>
            <content:encoded><![CDATA[<h3>#<strong>1. Unstoppable — Damn Vulnerable DeFi</strong></h3><p>There’s a tokenized vault with a million DVT tokens deposited. It’s offering flash loans for free, until the grace period ends.</p><p>To pass the challenge, make the vault stop offering flash loans.</p><p>You start with 10 DVT tokens in balance.</p><h4>Let’s First Understand What is Flash Loan?</h4><p>Basically flash loan is a loan in <strong>Decentralized Finance</strong> ecosystem that allow users to borrow assets without any collateral or fee and paid back within the same block of transaction.</p><p>Inside contracts folder their is a file<strong><em> DamnValuableToken.sol,</em></strong> the contract is inherited from ERC20.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*po-DgDXWUIW8e4L2c5bpSQ.png" /></figure><h4>Let’s Understand ERC20 Tokens</h4><p>ERC20 is standards for fungible assets. It defines a standard set of methods such as balance, transferFrom, approve, etc. A standard interface allows any tokens on Ethereum to be re-used by other applications.</p><p>Let’s understand it by a basic example.</p><p>Assume there are two tokens “Token X” and “Token Y” The function/method for transaction in X is named <strong><em>“send” </em></strong>while in Y it is named <strong><em>“transfer”. </em></strong>A wallet company that wants to support both tokens they have to write separate logic for both tokens. There is plenty of tokens out there so we need a standard way of defining tokens so that wallets and application can support the tokens.</p><p>Must Read:</p><p><a href="https://eips.ethereum.org/EIPS/eip-20">ERC-20: Token Standard</a></p><p><strong><em>UnstoppableVault</em></strong> is inherited from ERC4626 (TOKENIZED VAULT STANDARD). Have a look on ERC4626.</p><p><a href="https://eips.ethereum.org/EIPS/eip-4626">ERC-4626: Tokenized Vaults</a></p><h4>Play with the Smart Contract</h4><p>To interact with contracts I will be using Remix web IDE <a href="https://remix.ethereum.org/">https://remix.ethereum.org/</a></p><ol><li>Compile and Deploy the <strong><em>DamnValuableToken.sol</em></strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*Pn80Z29a-mTWzd9F12oFdQ.png" /></figure><p>As you can see the following code in DVT token</p><blockquote>constructor() ERC20(“DamnValuableToken”, “DVT”, 18) {</blockquote><blockquote>_mint(msg.sender, type(uint256).max);</blockquote><blockquote>}}</blockquote><p>When DVT token contract is deployed it mints tokens equivalent to <strong><em>type(uint256).max </em></strong>which is transferred to <strong><em>msg.sender (</em></strong>deployer of the contract). Verify it with interacting with <strong><em>balanceOf</em></strong> function.</p><p>2. Compile and Deploy <strong><em>UnstoppableVault.sol</em></strong></p><p>Pass arguments required by constructor and deploy it.</p><p><em>_token</em> : address of DVT you deployed.</p><p><em>_owner</em> : same address as deployer of DVT or also can be any another address.</p><p><em>_feeRecipient</em> : same address as deployer of DVT or also can be any another address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*-2u2aqVe1rI36jhyhPanCA.png" /></figure><p>3. using <strong><em>approve</em></strong> function of DVT token (inherited from ERC20) approve some tokens to spend by the Vault. (invoke function with the address you have deployed DVT contract).</p><p><em>assets : </em>ammout of tokens</p><p><em>spender </em>: address of UnstoppableVault.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*M19gqQFoIL35fuj4aahXjQ.png" /></figure><p>4. Now using <strong><em>deposit</em></strong> function of <strong><em>UnstoppableVault</em></strong> (inherited from ERC4626) deposit same amount of tokens you have approved.(invoke function with the address you have deployed DVT contract because in that address you have the minted tokens that you got when you deployed DVT contract).</p><p>Now Vault have some tokens to give as Flash Loans. In my case it is 100000.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*m9MCJ2TEHxOcB3gIOHJoUA.png" /><figcaption>.</figcaption></figure><p>5. Now Complie and Deploy <strong><em>ReceiverUnstoppable.sol</em></strong></p><p>Now your role is some user, deploy it using another address than deployer of Vault and Token contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*OyP8m-RCjElSnnFK_Ku5xA.png" /></figure><p>6. using <strong><em>executeFlashLoan </em></strong>get some flash loan.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*396h5Kmu75kBF7R1BGwbLA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*KXbklDd3n61Z2VxVonUoyA.png" /></figure><p>Now you have understood the purpose of the <strong><em>UnstoppableVault.sol</em></strong> pool contract and <strong><em>RecieverUnstoppable</em></strong> contract, let’s move forward.</p><p>Our objective was to make the vault stop offering flash loans permanently. This is a DOS attack — Denial of Service.</p><h4>Breaking The Contract</h4><p>You must have figured out that inside <strong><em>ReceiverUnstoppable</em></strong> contract <strong><em>executeFlashLoan </em></strong>is calling<strong><em> flashLoan </em></strong>funtion of the pool <strong><em>(UnstoppableVault.sol). </em></strong>So any logical error must be there in<strong><em> flashLoan </em></strong>funtion.</p><p><strong><em>executeFlashLoan </em></strong>function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*9WtRjrHTuoaFFKK2K5hiuw.png" /></figure><p><strong><em>flashLoan</em></strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*R_ezxfHDQQeQUYiUykOWbg.png" /></figure><p>You can se in code calls are reverted from…</p><ol><li><strong><em>if(amount == 0) : </em></strong>There is not any chance that we can do something that will make the contract to stop flash loan permanently</li><li><strong><em>if (address(asset) != _token) : </em></strong>It is<strong><em> </em></strong>checking receiver is making request for same token (DVT) or not.</li><li><strong><em>if (convertToShares(totalSupply) != balanceBefore) : </em></strong>Here can be chance of error.</li></ol><p><strong><em>convertToShares(uint assets) : </em></strong>This function is Inherited from ERC4626 it returns the amount of shares that the Vault would exchange for the amount of assets provided. If it is not overwritten it will return same as assets passed.</p><p><strong><em>totalSupply :</em></strong> inherited from ERC20 to ERC4626 and it operate on the Vault “shares” which represent a claim to ownership on a fraction of the Vault’s underlying holdings. (Specified in ERC4626).</p><p><a href="https://eips.ethereum.org/EIPS/eip-4626">ERC-4626: Tokenized Vaults</a></p><p><strong><em>balanceBefore : balanceBefore = totalAssets(), totalAssets </em></strong>function is overridden and inherited from ERC4626, which is returning DVT balance of contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*Ca_Dig_-wWZJhI4jNEc0dg.png" /></figure><blockquote><em>Can we somehow make</em> totalSupply != balanceOf(UnstoppableVault)??</blockquote><h4>Let’s begin with How “<em>totalSupply” for </em>calculated in <strong>UnstoppableVault</strong><em>?</em></h4><p><strong><em>totalSupply</em></strong> is amount of tokens are in Vault and it is inheriting from from ERC20, at below link there is the implementation of solmate ERC20.</p><p><a href="https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol">solmate/src/tokens/ERC20.sol at main · transmissions11/solmate</a></p><p>On line 13 public variable is declared named <strong><em>uint</em></strong> <strong><em>totalSupply</em></strong>.</p><p><strong><em>totalSupply</em></strong> is only modifed inside <strong><em>_mint </em></strong>and<strong><em> _burn </em></strong>function.</p><p>Now look for where <strong><em>totalSupply, _mint </em></strong>or<strong><em> _burn</em></strong> is invoked in ERC4626. Because Vault is inherited from ERC4626 and ERC4626 is inherited from ERC20.</p><p><a href="https://github.com/transmissions11/solmate/blob/main/src/mixins/ERC4626.sol">solmate/src/mixins/ERC4626.sol at main · transmissions11/solmate</a></p><p>No where <strong><em>totalSupply</em></strong> is changed directly but <strong><em>_mint</em></strong> and <strong><em>_burn </em></strong>function<strong><em> </em></strong>is called from <strong><em>deposit</em></strong>, <strong><em>withdraw</em></strong>, <strong><em>redeem</em></strong> and <strong><em>mint.</em></strong></p><p>But you can see inside <strong><em>deposit</em></strong> and <strong><em>mint,</em></strong> <strong><em>asset.safeTransferFrom</em></strong> is invoked which will transfer DVT token from <strong><em>msg.sender</em></strong> to <strong><em>this</em></strong> contract and it is minting same ammout of token to have internal calcuation of <strong><em>totalSupply</em></strong>.</p><p>Similarly, inside <strong><em>redeem</em></strong> and <strong><em>withdraw</em></strong> functions it is burning the token and sending the same ammount to <strong><em>reciver</em></strong> by invoking<strong><em> asset.safeTansfer.</em></strong></p><p>In this way <strong><em>totalSupply</em></strong> is always in sync with <strong><em>asset.balanceOf</em></strong></p><p>There is no chance that we can break the sync of <strong><em>totalSupply</em></strong> and <strong><em>balanceOf </em></strong>from Vault.</p><h4>Now Let’s Look for How “balanceOf” is calculated in DVT Token.</h4><p><strong><em>balanceOf</em></strong> is inherited from ERC20.</p><p><a href="https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol">solmate/src/tokens/ERC20.sol at main · transmissions11/solmate</a></p><p>On line 33 public <strong><em>balanceOf </em></strong>declared, a mapping of address to balance.</p><p><strong><em>balanceOf</em></strong> is modified in <strong><em>transfer</em></strong>, <strong><em>transferFrom</em></strong>, <strong><em>_mint</em></strong> and <strong><em>_burn</em></strong> function.</p><p>If we transfer some tokens from a account holding DVT token to the Vault then <strong><em>balanceOf </em></strong>will increase but <strong><em>totalSupply</em></strong> in Vault remains the same because <strong><em>totalSupply</em></strong> is not related to DVT contract by any means.</p><p><strong>And this is how we can break the sync between <em>balanceOf</em> and <em>totalSupply </em>and it will stop offering flash loan permanently.</strong></p><p>Write solution in test/unstoppable.challaenge.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*Jl9uayANo5WA0z7_mKiKEw.png" /></figure><p>Run command &gt; <strong><em>yarn run unstopabble</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*VXG5LgaQnkEJ_oqBzujq3Q.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7651c96f35d2" width="1" height="1" alt=""><hr><p><a href="https://coinsbench.com/1-unstoppable-damn-vulnerable-defi-7651c96f35d2">#1. Unstoppable — Damn Vulnerable DeFi</a> was originally published in <a href="https://coinsbench.com/">CoinsBench</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>