mirror of
https://github.com/Instadapp/InstaContract.git
synced 2024-07-29 22:47:45 +00:00
More refactoring around SafeMath and IERC20
This commit is contained in:
parent
8d706d8ae0
commit
221ff14957
|
@ -2,6 +2,8 @@
|
|||
|
||||
pragma solidity ^0.4.24;
|
||||
|
||||
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||
|
||||
|
||||
contract AddressRegistry {
|
||||
|
||||
|
@ -24,6 +26,9 @@ contract AddressRegistry {
|
|||
|
||||
contract ManageRegistry is AddressRegistry {
|
||||
|
||||
using SafeMath for uint;
|
||||
using SafeMath for uint256;
|
||||
|
||||
address public pendingAdmin;
|
||||
uint public pendingTime;
|
||||
|
||||
|
@ -33,25 +38,25 @@ contract ManageRegistry is AddressRegistry {
|
|||
}
|
||||
|
||||
function setAddr(string name, address newAddr) public {
|
||||
if (keccak256(name) != keccak256("admin")) {
|
||||
if (keccak256(abi.encodePacked(name)) != keccak256(abi.encodePacked("admin"))) {
|
||||
require(
|
||||
governors[msg.sender],
|
||||
"Permission Denied"
|
||||
);
|
||||
pendingAdmin = newAddr;
|
||||
pendingTime = block.timestamp + (24 * 60 * 60); // adding 24 hours
|
||||
pendingTime = block.timestamp.add(24 * 60 * 60); // adding 24 hours
|
||||
} else {
|
||||
require(
|
||||
msg.sender == getAddr("admin"),
|
||||
"Permission Denied"
|
||||
);
|
||||
registry[keccak256(name)] = newAddr;
|
||||
registry[keccak256(abi.encodePacked(name))] = newAddr;
|
||||
emit AddressChanged(name, newAddr);
|
||||
}
|
||||
}
|
||||
|
||||
function getAddr(string name) public view returns(address addr) {
|
||||
addr = registry[keccak256(name)];
|
||||
addr = registry[keccak256(abi.encodePacked(name))];
|
||||
require(addr != address(0), "Not a valid address.");
|
||||
}
|
||||
|
||||
|
@ -80,7 +85,7 @@ contract ManageResolvers is ManageRegistry {
|
|||
contract InitRegistry is ManageResolvers {
|
||||
|
||||
constructor() public {
|
||||
registry[keccak256("admin")] = msg.sender;
|
||||
registry[keccak256(abi.encodePacked("admin"))] = msg.sender;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
pragma solidity ^0.4.24;
|
||||
|
||||
interface token {
|
||||
function transfer(address receiver, uint amount) external returns (bool);
|
||||
}
|
||||
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||
|
||||
interface AddressRegistry {
|
||||
function getAddr(string name) external returns(address);
|
||||
|
@ -25,7 +23,7 @@ contract Registry {
|
|||
addr = aRegistry.getAddr(name);
|
||||
require(addr != address(0), "Invalid Address");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -35,7 +33,6 @@ contract FeeDetail is Registry {
|
|||
function setFees(uint cut) public onlyAdmin { // 200 means 0.5%
|
||||
fees = cut;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,18 +40,18 @@ contract MoatResolver is FeeDetail {
|
|||
|
||||
function () public payable {}
|
||||
|
||||
constructor(address rAddr, uint cut) public { // 200 means 0.5%
|
||||
constructor(address rAddr, uint cut) public { // 200 means 0.5%
|
||||
registryAddress = rAddr;
|
||||
setFees(cut);
|
||||
}
|
||||
|
||||
function collectToken(address tokenAddress, uint amount) public onlyAdmin {
|
||||
if (tokenAddress == 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee) {
|
||||
if (tokenAddress == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
|
||||
msg.sender.transfer(amount);
|
||||
} else {
|
||||
token tokenFunctions = token(tokenAddress);
|
||||
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||
tokenFunctions.transfer(msg.sender, amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ contract Registry {
|
|||
addr = aRegistry.getAddr(name);
|
||||
require(addr != address(0), "Invalid Address");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -41,7 +41,7 @@ contract AssetDB is Registry {
|
|||
using SafeMath for uint256;
|
||||
|
||||
mapping(address => mapping(address => uint)) balances;
|
||||
address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
||||
address eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
function getBalance(
|
||||
address assetHolder,
|
||||
|
@ -113,4 +113,4 @@ contract MoatAsset is AssetDB {
|
|||
|
||||
function () public payable {}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,9 @@
|
|||
|
||||
pragma solidity ^0.4.24;
|
||||
|
||||
interface token {
|
||||
function transfer(address receiver, uint amount) external returns (bool);
|
||||
function approve(address spender, uint256 value) external returns (bool);
|
||||
function transferFrom(address from, address to, uint amount) external returns (bool);
|
||||
}
|
||||
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||
|
||||
|
||||
interface AddressRegistry {
|
||||
function getAddr(string name) external returns(address);
|
||||
|
@ -62,7 +60,7 @@ contract Registry {
|
|||
addr = aRegistry.getAddr(name);
|
||||
require(addr != address(0), "Invalid Address");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -79,9 +77,9 @@ contract Trade is Registry {
|
|||
address affiliate
|
||||
);
|
||||
|
||||
// ropsten network
|
||||
// ropsten network
|
||||
address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
|
||||
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
||||
address public eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
function executeTrade(
|
||||
address trader,
|
||||
|
@ -121,7 +119,7 @@ contract Trade is Registry {
|
|||
|
||||
function fetchToken(address trader, address src, uint srcAmt) internal {
|
||||
if (src != eth) {
|
||||
token tokenFunctions = token(src);
|
||||
IERC20 tokenFunctions = IERC20(src);
|
||||
tokenFunctions.transferFrom(trader, address(this), srcAmt);
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +132,7 @@ contract Trade is Registry {
|
|||
if (src == eth) {
|
||||
getAddress("admin").transfer(fees);
|
||||
} else {
|
||||
token tokenFunctions = token(src);
|
||||
IERC20 tokenFunctions = IERC20(src);
|
||||
tokenFunctions.transfer(getAddress("admin"), fees);
|
||||
}
|
||||
}
|
||||
|
@ -142,7 +140,7 @@ contract Trade is Registry {
|
|||
|
||||
function allowKyber(address[] tokenArr) public {
|
||||
for (uint i = 0; i < tokenArr.length; i++) {
|
||||
token tokenFunctions = token(tokenArr[i]);
|
||||
IERC20 tokenFunctions = IERC20(tokenArr[i]);
|
||||
tokenFunctions.approve(getAddress("kyber"), 2**256 - 1);
|
||||
}
|
||||
}
|
||||
|
@ -162,9 +160,9 @@ contract MoatKyber is Trade {
|
|||
if (tokenAddress == eth) {
|
||||
msg.sender.transfer(amount);
|
||||
} else {
|
||||
token tokenFunctions = token(tokenAddress);
|
||||
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||
tokenFunctions.transfer(msg.sender, amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,11 +7,9 @@
|
|||
|
||||
pragma solidity 0.4.24;
|
||||
|
||||
interface token {
|
||||
function transfer(address receiver, uint amount) external returns (bool);
|
||||
function approve(address spender, uint256 value) external returns (bool);
|
||||
function transferFrom(address from, address to, uint amount) external returns (bool);
|
||||
}
|
||||
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||
|
||||
|
||||
interface AddressRegistry {
|
||||
function getAddr(string name) external returns(address);
|
||||
|
@ -72,7 +70,7 @@ contract Registry {
|
|||
addr = aRegistry.getAddr(name);
|
||||
require(addr != address(0), "Invalid Address");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -83,7 +81,7 @@ contract GlobalVar is Registry {
|
|||
address public peth = 0xf4d791139cE033Ad35DB2B2201435fAd668B1b64;
|
||||
address public mkr = 0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD;
|
||||
address public dai = 0xC4375B7De8af5a38a93548eb8453a498222C4fF2;
|
||||
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
||||
address public eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||
|
||||
address public cdpAddr = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2;
|
||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||
|
@ -103,7 +101,7 @@ contract IssueLoan is GlobalVar {
|
|||
address borrower,
|
||||
uint ethLock,
|
||||
uint daiDraw
|
||||
) public payable onlyUserOrResolver(borrower)
|
||||
) public payable onlyUserOrResolver(borrower)
|
||||
{
|
||||
if (cdps[borrower] == 0x0000000000000000000000000000000000000000000000000000000000000000) {
|
||||
cdps[borrower] = loanMaster.open();
|
||||
|
@ -129,7 +127,7 @@ contract IssueLoan is GlobalVar {
|
|||
function drawDAI(address borrower, uint daiDraw) public onlyUserOrResolver(borrower) {
|
||||
loanMaster.draw(cdps[borrower], daiDraw);
|
||||
uint fees = deductFees(daiDraw);
|
||||
token tokenFunctions = token(dai);
|
||||
IERC20 tokenFunctions = IERC20(dai);
|
||||
tokenFunctions.transfer(getAddress("resolver"), daiDraw - fees);
|
||||
emit LoanedDAI(borrower, daiDraw, fees);
|
||||
}
|
||||
|
@ -143,7 +141,7 @@ contract IssueLoan is GlobalVar {
|
|||
fees = moatRes.fees();
|
||||
if (fees > 0) {
|
||||
fees = volume / fees;
|
||||
token tokenFunctions = token(dai);
|
||||
IERC20 tokenFunctions = IERC20(dai);
|
||||
tokenFunctions.transfer(getAddress("admin"), fees);
|
||||
}
|
||||
}
|
||||
|
@ -160,7 +158,7 @@ contract RepayLoan is IssueLoan {
|
|||
address borrower,
|
||||
uint daiWipe,
|
||||
uint ethFree
|
||||
) public onlyUserOrResolver(borrower)
|
||||
) public onlyUserOrResolver(borrower)
|
||||
{
|
||||
if (daiWipe > 0) {
|
||||
wipeDAI(borrower, daiWipe);
|
||||
|
@ -171,7 +169,7 @@ contract RepayLoan is IssueLoan {
|
|||
}
|
||||
|
||||
function wipeDAI(address borrower, uint daiWipe) public {
|
||||
token tokenFunction = token(dai);
|
||||
IERC20 tokenFunction = IERC20(dai);
|
||||
tokenFunction.transferFrom(msg.sender, address(this), daiWipe);
|
||||
loanMaster.wipe(cdps[borrower], daiWipe);
|
||||
emit WipedDAI(borrower, daiWipe);
|
||||
|
@ -203,13 +201,13 @@ contract BorrowTasks is RepayLoan {
|
|||
}
|
||||
|
||||
function approveERC20() public {
|
||||
token wethTkn = token(weth);
|
||||
IERC20 wethTkn = IERC20(weth);
|
||||
wethTkn.approve(cdpAddr, 2**256 - 1);
|
||||
token pethTkn = token(peth);
|
||||
IERC20 pethTkn = IERC20(peth);
|
||||
pethTkn.approve(cdpAddr, 2**256 - 1);
|
||||
token mkrTkn = token(mkr);
|
||||
IERC20 mkrTkn = IERC20(mkr);
|
||||
mkrTkn.approve(cdpAddr, 2**256 - 1);
|
||||
token daiTkn = token(dai);
|
||||
IERC20 daiTkn = IERC20(dai);
|
||||
daiTkn.approve(cdpAddr, 2**256 - 1);
|
||||
}
|
||||
}
|
||||
|
@ -228,9 +226,9 @@ contract MoatMaker is BorrowTasks {
|
|||
if (tokenAddress == eth) {
|
||||
msg.sender.transfer(amount);
|
||||
} else {
|
||||
token tokenFunctions = token(tokenAddress);
|
||||
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||
tokenFunctions.transfer(msg.sender, amount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user