编译器
0.6.12+commit.27d51765
文件 1 的 17:Address.sol
pragma solidity >=0.6.2 <0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 17:BaseHelioswap.sol
pragma solidity ^0.6.12;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "../interfaces/IBaseHelioswapFactory.sol";
import "../libraries/HelioswapConstants.sol";
import "../libraries/SafeCast.sol";
abstract contract BaseHelioswap is ERC20, Ownable, ReentrancyGuard {
using SafeCast for uint256;
event SlippageFeeUpdate(address indexed user, uint256 slippageFee, bool isDefault, uint256 amount);
event DecayPeriodUpdate(address indexed user, uint256 decayPeriod, bool isDefault, uint256 amount);
IBaseHelioswapFactory public baseHelioswapFactory;
uint256 private _fee;
uint256 private _slippageFee;
uint256 private _decayPeriod;
constructor(IBaseHelioswapFactory _baseHelioswapFactory) internal {
baseHelioswapFactory = _baseHelioswapFactory;
_fee = baseHelioswapFactory.defaultFee().toUint104();
_slippageFee = baseHelioswapFactory.defaultSlippageFee().toUint104();
_decayPeriod = baseHelioswapFactory.defaultDecayPeriod().toUint104();
}
function setBaseHelioswapFactory(IBaseHelioswapFactory newBaseHelioswapFactory) external onlyOwner {
baseHelioswapFactory = newBaseHelioswapFactory;
}
function fee() public view returns(uint256) {
return _fee;
}
function slippageFee() public view returns(uint256) {
return _slippageFee;
}
function decayPeriod() public view returns(uint256) {
return _decayPeriod;
}
}
文件 3 的 17:Context.sol
pragma solidity >=0.6.0 <0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this;
return msg.data;
}
}
文件 4 的 17:ERC20.sol
pragma solidity >=0.6.0 <0.8.0;
import "../../utils/Context.sol";
import "./IERC20.sol";
import "../../math/SafeMath.sol";
contract ERC20 is Context, IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
function name() public view virtual returns (string memory) {
return _name;
}
function symbol() public view virtual returns (string memory) {
return _symbol;
}
function decimals() public view virtual returns (uint8) {
return _decimals;
}
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
return true;
}
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
_totalSupply = _totalSupply.sub(amount);
emit Transfer(account, address(0), amount);
}
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function _setupDecimals(uint8 decimals_) internal virtual {
_decimals = decimals_;
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
文件 5 的 17:Helioswap.sol
pragma solidity ^0.6.12;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./libraries/UniERC20.sol";
import "./libraries/Sqrt.sol";
import "./libraries/VirtualBalance.sol";
import "./base/BaseHelioswap.sol";
contract Helioswap is BaseHelioswap {
using Sqrt for uint256;
using SafeMath for uint256;
using UniERC20 for IERC20;
using VirtualBalance for VirtualBalance.Data;
struct Balances {
uint256 src;
uint256 dst;
}
struct HelioswapVolumes {
uint128 confirmed;
uint128 results;
}
struct Fees {
uint256 fee;
uint256 slippageFee;
}
event HelioswapDeposited(
address indexed sender,
address indexed receiver,
uint256 share,
uint256 token0Amount,
uint256 token1Amount
);
event HelioswapWithdrawn(
address indexed sender,
address indexed receiver,
uint256 share,
uint256 token0Amount,
uint256 token1Amount
);
event HelioswapSwapped(
address indexed sender,
address indexed receiver,
address indexed srcToken,
address dstToken,
uint256 amount,
uint256 result,
uint256 srcAdditionBalance,
uint256 dstRemovalBalance
);
event HelioswapSync(
uint256 srcBalance,
uint256 dstBalance,
uint256 fee,
uint256 slippageFee
);
uint256 private constant _BASE_SUPPLY = 1000;
IERC20 public token0;
IERC20 public token1;
mapping(IERC20 => HelioswapVolumes) public volumes;
mapping(IERC20 => VirtualBalance.Data) public virtualBalancesForAddition;
mapping(IERC20 => VirtualBalance.Data) public virtualBalancesForRemoval;
modifier whenNotShutdown {
require(baseHelioswapFactory.isActive(), "Helioswap: factory shutdown");
_;
}
constructor(
IERC20 _token0,
IERC20 _token1,
string memory name,
string memory symbol,
IBaseHelioswapFactory _baseHelioswapFactory
) public ERC20(name, symbol) BaseHelioswap(_baseHelioswapFactory) {
require(bytes(name).length > 0, "Helioswap: name is empty");
require(bytes(symbol).length > 0, "Helioswap: symbol is empty");
require(_token0 != _token1, "Helioswap: duplicate tokens");
token0 = _token0;
token1 = _token1;
}
function tokens(uint256 i) external view returns (IERC20) {
if (i == 0) {
return token0;
} else if (i == 1) {
return token1;
} else {
revert("Pool has two tokens");
}
}
function getReserves() public view returns (uint256 _reserve0, uint256 _reserve1) {
_reserve0 = token0.uniBalanceOf(address(this));
_reserve1 = token1.uniBalanceOf(address(this));
}
function estimateBalanceForAddition(IERC20 token) public view returns (uint256) {
uint256 balance = token.uniBalanceOf(address(this));
return
Math.max(
virtualBalancesForAddition[token].current(
decayPeriod(),
balance
),
balance
);
}
function estimateBalanceForRemoval(IERC20 token) public view returns (uint256) {
uint256 balance = token.uniBalanceOf(address(this));
return
Math.min(
virtualBalancesForRemoval[token].current(
decayPeriod(),
balance
),
balance
);
}
function deposit(uint256[2] calldata maxAmounts, uint256[2] calldata minAmounts)
external
payable
returns (uint256 failSupply, uint256[2] memory receivedAmounts)
{
return depositFor(maxAmounts, minAmounts, msg.sender);
}
function depositFor(
uint256[2] memory maxAmounts,
uint256[2] memory minAmounts,
address target
)
public
payable
nonReentrant
returns (uint256 fairSupply, uint256[2] memory receivedAmounts)
{
IERC20[2] memory _tokens = [token0, token1];
require(
msg.value ==
(
_tokens[0].isETH()
? maxAmounts[0]
: (_tokens[1].isETH() ? maxAmounts[1] : 0)
),
"Helioswap: wrong value usage"
);
uint256 totalSupply = totalSupply();
if (totalSupply == 0) {
fairSupply = _BASE_SUPPLY.mul(99);
_mint(address(this), _BASE_SUPPLY);
for (uint256 i = 0; i < maxAmounts.length; i++) {
fairSupply = Math.max(fairSupply, maxAmounts[i]);
require(maxAmounts[i] > 0, "Helioswap: amount is zero");
require(
maxAmounts[i] >= minAmounts[i],
"Helioswap: minAmount not reached"
);
_tokens[i].uniTransferFrom(
msg.sender,
address(this),
maxAmounts[i]
);
receivedAmounts[i] = maxAmounts[i];
}
} else {
uint256[2] memory realBalances;
for (uint256 i = 0; i < realBalances.length; i++) {
realBalances[i] = _tokens[i].uniBalanceOf(address(this)).sub(
_tokens[i].isETH() ? msg.value : 0
);
}
fairSupply = uint256(-1);
for (uint256 i = 0; i < maxAmounts.length; i++) {
fairSupply = Math.min(
fairSupply,
totalSupply.mul(maxAmounts[i]).div(realBalances[i])
);
}
uint256 fairSupplyCached = fairSupply;
for (uint256 i = 0; i < maxAmounts.length; i++) {
require(maxAmounts[i] > 0, "Helioswap: amount is zero");
uint256 amount = realBalances[i]
.mul(fairSupplyCached)
.add(totalSupply - 1)
.div(totalSupply);
require(amount >= minAmounts[i], "Helioswap: minAmount not reached");
_tokens[i].uniTransferFrom(msg.sender, address(this), amount);
receivedAmounts[i] = _tokens[i].uniBalanceOf(address(this)).sub(
realBalances[i]
);
fairSupply = Math.min(
fairSupply,
totalSupply.mul(receivedAmounts[i]).div(realBalances[i])
);
}
uint256 _decayPeriod = decayPeriod();
for (uint256 i = 0; i < maxAmounts.length; i++) {
virtualBalancesForRemoval[_tokens[i]].scale(
_decayPeriod,
realBalances[i],
totalSupply.add(fairSupply),
totalSupply
);
virtualBalancesForAddition[_tokens[i]].scale(
_decayPeriod,
realBalances[i],
totalSupply.add(fairSupply),
totalSupply
);
}
}
require(fairSupply > 0, "Helioswap: result is not enough");
_mint(target, fairSupply);
emit HelioswapDeposited(
msg.sender,
target,
fairSupply,
receivedAmounts[0],
receivedAmounts[1]
);
}
function withdraw(uint256 amount, uint256[] calldata minReturns) external returns(uint256[2] memory withdrawnAmounts) {
return withdrawFor(amount, minReturns, msg.sender);
}
function withdrawFor(uint256 amount, uint256[] memory minReturns, address payable target) public nonReentrant returns(uint256[2] memory withdrawnAmounts) {
IERC20[2] memory _tokens = [token0, token1];
uint256 totalSupply = totalSupply();
uint256 _decayPeriod = decayPeriod();
_burn(msg.sender, amount);
for (uint i = 0; i < _tokens.length; i++) {
IERC20 token = _tokens[i];
uint256 preBalance = token.uniBalanceOf(address(this));
uint256 value = preBalance.mul(amount).div(totalSupply);
token.uniTransfer(target, value);
withdrawnAmounts[i] = value;
require(i >= minReturns.length || value >= minReturns[i], "Helioswap: result is not enough");
virtualBalancesForAddition[token].scale(_decayPeriod, preBalance, totalSupply.sub(amount), totalSupply);
virtualBalancesForRemoval[token].scale(_decayPeriod, preBalance, totalSupply.sub(amount), totalSupply);
}
emit HelioswapWithdrawn(msg.sender, target, amount, withdrawnAmounts[0], withdrawnAmounts[1]);
}
function swap(IERC20 src, IERC20 dst, uint256 amount, uint256 minReturn) external payable returns(uint256 result) {
return swapFor(src, dst, amount, minReturn, msg.sender);
}
function swapFor(IERC20 src, IERC20 dst, uint256 amount, uint256 minReturn, address payable receiver) public payable nonReentrant whenNotShutdown returns (uint256 result) {
require(msg.value == (src.isETH() ? amount : 0), "Helioswap: wrong value usage");
Balances memory balances = Balances({
src: src.uniBalanceOf(address(this)).sub(src.isETH() ? msg.value : 0),
dst: dst.uniBalanceOf(address(this))
});
uint256 confirmed;
Balances memory virtualBalances;
Fees memory fees = Fees({
fee: fee(),
slippageFee: slippageFee()
});
(confirmed, result, virtualBalances) = _doTransfers(src, dst, amount, minReturn, receiver, balances, fees);
emit HelioswapSwapped(msg.sender, receiver, address(src), address(dst), confirmed, result, virtualBalances.src, virtualBalances.dst);
volumes[src].confirmed += uint128(confirmed);
volumes[src].results += uint128(result);
emit HelioswapSync(balances.src, balances.dst, fees.fee, fees.slippageFee);
}
function _doTransfers(IERC20 src, IERC20 dst, uint256 amount, uint256 minReturn, address payable receiver, Balances memory balances, Fees memory fees)
private returns(uint256 confirmed, uint256 result, Balances memory virtualBalances)
{
uint256 _decayPeriod = decayPeriod();
virtualBalances.src = virtualBalancesForAddition[src].current(_decayPeriod, balances.src);
virtualBalances.src = Math.max(virtualBalances.src, balances.src);
virtualBalances.dst = virtualBalancesForRemoval
[dst].current(_decayPeriod, balances.dst);
virtualBalances.dst = Math.min(virtualBalances.dst, balances.dst);
src.uniTransferFrom(msg.sender, address(this), amount);
confirmed = src.uniBalanceOf(address(this)).sub(balances.src);
result = _getReturn(src, dst, confirmed, virtualBalances.src, virtualBalances.dst, fees.fee, fees.slippageFee);
require(result > 0 && result >= minReturn, "Helioswap: return is not enough");
dst.uniTransfer(receiver, result);
if (virtualBalances.src != balances.src) {
virtualBalancesForAddition[src].set(virtualBalances.src.add(confirmed));
}
if (virtualBalances.dst != balances.dst) {
virtualBalancesForRemoval[dst].set(virtualBalances.dst.sub(result));
}
virtualBalancesForRemoval[src].update(_decayPeriod, balances.src);
virtualBalancesForAddition[dst].update(_decayPeriod, balances.dst);
}
function _getReturn(IERC20 src, IERC20 dst, uint256 amount, uint256 srcBalance, uint256 dstBalance, uint256 fee, uint256 slippageFee) internal view returns(uint256) {
if (src > dst) {
(src, dst) = (dst, src);
}
if (amount > 0 && src == token0 && dst == token1) {
uint256 taxedAmount = amount.sub(amount.mul(fee).div(HelioswapConstants._FEE_DENOMINATOR));
uint256 srcBalancePlusTaxedAmount = srcBalance.add(taxedAmount);
uint256 ret = taxedAmount.mul(dstBalance).div(srcBalancePlusTaxedAmount);
uint256 feeNumerator = HelioswapConstants._FEE_DENOMINATOR.mul(srcBalancePlusTaxedAmount).sub(slippageFee.mul(taxedAmount));
uint256 feeDenominator = HelioswapConstants._FEE_DENOMINATOR.mul(srcBalancePlusTaxedAmount);
return ret.mul(feeNumerator).div(feeDenominator);
}
}
function rescueFunds(IERC20 token, uint256 amount) external nonReentrant onlyOwner {
uint256 balance0 = token0.uniBalanceOf(address(this));
uint256 balance1 = token1.uniBalanceOf(address(this));
token.uniTransfer(msg.sender, amount);
require(token0.uniBalanceOf(address(this)) >= balance0, "Helioswap: access denied");
require(token1.uniBalanceOf(address(this)) >= balance1, "Helioswap: access denied");
require(balanceOf(address(this)) >= _BASE_SUPPLY, "Helioswap: access denied");
}
function getReturn(IERC20 src, IERC20 dst, uint256 amount) public view returns(uint256 result) {
if (src.uniBalanceOf(address(this)) == 0 || dst.uniBalanceOf(address(this)) == 0) {
return 0;
}
Balances memory balances = Balances({
src: src.uniBalanceOf(address(this)),
dst: dst.uniBalanceOf(address(this))
});
Balances memory virtualBalances;
Fees memory fees = Fees({
fee: fee(),
slippageFee: slippageFee()
});
uint256 _decayPeriod = decayPeriod();
virtualBalances.src = virtualBalancesForAddition[src].current(_decayPeriod, balances.src);
virtualBalances.src = Math.max(virtualBalances.src, balances.src);
virtualBalances.dst = virtualBalancesForRemoval
[dst].current(_decayPeriod, balances.dst);
virtualBalances.dst = Math.min(virtualBalances.dst, balances.dst);
return _getReturn(src, dst, amount, virtualBalances.src, virtualBalances.dst, fees.fee, fees.slippageFee);
}
}
文件 6 的 17:HelioswapConstants.sol
pragma solidity ^0.6.12;
library HelioswapConstants {
uint256 internal constant _FEE_DENOMINATOR = 1e18;
uint256 internal constant _MIN_DECAY_PERIOD = 1 minutes;
uint256 internal constant _MAX_FEE = 0.01e18;
uint256 internal constant _MAX_SLIPPAGE_FEE = 1e18;
uint256 internal constant _MAX_DECAY_PERIOD = 5 minutes;
uint256 internal constant _DEFAULT_FEE = 3e15;
uint256 internal constant _DEFAULT_SLIPPAGE_FEE = 466e15;
uint256 internal constant _DEFAULT_DECAY_PERIOD = 1 minutes;
}
文件 7 的 17:IBaseHelioswapFactory.sol
pragma solidity ^0.6.12;
interface IBaseHelioswapFactory {
function defaults() external view returns(uint256 defaultFee, uint256 defaultSlippageFee, uint256 defaultDecayPeriod);
function defaultFee() external view returns(uint256);
function defaultSlippageFee() external view returns(uint256);
function defaultDecayPeriod() external view returns(uint256);
function setDefaultFee(uint256) external returns(uint256);
function setDefaultSlippageFee(uint256) external returns(uint256);
function setDefaultDecayPeriod(uint256) external returns(uint256);
function isActive() external view returns (bool);
}
文件 8 的 17:IERC20.sol
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 9 的 17:Math.sol
pragma solidity >=0.6.0 <0.8.0;
library Math {
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
}
文件 10 的 17:Ownable.sol
pragma solidity >=0.6.0 <0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
文件 11 的 17:ReentrancyGuard.sol
pragma solidity >=0.6.0 <0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 12 的 17:SafeCast.sol
pragma solidity ^0.6.12;
library SafeCast {
function toUint216(uint256 value) internal pure returns (uint216) {
require(value < 2**216, "value does not fit in 216 bits");
return uint216(value);
}
function toUint104(uint256 value) internal pure returns (uint104) {
require(value < 2**104, "value does not fit in 104 bits");
return uint104(value);
}
function toUint48(uint256 value) internal pure returns (uint48) {
require(value < 2**48, "value does not fit in 48 bits");
return uint48(value);
}
function toUint40(uint256 value) internal pure returns (uint40) {
require(value < 2**40, "value does not fit in 40 bits");
return uint40(value);
}
}
文件 13 的 17:SafeERC20.sol
pragma solidity >=0.6.0 <0.8.0;
import "./IERC20.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.sol";
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 14 的 17:SafeMath.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
文件 15 的 17:Sqrt.sol
pragma solidity ^0.6.12;
library Sqrt {
function sqrt(uint256 y) internal pure returns (uint256) {
if (y > 3) {
uint256 z = y;
uint256 x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
return z;
} else if (y != 0) {
return 1;
} else {
return 0;
}
}
}
文件 16 的 17:UniERC20.sol
pragma solidity ^0.6.12;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
library UniERC20 {
using SafeMath for uint256;
using SafeERC20 for IERC20;
function isETH(IERC20 token) internal pure returns(bool) {
return (address(token) == address(0));
}
function uniBalanceOf(IERC20 token, address account) internal view returns (uint256) {
if (isETH(token)) {
return account.balance;
} else {
return token.balanceOf(account);
}
}
function uniTransfer(IERC20 token, address payable to, uint256 amount) internal {
if (amount > 0) {
if (isETH(token)) {
to.transfer(amount);
} else {
token.safeTransfer(to, amount);
}
}
}
function uniTransferFrom(IERC20 token, address payable from, address to, uint256 amount) internal {
if (amount > 0) {
if (isETH(token)) {
require(msg.value >= amount, "UniERC20: not enough value");
require(from == msg.sender, "from is not msg.sender");
require(to == address(this), "to is not this");
if (msg.value > amount) {
from.transfer(msg.value.sub(amount));
}
} else {
token.safeTransferFrom(from, to, amount);
}
}
}
function uniSymbol(IERC20 token) internal view returns(string memory) {
if (isETH(token)) {
return "ETH";
}
(bool success, bytes memory data) = address(token).staticcall{ gas: 20000 }(
abi.encodeWithSignature("symbol()")
);
if (!success) {
(success, data) = address(token).staticcall{ gas: 20000 }(
abi.encodeWithSignature("SYMBOL()")
);
}
if (success && data.length >= 96) {
(uint256 offset, uint256 len) = abi.decode(data, (uint256, uint256));
if (offset == 0x20 && len > 0 && len <= 256) {
return string(abi.decode(data, (bytes)));
}
}
if (success && data.length == 32) {
uint len = 0;
while (len < data.length && data[len] >= 0x20 && data[len] <= 0x7E) {
len++;
}
if (len > 0) {
bytes memory result = new bytes(len);
for (uint i = 0; i < len; i++) {
result[i] = data[i];
}
return string(result);
}
}
return _toHex(address(token));
}
function _toHex(address account) private pure returns(string memory) {
return _toHex(abi.encodePacked(account));
}
function _toHex(bytes memory data) private pure returns(string memory) {
bytes memory str = new bytes(2 + data.length * 2);
str[0] = "0";
str[1] = "x";
uint j = 2;
for (uint i = 0; i < data.length; i++) {
uint a = uint8(data[i]) >> 4;
uint b = uint8(data[i]) & 0x0f;
str[j++] = byte(uint8(a + 48 + (a/10)*39));
str[j++] = byte(uint8(b + 48 + (b/10)*39));
}
return string(str);
}
}
文件 17 的 17:VirtualBalance.sol
pragma solidity ^0.6.12;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "./SafeCast.sol";
library VirtualBalance {
using SafeMath for uint256;
using SafeCast for uint256;
struct Data {
uint256 balance;
uint40 time;
}
function set(VirtualBalance.Data storage self, uint256 balance) internal {
(self.balance, self.time) = (
balance.toUint216(),
block.timestamp.toUint40()
);
}
function update(VirtualBalance.Data storage self, uint256 decayPeriod, uint256 realBalance) internal {
set(self, current(self, decayPeriod, realBalance));
}
function scale(VirtualBalance.Data storage self, uint256 decayPeriod, uint256 realBalance, uint256 num, uint256 demon) internal {
set(self, current(self, decayPeriod, realBalance).mul(num).add(demon.sub(1)).div(demon));
}
function current(VirtualBalance.Data memory self, uint256 decayPeriod, uint256 realBalance) internal view returns (uint256) {
uint256 timePassed = Math.min(decayPeriod, block.timestamp.sub(self.time));
uint256 timeRemain = decayPeriod.sub(timePassed);
return uint256(self.balance).mul(timeRemain).add(
realBalance.mul(timePassed)
).div(decayPeriod);
}
}
{
"compilationTarget": {
"contracts/Helioswap.sol": "Helioswap"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC20","name":"_token0","type":"address"},{"internalType":"contract IERC20","name":"_token1","type":"address"},{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"contract IBaseHelioswapFactory","name":"_baseHelioswapFactory","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"decayPeriod","type":"uint256"},{"indexed":false,"internalType":"bool","name":"isDefault","type":"bool"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"DecayPeriodUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"share","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token0Amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token1Amount","type":"uint256"}],"name":"HelioswapDeposited","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":true,"internalType":"address","name":"srcToken","type":"address"},{"indexed":false,"internalType":"address","name":"dstToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"result","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"srcAdditionBalance","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"dstRemovalBalance","type":"uint256"}],"name":"HelioswapSwapped","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"srcBalance","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"dstBalance","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slippageFee","type":"uint256"}],"name":"HelioswapSync","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"share","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token0Amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"token1Amount","type":"uint256"}],"name":"HelioswapWithdrawn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"slippageFee","type":"uint256"},{"indexed":false,"internalType":"bool","name":"isDefault","type":"bool"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"SlippageFeeUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"baseHelioswapFactory","outputs":[{"internalType":"contract IBaseHelioswapFactory","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decayPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[2]","name":"maxAmounts","type":"uint256[2]"},{"internalType":"uint256[2]","name":"minAmounts","type":"uint256[2]"}],"name":"deposit","outputs":[{"internalType":"uint256","name":"failSupply","type":"uint256"},{"internalType":"uint256[2]","name":"receivedAmounts","type":"uint256[2]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256[2]","name":"maxAmounts","type":"uint256[2]"},{"internalType":"uint256[2]","name":"minAmounts","type":"uint256[2]"},{"internalType":"address","name":"target","type":"address"}],"name":"depositFor","outputs":[{"internalType":"uint256","name":"fairSupply","type":"uint256"},{"internalType":"uint256[2]","name":"receivedAmounts","type":"uint256[2]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"}],"name":"estimateBalanceForAddition","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"}],"name":"estimateBalanceForRemoval","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getReserves","outputs":[{"internalType":"uint256","name":"_reserve0","type":"uint256"},{"internalType":"uint256","name":"_reserve1","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"src","type":"address"},{"internalType":"contract IERC20","name":"dst","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getReturn","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"rescueFunds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IBaseHelioswapFactory","name":"newBaseHelioswapFactory","type":"address"}],"name":"setBaseHelioswapFactory","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"slippageFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"src","type":"address"},{"internalType":"contract IERC20","name":"dst","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"}],"name":"swap","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"src","type":"address"},{"internalType":"contract IERC20","name":"dst","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minReturn","type":"uint256"},{"internalType":"address payable","name":"receiver","type":"address"}],"name":"swapFor","outputs":[{"internalType":"uint256","name":"result","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token0","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token1","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"i","type":"uint256"}],"name":"tokens","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"name":"virtualBalancesForAddition","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"},{"internalType":"uint40","name":"time","type":"uint40"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"name":"virtualBalancesForRemoval","outputs":[{"internalType":"uint256","name":"balance","type":"uint256"},{"internalType":"uint40","name":"time","type":"uint40"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"name":"volumes","outputs":[{"internalType":"uint128","name":"confirmed","type":"uint128"},{"internalType":"uint128","name":"results","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256[]","name":"minReturns","type":"uint256[]"}],"name":"withdraw","outputs":[{"internalType":"uint256[2]","name":"withdrawnAmounts","type":"uint256[2]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256[]","name":"minReturns","type":"uint256[]"},{"internalType":"address payable","name":"target","type":"address"}],"name":"withdrawFor","outputs":[{"internalType":"uint256[2]","name":"withdrawnAmounts","type":"uint256[2]"}],"stateMutability":"nonpayable","type":"function"}]