Skip to content

Commit

Permalink
Update flash-loans.md
Browse files Browse the repository at this point in the history
updating imports to match with balancer-v2-monorepo
And add the return loan logic
  • Loading branch information
yehoudaB authored Dec 13, 2023
1 parent 6ad1f36 commit c62f54d
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions docs/guides/arbitrageurs/flash-loans.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ Since the Vault holds all tokens for all pools, the consolidated token balances
```solidity
pragma solidity ^0.7.0;
import "@balancer-labs/v2-vault/contracts/interfaces/IVault.sol";
import "@balancer-labs/v2-vault/contracts/interfaces/IFlashLoanRecipient.sol";
/*
* "@balancer-labs/v2-interfaces/contracts" is refering to "balancer-v2-monorepo/pkg/interfaces/contracts"
*/
import {IVault} from "@balancer-labs/v2-interfaces/contracts/vault/IVault.sol";
import {IFlashLoanRecipient} from "@balancer-labs/v2-interfaces/contracts/vault/IFlashLoanRecipient.sol";
import {IERC20} from "@balancer-labs/v2-interfaces/contracts/solidity-utils/openzeppelin/IERC20.sol";
contract FlashLoanRecipient is IFlashLoanRecipient {
IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
IVault private constant vault = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
function makeFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external {
vault.flashLoan(this, tokens, amounts, userData);
function makeFlashLoan(IERC20[] memory tokens, uint256[] memory amounts, bytes memory userData) external {
vault.flashLoan(this, tokens, amounts, userData);
}
function receiveFlashLoan(
Expand All @@ -29,8 +30,23 @@ contract FlashLoanRecipient is IFlashLoanRecipient {
uint256[] memory feeAmounts,
bytes memory userData
) external override {
require(msg.sender == vault);
...
require(msg.sender == address(vault));
// This contract now has the funds requested.
// Your logic goes here...
/*
At the end of your logic above, this contract owes
the flashloaned amounts + feeAmounts.
Therefore ensure your contract has enough to repay these amounts.
*/
// Return loan
for (uint256 i = 0; i < tokens.length; i++) {
tokens[i].transfer(address(vault), amounts[i] + feeAmounts[i]);
}
}
}
```

0 comments on commit c62f54d

Please sign in to comment.