2021-02-07 15:31:36 +00:00
|
|
|
pragma solidity ^0.7.0;
|
2020-11-20 14:49:48 +00:00
|
|
|
|
|
|
|
import { TokenInterface } from "./interfaces.sol";
|
|
|
|
import { Stores } from "./stores.sol";
|
|
|
|
import { DSMath } from "./math.sol";
|
|
|
|
|
2021-02-05 18:33:49 +00:00
|
|
|
abstract contract Basic is DSMath, Stores {
|
2020-11-20 14:49:48 +00:00
|
|
|
|
|
|
|
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
|
|
|
amt = (_amt / 10 ** (18 - _dec));
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
|
|
|
amt = mul(_amt, 10 ** (18 - _dec));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTokenBal(TokenInterface token) internal view returns(uint _amt) {
|
2021-02-07 17:33:48 +00:00
|
|
|
_amt = address(token) == ethAddr ? address(this).balance : token.balanceOf(address(this));
|
2020-11-20 14:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTokensDec(TokenInterface buyAddr, TokenInterface sellAddr) internal view returns(uint buyDec, uint sellDec) {
|
2021-02-07 17:33:48 +00:00
|
|
|
buyDec = address(buyAddr) == ethAddr ? 18 : buyAddr.decimals();
|
|
|
|
sellDec = address(sellAddr) == ethAddr ? 18 : sellAddr.decimals();
|
2020-11-20 14:49:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:40:28 +00:00
|
|
|
function encodeEvent(string memory eventName, bytes memory eventParam) internal pure returns (bytes memory) {
|
2021-02-07 15:31:36 +00:00
|
|
|
return abi.encode(eventName, eventParam);
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:55:04 +00:00
|
|
|
function approve(TokenInterface token, address spender, uint256 amount) internal {
|
|
|
|
try token.approve(spender, amount) {
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
token.approve(spender, 0);
|
|
|
|
token.approve(spender, amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:49:22 +00:00
|
|
|
function changeEthAddress(address buy, address sell) internal pure returns(TokenInterface _buy, TokenInterface _sell){
|
|
|
|
_buy = buy == ethAddr ? TokenInterface(wethAddr) : TokenInterface(buy);
|
|
|
|
_sell = sell == ethAddr ? TokenInterface(wethAddr) : TokenInterface(sell);
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:58:14 +00:00
|
|
|
function changeEthAddrToWethAddr(address token) internal pure returns(address tokenAddr){
|
|
|
|
tokenAddr = token == ethAddr ? wethAddr : token;
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:58:48 +00:00
|
|
|
function convertEthToWeth(bool isEth, TokenInterface token, uint amount) internal {
|
|
|
|
if(isEth) token.deposit{value: amount}();
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertWethToEth(bool isEth, TokenInterface token, uint amount) internal {
|
|
|
|
if(isEth) {
|
2021-06-24 19:55:04 +00:00
|
|
|
approve(token, address(token), amount);
|
2021-02-08 14:58:48 +00:00
|
|
|
token.withdraw(amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|