编译器
0.8.20+commit.a1b79de6
文件 1 的 15:Address.sol
pragma solidity ^0.8.20;
library Address {
error AddressInsufficientBalance(address account);
error AddressEmptyCode(address target);
error FailedInnerCall();
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert AddressInsufficientBalance(address(this));
}
(bool success, ) = recipient.call{value: amount}("");
if (!success) {
revert FailedInnerCall();
}
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert AddressInsufficientBalance(address(this));
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
function _revert(bytes memory returndata) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert FailedInnerCall();
}
}
}
文件 2 的 15:Context.sol
pragma solidity ^0.8.20;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
文件 3 的 15:Doubler.sol
pragma solidity ^0.8.20;
import '@openzeppelin/contracts/access/Ownable2Step.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/utils/ReentrancyGuard.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import './interfaces/IFastPriceFeed.sol';
import './interfaces/IDoubler.sol';
import './interfaces/IRBToken.sol';
import './interfaces/IWETH.sol';
import './RBToken.sol';
contract Doubler is IDoubler, ReentrancyGuard, Ownable2Step {
using SafeERC20 for IERC20;
bool private _initialized;
uint16 private constant _slip = 50;
uint16 private constant _perMil = 1000;
uint16 private _rebaseBTokenShare;
address private _fastPriceFeed;
address private _ecoAddr;
address private _rewardAddr;
address private _wethAddr;
mapping(address => uint256) private _lastBlockCalled;
mapping(address => Pool) private _pools;
mapping(address => mapping(uint256 => bool)) private _dealRecord;
constructor(address _initOwner) Ownable(_initOwner) {}
function initializeDoubler(
address _initFastPriceFeed,
address _initEcoAddr,
address _initWethAddr,
uint16 _initRebaseBTokenShare
) external onlyOwner {
if (_initFastPriceFeed == address(0) || _initEcoAddr == address(0) || _initWethAddr == address(0)) revert E_ZeroAddr();
if (_initialized == true) revert E_Initialized();
_initialized = true;
_rebaseBTokenShare = _initRebaseBTokenShare;
_fastPriceFeed = _initFastPriceFeed;
_ecoAddr = _initEcoAddr;
_wethAddr = _initWethAddr;
RBToken reward = new RBToken('Reward Token', 'Reward', address(this));
_rewardAddr = address(reward);
}
function getPrivateVar() external view returns (address, address, address, address, uint16) {
return (_fastPriceFeed, _ecoAddr, _rewardAddr, _wethAddr, _rebaseBTokenShare);
}
modifier onlyAssetAndBlock(address _asset) {
if (_pools[_asset].aToken == address(0)) revert E_Asset();
if (_lastBlockCalled[tx.origin] >= block.number) revert E_BlockOnce();
_;
_lastBlockCalled[tx.origin] = block.number;
}
function _concatenate(string memory _a, string memory _b) private pure returns (string memory) {
return string(abi.encodePacked(_a, _b));
}
function newPool(AddPool calldata _addPool) external onlyOwner {
_checkPoolParam(_addPool);
if (_pools[_addPool.asset].aToken != address(0)) revert E_PoolInit();
RBToken aToken = new RBToken(
_concatenate(_addPool.tokenPre, '-E'),
_concatenate(_addPool.tokenPre, '-E'),
address(this)
);
RBToken bToken = new RBToken(
_concatenate(_addPool.tokenPre, '-10x'),
_concatenate(_addPool.tokenPre, '-10x'),
address(this)
);
RBToken cToken = new RBToken(
_concatenate(_addPool.tokenPre, '-C'),
_concatenate(_addPool.tokenPre, '-C'),
address(this)
);
Pool storage pool = _pools[_addPool.asset];
pool.aToken = address(aToken);
pool.bToken = address(bToken);
pool.cToken = address(cToken);
pool.asset = _addPool.asset;
pool.inputFee = _addPool.inputFee;
pool.redeemFee = _addPool.redeemFee;
pool.withdrawFee = _addPool.withdrawFee;
pool.lowerOfInputMaximum = _addPool.lowerOfInputMaximum;
pool.creator = _addPool.creator;
emit NewPool(_addPool.asset, pool.aToken, pool.bToken, pool.cToken);
}
function _checkPoolParam(AddPool calldata _pool) internal pure {
if (_pool.asset == address(0)) revert E_ZeroAddr();
if (_pool.inputFee > 20) revert E_FeeLimit();
if (_pool.redeemFee > 20) revert E_FeeLimit();
if (_pool.withdrawFee > 20) revert E_FeeLimit();
}
function updatePool(AddPool calldata _updatePool) external onlyOwner {
_checkPoolParam(_updatePool);
if (_pools[_updatePool.asset].aToken == address(0)) revert E_PoolInit();
Pool storage pool = _pools[_updatePool.asset];
pool.inputFee = _updatePool.inputFee;
pool.redeemFee = _updatePool.redeemFee;
pool.withdrawFee = _updatePool.withdrawFee;
pool.creator = _updatePool.creator;
emit UpdatePool(
_updatePool.asset,
_updatePool.creator,
_updatePool.inputFee,
_updatePool.redeemFee,
_updatePool.withdrawFee
);
}
function _getAssetBalance(address _asset) private view returns (uint256) {
return IERC20(_asset).balanceOf(address(this));
}
function _getAssetAvg(address _asset) private view returns (uint256) {
uint256 assetTotal = _getAssetBalance(_asset);
if (assetTotal == 0) {
return 0;
}
return (IERC20(_pools[_asset].cToken).totalSupply() * _getUnitSize(_asset)) / assetTotal;
}
function _getCurPrice(address _asset) internal view returns (uint256) {
return IFastPriceFeed(_fastPriceFeed).getPrice(_asset);
}
function _getUnitSize(address _asset) internal view returns (uint256) {
return 10 ** IERC20Metadata(_asset).decimals();
}
function _getTokenPoolTotal(address _asset, uint256 _price) internal view returns (uint256 poolTotal) {
poolTotal = (_price * _getAssetBalance(_asset)) / _getUnitSize(_asset) / 10;
}
function inputEth(
uint256 _qAmount,
address _to
) external payable nonReentrant onlyAssetAndBlock(_wethAddr) {
if (msg.value != _qAmount) revert E_Balance();
_input(_wethAddr, _qAmount, _to, true);
}
function input(
address _asset,
uint256 _qAmount,
address _to
) external nonReentrant onlyAssetAndBlock(_asset) {
if (IERC20(_asset).balanceOf(_msgSender()) < _qAmount) revert E_Balance();
_input(_asset, _qAmount, _to, false);
}
function _checkInputQAmount(
address _asset,
uint256 _qAmount,
uint256 _curPrice,
uint256 _assetUnitSize,
uint256 _lowerOfInputMaximum
) internal view {
uint256 inputValue = (_curPrice * _qAmount) / _assetUnitSize;
uint256 inputMax = (_curPrice * _getAssetBalance(_asset)) / _assetUnitSize / 100;
inputMax = inputMax > _lowerOfInputMaximum ? inputMax : _lowerOfInputMaximum;
if (inputValue < 1 ether || inputValue > inputMax) revert E_InputLimit();
}
function _input(address _asset, uint256 _qAmount, address _to, bool _isNative) internal {
_rebaseCToken(_asset);
_rebaseBToken(_asset);
Pool storage pool = _pools[_asset];
uint256 curPrice = _getCurPrice(_asset);
uint256 avg = _getAssetAvg(_asset);
avg = avg == 0 ? curPrice : avg;
uint256 assetUnitSize = _getUnitSize(_asset);
_checkInputQAmount(_asset, _qAmount, curPrice, assetUnitSize, pool.lowerOfInputMaximum);
uint256 aAmount;
uint256 bAmount;
uint256 cAmount;
uint256 aTokenUnitSize = _getUnitSize(pool.aToken);
if (curPrice <= avg) {
aAmount = (_qAmount * aTokenUnitSize) / assetUnitSize;
cAmount = (curPrice * _qAmount) / assetUnitSize;
bAmount = (curPrice * _qAmount - (curPrice * curPrice * _qAmount) / avg) / assetUnitSize;
} else {
aAmount = (curPrice * _qAmount * aTokenUnitSize) / avg / assetUnitSize;
cAmount = avg * _qAmount / assetUnitSize;
}
if (_isNative) {
IWETH(_asset).deposit{ value: msg.value }();
} else {
IERC20(_asset).safeTransferFrom(_msgSender(), address(this), _qAmount);
}
_mintABCToken(pool, _to, aAmount, bAmount, cAmount, curPrice);
emit Input(_asset, _to, _qAmount, aAmount, bAmount, cAmount, curPrice, avg);
}
function _getSrvFeeAddr(address _creator) internal view returns (address[] memory srvFeeAddr) {
srvFeeAddr = new address[](2);
srvFeeAddr[0] = _creator;
srvFeeAddr[1] = _ecoAddr;
}
function _mintABCToken(
Pool memory _pool,
address _to,
uint256 _aAmount,
uint256 _bAmount,
uint256 _cAmount,
uint256 _curPrice
) internal {
if (_bAmount > 0) {
uint256 bTotal = _getTokenPoolTotal(_pool.asset, _curPrice);
IRBToken(_pool.bToken).limitMint(_to, _bAmount, bTotal, _getSrvFeeAddr(_pool.creator), _pool.inputFee);
}
IRBToken(_pool.aToken).limitMint(_to, _aAmount, 0, _getSrvFeeAddr(_pool.creator), _pool.inputFee);
IRBToken(_pool.cToken).mint(_to, _cAmount);
}
function _getWithdrawAssetAmount(
Pool memory _pool,
uint256 _cAmount,
uint256 _bTotal,
uint256 _cTotal,
uint256 _spendBAmount,
uint256 _curPrice,
uint256 _avgPrice
) internal view returns (uint256 assetAmount) {
uint256 assetUnitSize = _getUnitSize(_pool.asset);
if (_curPrice >= _avgPrice) {
uint256 profit = (((_getAssetBalance(_pool.asset) * _curPrice) / assetUnitSize - _cTotal) * _spendBAmount) /
_bTotal;
assetAmount = ((_cAmount + profit) * assetUnitSize) / _curPrice;
} else {
assetAmount = (_cAmount * assetUnitSize) / _avgPrice;
}
}
function _getSpendBAmount(
Pool memory _pool,
uint256 _cAmount,
uint256 _bTotal,
uint256 _cTotal
) internal view returns (uint256 spendBAmount) {
spendBAmount = (_cAmount * _bTotal) / _cTotal;
uint256 uBTokenBalance = IERC20(_pool.bToken).balanceOf(_msgSender());
if (_cTotal == _cAmount && spendBAmount > uBTokenBalance) {
if (spendBAmount - uBTokenBalance <= _getUnitSize(_pool.bToken) / 10) spendBAmount = uBTokenBalance;
}
if (IERC20(_pool.cToken).balanceOf(_msgSender()) < _cAmount) revert E_Balance();
if (uBTokenBalance < spendBAmount) revert E_Balance();
}
function withdraw(
address _asset,
uint256 _cAmount,
uint256 _qAmount,
uint16 _clientSlip
) external nonReentrant onlyAssetAndBlock(_asset) {
_rebaseCToken(_asset);
_rebaseBToken(_asset);
Pool storage pool = _pools[_asset];
uint256 curPrice = _getCurPrice(_asset);
uint256 avgPrice = _getAssetAvg(_asset);
uint256 bTotal = _getTokenPoolTotal(_asset, curPrice);
uint256 cTotal = IERC20(pool.cToken).totalSupply();
uint256 spendBAmount = _getSpendBAmount(pool, _cAmount, bTotal, cTotal);
uint256 assetAmount = _getWithdrawAssetAmount(pool, _cAmount, bTotal, cTotal, spendBAmount, curPrice, avgPrice);
if(_getAssetBalance(_asset) <= assetAmount) {
assetAmount = _getAssetBalance(_asset);
}
uint256 srvFee = (assetAmount * pool.withdrawFee) / _perMil;
IRBToken(pool.cToken).burnFrom(_msgSender(), _cAmount);
IRBToken(pool.bToken).burnFrom(_msgSender(), spendBAmount);
IERC20(_asset).safeTransfer(pool.creator, srvFee/2);
IERC20(_asset).safeTransfer(_ecoAddr, srvFee-srvFee/2);
IERC20(_asset).safeTransfer(_msgSender(), assetAmount - srvFee);
_checkAmountSlip(_qAmount, assetAmount - srvFee, _clientSlip);
_checkWithdrawLimit(_asset, curPrice);
emit Withdraw(_asset, _msgSender(), _cAmount, spendBAmount, assetAmount, curPrice, avgPrice);
}
function _checkWithdrawLimit(address _asset, uint256 _curPrice) internal view {
uint256 lastBalance = _getAssetBalance(_asset);
if (lastBalance != 0 && (lastBalance * _curPrice / _getUnitSize(_asset)) < 1 ether) {
revert E_WithdrawLimit();
}
}
function _checkAmountSlip(uint256 _eAmount, uint256 _rAmount, uint16 _clientSlip) internal pure {
if (_clientSlip > _slip) revert E_SlipLimit();
if ((_eAmount * (_perMil - _clientSlip)) / _perMil > _rAmount) {
revert E_Expected();
}
}
function redeem(
address _asset,
uint256 _aAmount,
uint256 _bAmount,
uint16 _clientSlip
) external nonReentrant onlyAssetAndBlock(_asset) {
Pool memory pool = _pools[_asset];
if (IERC20(pool.aToken).balanceOf(_msgSender()) < _aAmount) revert E_Balance();
_rebaseBToken(_asset);
uint256 curPrice = _getCurPrice(_asset);
uint256 bPoolTotal = _getTokenPoolTotal(pool.asset, curPrice);
uint256 bPooledTotal = IERC20(pool.bToken).totalSupply();
uint256 aTotal = IERC20(pool.aToken).totalSupply();
IRBToken(pool.aToken).burnFrom(_msgSender(), _aAmount);
uint256 bAmount = bPoolTotal > bPooledTotal ? (_aAmount * (bPoolTotal - bPooledTotal)) / aTotal : 0;
uint256 recipientTokenAmount;
if (bAmount > 0) {
recipientTokenAmount = IRBToken(pool.bToken).limitMint(
_msgSender(),
bAmount,
bPoolTotal,
_getSrvFeeAddr(pool.creator),
pool.redeemFee
);
}
_checkAmountSlip(_bAmount, recipientTokenAmount, _clientSlip);
emit Redeem(_asset, _aAmount, bAmount);
}
function getBTokenPoolTotal(address _asset) external view returns (uint256 poolTotal) {
Pool memory pool = _pools[_asset];
uint256 curPrice = _getCurPrice(_asset);
return _getTokenPoolTotal(pool.asset, curPrice);
}
function _rebaseBToken(address _asset) internal {
uint256 curPrice = _getCurPrice(_asset);
Pool storage pool = _pools[_asset];
uint256 poolTotal = _getTokenPoolTotal(pool.asset, curPrice);
uint256 pooledTotal = IERC20(pool.bToken).totalSupply();
uint256 lastBTotal = _getTokenPoolTotal(pool.asset, pool.lastPrice);
if (poolTotal > lastBTotal && poolTotal > pooledTotal && pooledTotal > 0) {
pooledTotal = pooledTotal + ((poolTotal - lastBTotal) * _rebaseBTokenShare) / _perMil;
IRBToken(pool.bToken).rebase(pooledTotal);
}
if (poolTotal < pooledTotal) {
IRBToken(pool.bToken).rebase(poolTotal);
}
pool.lastPrice = curPrice;
}
function rebaseCToken(address _asset) external nonReentrant {
_rebaseCToken(_asset);
}
function _rebaseCToken(address _asset) internal {
uint256 avg = _getAssetAvg(_asset);
uint256 curPrice = _getCurPrice(_asset);
if (avg == 0 || curPrice <= avg || block.timestamp - _pools[_asset].cLastRbTime <= 1 days) {
if (avg == 0) _pools[_asset].cLastRbTime = block.timestamp;
return;
}
uint256 rewardUnitSize = _getUnitSize(_rewardAddr);
IRBToken(_rewardAddr).mint(_msgSender(), 1000 * rewardUnitSize);
_pools[_asset].cLastRbTime = block.timestamp;
uint256 cTokenTotal = IERC20(_pools[_asset].cToken).totalSupply();
cTokenTotal = cTokenTotal + (cTokenTotal * (curPrice - avg)) / avg / 100;
IRBToken(_pools[_asset].cToken).rebase(cTokenTotal);
}
function getPool(address _asset) external view returns (Pool memory pool) {
pool = _pools[_asset];
}
}
文件 4 的 15:IDoubler.sol
pragma solidity ^0.8.20;
interface IDoubler {
error E_Initialized();
error E_Asset();
error E_PoolInit();
error E_FeeLimit();
error E_ZeroAddr();
error E_Balance();
error E_InputLimit();
error E_Expected();
error E_SlipLimit();
error E_BlockOnce();
error E_WithdrawLimit();
struct Pool {
uint16 inputFee;
uint16 redeemFee;
uint16 withdrawFee;
uint256 lastPrice;
uint256 cLastRbTime;
uint256 lowerOfInputMaximum;
address asset;
address aToken;
address bToken;
address cToken;
address creator;
}
struct WithdrawParam {
address asset;
uint256 cAmount;
uint256 qAmount;
uint16 clientSlip;
}
struct AddPool {
uint16 inputFee;
uint16 redeemFee;
uint16 withdrawFee;
uint256 lowerOfInputMaximum;
address asset;
address creator;
string tokenPre;
}
event NewPool(address indexed asset, address aToken, address bToken, address cToken);
event Input(
address indexed asset,
address indexed to,
uint256 qAmount,
uint256 aAmount,
uint256 bAmount,
uint256 cAmount,
uint256 curPrice,
uint256 avg
);
event Withdraw(
address indexed asset,
address indexed to,
uint256 cAmount,
uint256 bAmount,
uint256 assetAmount,
uint256 curPrice,
uint256 avg
);
event Redeem(address indexed asset, uint256 aAmount, uint256 bAmount);
event UpdatePool(address indexed asset, address creator, uint16 inputFee, uint16 redeemFee, uint16 withdrawFee);
event UpdateLowerOfInputMaximum(uint256 oldInputLowerOfMaximum, uint256 newInputLowerOfMaximum);
function getPrivateVar()
external
view
returns (
address fastPriceFeed,
address ecoAddr,
address rewardAddr,
address wethAddr,
uint16 rebaseBTokenShare
);
function newPool(AddPool calldata _addPool) external;
function updatePool(AddPool calldata _updatePool) external;
function inputEth(uint256 _qAmount, address _to) external payable;
function input(address _asset, uint256 _qAmount, address _to) external;
function redeem(address _asset, uint256 _aAmount, uint256 _bAmount, uint16 _clientSlip) external;
function withdraw(address _asset, uint256 _cAmount, uint256 _qAmount, uint16 _clientSlip) external;
function getPool(address _asset) external view returns (Pool memory pool);
function getBTokenPoolTotal(address _asset) external view returns (uint256 poolTotal);
function rebaseCToken(address _asset) external;
}
文件 5 的 15:IERC20.sol
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 6 的 15:IERC20Metadata.sol
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 7 的 15:IERC20Permit.sol
pragma solidity ^0.8.20;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
文件 8 的 15:IFastPriceFeed.sol
pragma solidity ^0.8.20;
interface IFastPriceFeed {
error E_PriceLimits();
error E_ZeroAddr();
error E_AssetSupported();
error E_AssetExist();
error E_PlanExist();
error E_Switch();
error E_PlanNotExist();
error E_TimeLimit();
error E_PriceRange();
error E_InitOracle();
error E_TwapInterval();
enum Plan {
DEX,
CHAINLINK
}
struct PriceFeed {
Plan plan;
address oracleAddr;
uint32 timeLimit;
uint256 priceMin;
uint256 priceMax;
}
event SetAssetTimeLimit(address asset, uint256 oldTimeLimit, uint256 newTimeLimit);
event SetPriceLimit(address indexed asset, Plan plan, uint256 priceMin, uint256 priceMax);
event SetPriceFee(
address indexed asset,
Plan plan,
address oracleAddr,
uint256 timeLimit,
uint256 priceMin,
uint256 priceMax
);
function getPrice(address _asset) external view returns (uint256 price);
function getAssetPlan(address _asset) external view returns (Plan);
function getPriceFeeds(address _asset, Plan _plan) external view returns (PriceFeed memory);
}
文件 9 的 15:IRBToken.sol
pragma solidity ^0.8.20;
import '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol';
interface IRBToken is IERC20Metadata {
error E_ZeroAddr();
error E_Rebase();
error E_AllowanceInvalid();
error E_TransferRBContract();
error E_BalanceInvalid();
error E_AmountInvalid();
event TransferShares(address indexed from, address indexed to, uint256 sharesValue);
event SharesBurnt(
address indexed account,
uint256 preRebaseTokenAmount,
uint256 postRebaseTokenAmount,
uint256 sharesAmount
);
event Rebase(address asset, uint256 originTotalSupply, uint256 newTotalSupply);
function mint(address _recipient, uint256 _tokenAmount) external;
function limitMint(
address _recipient,
uint256 _tokenAmount,
uint256 _poolTotalLimit,
address[] memory _srvFeeAddr,
uint16 _srvFeeRatio
) external returns (uint256 recipientTokenAmount);
function burnFrom(address _from, uint256 _tokenAmount) external;
function rebase(uint256 _newTotalSupply) external;
function totalShare() external view returns (uint256);
function sharesOf(address _account) external view returns (uint256);
function transferShares(address _recipient, uint256 _sharesAmount) external returns (uint256);
function transferSharesFrom(
address _sender, address _recipient, uint256 _sharesAmount
) external returns (uint256) ;
}
文件 10 的 15:IWETH.sol
pragma solidity ^0.8.20;
interface IWETH {
function deposit() external payable;
}
文件 11 的 15:Ownable.sol
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 12 的 15:Ownable2Step.sol
pragma solidity ^0.8.20;
import {Ownable} from "./Ownable.sol";
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
function acceptOwnership() public virtual {
address sender = _msgSender();
if (pendingOwner() != sender) {
revert OwnableUnauthorizedAccount(sender);
}
_transferOwnership(sender);
}
}
文件 13 的 15:RBToken.sol
pragma solidity ^0.8.20;
import '@openzeppelin/contracts/access/Ownable.sol';
import './interfaces/IRBToken.sol';
contract RBToken is IRBToken, Ownable {
uint16 private constant _perMil = 1000;
string private _name;
string private _symbol;
uint256 internal constant INFINITE_ALLOWANCE = ~uint256(0);
uint256 private _totalShare;
uint256 private _pooledTotal;
mapping(address => uint256) private _shares;
mapping(address => mapping(address => uint256)) private _allowances;
constructor(string memory _initName, string memory _initSymbol, address _initOwner) Ownable(_initOwner) {
_name = _initName;
_symbol = _initSymbol;
}
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 18;
}
function totalSupply() external view returns (uint256) {
return _pooledTotal;
}
function balanceOf(address _account) external view returns (uint256) {
return _getPooledTokenByShares(_shares[_account]);
}
function transfer(address _recipient, uint256 _amount) external returns (bool) {
_transfer(msg.sender, _recipient, _amount);
return true;
}
function allowance(address _owner, address _spender) external view returns (uint256) {
return _allowances[_owner][_spender];
}
function approve(address _spender, uint256 _amount) external returns (bool) {
_approve(msg.sender, _spender, _amount);
return true;
}
function transferFrom(address _sender, address _recipient, uint256 _amount) external returns (bool) {
_spendAllowance(_sender, msg.sender, _amount);
_transfer(_sender, _recipient, _amount);
return true;
}
function _getSharesByPooledToken(uint256 _tokenAmount) private view returns (uint256) {
if (_pooledTotal == 0) {
return _tokenAmount;
}
return (_tokenAmount * _totalShare) / _pooledTotal;
}
function _getPooledTokenByShares(uint256 _sharesAmount) private view returns (uint256) {
if (_totalShare == 0) {
return _sharesAmount;
}
return (_sharesAmount * _pooledTotal) / _totalShare;
}
function _transfer(address _sender, address _recipient, uint256 _amount) internal {
uint256 _sharesToTransfer = _getSharesByPooledToken(_amount);
_transferShares(_sender, _recipient, _sharesToTransfer);
_emitTransferEvents(_sender, _recipient, _amount, _sharesToTransfer);
}
function _approve(address _owner, address _spender, uint256 _amount) internal {
if (_spender == address(0)) revert E_ZeroAddr();
_allowances[_owner][_spender] = _amount;
emit Approval(_owner, _spender, _amount);
}
function _spendAllowance(address _owner, address _spender, uint256 _amount) internal {
uint256 currentAllowance = _allowances[_owner][_spender];
if (currentAllowance != INFINITE_ALLOWANCE) {
if (currentAllowance < _amount) revert E_AllowanceInvalid();
_approve(_owner, _spender, currentAllowance - _amount);
}
}
function _transferShares(address _sender, address _recipient, uint256 _sharesAmount) internal {
if (_recipient == address(0)) revert E_ZeroAddr();
if (_recipient == address(this)) revert E_TransferRBContract();
uint256 currentSenderShares = _shares[_sender];
if (_sharesAmount > currentSenderShares) revert E_BalanceInvalid();
_shares[_sender] = currentSenderShares - _sharesAmount;
_shares[_recipient] = _shares[_recipient] + _sharesAmount;
}
function mint(address _recipient, uint256 _tokenAmount) external onlyOwner {
address[] memory srvFeeAddr;
_limitMint(_recipient, _tokenAmount, 0, srvFeeAddr, 0);
}
function limitMint(
address _recipient,
uint256 _tokenAmount,
uint256 _poolTotalLimit,
address[] memory _srvFeeAddr,
uint16 _srvFeeRatio
) external onlyOwner returns (uint256 recipientTokenAmount) {
return _limitMint(_recipient, _tokenAmount, _poolTotalLimit, _srvFeeAddr, _srvFeeRatio);
}
function _limitMint(
address _recipient,
uint256 _tokenAmount,
uint256 _poolTotalLimit,
address[] memory _srvFeeAddr,
uint16 _srvFeeRatio
) internal returns (uint256 recipientTokenAmount) {
if (_recipient == address(0)) revert E_ZeroAddr();
uint256 newShares;
if (_poolTotalLimit > 0 && _pooledTotal + _tokenAmount > _poolTotalLimit) {
if (_poolTotalLimit > _tokenAmount) {
newShares = (_tokenAmount * _totalShare) / (_poolTotalLimit - _tokenAmount);
} else {
newShares = _totalShare * 10000 == 0 ? 10000 : _totalShare * 10000;
_tokenAmount = (_poolTotalLimit * newShares) / (_totalShare + newShares);
}
_pooledTotal = _poolTotalLimit;
} else {
newShares = _getSharesByPooledToken(_tokenAmount);
_pooledTotal = _pooledTotal + _tokenAmount;
}
_totalShare = _totalShare + newShares;
uint256 recipientNewShare = newShares;
recipientTokenAmount = _tokenAmount;
if (_srvFeeRatio > 0) {
uint256 srvFee = (newShares * _srvFeeRatio) / _perMil;
recipientTokenAmount = recipientTokenAmount - (_tokenAmount * _srvFeeRatio) / _perMil;
uint256 shareFeeRatio = _srvFeeAddr.length;
for (uint16 i = 0; i < _srvFeeAddr.length; i++) {
_shares[_srvFeeAddr[i]] = _shares[_srvFeeAddr[i]] + srvFee / shareFeeRatio;
recipientNewShare = recipientNewShare - srvFee / shareFeeRatio;
_emitTransferEvents(address(0x0), _srvFeeAddr[i], (_tokenAmount * _srvFeeRatio) / _perMil / shareFeeRatio, srvFee /shareFeeRatio);
}
}
_shares[_recipient] = _shares[_recipient] + recipientNewShare;
_emitTransferEvents(address(0x0), _recipient, recipientTokenAmount, recipientNewShare);
}
function burnFrom(address _from, uint256 _tokenAmount) external override onlyOwner {
if (_from == address(0)) revert E_ZeroAddr();
if (_getPooledTokenByShares(_shares[_from]) < _tokenAmount) revert E_AmountInvalid();
uint256 burnShares = _getSharesByPooledToken(_tokenAmount);
_totalShare = _totalShare - burnShares;
_pooledTotal = _pooledTotal - _tokenAmount;
_shares[_from] = _shares[_from] - burnShares;
_emitTransferEvents(_from, address(0x0), _tokenAmount, burnShares);
}
function _emitTransferEvents(address _from, address _to, uint256 _tokenAmount, uint256 _sharesAmount) internal {
emit Transfer(_from, _to, _tokenAmount);
emit TransferShares(_from, _to, _sharesAmount);
}
function rebase(uint256 _newTotalPooledToken) external override onlyOwner {
if (_totalShare == 0) revert E_Rebase();
emit Rebase(address(this), _pooledTotal, _newTotalPooledToken);
_pooledTotal = _newTotalPooledToken;
}
function getSharesByPooledToken(uint256 _tokenAmount) public view returns (uint256) {
return _getSharesByPooledToken(_tokenAmount);
}
function getPooledByShares(uint256 _sharesAmount) public view returns (uint256) {
return _getPooledTokenByShares(_sharesAmount);
}
function totalShare() external view returns (uint256) {
return _totalShare;
}
function sharesOf(address _account) external view returns (uint256) {
return _shares[_account];
}
function transferShares(address _recipient, uint256 _sharesAmount) external returns (uint256) {
_transferShares(msg.sender, _recipient, _sharesAmount);
uint256 tokensAmount = _getSharesByPooledToken(_sharesAmount);
_emitTransferEvents(msg.sender, _recipient, tokensAmount, _sharesAmount);
return tokensAmount;
}
function transferSharesFrom(
address _sender, address _recipient, uint256 _sharesAmount
) external returns (uint256) {
uint256 tokensAmount = _getSharesByPooledToken(_sharesAmount);
_spendAllowance(_sender, msg.sender, tokensAmount);
_transferShares(_sender, _recipient, _sharesAmount);
_emitTransferEvents(_sender, _recipient, tokensAmount, _sharesAmount);
return tokensAmount;
}
}
文件 14 的 15:ReentrancyGuard.sol
pragma solidity ^0.8.20;
abstract contract ReentrancyGuard {
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
_status = ENTERED;
}
function _nonReentrantAfter() private {
_status = NOT_ENTERED;
}
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
文件 15 的 15:SafeERC20.sol
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC20Permit} from "../extensions/IERC20Permit.sol";
import {Address} from "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
error SafeERC20FailedOperation(address token);
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data);
if (returndata.length != 0 && !abi.decode(returndata, (bool))) {
revert SafeERC20FailedOperation(address(token));
}
}
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
(bool success, bytes memory returndata) = address(token).call(data);
return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && address(token).code.length > 0;
}
}
{
"compilationTarget": {
"contracts/Doubler.sol": "Doubler"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_initOwner","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"target","type":"address"}],"name":"AddressEmptyCode","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"AddressInsufficientBalance","type":"error"},{"inputs":[],"name":"E_Asset","type":"error"},{"inputs":[],"name":"E_Balance","type":"error"},{"inputs":[],"name":"E_BlockOnce","type":"error"},{"inputs":[],"name":"E_Expected","type":"error"},{"inputs":[],"name":"E_FeeLimit","type":"error"},{"inputs":[],"name":"E_Initialized","type":"error"},{"inputs":[],"name":"E_InputLimit","type":"error"},{"inputs":[],"name":"E_PoolInit","type":"error"},{"inputs":[],"name":"E_SlipLimit","type":"error"},{"inputs":[],"name":"E_WithdrawLimit","type":"error"},{"inputs":[],"name":"E_ZeroAddr","type":"error"},{"inputs":[],"name":"FailedInnerCall","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"inputs":[],"name":"ReentrancyGuardReentrantCall","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"SafeERC20FailedOperation","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"qAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"aAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"bAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"cAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"curPrice","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"avg","type":"uint256"}],"name":"Input","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"address","name":"aToken","type":"address"},{"indexed":false,"internalType":"address","name":"bToken","type":"address"},{"indexed":false,"internalType":"address","name":"cToken","type":"address"}],"name":"NewPool","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","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":"asset","type":"address"},{"indexed":false,"internalType":"uint256","name":"aAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"bAmount","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"oldInputLowerOfMaximum","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"newInputLowerOfMaximum","type":"uint256"}],"name":"UpdateLowerOfInputMaximum","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":false,"internalType":"address","name":"creator","type":"address"},{"indexed":false,"internalType":"uint16","name":"inputFee","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"redeemFee","type":"uint16"},{"indexed":false,"internalType":"uint16","name":"withdrawFee","type":"uint16"}],"name":"UpdatePool","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"asset","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"cAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"bAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"assetAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"curPrice","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"avg","type":"uint256"}],"name":"Withdraw","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"}],"name":"getBTokenPoolTotal","outputs":[{"internalType":"uint256","name":"poolTotal","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"}],"name":"getPool","outputs":[{"components":[{"internalType":"uint16","name":"inputFee","type":"uint16"},{"internalType":"uint16","name":"redeemFee","type":"uint16"},{"internalType":"uint16","name":"withdrawFee","type":"uint16"},{"internalType":"uint256","name":"lastPrice","type":"uint256"},{"internalType":"uint256","name":"cLastRbTime","type":"uint256"},{"internalType":"uint256","name":"lowerOfInputMaximum","type":"uint256"},{"internalType":"address","name":"asset","type":"address"},{"internalType":"address","name":"aToken","type":"address"},{"internalType":"address","name":"bToken","type":"address"},{"internalType":"address","name":"cToken","type":"address"},{"internalType":"address","name":"creator","type":"address"}],"internalType":"struct IDoubler.Pool","name":"pool","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPrivateVar","outputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint16","name":"","type":"uint16"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_initFastPriceFeed","type":"address"},{"internalType":"address","name":"_initEcoAddr","type":"address"},{"internalType":"address","name":"_initWethAddr","type":"address"},{"internalType":"uint16","name":"_initRebaseBTokenShare","type":"uint16"}],"name":"initializeDoubler","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"},{"internalType":"uint256","name":"_qAmount","type":"uint256"},{"internalType":"address","name":"_to","type":"address"}],"name":"input","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_qAmount","type":"uint256"},{"internalType":"address","name":"_to","type":"address"}],"name":"inputEth","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint16","name":"inputFee","type":"uint16"},{"internalType":"uint16","name":"redeemFee","type":"uint16"},{"internalType":"uint16","name":"withdrawFee","type":"uint16"},{"internalType":"uint256","name":"lowerOfInputMaximum","type":"uint256"},{"internalType":"address","name":"asset","type":"address"},{"internalType":"address","name":"creator","type":"address"},{"internalType":"string","name":"tokenPre","type":"string"}],"internalType":"struct IDoubler.AddPool","name":"_addPool","type":"tuple"}],"name":"newPool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"}],"name":"rebaseCToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"},{"internalType":"uint256","name":"_aAmount","type":"uint256"},{"internalType":"uint256","name":"_bAmount","type":"uint256"},{"internalType":"uint16","name":"_clientSlip","type":"uint16"}],"name":"redeem","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint16","name":"inputFee","type":"uint16"},{"internalType":"uint16","name":"redeemFee","type":"uint16"},{"internalType":"uint16","name":"withdrawFee","type":"uint16"},{"internalType":"uint256","name":"lowerOfInputMaximum","type":"uint256"},{"internalType":"address","name":"asset","type":"address"},{"internalType":"address","name":"creator","type":"address"},{"internalType":"string","name":"tokenPre","type":"string"}],"internalType":"struct IDoubler.AddPool","name":"_updatePool","type":"tuple"}],"name":"updatePool","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_asset","type":"address"},{"internalType":"uint256","name":"_cAmount","type":"uint256"},{"internalType":"uint256","name":"_qAmount","type":"uint256"},{"internalType":"uint16","name":"_clientSlip","type":"uint16"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]