Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: format all files #11

Merged
merged 15 commits into from
Jan 10, 2024
Merged
14 changes: 5 additions & 9 deletions lendmixer/src/interface/IERC3156FlashBorrower.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface IERC3156FlashBorrower {

interface IERC3156FlashBorrower {
/**
* @dev Receive a flash loan.
* @param initiator The initiator of the loan.
Expand All @@ -11,11 +11,7 @@ interface IERC3156FlashBorrower {
* @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);
}
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data)
external
returns (bytes32);
}
28 changes: 12 additions & 16 deletions lendmixer/src/interface/ILendMixer.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { AssetToken } from "../../src/lending/asset.sol";
import { IERC3156FlashBorrower } from "./IERC3156FlashBorrower.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {AssetToken} from "../../src/lending/asset.sol";
import {IERC3156FlashBorrower} from "./IERC3156FlashBorrower.sol";

interface ILendMixer {
function deposit(ERC20 token, uint256 amount) external;
function withdraw(AssetToken assetToken, uint256 amount) external;
function maxFlashLoan(address token) external returns(uint256);
function flashFee(address token, uint256 amount) external returns(uint256);
function flashLoan(
address receiver,
address token,
uint256 amount,
bytes calldata data
) external returns(bool);
function setAssetTokenConfig(ERC20 token, bool permission) external returns(AssetToken);
function isSupportToken(ERC20 token) external view returns(bool);
function getAssetTokenFromToken(ERC20 token) external view returns(AssetToken);
function version() external returns(string memory);
}
function maxFlashLoan(address token) external returns (uint256);
function flashFee(address token, uint256 amount) external returns (uint256);
function flashLoan(address receiver, address token, uint256 amount, bytes calldata data) external returns (bool);
function setAssetTokenConfig(ERC20 token, bool permission) external returns (AssetToken);
function isSupportToken(ERC20 token) external view returns (bool);
function getAssetTokenFromToken(ERC20 token) external view returns (AssetToken);
function version() external returns (string memory);
}
2 changes: 1 addition & 1 deletion lendmixer/src/interface/IProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ interface IProxy {
function upgradeTo(address _implementation) external;
function getAdmin() external;
function getImplementation() external;
}
}
27 changes: 10 additions & 17 deletions lendmixer/src/lending/asset.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.20;

import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract AssetToken is ERC20 {
/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -31,7 +31,7 @@ contract AssetToken is ERC20 {
MODIFIER
//////////////////////////////////////////////////////////////*/
modifier onlyLendMixer() {
if(msg.sender!=lendMixer) {
if (msg.sender != lendMixer) {
revert AssetToken_onlyLendMixer();
}
_;
Expand All @@ -40,24 +40,18 @@ contract AssetToken is ERC20 {
/*//////////////////////////////////////////////////////////////
CONSTUCTOR
//////////////////////////////////////////////////////////////*/
constructor(
address _lendMixer,
ERC20 _underlyingToken,
string memory _assetName,
string memory _assetSymbol
)
constructor(address _lendMixer, ERC20 _underlyingToken, string memory _assetName, string memory _assetSymbol)
ERC20(_assetName, _assetSymbol)
{
if(_lendMixer==address(0)) revert AssetToken_zeroAddressNotAllowed();
if (_lendMixer == address(0)) revert AssetToken_zeroAddressNotAllowed();
lendMixer = _lendMixer;

if(address(_underlyingToken)==address(0)) revert AssetToken_zeroAddressNotAllowed();
if (address(_underlyingToken) == address(0)) revert AssetToken_zeroAddressNotAllowed();
underlyingToken = _underlyingToken;

feeRate = feeInitial;
}


/*//////////////////////////////////////////////////////////////
FUNCTION
//////////////////////////////////////////////////////////////*/
Expand All @@ -72,18 +66,17 @@ contract AssetToken is ERC20 {
emit AssetToken__burnToken(account, amount);
}

function getUnderlyingToken() external view returns(ERC20){
function getUnderlyingToken() external view returns (ERC20) {
return underlyingToken;
}

function getFeeRate(uint256 amount) external view returns(uint256) {
function getFeeRate(uint256 amount) external view returns (uint256) {
return amount * feeRate / feePrecision;
}

function updateFeeRate(uint256 amount) external onlyLendMixer returns(uint256) {
function updateFeeRate(uint256 amount) external onlyLendMixer returns (uint256) {
uint256 originalFee = feeRate;
feeRate = amount;
emit AssetToken__updateFeeRate(originalFee, amount);
}

}
}
78 changes: 34 additions & 44 deletions lendmixer/src/lending/lendMixer.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;

import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import { AssetToken } from "../lending/asset.sol";
import { Storage } from "../proxy/storage.sol";
import { IERC3156FlashBorrower } from "../interface/IERC3156FlashBorrower.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {AssetToken} from "../lending/asset.sol";
import {Storage} from "../proxy/storage.sol";
import {IERC3156FlashBorrower} from "../interface/IERC3156FlashBorrower.sol";

contract LendMixer is Storage, Initializable, ReentrancyGuard {
/*//////////////////////////////////////////////////////////////
Expand All @@ -32,10 +32,10 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
MODIFIER
//////////////////////////////////////////////////////////////*/
modifier nonFlashLoan(ERC20 token) {
if(isFlashLoanActive[token]) revert LendMixer__FlashloanNotComplete();
if (isFlashLoanActive[token]) revert LendMixer__FlashloanNotComplete();
_;
}

/*//////////////////////////////////////////////////////////////
CONSTUCTOR
//////////////////////////////////////////////////////////////*/
Expand All @@ -49,16 +49,16 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
//////////////////////////////////////////////////////////////*/
function initialize() external initializer {}

function deposit(ERC20 token, uint256 amount) external nonReentrant nonFlashLoan(token){
if(!isSupportToken(token)) revert LendMixer__TokenNotSupport();
function deposit(ERC20 token, uint256 amount) external nonReentrant nonFlashLoan(token) {
if (!isSupportToken(token)) revert LendMixer__TokenNotSupport();
token.transferFrom(msg.sender, address(this), amount);
AssetToken assetToken = tokenToAssetToken[token];
assetToken.mint(msg.sender, amount);
}

function withdraw(AssetToken assetToken, uint256 amount) external nonReentrant {
ERC20 token = assetToken.getUnderlyingToken();
if(!isSupportToken(token)) revert LendMixer__TokenNotSupport();
if (!isSupportToken(token)) revert LendMixer__TokenNotSupport();
uint256 totalSupply = assetToken.totalSupply();
uint256 share = amount * token.balanceOf(address(this)) / totalSupply;
assetToken.burn(msg.sender, amount);
Expand All @@ -70,10 +70,8 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
* @param token The loan currency.
* @return The amount of `token` that can be borrowed.
*/
function maxFlashLoan(
address token
) external view returns (uint256) {
if(!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
function maxFlashLoan(address token) external view returns (uint256) {
if (!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
return ERC20(token).balanceOf(address(this));
}

Expand All @@ -83,11 +81,8 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
* @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) {
if(!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
function flashFee(address token, uint256 amount) external view returns (uint256) {
if (!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
return amount * 3 / 1000;
}

Expand All @@ -98,44 +93,39 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
* @param amount The amount of tokens lent.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
*/
function flashLoan(
address receiver,
address token,
uint256 amount,
bytes calldata data
) external returns (bool) {
function flashLoan(address receiver, address token, uint256 amount, bytes calldata data) external returns (bool) {
// Receiver Validati
if(!isContract(address(receiver))) revert LendMixer__ReceiverNotContract();
if (!isContract(address(receiver))) revert LendMixer__ReceiverNotContract();
// Token Validation
if(!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
if (!isSupportToken(ERC20(token))) revert LendMixer__TokenNotSupport();
// Amount Validation
uint256 balanceBefore = ERC20(token).balanceOf(address(this));
if(amount > balanceBefore) revert LendMixer__FlashLoanAmountNotEnough();

if (amount > balanceBefore) revert LendMixer__FlashLoanAmountNotEnough();

emit LendMixer__FlashLoanService(address(receiver), ERC20(token), amount);
isFlashLoanActive[ERC20(token)] = true;
ERC20(token).transfer(address(receiver), amount);

AssetToken assetToken = tokenToAssetToken[ERC20(token)];
uint256 fee = assetToken.getFeeRate(amount);
uint256 fee = assetToken.getFeeRate(amount);
bytes32 result = IERC3156FlashBorrower(receiver).onFlashLoan(msg.sender, token, amount, fee, data);

if(result!=keccak256("ERC3156FlashBorrower.onFlashLoan")) revert LendMixer__FlashLoanReceiverBadImplement();
if (result != keccak256("ERC3156FlashBorrower.onFlashLoan")) revert LendMixer__FlashLoanReceiverBadImplement();

uint256 balanceAfter = ERC20(token).balanceOf(address(this));

if(balanceAfter < balanceBefore+fee) revert LendMixer__FlashLoanPaidBackNotEnough();
if (balanceAfter < balanceBefore + fee) revert LendMixer__FlashLoanPaidBackNotEnough();

isFlashLoanActive[ERC20(token)] = false;

return true;
}

function setAssetTokenConfig(ERC20 token, bool permission) external returns(AssetToken){
function setAssetTokenConfig(ERC20 token, bool permission) external returns (AssetToken) {
AssetToken assetToken = tokenToAssetToken[token];
if(permission) {
if(address(assetToken)!=address(0)) {
if (permission) {
if (address(assetToken) != address(0)) {
revert LendMixer__TokenAlreadyUnderService();
}

Expand All @@ -151,24 +141,24 @@ contract LendMixer is Storage, Initializable, ReentrancyGuard {
return assetToken;
}

function isSupportToken(ERC20 token) public view returns(bool) {
function isSupportToken(ERC20 token) public view returns (bool) {
AssetToken assetToken = tokenToAssetToken[token];
return address(assetToken) != address(0);
}

function getAssetTokenFromToken(ERC20 token) external view returns(AssetToken) {
function getAssetTokenFromToken(ERC20 token) external view returns (AssetToken) {
return tokenToAssetToken[token];
}

function isContract(address addr) internal view returns(bool){
function isContract(address addr) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(addr)
}
return size!=0;
return size != 0;
}

function version() external pure returns(string memory){
function version() external pure returns (string memory) {
return "1.0.0";
}
}
}
Loading