-
-
Save toastedsteaksandwich/2e92b9c259a223e3f242974d766ebd27 to your computer and use it in GitHub Desktop.
n-lend smart contract
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
// Sources flattened with hardhat v2.6.3 https://hardhat.org | |
// File @openzeppelin/contracts/security/ReentrancyGuard.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Contract module that helps prevent reentrant calls to a function. | |
* | |
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier | |
* available, which can be applied to functions to make sure there are no nested | |
* (reentrant) calls to them. | |
* | |
* Note that because there is a single `nonReentrant` guard, functions marked as | |
* `nonReentrant` may not call one another. This can be worked around by making | |
* those functions `private`, and then adding `external` `nonReentrant` entry | |
* points to them. | |
* | |
* TIP: If you would like to learn more about reentrancy and alternative ways | |
* to protect against it, check out our blog post | |
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul]. | |
*/ | |
abstract contract ReentrancyGuard { | |
// Booleans are more expensive than uint256 or any type that takes up a full | |
// word because each write operation emits an extra SLOAD to first read the | |
// slot's contents, replace the bits taken up by the boolean, and then write | |
// back. This is the compiler's defense against contract upgrades and | |
// pointer aliasing, and it cannot be disabled. | |
// The values being non-zero value makes deployment a bit more expensive, | |
// but in exchange the refund on every call to nonReentrant will be lower in | |
// amount. Since refunds are capped to a percentage of the total | |
// transaction's gas, it is best to keep them low in cases like this one, to | |
// increase the likelihood of the full refund coming into effect. | |
uint256 private constant _NOT_ENTERED = 1; | |
uint256 private constant _ENTERED = 2; | |
uint256 private _status; | |
constructor() { | |
_status = _NOT_ENTERED; | |
} | |
/** | |
* @dev Prevents a contract from calling itself, directly or indirectly. | |
* Calling a `nonReentrant` function from another `nonReentrant` | |
* function is not supported. It is possible to prevent this from happening | |
* by making the `nonReentrant` function external, and make it call a | |
* `private` function that does the actual work. | |
*/ | |
modifier nonReentrant() { | |
// On the first call to nonReentrant, _notEntered will be true | |
require(_status != _ENTERED, "ReentrancyGuard: reentrant call"); | |
// Any calls to nonReentrant after this point will fail | |
_status = _ENTERED; | |
_; | |
// By storing the original value once again, a refund is triggered (see | |
// https://eips.ethereum.org/EIPS/eip-2200) | |
_status = _NOT_ENTERED; | |
} | |
} | |
// File @openzeppelin/contracts/utils/Context.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Provides information about the current execution context, including the | |
* sender of the transaction and its data. While these are generally available | |
* via msg.sender and msg.data, they should not be accessed in such a direct | |
* manner, since when dealing with meta-transactions the account sending and | |
* paying for execution may not be the actual sender (as far as an application | |
* is concerned). | |
* | |
* This contract is only required for intermediate, library-like contracts. | |
*/ | |
abstract contract Context { | |
function _msgSender() internal view virtual returns (address) { | |
return msg.sender; | |
} | |
function _msgData() internal view virtual returns (bytes calldata) { | |
return msg.data; | |
} | |
} | |
// File @openzeppelin/contracts/access/Ownable.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Contract module which provides a basic access control mechanism, where | |
* there is an account (an owner) that can be granted exclusive access to | |
* specific functions. | |
* | |
* By default, the owner account will be the one that deploys the contract. This | |
* can later be changed with {transferOwnership}. | |
* | |
* This module is used through inheritance. It will make available the modifier | |
* `onlyOwner`, which can be applied to your functions to restrict their use to | |
* the owner. | |
*/ | |
abstract contract Ownable is Context { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev Initializes the contract setting the deployer as the initial owner. | |
*/ | |
constructor() { | |
_setOwner(_msgSender()); | |
} | |
/** | |
* @dev Returns the address of the current owner. | |
*/ | |
function owner() public view virtual returns (address) { | |
return _owner; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(owner() == _msgSender(), "Ownable: caller is not the owner"); | |
_; | |
} | |
/** | |
* @dev Leaves the contract without owner. It will not be possible to call | |
* `onlyOwner` functions anymore. Can only be called by the current owner. | |
* | |
* NOTE: Renouncing ownership will leave the contract without an owner, | |
* thereby removing any functionality that is only available to the owner. | |
*/ | |
function renounceOwnership() public virtual onlyOwner { | |
_setOwner(address(0)); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
* Can only be called by the current owner. | |
*/ | |
function transferOwnership(address newOwner) public virtual onlyOwner { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
_setOwner(newOwner); | |
} | |
function _setOwner(address newOwner) private { | |
address oldOwner = _owner; | |
_owner = newOwner; | |
emit OwnershipTransferred(oldOwner, newOwner); | |
} | |
} | |
// File @openzeppelin/contracts/utils/introspection/IERC165.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Interface of the ERC165 standard, as defined in the | |
* https://eips.ethereum.org/EIPS/eip-165[EIP]. | |
* | |
* Implementers can declare support of contract interfaces, which can then be | |
* queried by others ({ERC165Checker}). | |
* | |
* For an implementation, see {ERC165}. | |
*/ | |
interface IERC165 { | |
/** | |
* @dev Returns true if this contract implements the interface defined by | |
* `interfaceId`. See the corresponding | |
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section] | |
* to learn more about how these ids are created. | |
* | |
* This function call must use less than 30 000 gas. | |
*/ | |
function supportsInterface(bytes4 interfaceId) external view returns (bool); | |
} | |
// File @openzeppelin/contracts/token/ERC721/IERC721.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @dev Required interface of an ERC721 compliant contract. | |
*/ | |
interface IERC721 is IERC165 { | |
/** | |
* @dev Emitted when `tokenId` token is transferred from `from` to `to`. | |
*/ | |
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId); | |
/** | |
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token. | |
*/ | |
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId); | |
/** | |
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets. | |
*/ | |
event ApprovalForAll(address indexed owner, address indexed operator, bool approved); | |
/** | |
* @dev Returns the number of tokens in ``owner``'s account. | |
*/ | |
function balanceOf(address owner) external view returns (uint256 balance); | |
/** | |
* @dev Returns the owner of the `tokenId` token. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must exist. | |
*/ | |
function ownerOf(uint256 tokenId) external view returns (address owner); | |
/** | |
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients | |
* are aware of the ERC721 protocol to prevent tokens from being forever locked. | |
* | |
* Requirements: | |
* | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must exist and be owned by `from`. | |
* - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}. | |
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) external; | |
/** | |
* @dev Transfers `tokenId` token from `from` to `to`. | |
* | |
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible. | |
* | |
* Requirements: | |
* | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must be owned by `from`. | |
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function transferFrom( | |
address from, | |
address to, | |
uint256 tokenId | |
) external; | |
/** | |
* @dev Gives permission to `to` to transfer `tokenId` token to another account. | |
* The approval is cleared when the token is transferred. | |
* | |
* Only a single account can be approved at a time, so approving the zero address clears previous approvals. | |
* | |
* Requirements: | |
* | |
* - The caller must own the token or be an approved operator. | |
* - `tokenId` must exist. | |
* | |
* Emits an {Approval} event. | |
*/ | |
function approve(address to, uint256 tokenId) external; | |
/** | |
* @dev Returns the account approved for `tokenId` token. | |
* | |
* Requirements: | |
* | |
* - `tokenId` must exist. | |
*/ | |
function getApproved(uint256 tokenId) external view returns (address operator); | |
/** | |
* @dev Approve or remove `operator` as an operator for the caller. | |
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller. | |
* | |
* Requirements: | |
* | |
* - The `operator` cannot be the caller. | |
* | |
* Emits an {ApprovalForAll} event. | |
*/ | |
function setApprovalForAll(address operator, bool _approved) external; | |
/** | |
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`. | |
* | |
* See {setApprovalForAll} | |
*/ | |
function isApprovedForAll(address owner, address operator) external view returns (bool); | |
/** | |
* @dev Safely transfers `tokenId` token from `from` to `to`. | |
* | |
* Requirements: | |
* | |
* - `from` cannot be the zero address. | |
* - `to` cannot be the zero address. | |
* - `tokenId` token must exist and be owned by `from`. | |
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}. | |
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function safeTransferFrom( | |
address from, | |
address to, | |
uint256 tokenId, | |
bytes calldata data | |
) external; | |
} | |
// File @openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional enumeration extension | |
* @dev See https://eips.ethereum.org/EIPS/eip-721 | |
*/ | |
interface IERC721Enumerable is IERC721 { | |
/** | |
* @dev Returns the total amount of tokens stored by the contract. | |
*/ | |
function totalSupply() external view returns (uint256); | |
/** | |
* @dev Returns a token ID owned by `owner` at a given `index` of its token list. | |
* Use along with {balanceOf} to enumerate all of ``owner``'s tokens. | |
*/ | |
function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId); | |
/** | |
* @dev Returns a token ID at a given `index` of all the tokens stored by the contract. | |
* Use along with {totalSupply} to enumerate all tokens. | |
*/ | |
function tokenByIndex(uint256 index) external view returns (uint256); | |
} | |
// File @openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol@v4.3.1 | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension | |
* @dev See https://eips.ethereum.org/EIPS/eip-721 | |
*/ | |
interface IERC721Metadata is IERC721 { | |
/** | |
* @dev Returns the token collection name. | |
*/ | |
function name() external view returns (string memory); | |
/** | |
* @dev Returns the token collection symbol. | |
*/ | |
function symbol() external view returns (string memory); | |
/** | |
* @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token. | |
*/ | |
function tokenURI(uint256 tokenId) external view returns (string memory); | |
} | |
// File contracts/interfaces/IN.sol | |
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.6; | |
interface IN is IERC721Enumerable, IERC721Metadata { | |
function getFirst(uint256 tokenId) external view returns (uint256); | |
function getSecond(uint256 tokenId) external view returns (uint256); | |
function getThird(uint256 tokenId) external view returns (uint256); | |
function getFourth(uint256 tokenId) external view returns (uint256); | |
function getFifth(uint256 tokenId) external view returns (uint256); | |
function getSixth(uint256 tokenId) external view returns (uint256); | |
function getSeventh(uint256 tokenId) external view returns (uint256); | |
function getEight(uint256 tokenId) external view returns (uint256); | |
} | |
// File contracts/core/NLend.sol | |
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.8.4; | |
interface IReceiver { | |
function receiveN(uint256 id, address owner, bytes calldata data) external payable; | |
} | |
interface TokenOrNFT { | |
function transferFrom(address from, address to, uint256 amountOrId) external; | |
} | |
contract nLend is ReentrancyGuard, Ownable { | |
//State variables | |
IN public immutable n; | |
uint256 public adminFee; | |
mapping(uint256 => address) public nOwner; | |
mapping(uint256 => uint256) public borrowPrice; | |
//Do I need arrays with the current NFTs? | |
//With it, we easily find which NFTs are available for loaning | |
//Events | |
event Deposit(address indexed _from, uint256 indexed _id, uint256 _value); | |
event Withdraw(address indexed _from, uint256 indexed _id); | |
event FlashBorrow(address indexed borrower,address indexed recipient, uint256 indexed _id, uint256 _value); | |
event borrowFeeUpdated(uint256 indexed id, uint256 price); | |
event adminFeeUpdated(uint256 indexed newAdminFee); | |
constructor( | |
IN _n, | |
uint256 _adminFee | |
) | |
{ | |
n = _n; | |
adminFee = _adminFee; | |
} | |
/* | |
* unprotected write functions | |
* All functions should be protected with a reentrancy guard | |
* None should have access control | |
*/ | |
/** | |
* @notice Allow a n token holder to bulk mint tokens with id of their n tokens' id | |
* @param id The n with ID id to deposit | |
* @param price The price the owner wants to set to borrow their n, in wei | |
* @notice Is checks-effects followed here? Yes | |
*/ | |
function deposit(uint256 id, uint256 price) external nonReentrant { | |
//check if the msg.sender is the owner of id N | |
require(n.ownerOf(id) == msg.sender, "nLend:INVALID_OWNER"); | |
//credit the owner with the ID in the nOwners mapping | |
nOwner[id] = msg.sender; | |
//assign the NFT the owner's price | |
borrowPrice[id] = price; | |
//transfer the NFT from the owner to this contract | |
//NFT transfer does not return a bool so no need to require | |
//we don't want safeTransferFrom since we don't want a callback | |
n.transferFrom(msg.sender, address(this), id); | |
//emit a deposit event | |
emit Deposit(msg.sender, id, price); | |
} | |
/** | |
* @notice Allow user's to withdraw their deposited n NFT with ID id | |
* @param id The n with ID id to withdraw | |
* @notice Is checks-effects followed here? Yes | |
*/ | |
function withdraw(uint256 id) external nonReentrant { | |
//check if the msg.sender is the owner of id N | |
require(nOwner[id] == msg.sender, "nLend:INVALID_OWNER"); | |
//clear the ownership | |
nOwner[id] = address(0); | |
//clear the price | |
borrowPrice[id] = 0 ether; | |
//transfer the NFT from this contract to the owner | |
//NFT transfer does not return a bool so no need to require | |
//we don't want safeTransferFrom since we don't want a callback | |
n.transferFrom(address(this), msg.sender, id); | |
//emit a deposit event | |
emit Withdraw(msg.sender,id); | |
} | |
/** | |
* @notice Flash borrow an n NFT with ID id | |
* @param id The n NFT with ID id you want to borrow | |
* @notice The receive contract needs to approve this contract in it's logic | |
* @notice You can grief an unprotected receiver that has enough ETH in it + working logic to execute | |
*/ | |
function flashBorrow(uint256 id, address receiver, bytes calldata data) external payable nonReentrant { | |
//check that we have the NFT - this is to say that there is some owner in our mapping | |
require(nOwner[id] != address(0)); | |
//check that the msg.value is enough incl fees | |
uint amountReceived = msg.value; | |
require(amountReceived >= borrowPrice[id] + (borrowPrice[id]*adminFee)/10000); | |
//duduct admin fees and borrow fee from the amountReceived | |
amountReceived -= borrowPrice[id] + (borrowPrice[id]*adminFee)/10000; | |
//transfer the NFT | |
//NFT transfer does not return a bool so no need to require | |
//we don't want safeTransferFrom since we don't want a callback | |
n.transferFrom(address(this), receiver, id); | |
//send the rest of the msg.value to the receiver | |
//Do the things | |
IReceiver(receiver).receiveN{value: amountReceived}(id, nOwner[id], data); | |
//transfer the NFT back | |
//NFT transfer does not return a bool so no need to require | |
//we don't want safeTransferFrom since we don't want a callback | |
n.transferFrom(receiver, address(this), id); | |
//send the borrower fees to the id owner | |
payable(nOwner[id]).transfer(borrowPrice[id]); | |
//emit an event - should we use the tracked msg.value? | |
emit FlashBorrow(msg.sender, receiver, id, msg.value); | |
} | |
/** | |
* @notice Allows the owner to withdraw all admin fees | |
*/ | |
function withdrawAll() external onlyOwner { | |
payable(owner()).transfer(address(this).balance); | |
} | |
/** | |
* @notice Allows the owner to withdraw all admin fees | |
*/ | |
function updateAdminFee(uint256 newAdminFee) external onlyOwner { | |
require(newAdminFee <= 10000); | |
adminFee = newAdminFee; | |
emit adminFeeUpdated(newAdminFee); | |
} | |
/** | |
* @notice Allows an owner to update their borrow fee | |
*/ | |
function updateBorrowFee(uint256 id, uint256 price) external nonReentrant { | |
require(msg.sender == nOwner[id]); | |
borrowPrice[id] = price; | |
emit borrowFeeUpdated(id, price); | |
} | |
/** | |
* @notice Allows an owner to recover any token or NFT other than n | |
*/ | |
function recovertokenOrNFT(address tokenOrNFT, uint256 amountOrAmount) external nonReentrant onlyOwner { | |
require(tokenOrNFT != n.address); | |
TokenOrNFT(token).transferFrom(address(this), owner(), amountOrAmount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment