-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into batch-call
- Loading branch information
Showing
16 changed files
with
478 additions
and
11 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,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Interface of the ERC1271 standard signature validation method for | ||
* contracts as defined in https://eips.ethereum.org/EIPS/eip-1271[ERC-1271]. | ||
*/ | ||
interface IERC1271 { | ||
/** | ||
* @dev Should return whether the signature provided is valid for the provided data | ||
* @param hash Hash of the data to be signed | ||
* @param signature Signature byte array associated with _data | ||
*/ | ||
function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue); | ||
} |
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,66 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Interface of the ERC3156 FlashBorrower, as defined in | ||
* https://eips.ethereum.org/EIPS/eip-3156[ERC-3156]. | ||
*/ | ||
interface IERC3156FlashBorrower { | ||
/** | ||
* @dev Receive a flash loan. | ||
* @param initiator The initiator of the loan. | ||
* @param token The loan currency. | ||
* @param amount The amount of tokens lent. | ||
* @param fee The additional amount of tokens to repay. | ||
* @param data Arbitrary data structure, intended to contain user-defined parameters. | ||
* @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan" | ||
*/ | ||
function onFlashLoan( | ||
address initiator, | ||
address token, | ||
uint256 amount, | ||
uint256 fee, | ||
bytes calldata data | ||
) external returns (bytes32); | ||
} | ||
|
||
/** | ||
* @dev Interface of the ERC3156 FlashLender, as defined in | ||
* https://eips.ethereum.org/EIPS/eip-3156[ERC-3156]. | ||
*/ | ||
interface IERC3156FlashLender { | ||
/** | ||
* @dev The amount of currency available to be lended. | ||
* @param token The loan currency. | ||
* @return The amount of `token` that can be borrowed. | ||
*/ | ||
function maxFlashLoan( | ||
address token | ||
) external view returns (uint256); | ||
|
||
/** | ||
* @dev The fee to be charged for a given loan. | ||
* @param token The loan currency. | ||
* @param amount The amount of tokens lent. | ||
* @return The amount of `token` to be charged for the loan, on top of the returned principal. | ||
*/ | ||
function flashFee( | ||
address token, | ||
uint256 amount | ||
) external view returns (uint256); | ||
|
||
/** | ||
* @dev Initiate a flash loan. | ||
* @param receiver The receiver of the tokens in the loan, and the receiver of the callback. | ||
* @param token The loan currency. | ||
* @param amount The amount of tokens lent. | ||
* @param data Arbitrary data structure, intended to contain user-defined parameters. | ||
*/ | ||
function flashLoan( | ||
IERC3156FlashBorrower receiver, | ||
address token, | ||
uint256 amount, | ||
bytes calldata data | ||
) external returns (bool); | ||
} |
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: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../access/Ownable.sol"; | ||
import "../interfaces/IERC1271.sol"; | ||
import "../utils/cryptography/ECDSA.sol"; | ||
|
||
contract ERC1271WalletMock is Ownable, IERC1271 { | ||
constructor(address originalOwner) { | ||
transferOwnership(originalOwner); | ||
} | ||
|
||
function isValidSignature(bytes32 hash, bytes memory signature) public view override returns (bytes4 magicValue) { | ||
return ECDSA.recover(hash, signature) == owner() ? this.isValidSignature.selector : bytes4(0); | ||
} | ||
} |
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,54 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
|
||
import "../token/ERC20/IERC20.sol"; | ||
import "../interfaces/IERC3156.sol"; | ||
import "../utils/Address.sol"; | ||
|
||
/** | ||
* @dev WARNING: this IERC3156FlashBorrower mock implementation is for testing purposes ONLY. | ||
* Writing a secure flash lock borrower is not an easy task, and should be done with the utmost care. | ||
* This is not an example of how it should be done, and no pattern present in this mock should be considered secure. | ||
* Following best practices, always have your contract properly audited before using them to manipulate important funds on | ||
* live networks. | ||
*/ | ||
contract ERC3156FlashBorrowerMock is IERC3156FlashBorrower { | ||
bytes32 constant internal RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan"); | ||
|
||
bool immutable _enableApprove; | ||
bool immutable _enableReturn; | ||
|
||
event BalanceOf(address token, address account, uint256 value); | ||
event TotalSupply(address token, uint256 value); | ||
|
||
constructor(bool enableReturn, bool enableApprove) { | ||
_enableApprove = enableApprove; | ||
_enableReturn = enableReturn; | ||
} | ||
|
||
function onFlashLoan( | ||
address /*initiator*/, | ||
address token, | ||
uint256 amount, | ||
uint256 fee, | ||
bytes calldata data | ||
) public override returns (bytes32) { | ||
require(msg.sender == token); | ||
|
||
emit BalanceOf(token, address(this), IERC20(token).balanceOf(address(this))); | ||
emit TotalSupply(token, IERC20(token).totalSupply()); | ||
|
||
if (data.length > 0) { | ||
// WARNING: This code is for testing purposes only! Do not use. | ||
Address.functionCall(token, data); | ||
} | ||
|
||
if (_enableApprove) { | ||
IERC20(token).approve(token, amount + fee); | ||
} | ||
|
||
return _enableReturn ? RETURN_VALUE : bytes32(0); | ||
} | ||
} |
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: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
|
||
import "../token/ERC20/extensions/draft-ERC20FlashMint.sol"; | ||
|
||
contract ERC20FlashMintMock is ERC20FlashMint { | ||
constructor ( | ||
string memory name, | ||
string memory symbol, | ||
address initialAccount, | ||
uint256 initialBalance | ||
) ERC20(name, symbol) { | ||
_mint(initialAccount, initialBalance); | ||
} | ||
} |
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,13 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../utils/cryptography/SignatureChecker.sol"; | ||
|
||
contract SignatureCheckerMock { | ||
using SignatureChecker for address; | ||
|
||
function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) public view returns (bool) { | ||
return signer.isValidSignatureNow(hash, signature); | ||
} | ||
} |
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,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../../../interfaces/IERC3156.sol"; | ||
import "../ERC20.sol"; | ||
|
||
/** | ||
* @dev Implementation of the ERC3156 Flash loans extension, as defined in | ||
* https://eips.ethereum.org/EIPS/eip-3156[ERC-3156]. | ||
* | ||
* Adds the {flashLoan} method, which provides flash loan support at the token | ||
* level. By default there is no fee, but this can be changed by overriding {flashFee}. | ||
*/ | ||
abstract contract ERC20FlashMint is ERC20, IERC3156FlashLender { | ||
bytes32 constant private RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan"); | ||
|
||
/** | ||
* @dev Returns the maximum amount of tokens available for loan. | ||
* @param token The address of the token that is requested. | ||
* @return The amont of token that can be loaned. | ||
*/ | ||
function maxFlashLoan(address token) public view override returns (uint256) { | ||
return token == address(this) ? type(uint256).max - ERC20.totalSupply() : 0; | ||
} | ||
|
||
/** | ||
* @dev Returns the fee applied when doing flash loans. By default this | ||
* implementation has 0 fees. This function can be overloaded to make | ||
* the flash loan mechanism deflationary. | ||
* @param token The token to be flash loaned. | ||
* @param amount The amount of tokens to be loaned. | ||
* @return The fees applied to the corresponding flash loan. | ||
*/ | ||
function flashFee(address token, uint256 amount) public view virtual override returns (uint256) { | ||
require(token == address(this), "ERC20FlashMint: wrong token"); | ||
// silence warning about unused variable without the addition of bytecode. | ||
amount; | ||
return 0; | ||
} | ||
|
||
/** | ||
* @dev Performs a flash loan. New tokens are minted and sent to the | ||
* `receiver`, who is required to implement the {IERC3156FlashBorrower} | ||
* interface. By the end of the flash loan, the receiver is expected to own | ||
* amount + fee tokens and have them approved back to the token contract itself so | ||
* they can be burned. | ||
* @param receiver The receiver of the flash loan. Should implement the | ||
* {IERC3156FlashBorrower.onFlashLoan} interface. | ||
* @param token The token to be flash loaned. Only `address(this)` is | ||
* supported. | ||
* @param amount The amount of tokens to be loaned. | ||
* @param data An arbitrary datafield that is passed to the receiver. | ||
* @return `true` is the flash loan was successfull. | ||
*/ | ||
function flashLoan( | ||
IERC3156FlashBorrower receiver, | ||
address token, | ||
uint256 amount, | ||
bytes calldata data | ||
) | ||
public virtual override returns (bool) | ||
{ | ||
uint256 fee = flashFee(token, amount); | ||
_mint(address(receiver), amount); | ||
require(receiver.onFlashLoan(msg.sender, token, amount, fee, data) == RETURN_VALUE, "ERC20FlashMint: invalid return value"); | ||
uint256 currentAllowance = allowance(address(receiver), address(this)); | ||
require(currentAllowance >= amount + fee, "ERC20FlashMint: allowance does not allow refund"); | ||
_approve(address(receiver), address(this), currentAllowance - amount - fee); | ||
_burn(address(receiver), amount + fee); | ||
return true; | ||
} | ||
} |
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,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "./ECDSA.sol"; | ||
import "../Address.sol"; | ||
import "../../interfaces/IERC1271.sol"; | ||
|
||
/** | ||
* @dev Signature verification helper: Provide a single mechanism to verify both private-key (EOA) ECDSA signature and | ||
* ERC1271 contract sigantures. Using this instead of ECDSA.recover in your contract will make them compatible with | ||
* smart contract wallets such as Argent and Gnosis. | ||
* | ||
* Note: unlike ECDSA signatures, contract signature's are revocable, and the outcome of this function can thus change | ||
* through time. It could return true at block N and false at block N+1 (or the opposite). | ||
*/ | ||
library SignatureChecker { | ||
function isValidSignatureNow(address signer, bytes32 hash, bytes memory signature) internal view returns (bool) { | ||
if (Address.isContract(signer)) { | ||
try IERC1271(signer).isValidSignature(hash, signature) returns (bytes4 magicValue) { | ||
return magicValue == IERC1271(signer).isValidSignature.selector; | ||
} catch { | ||
return false; | ||
} | ||
} else { | ||
return ECDSA.recover(hash, signature) == signer; | ||
} | ||
} | ||
} |
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,15 @@ | ||
= Contracts Wizard | ||
:page-notoc: | ||
|
||
Not sure where to start? Use the interactive generator below to bootstrap your | ||
contract and learn about the components offered in OpenZeppelin Contracts. | ||
|
||
TIP: Place the resulting contract in your `contracts` directory in order to compile it with a tool like Hardhat or Truffle. Consider reading our guide on xref:learn::developing-smart-contracts.adoc[Developing Smart Contracts] for more guidance! | ||
|
||
++++ | ||
<script async defer src="https://openzeppelin-contracts-wizard.netlify.app/build/embed.js"></script> | ||
<oz-wizard style="display: block; min-height: 40rem;"></oz-wizard> | ||
++++ | ||
|
||
|
Oops, something went wrong.