fix dvm test context
This commit is contained in:
65
contracts/DODOVendingMachine/impl/DVM.sol
Normal file
65
contracts/DODOVendingMachine/impl/DVM.sol
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
import {IExternalValue} from "../../lib/ExternalValue.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {DVMTrader} from "./DVMTrader.sol";
|
||||
import {DVMFunding} from "./DVMFunding.sol";
|
||||
import {DVMVault} from "./DVMVault.sol";
|
||||
|
||||
contract DVM is DVMTrader, DVMFunding {
|
||||
function init(
|
||||
address owner,
|
||||
address maintainer,
|
||||
address baseTokenAddress,
|
||||
address quoteTokenAddress,
|
||||
address lpFeeRateModel,
|
||||
address mtFeeRateModel,
|
||||
address tradePermissionManager,
|
||||
address gasPriceSource,
|
||||
uint256 i,
|
||||
uint256 k
|
||||
) external {
|
||||
initOwner(owner);
|
||||
_BASE_TOKEN_ = IERC20(baseTokenAddress);
|
||||
_QUOTE_TOKEN_ = IERC20(quoteTokenAddress);
|
||||
_LP_FEE_RATE_MODEL_ = IFeeRateModel(lpFeeRateModel);
|
||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(mtFeeRateModel);
|
||||
_TRADE_PERMISSION_ = IPermissionManager(tradePermissionManager);
|
||||
_GAS_PRICE_LIMIT_ = IExternalValue(gasPriceSource);
|
||||
_MAINTAINER_ = maintainer;
|
||||
_I_ = i;
|
||||
_K_ = k;
|
||||
|
||||
string memory connect = "_";
|
||||
string memory suffix = "DLP";
|
||||
string memory uid = string(abi.encodePacked(address(this)));
|
||||
name = string(
|
||||
abi.encodePacked(
|
||||
suffix,
|
||||
connect,
|
||||
_BASE_TOKEN_.symbol(),
|
||||
connect,
|
||||
_QUOTE_TOKEN_.symbol(),
|
||||
connect,
|
||||
uid
|
||||
)
|
||||
);
|
||||
symbol = "DLP";
|
||||
decimals = _BASE_TOKEN_.decimals();
|
||||
}
|
||||
|
||||
// ============ Version Control ============
|
||||
function version() external pure returns (uint256) {
|
||||
return 100; // 1.0.0
|
||||
}
|
||||
}
|
||||
73
contracts/DODOVendingMachine/impl/DVMFunding.sol
Normal file
73
contracts/DODOVendingMachine/impl/DVMFunding.sol
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {DVMVault} from "./DVMVault.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||
|
||||
contract DVMFunding is DVMVault {
|
||||
function buyShares(address to) external preventReentrant returns (uint256) {
|
||||
uint256 baseInput = getBaseInput();
|
||||
uint256 quoteInput = getQuoteInput();
|
||||
require(baseInput > 0, "NO_BASE_INPUT");
|
||||
uint256 baseReserve = _BASE_RESERVE_; // may save gas? 待确认
|
||||
uint256 quoteReserve = _QUOTE_RESERVE_;
|
||||
uint256 mintAmount;
|
||||
// case 1. initial supply
|
||||
if (baseReserve == 0 && quoteReserve == 0) {
|
||||
mintAmount = baseInput;
|
||||
}
|
||||
// case 2. supply when quote reserve is 0
|
||||
if (baseReserve > 0 && quoteReserve == 0) {
|
||||
uint256 mintRatio = DecimalMath.divFloor(baseInput, baseReserve);
|
||||
mintAmount = DecimalMath.mulFloor(totalSupply, mintRatio);
|
||||
}
|
||||
// case 3. normal case
|
||||
if (baseReserve > 0 && quoteReserve > 0) {
|
||||
uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
|
||||
uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
|
||||
uint256 mintRatio = baseInputRatio > quoteInputRatio ? quoteInputRatio : baseInputRatio;
|
||||
// 在提币的时候向下取整。因此永远不会出现,balance为0但totalsupply不为0的情况
|
||||
// 但有可能出现,reserve>0但totalSupply=0的场景
|
||||
uint256 totalShare = totalSupply;
|
||||
if (totalShare > 0) {
|
||||
mintAmount = DecimalMath.mulFloor(totalShare, mintRatio);
|
||||
} else {
|
||||
mintAmount = baseInput;
|
||||
}
|
||||
}
|
||||
_mint(to, mintAmount);
|
||||
_sync();
|
||||
}
|
||||
|
||||
function sellShares(
|
||||
address to,
|
||||
uint256 shareAmount,
|
||||
bytes calldata data
|
||||
) external preventReentrant returns (uint256) {
|
||||
require(_SHARES_[msg.sender] >= shareAmount, "SHARES_NOT_ENOUGH");
|
||||
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
|
||||
uint256 totalShares = totalSupply;
|
||||
_burn(msg.sender, shareAmount);
|
||||
uint256 baseAmount = baseBalance.mul(shareAmount).div(totalShares);
|
||||
uint256 quoteAmount = quoteBalance.mul(shareAmount).div(totalShares);
|
||||
_transferBaseOut(to, baseAmount);
|
||||
_transferQuoteOut(to, quoteAmount);
|
||||
_sync();
|
||||
if (data.length > 0)
|
||||
IDODOCallee(msg.sender).DVMSellShareCall(
|
||||
to,
|
||||
shareAmount,
|
||||
baseAmount,
|
||||
quoteAmount,
|
||||
data
|
||||
);
|
||||
}
|
||||
}
|
||||
91
contracts/DODOVendingMachine/impl/DVMStorage.sol
Normal file
91
contracts/DODOVendingMachine/impl/DVMStorage.sol
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {InitializableOwnable} from "../../lib/InitializableOwnable.sol";
|
||||
import {ReentrancyGuard} from "../../lib/ReentrancyGuard.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DODOMath} from "../../lib/DODOMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {IPermissionManager} from "../../lib/PermissionManager.sol";
|
||||
import {IExternalValue} from "../../lib/ExternalValue.sol";
|
||||
import {IFeeRateModel} from "../../intf/IFeeRateModel.sol";
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
|
||||
contract DVMStorage is InitializableOwnable, ReentrancyGuard {
|
||||
using SafeMath for uint256;
|
||||
|
||||
// ============ Variables for Control ============
|
||||
|
||||
IExternalValue public _GAS_PRICE_LIMIT_;
|
||||
|
||||
// ============ Advanced Controls ============
|
||||
|
||||
bool public _BUYING_CLOSE_;
|
||||
bool public _SELLING_CLOSE_;
|
||||
|
||||
IPermissionManager public _TRADE_PERMISSION_;
|
||||
|
||||
// ============ Core Address ============
|
||||
|
||||
address public _MAINTAINER_; // collect maintainer fee
|
||||
|
||||
IERC20 public _BASE_TOKEN_;
|
||||
IERC20 public _QUOTE_TOKEN_;
|
||||
|
||||
uint256 public _BASE_RESERVE_;
|
||||
uint256 public _QUOTE_RESERVE_;
|
||||
|
||||
// ============ Shares ============
|
||||
|
||||
string public symbol;
|
||||
uint256 public decimals;
|
||||
string public name;
|
||||
|
||||
uint256 public totalSupply;
|
||||
mapping(address => uint256) internal _SHARES_;
|
||||
mapping(address => mapping(address => uint256)) internal _ALLOWED_;
|
||||
|
||||
// ============ Variables for Pricing ============
|
||||
|
||||
IFeeRateModel public _LP_FEE_RATE_MODEL_;
|
||||
IFeeRateModel public _MT_FEE_RATE_MODEL_;
|
||||
uint256 public _K_;
|
||||
uint256 public _I_;
|
||||
|
||||
// ============ Setting Functions ============
|
||||
|
||||
function setLpFeeRateModel(address newLpFeeRateModel) external onlyOwner {
|
||||
_LP_FEE_RATE_MODEL_ = IFeeRateModel(newLpFeeRateModel);
|
||||
}
|
||||
|
||||
function setMtFeeRateModel(address newMtFeeRateModel) external onlyOwner {
|
||||
_MT_FEE_RATE_MODEL_ = IFeeRateModel(newMtFeeRateModel);
|
||||
}
|
||||
|
||||
function setTradePermissionManager(address newTradePermissionManager) external onlyOwner {
|
||||
_TRADE_PERMISSION_ = IPermissionManager(newTradePermissionManager);
|
||||
}
|
||||
|
||||
function setMaintainer(address newMaintainer) external onlyOwner {
|
||||
_MAINTAINER_ = newMaintainer;
|
||||
}
|
||||
|
||||
function setGasPriceSource(address newGasPriceLimitSource) external onlyOwner {
|
||||
_GAS_PRICE_LIMIT_ = IExternalValue(newGasPriceLimitSource);
|
||||
}
|
||||
|
||||
function setBuy(bool open) external onlyOwner {
|
||||
_BUYING_CLOSE_ = !open;
|
||||
}
|
||||
|
||||
function setSell(bool open) external onlyOwner {
|
||||
_SELLING_CLOSE_ = !open;
|
||||
}
|
||||
}
|
||||
234
contracts/DODOVendingMachine/impl/DVMTrader.sol
Normal file
234
contracts/DODOVendingMachine/impl/DVMTrader.sol
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {DVMVault} from "./DVMVault.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {DODOMath} from "../../lib/DODOMath.sol";
|
||||
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
|
||||
import {PMMPricing} from "../../lib/PMMPricing.sol";
|
||||
|
||||
contract DVMTrader is DVMVault {
|
||||
using SafeMath for uint256;
|
||||
|
||||
// ============ Modifiers ============
|
||||
|
||||
modifier isBuyAllow(address trader) {
|
||||
require(!_BUYING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader), "TRADER_BUY_NOT_ALLOWED");
|
||||
_;
|
||||
}
|
||||
|
||||
modifier isSellAllow(address trader) {
|
||||
require(
|
||||
!_SELLING_CLOSE_ && _TRADE_PERMISSION_.isAllowed(trader),
|
||||
"TRADER_SELL_NOT_ALLOWED"
|
||||
);
|
||||
_;
|
||||
}
|
||||
|
||||
modifier limitGasPrice() {
|
||||
require(tx.gasprice <= _GAS_PRICE_LIMIT_.get(), "GAS_PRICE_EXCEED");
|
||||
_;
|
||||
}
|
||||
|
||||
// ============ Execute ============
|
||||
|
||||
function sellBase(address to)
|
||||
external
|
||||
preventReentrant
|
||||
limitGasPrice
|
||||
isSellAllow(to)
|
||||
returns (uint256 receiveQuoteAmount)
|
||||
{
|
||||
uint256 baseInput = getBaseInput();
|
||||
uint256 mtFee;
|
||||
(receiveQuoteAmount, mtFee) = querySellBase(tx.origin, baseInput);
|
||||
_transferQuoteOut(to, receiveQuoteAmount);
|
||||
_transferQuoteOut(_MAINTAINER_, mtFee);
|
||||
_sync();
|
||||
return receiveQuoteAmount;
|
||||
}
|
||||
|
||||
function sellQuote(address to)
|
||||
external
|
||||
preventReentrant
|
||||
limitGasPrice
|
||||
isBuyAllow(to)
|
||||
returns (uint256 receiveBaseAmount)
|
||||
{
|
||||
uint256 quoteInput = getQuoteInput();
|
||||
uint256 mtFee;
|
||||
(receiveBaseAmount, mtFee) = querySellQuote(tx.origin, quoteInput);
|
||||
_transferBaseOut(to, receiveBaseAmount);
|
||||
_transferBaseOut(_MAINTAINER_, mtFee);
|
||||
_sync();
|
||||
return receiveBaseAmount;
|
||||
}
|
||||
|
||||
// 这是一个试验性质的函数
|
||||
// 没有走标准库,需要仔细考虑下
|
||||
function flashLoan(
|
||||
uint256 baseAmount,
|
||||
uint256 quoteAmount,
|
||||
address assetTo,
|
||||
bytes calldata data
|
||||
) external preventReentrant {
|
||||
_transferBaseOut(assetTo, baseAmount);
|
||||
_transferQuoteOut(assetTo, quoteAmount);
|
||||
|
||||
if (data.length > 0)
|
||||
IDODOCallee(assetTo).DVMFlashLoanCall(msg.sender, baseAmount, quoteAmount, data);
|
||||
|
||||
(uint256 baseReserve, uint256 quoteReserve) = getVaultReserve();
|
||||
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
|
||||
|
||||
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(tx.origin);
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(tx.origin);
|
||||
if (baseBalance < baseReserve) {
|
||||
uint256 validBaseOut = DecimalMath.divCeil(
|
||||
baseReserve - baseBalance,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
baseBalance = baseReserve.sub(validBaseOut);
|
||||
_transferBaseOut(_MAINTAINER_, DecimalMath.mulCeil(validBaseOut, mtFeeRate));
|
||||
}
|
||||
if (quoteBalance < quoteReserve) {
|
||||
uint256 validQuoteOut = DecimalMath.divCeil(
|
||||
quoteReserve - quoteBalance,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
quoteBalance = quoteReserve.sub(validQuoteOut);
|
||||
_transferQuoteOut(_MAINTAINER_, DecimalMath.mulCeil(validQuoteOut, mtFeeRate));
|
||||
}
|
||||
|
||||
require(
|
||||
calculateBase0(baseBalance, quoteBalance) >= calculateBase0(baseReserve, quoteReserve),
|
||||
"FLASH_LOAN_FAILED"
|
||||
);
|
||||
|
||||
_sync();
|
||||
}
|
||||
|
||||
function querySellBase(address trader, uint256 payBaseAmount)
|
||||
public
|
||||
view
|
||||
returns (uint256 receiveQuoteAmount, uint256 mtFee)
|
||||
{
|
||||
(receiveQuoteAmount, ) = PMMPricing.sellBaseToken(getPMMState(), payBaseAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
mtFee = DecimalMath.mulCeil(receiveQuoteAmount, mtFeeRate);
|
||||
receiveQuoteAmount = DecimalMath.mulFloor(
|
||||
receiveQuoteAmount,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
|
||||
return (receiveQuoteAmount, mtFee);
|
||||
}
|
||||
|
||||
function querySellQuote(address trader, uint256 payQuoteAmount)
|
||||
public
|
||||
view
|
||||
returns (uint256 receiveBaseAmount, uint256 mtFee)
|
||||
{
|
||||
(receiveBaseAmount, ) = PMMPricing.sellQuoteToken(getPMMState(), payQuoteAmount);
|
||||
|
||||
uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
mtFee = DecimalMath.mulCeil(receiveBaseAmount, mtFeeRate);
|
||||
receiveBaseAmount = DecimalMath.mulFloor(
|
||||
receiveBaseAmount,
|
||||
DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
);
|
||||
return (receiveBaseAmount, mtFee);
|
||||
}
|
||||
|
||||
// // 这是一个仅供查询的合约,所有交易都是基于先给input,再输出output的
|
||||
// // 所以想要买10ETH,这个函数可以给你一个大概的成本,你用这个成本输入,最后能否得到10ETH是要看情况的
|
||||
// function queryBuyBase(address trader, uint256 receiveBaseAmount)
|
||||
// public
|
||||
// view
|
||||
// returns (uint256 payQuoteAmount)
|
||||
// {
|
||||
// uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
// uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
// uint256 validReceiveBaseAmount = DecimalMath.divCeil(
|
||||
// receiveBaseAmount,
|
||||
// DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
// );
|
||||
// (uint256 baseReserve, uint256 quoteReserve) = getVaultReserve();
|
||||
// require(baseReserve > validReceiveBaseAmount, "DODO_BASE_BALANCE_NOT_ENOUGH");
|
||||
|
||||
// uint256 B0 = calculateBase0(baseReserve, quoteReserve);
|
||||
// uint256 B2 = baseReserve.sub(validReceiveBaseAmount);
|
||||
// payQuoteAmount = DODOMath._GeneralIntegrate(B0, baseReserve, B2, _I_, _K_);
|
||||
// return payQuoteAmount;
|
||||
// }
|
||||
|
||||
// function queryBuyQuote(address trader, uint256 receiveQuoteAmount)
|
||||
// public
|
||||
// view
|
||||
// returns (uint256 payBaseAmount)
|
||||
// {
|
||||
// uint256 mtFeeRate = _MT_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
// uint256 lpFeeRate = _LP_FEE_RATE_MODEL_.getFeeRate(trader);
|
||||
// uint256 validReceiveQuoteAmount = DecimalMath.divCeil(
|
||||
// receiveQuoteAmount,
|
||||
// DecimalMath.ONE.sub(mtFeeRate).sub(lpFeeRate)
|
||||
// );
|
||||
// (uint256 baseReserve, uint256 quoteReserve) = getVaultReserve();
|
||||
// require(quoteReserve > validReceiveQuoteAmount, "DODO_QUOTE_BALANCE_NOT_ENOUGH");
|
||||
|
||||
// uint256 B0 = calculateBase0(baseReserve, quoteReserve);
|
||||
// uint256 fairAmount = DecimalMath.divFloor(validReceiveQuoteAmount, _I_);
|
||||
// payBaseAmount = DODOMath._SolveQuadraticFunctionForTrade(
|
||||
// B0,
|
||||
// baseReserve,
|
||||
// fairAmount,
|
||||
// true,
|
||||
// _K_
|
||||
// );
|
||||
// return payBaseAmount;
|
||||
// }
|
||||
|
||||
function getMidPrice() public view returns (uint256 midPrice) {
|
||||
(uint256 baseReserve, uint256 quoteReserve) = getVaultReserve();
|
||||
uint256 B0 = calculateBase0(baseReserve, quoteReserve);
|
||||
|
||||
uint256 offsetRatio = DecimalMath.ONE.mul(B0).div(baseReserve).mul(B0).div(baseReserve);
|
||||
uint256 offset = DecimalMath.ONE.sub(_K_).add(DecimalMath.mulFloor(offsetRatio, _K_));
|
||||
return DecimalMath.mulFloor(_I_, offset);
|
||||
}
|
||||
|
||||
// ============ Helper Functions ============
|
||||
|
||||
function getPMMState() public view returns (PMMPricing.PMMState memory state) {
|
||||
state.i = _I_;
|
||||
state.K = _K_;
|
||||
state.B = _BASE_RESERVE_;
|
||||
state.Q = _QUOTE_RESERVE_;
|
||||
state.B0 = calculateBase0(state.B, state.Q);
|
||||
state.Q0 = 0;
|
||||
state.R = PMMPricing.RState.ABOVE_ONE;
|
||||
return state;
|
||||
}
|
||||
|
||||
function calculateBase0(uint256 baseAmount, uint256 quoteAmount) public view returns (uint256) {
|
||||
uint256 fairAmount = DecimalMath.divFloor(quoteAmount, _I_);
|
||||
return DODOMath._SolveQuadraticFunctionForTarget(baseAmount, _K_, fairAmount);
|
||||
}
|
||||
|
||||
function getBase0() public view returns (uint256) {
|
||||
(uint256 baseAmount, uint256 quoteAmount) = getVaultReserve();
|
||||
uint256 fairAmount = DecimalMath.divFloor(quoteAmount, _I_);
|
||||
return DODOMath._SolveQuadraticFunctionForTarget(baseAmount, _K_, fairAmount);
|
||||
}
|
||||
}
|
||||
164
contracts/DODOVendingMachine/impl/DVMVault.sol
Normal file
164
contracts/DODOVendingMachine/impl/DVMVault.sol
Normal file
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
import {IERC20} from "../../intf/IERC20.sol";
|
||||
import {SafeMath} from "../../lib/SafeMath.sol";
|
||||
import {DecimalMath} from "../../lib/DecimalMath.sol";
|
||||
import {SafeERC20} from "../../lib/SafeERC20.sol";
|
||||
import {DVMStorage} from "./DVMStorage.sol";
|
||||
|
||||
contract DVMVault is DVMStorage {
|
||||
using SafeMath for uint256;
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
// ============ Events ============
|
||||
|
||||
event Transfer(address indexed from, address indexed to, uint256 amount);
|
||||
|
||||
event Approval(address indexed owner, address indexed spender, uint256 amount);
|
||||
|
||||
event Mint(address indexed user, uint256 value);
|
||||
|
||||
event Burn(address indexed user, uint256 value);
|
||||
|
||||
// Vault related
|
||||
|
||||
function getVaultBalance() public view returns (uint256 baseBalance, uint256 quoteBalance) {
|
||||
return (_BASE_TOKEN_.balanceOf(address(this)), _QUOTE_TOKEN_.balanceOf(address(this)));
|
||||
}
|
||||
|
||||
function getVaultReserve() public view returns (uint256 baseReserve, uint256 quoteReserve) {
|
||||
return (_BASE_RESERVE_, _QUOTE_RESERVE_);
|
||||
}
|
||||
|
||||
function getBaseBalance() public view returns (uint256 baseBalance) {
|
||||
return _BASE_TOKEN_.balanceOf(address(this));
|
||||
}
|
||||
|
||||
function getQuoteBalance() public view returns (uint256 quoteBalance) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this));
|
||||
}
|
||||
|
||||
function getBaseInput() public view returns (uint256 input) {
|
||||
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
|
||||
}
|
||||
|
||||
function getQuoteInput() public view returns (uint256 input) {
|
||||
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
|
||||
}
|
||||
|
||||
function _sync() internal {
|
||||
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
|
||||
if (baseBalance != _BASE_RESERVE_) {
|
||||
_BASE_RESERVE_ = baseBalance;
|
||||
}
|
||||
if (quoteBalance != _QUOTE_RESERVE_) {
|
||||
_QUOTE_RESERVE_ = quoteBalance;
|
||||
}
|
||||
}
|
||||
|
||||
function _transferBaseOut(address to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
_BASE_TOKEN_.safeTransfer(to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
function _transferQuoteOut(address to, uint256 amount) internal {
|
||||
if (amount > 0) {
|
||||
_QUOTE_TOKEN_.safeTransfer(to, amount);
|
||||
}
|
||||
}
|
||||
|
||||
// Shares related
|
||||
/**
|
||||
* @dev transfer token for a specified address
|
||||
* @param to The address to transfer to.
|
||||
* @param amount The amount to be transferred.
|
||||
*/
|
||||
function transfer(address to, uint256 amount) public returns (bool) {
|
||||
require(amount <= _SHARES_[msg.sender], "BALANCE_NOT_ENOUGH");
|
||||
|
||||
_SHARES_[msg.sender] = _SHARES_[msg.sender].sub(amount);
|
||||
_SHARES_[to] = _SHARES_[to].add(amount);
|
||||
emit Transfer(msg.sender, to, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Gets the balance of the specified address.
|
||||
* @param owner The address to query the the balance of.
|
||||
* @return balance An uint256 representing the amount owned by the passed address.
|
||||
*/
|
||||
function balanceOf(address owner) external view returns (uint256 balance) {
|
||||
return _SHARES_[owner];
|
||||
}
|
||||
|
||||
function shareRatioOf(address owner) external view returns (uint256 shareRatio) {
|
||||
return DecimalMath.divFloor(_SHARES_[owner], totalSupply);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Transfer tokens from one address to another
|
||||
* @param from address The address which you want to send tokens from
|
||||
* @param to address The address which you want to transfer to
|
||||
* @param amount uint256 the amount of tokens to be transferred
|
||||
*/
|
||||
function transferFrom(
|
||||
address from,
|
||||
address to,
|
||||
uint256 amount
|
||||
) public returns (bool) {
|
||||
require(amount <= _SHARES_[from], "BALANCE_NOT_ENOUGH");
|
||||
require(amount <= _ALLOWED_[from][msg.sender], "ALLOWANCE_NOT_ENOUGH");
|
||||
|
||||
_SHARES_[from] = _SHARES_[from].sub(amount);
|
||||
_SHARES_[to] = _SHARES_[to].add(amount);
|
||||
_ALLOWED_[from][msg.sender] = _ALLOWED_[from][msg.sender].sub(amount);
|
||||
emit Transfer(from, to, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
|
||||
* @param spender The address which will spend the funds.
|
||||
* @param amount The amount of tokens to be spent.
|
||||
*/
|
||||
function approve(address spender, uint256 amount) public returns (bool) {
|
||||
_ALLOWED_[msg.sender][spender] = amount;
|
||||
emit Approval(msg.sender, spender, amount);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Function to check the amount of tokens that an owner _ALLOWED_ to a spender.
|
||||
* @param owner address The address which owns the funds.
|
||||
* @param spender address The address which will spend the funds.
|
||||
* @return A uint256 specifying the amount of tokens still available for the spender.
|
||||
*/
|
||||
function allowance(address owner, address spender) public view returns (uint256) {
|
||||
return _ALLOWED_[owner][spender];
|
||||
}
|
||||
|
||||
function _mint(address user, uint256 value) internal {
|
||||
_SHARES_[user] = _SHARES_[user].add(value);
|
||||
totalSupply = totalSupply.add(value);
|
||||
emit Mint(user, value);
|
||||
emit Transfer(address(0), user, value);
|
||||
}
|
||||
|
||||
function _burn(address user, uint256 value) internal {
|
||||
_SHARES_[user] = _SHARES_[user].sub(value);
|
||||
totalSupply = totalSupply.sub(value);
|
||||
emit Burn(user, value);
|
||||
emit Transfer(user, address(0), value);
|
||||
}
|
||||
|
||||
// function approveAndCall()
|
||||
}
|
||||
Reference in New Issue
Block a user