Files
dodo-contractV2/contracts/DODOVendingMachine/impl/DVMFunding.sol

78 lines
2.8 KiB
Solidity
Raw Normal View History

2020-10-23 01:16:52 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
pragma solidity 0.6.9;
pragma experimental ABIEncoderV2;
2020-11-18 17:51:50 +08:00
import {DVMVault} from "./DVMVault.sol";
2020-10-23 01:16:52 +08:00
import {DecimalMath} from "../../lib/DecimalMath.sol";
2020-11-06 16:03:18 +08:00
import {IDODOCallee} from "../../intf/IDODOCallee.sol";
2020-10-23 01:16:52 +08:00
2020-11-18 17:51:50 +08:00
contract DVMFunding is DVMVault {
2020-11-24 19:18:03 +08:00
// shares [round down]
function buyShares(address to)
external
preventReentrant
returns (
uint256 shares,
uint256 baseAmount,
uint256 quoteAmount
)
{
2020-11-18 17:51:50 +08:00
uint256 baseInput = getBaseInput();
uint256 quoteInput = getQuoteInput();
2020-10-23 01:16:52 +08:00
require(baseInput > 0, "NO_BASE_INPUT");
2020-11-23 10:43:12 +08:00
uint256 baseReserve = _BASE_RESERVE_;
2020-11-18 17:51:50 +08:00
uint256 quoteReserve = _QUOTE_RESERVE_;
2020-10-23 01:16:52 +08:00
// case 1. initial supply
2020-11-24 19:18:03 +08:00
// 包含了 baseReserve == 0 && quoteReserve == 0 的情况
// 在提币的时候向下取整。因此永远不会出现balance为0但totalsupply不为0的情况
// 但有可能出现reserve>0但totalSupply=0的场景
if (totalSupply == 0) {
2020-11-26 21:03:36 +08:00
shares = getBaseBalance(); // 以免出现balance很大但shares很小的情况
2020-11-24 19:18:03 +08:00
} else if (baseReserve > 0 && quoteReserve == 0) {
// case 2. supply when quote reserve is 0
2020-11-26 21:03:36 +08:00
shares = baseInput.mul(totalSupply).div(baseReserve);
2020-11-24 19:18:03 +08:00
} else if (baseReserve > 0 && quoteReserve > 0) {
// case 3. normal case
2020-10-27 02:53:23 +08:00
uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
2020-11-24 19:18:03 +08:00
uint256 mintRatio = quoteInputRatio < baseInputRatio ? quoteInputRatio : baseInputRatio;
2020-11-26 21:03:36 +08:00
shares = DecimalMath.mulFloor(totalSupply, mintRatio);
2020-10-27 02:53:23 +08:00
}
2020-11-26 21:03:36 +08:00
_mint(to, shares);
2020-11-18 17:51:50 +08:00
_sync();
2020-11-26 21:03:36 +08:00
return (shares, baseInput, quoteInput);
2020-10-23 01:16:52 +08:00
}
2020-11-24 19:18:03 +08:00
// withdraw amount [round down]
function sellShares(
uint256 shareAmount,
address to,
bytes calldata data
) external preventReentrant returns (uint256 baseAmount, uint256 quoteAmount) {
2020-11-18 17:51:50 +08:00
(uint256 baseBalance, uint256 quoteBalance) = getVaultBalance();
uint256 totalShares = totalSupply;
2020-11-24 19:18:03 +08:00
require(shareAmount <= _SHARES_[msg.sender], "DLP_NOT_ENOUGH");
2020-11-23 22:33:23 +08:00
baseAmount = baseBalance.mul(shareAmount).div(totalShares);
quoteAmount = quoteBalance.mul(shareAmount).div(totalShares);
2020-11-24 19:18:03 +08:00
_burn(msg.sender, shareAmount);
2020-11-18 17:51:50 +08:00
_transferBaseOut(to, baseAmount);
_transferQuoteOut(to, quoteAmount);
2020-11-24 19:18:03 +08:00
if (data.length > 0) {
IDODOCallee(to).DVMSellShareCall(
msg.sender,
shareAmount,
baseAmount,
quoteAmount,
data
);
}
2020-11-18 17:51:50 +08:00
_sync();
2020-10-23 01:16:52 +08:00
}
}