FlashLoan

作者: 小吖么小一郎 | 来源:发表于2020-11-23 16:06 被阅读0次
pragma solidity ^0.6.2;

/*
    闪电贷
*/
contract FlashLoan {
    
    using SafeMath for uint256;
    using address_make_payable for address;
    uint256 interestPercentage = 1; // 借出金额收取1%利息
    ERC20 erc20;
    
    // 闪电贷-ETH
    function flashLoanByEth(address contractAddress,uint256 value) public payable{
        // 查询当前合约ETH余额
        uint256 beforeBalance = address(this).balance;
        require(beforeBalance >= value,"transfer value should be less than balance");
        // 利息
        uint256 interestValue = value.mul(interestPercentage).div(100); 
        address payable addr = contractAddress.make_payable();
        (bool success,) = addr.call{value: value}(abi.encodeWithSignature("implementFlashLoanByEth(uint256,uint256)",value,interestValue));
        require(success,"call is fail 1");
        //  查询当前合约ETH余额
        uint256 afterBalance = address(this).balance;
        // 合约初始余额 + 利息 = 结束余额
        require(beforeBalance + interestValue == afterBalance,"flashLoan is error");
    }
    // 闪电贷-ERC20
    function flashLoanByErc(address contractAddress,address ercContractAddress,uint256 value) public {
        erc20 = ERC20(ercContractAddress);
        uint256 beforeBalance = erc20.balanceOf(address(this));
        require(beforeBalance >= value,"transfer value should be less than balance");
        // 利息
        uint256 interestValue = value.mul(interestPercentage).div(100);
        (bool success,) = contractAddress.call(abi.encodeWithSignature("implementFlashLoanByErc(uint256,uint256,address)",value,interestValue,ercContractAddress));
        require(success,"call is fail 1");
        //  查询当前合约ERC20余额
        uint256 afterBalance = erc20.balanceOf(address(this));        
        // 合约初始余额 + 利息 = 结束余额
        require(beforeBalance + interestValue == afterBalance,"flashLoan is error");
        
    }    
        
    // 充值资金进来,作为可准许借出资产
    function receiveEth() public payable{
        
    }
    
    function repayEth(address accountAddress, uint256 asset) private {
        address payable addr = accountAddress.make_payable();
        addr.transfer(asset);
    }

}

contract SenderByFlashLoan{
    
    using address_make_payable for address;
    using SafeMath for uint256;
    ERC20 erc20;
    uint256 public flashLoanValue;
    
    function implementFlashLoanByEth(uint256 value,uint256 interestValue) public payable{
        // 添加业务
        
        // 借的资金加上利息,还回去
        (bool success,) = (msg.sender).call{value: value.add(interestValue)}(abi.encodeWithSignature("receiveEth()"));
        require(success,"call is fail 2");
    }
    
    function implementFlashLoanByErc(uint256 value,uint256 interestValue,address ercContractAddress) public{
        // 添加业务
        
        // 借的资金加上利息,还回去
        erc20 = ERC20(ercContractAddress);
        erc20.transfer(msg.sender,value.add(interestValue));
    }
    
    function getEthBalance(address addr) public view returns(uint256){
        return addr.balance;
    }
    // 充值资金进来,用于支付利息
    function receiveEth() public payable{
        
    }
    function repayEth(address accountAddress, uint256 asset) private {
        address payable addr = accountAddress.make_payable();
        addr.transfer(asset);
    }

}

library address_make_payable {
   function make_payable(address x) internal pure returns (address payable) {
      return address(uint160(x));
   }
}
interface ERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external;
    function transferFrom(address sender, address recipient, uint256 amount) external;
}
library SafeMath {
    int256 constant private INT256_MIN = -2**255;
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b);

        return c;
    }
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a);
        uint256 c = a - b;

        return c;
    }
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a);

        return c;
    }
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b != 0);
        return a % b;
    }
}

相关文章

网友评论

      本文标题:FlashLoan

      本文链接:https://www.haomeiwen.com/subject/jzmkiktx.html