Skip to content

Commit c62f54d

Browse files
authored
Update flash-loans.md
updating imports to match with balancer-v2-monorepo And add the return loan logic
1 parent 6ad1f36 commit c62f54d

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/guides/arbitrageurs/flash-loans.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ Since the Vault holds all tokens for all pools, the consolidated token balances
99
```solidity
1010
pragma solidity ^0.7.0;
1111
12-
import "@balancer-labs/v2-vault/contracts/interfaces/IVault.sol";
13-
import "@balancer-labs/v2-vault/contracts/interfaces/IFlashLoanRecipient.sol";
12+
/*
13+
* "@balancer-labs/v2-interfaces/contracts" is refering to "balancer-v2-monorepo/pkg/interfaces/contracts"
14+
*/
15+
import {IVault} from "@balancer-labs/v2-interfaces/contracts/vault/IVault.sol";
16+
import {IFlashLoanRecipient} from "@balancer-labs/v2-interfaces/contracts/vault/IFlashLoanRecipient.sol";
17+
import {IERC20} from "@balancer-labs/v2-interfaces/contracts/solidity-utils/openzeppelin/IERC20.sol";
18+
1419
1520
contract FlashLoanRecipient is IFlashLoanRecipient {
16-
IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
21+
IVault private constant vault = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);
1722
18-
function makeFlashLoan(
19-
IERC20[] memory tokens,
20-
uint256[] memory amounts,
21-
bytes memory userData
22-
) external {
23-
vault.flashLoan(this, tokens, amounts, userData);
23+
function makeFlashLoan(IERC20[] memory tokens, uint256[] memory amounts, bytes memory userData) external {
24+
vault.flashLoan(this, tokens, amounts, userData);
2425
}
2526
2627
function receiveFlashLoan(
@@ -29,8 +30,23 @@ contract FlashLoanRecipient is IFlashLoanRecipient {
2930
uint256[] memory feeAmounts,
3031
bytes memory userData
3132
) external override {
32-
require(msg.sender == vault);
33-
...
33+
require(msg.sender == address(vault));
34+
35+
// This contract now has the funds requested.
36+
37+
// Your logic goes here...
38+
39+
/*
40+
At the end of your logic above, this contract owes
41+
the flashloaned amounts + feeAmounts.
42+
Therefore ensure your contract has enough to repay these amounts.
43+
*/
44+
45+
// Return loan
46+
for (uint256 i = 0; i < tokens.length; i++) {
47+
tokens[i].transfer(address(vault), amounts[i] + feeAmounts[i]);
48+
}
3449
}
3550
}
51+
3652
```

0 commit comments

Comments
 (0)