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

193 lines
6.2 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;
import {IERC20} from "../../intf/IERC20.sol";
import {SafeMath} from "../../lib/SafeMath.sol";
import {DecimalMath} from "../../lib/DecimalMath.sol";
import {SafeERC20} from "../../lib/SafeERC20.sol";
2020-11-18 17:51:50 +08:00
import {DVMStorage} from "./DVMStorage.sol";
2020-10-23 01:16:52 +08:00
2020-11-18 17:51:50 +08:00
contract DVMVault is DVMStorage {
2020-10-23 01:16:52 +08:00
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);
2020-11-30 00:04:57 +08:00
// ============ View Functions ============
function getVaultReserve() external view returns (uint256 baseReserve, uint256 quoteReserve) {
baseReserve = _BASE_RESERVE_;
quoteReserve = _QUOTE_RESERVE_;
}
2020-11-29 17:38:13 +08:00
// ============ Asset In ============
2020-10-23 01:16:52 +08:00
function getBaseInput() public view returns (uint256 input) {
2020-11-18 17:51:50 +08:00
return _BASE_TOKEN_.balanceOf(address(this)).sub(_BASE_RESERVE_);
2020-10-23 01:16:52 +08:00
}
function getQuoteInput() public view returns (uint256 input) {
2020-11-18 17:51:50 +08:00
return _QUOTE_TOKEN_.balanceOf(address(this)).sub(_QUOTE_RESERVE_);
2020-10-23 01:16:52 +08:00
}
2020-11-29 17:38:13 +08:00
// ============ Set States ============
2020-11-18 17:51:50 +08:00
function _sync() internal {
2020-11-29 17:38:13 +08:00
uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
uint256 quoteBalance = _QUOTE_TOKEN_.balanceOf(address(this));
2020-10-23 01:16:52 +08:00
if (baseBalance != _BASE_RESERVE_) {
_BASE_RESERVE_ = baseBalance;
}
if (quoteBalance != _QUOTE_RESERVE_) {
_QUOTE_RESERVE_ = quoteBalance;
}
}
2020-11-29 17:38:13 +08:00
// ============ Asset Out ============
2020-11-18 17:51:50 +08:00
function _transferBaseOut(address to, uint256 amount) internal {
2020-11-06 00:31:30 +08:00
if (amount > 0) {
2020-11-18 17:51:50 +08:00
_BASE_TOKEN_.safeTransfer(to, amount);
2020-11-06 00:31:30 +08:00
}
2020-10-23 01:16:52 +08:00
}
2020-11-18 17:51:50 +08:00
function _transferQuoteOut(address to, uint256 amount) internal {
2020-11-06 00:31:30 +08:00
if (amount > 0) {
2020-11-18 17:51:50 +08:00
_QUOTE_TOKEN_.safeTransfer(to, amount);
2020-11-06 00:31:30 +08:00
}
2020-10-23 01:16:52 +08:00
}
2020-11-29 17:38:13 +08:00
// ============ Shares (ERC20) ============
2020-10-23 01:16:52 +08:00
/**
* @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];
}
/**
* @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) {
2020-11-29 23:40:19 +08:00
_approve(msg.sender, spender, amount);
2020-10-23 01:16:52 +08:00
return true;
}
2020-11-30 00:04:57 +08:00
function _approve(
address owner,
address spender,
uint256 amount
) private {
2020-11-29 23:40:19 +08:00
_ALLOWED_[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
2020-10-23 01:16:52 +08:00
/**
* @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];
}
2020-11-18 17:51:50 +08:00
function _mint(address user, uint256 value) internal {
2020-10-23 01:16:52 +08:00
_SHARES_[user] = _SHARES_[user].add(value);
totalSupply = totalSupply.add(value);
emit Mint(user, value);
emit Transfer(address(0), user, value);
}
2020-11-18 17:51:50 +08:00
function _burn(address user, uint256 value) internal {
2020-10-23 01:16:52 +08:00
_SHARES_[user] = _SHARES_[user].sub(value);
totalSupply = totalSupply.sub(value);
emit Burn(user, value);
emit Transfer(user, address(0), value);
}
2020-11-05 00:26:45 +08:00
2020-11-29 23:40:19 +08:00
// ============================ Permit ======================================
2020-11-30 00:04:57 +08:00
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
require(deadline >= block.timestamp, "DODO_DVM_LP: EXPIRED");
2020-11-29 23:40:19 +08:00
bytes32 digest = keccak256(
abi.encodePacked(
2020-11-30 00:04:57 +08:00
"\x19\x01",
2020-11-29 23:40:19 +08:00
DOMAIN_SEPARATOR,
2020-11-30 00:04:57 +08:00
keccak256(
abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline)
)
2020-11-29 23:40:19 +08:00
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
2020-11-30 00:04:57 +08:00
require(
recoveredAddress != address(0) && recoveredAddress == owner,
"DODO_DVM_LP: INVALID_SIGNATURE"
);
2020-11-29 23:40:19 +08:00
_approve(owner, spender, value);
}
// ===========================================================================
2020-10-23 01:16:52 +08:00
}