-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
198 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "amount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "userData", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "receiveFlashLoan", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[ | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "recipient", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": false, | ||
"internalType": "uint256", | ||
"name": "amount", | ||
"type": "uint256" | ||
} | ||
], | ||
"name": "OsTokenFlashLoan", | ||
"type": "event" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "address", | ||
"name": "recipient", | ||
"type": "address" | ||
}, | ||
{ | ||
"internalType": "uint256", | ||
"name": "amount", | ||
"type": "uint256" | ||
}, | ||
{ | ||
"internalType": "bytes", | ||
"name": "userData", | ||
"type": "bytes" | ||
} | ||
], | ||
"name": "flashLoan", | ||
"outputs": [], | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
pragma solidity ^0.8.22; | ||
|
||
/** | ||
* @title IOsTokenFlashLoanRecipient | ||
* @author StakeWise | ||
* @notice Interface for OsTokenFlashLoanRecipient contract | ||
*/ | ||
interface IOsTokenFlashLoanRecipient { | ||
/** | ||
* @notice Receive flash loan hook | ||
* @param amount The osToken flash loan amount | ||
* @param userData Arbitrary data passed to the hook | ||
*/ | ||
function receiveFlashLoan(uint256 amount, bytes memory userData) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
pragma solidity ^0.8.22; | ||
|
||
/** | ||
* @title IOsTokenFlashLoans | ||
* @author StakeWise | ||
* @notice Interface for OsTokenFlashLoans contract | ||
*/ | ||
interface IOsTokenFlashLoans { | ||
/** | ||
* @notice Event emitted on position creation | ||
* @param recipient The address of the recipient | ||
* @param amount The flashLoan osToken shares amount | ||
*/ | ||
event OsTokenFlashLoan(address indexed recipient, uint256 amount); | ||
|
||
/** | ||
* @notice Flash loan OsToken shares | ||
* @param recipient The address of the recipient | ||
* @param amount The flashLoan osToken shares amount | ||
* @param userData Arbitrary data passed to the `IOsTokenFlashLoanRecipient.receiveFlashLoan` function | ||
*/ | ||
function flashLoan(address recipient, uint256 amount, bytes memory userData) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
pragma solidity ^0.8.22; | ||
|
||
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
import {ReentrancyGuard} from '@openzeppelin/contracts/utils/ReentrancyGuard.sol'; | ||
import {IOsTokenFlashLoans} from '../interfaces/IOsTokenFlashLoans.sol'; | ||
import {IOsTokenFlashLoanRecipient} from '../interfaces/IOsTokenFlashLoanRecipient.sol'; | ||
import {IOsToken} from '../interfaces/IOsToken.sol'; | ||
import {Errors} from '../libraries/Errors.sol'; | ||
|
||
/** | ||
* @title OsTokenFlashLoans | ||
* @author StakeWise | ||
* @notice Handles OsToken flash loans | ||
*/ | ||
contract OsTokenFlashLoans is ReentrancyGuard, IOsTokenFlashLoans { | ||
uint256 private constant _maxFlashLoanAmount = 1_000_000 ether; | ||
address private immutable _osToken; | ||
|
||
/** | ||
* @dev Constructor | ||
* @param osToken The address of the OsToken contract | ||
*/ | ||
constructor(address osToken) ReentrancyGuard() { | ||
_osToken = osToken; | ||
} | ||
|
||
/// @inheritdoc IOsTokenFlashLoans | ||
function flashLoan( | ||
address recipient, | ||
uint256 osTokenShares, | ||
bytes memory userData | ||
) external override nonReentrant { | ||
// check if not more than max flash loan amount requested | ||
if (osTokenShares == 0 || osTokenShares > _maxFlashLoanAmount) { | ||
revert Errors.InvalidShares(); | ||
} | ||
|
||
// get current balance | ||
uint256 preLoanBalance = IERC20(_osToken).balanceOf(address(this)); | ||
|
||
// mint OsToken shares for the recipient | ||
IOsToken(_osToken).mint(recipient, osTokenShares); | ||
|
||
// execute callback | ||
IOsTokenFlashLoanRecipient(recipient).receiveFlashLoan(osTokenShares, userData); | ||
|
||
// get post loan balance | ||
uint256 postLoanBalance = IERC20(_osToken).balanceOf(address(this)); | ||
|
||
// check if the amount was repaid | ||
if (postLoanBalance < preLoanBalance + osTokenShares) { | ||
revert Errors.FlashLoanFailed(); | ||
} | ||
|
||
// burn OsToken shares | ||
IOsToken(address(_osToken)).burn(address(this), osTokenShares); | ||
|
||
// emit event | ||
emit OsTokenFlashLoan(recipient, osTokenShares); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters