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;
|
pragma solidity ^0.4.24;
|
||||||
|
|
||||||
|
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||||
|
|
||||||
|
|
||||||
contract AddressRegistry {
|
contract AddressRegistry {
|
||||||
|
|
||||||
|
@ -24,6 +26,9 @@ contract AddressRegistry {
|
||||||
|
|
||||||
contract ManageRegistry is AddressRegistry {
|
contract ManageRegistry is AddressRegistry {
|
||||||
|
|
||||||
|
using SafeMath for uint;
|
||||||
|
using SafeMath for uint256;
|
||||||
|
|
||||||
address public pendingAdmin;
|
address public pendingAdmin;
|
||||||
uint public pendingTime;
|
uint public pendingTime;
|
||||||
|
|
||||||
|
@ -33,25 +38,25 @@ contract ManageRegistry is AddressRegistry {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setAddr(string name, address newAddr) public {
|
function setAddr(string name, address newAddr) public {
|
||||||
if (keccak256(name) != keccak256("admin")) {
|
if (keccak256(abi.encodePacked(name)) != keccak256(abi.encodePacked("admin"))) {
|
||||||
require(
|
require(
|
||||||
governors[msg.sender],
|
governors[msg.sender],
|
||||||
"Permission Denied"
|
"Permission Denied"
|
||||||
);
|
);
|
||||||
pendingAdmin = newAddr;
|
pendingAdmin = newAddr;
|
||||||
pendingTime = block.timestamp + (24 * 60 * 60); // adding 24 hours
|
pendingTime = block.timestamp.add(24 * 60 * 60); // adding 24 hours
|
||||||
} else {
|
} else {
|
||||||
require(
|
require(
|
||||||
msg.sender == getAddr("admin"),
|
msg.sender == getAddr("admin"),
|
||||||
"Permission Denied"
|
"Permission Denied"
|
||||||
);
|
);
|
||||||
registry[keccak256(name)] = newAddr;
|
registry[keccak256(abi.encodePacked(name))] = newAddr;
|
||||||
emit AddressChanged(name, newAddr);
|
emit AddressChanged(name, newAddr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAddr(string name) public view returns(address addr) {
|
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.");
|
require(addr != address(0), "Not a valid address.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +85,7 @@ contract ManageResolvers is ManageRegistry {
|
||||||
contract InitRegistry is ManageResolvers {
|
contract InitRegistry is ManageResolvers {
|
||||||
|
|
||||||
constructor() public {
|
constructor() public {
|
||||||
registry[keccak256("admin")] = msg.sender;
|
registry[keccak256(abi.encodePacked("admin"))] = msg.sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,6 @@
|
||||||
pragma solidity ^0.4.24;
|
pragma solidity ^0.4.24;
|
||||||
|
|
||||||
interface token {
|
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||||
function transfer(address receiver, uint amount) external returns (bool);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressRegistry {
|
interface AddressRegistry {
|
||||||
function getAddr(string name) external returns(address);
|
function getAddr(string name) external returns(address);
|
||||||
|
@ -35,7 +33,6 @@ contract FeeDetail is Registry {
|
||||||
function setFees(uint cut) public onlyAdmin { // 200 means 0.5%
|
function setFees(uint cut) public onlyAdmin { // 200 means 0.5%
|
||||||
fees = cut;
|
fees = cut;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,10 +46,10 @@ contract MoatResolver is FeeDetail {
|
||||||
}
|
}
|
||||||
|
|
||||||
function collectToken(address tokenAddress, uint amount) public onlyAdmin {
|
function collectToken(address tokenAddress, uint amount) public onlyAdmin {
|
||||||
if (tokenAddress == 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee) {
|
if (tokenAddress == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
|
||||||
msg.sender.transfer(amount);
|
msg.sender.transfer(amount);
|
||||||
} else {
|
} else {
|
||||||
token tokenFunctions = token(tokenAddress);
|
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||||
tokenFunctions.transfer(msg.sender, amount);
|
tokenFunctions.transfer(msg.sender, amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ contract AssetDB is Registry {
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
|
|
||||||
mapping(address => mapping(address => uint)) balances;
|
mapping(address => mapping(address => uint)) balances;
|
||||||
address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
address eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
function getBalance(
|
function getBalance(
|
||||||
address assetHolder,
|
address assetHolder,
|
||||||
|
|
|
@ -2,11 +2,9 @@
|
||||||
|
|
||||||
pragma solidity ^0.4.24;
|
pragma solidity ^0.4.24;
|
||||||
|
|
||||||
interface token {
|
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||||
function transfer(address receiver, uint amount) external returns (bool);
|
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||||
function approve(address spender, uint256 value) external returns (bool);
|
|
||||||
function transferFrom(address from, address to, uint amount) external returns (bool);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressRegistry {
|
interface AddressRegistry {
|
||||||
function getAddr(string name) external returns(address);
|
function getAddr(string name) external returns(address);
|
||||||
|
@ -81,7 +79,7 @@ contract Trade is Registry {
|
||||||
|
|
||||||
// ropsten network
|
// ropsten network
|
||||||
address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
|
address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
|
||||||
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
address public eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
function executeTrade(
|
function executeTrade(
|
||||||
address trader,
|
address trader,
|
||||||
|
@ -121,7 +119,7 @@ contract Trade is Registry {
|
||||||
|
|
||||||
function fetchToken(address trader, address src, uint srcAmt) internal {
|
function fetchToken(address trader, address src, uint srcAmt) internal {
|
||||||
if (src != eth) {
|
if (src != eth) {
|
||||||
token tokenFunctions = token(src);
|
IERC20 tokenFunctions = IERC20(src);
|
||||||
tokenFunctions.transferFrom(trader, address(this), srcAmt);
|
tokenFunctions.transferFrom(trader, address(this), srcAmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +132,7 @@ contract Trade is Registry {
|
||||||
if (src == eth) {
|
if (src == eth) {
|
||||||
getAddress("admin").transfer(fees);
|
getAddress("admin").transfer(fees);
|
||||||
} else {
|
} else {
|
||||||
token tokenFunctions = token(src);
|
IERC20 tokenFunctions = IERC20(src);
|
||||||
tokenFunctions.transfer(getAddress("admin"), fees);
|
tokenFunctions.transfer(getAddress("admin"), fees);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,7 +140,7 @@ contract Trade is Registry {
|
||||||
|
|
||||||
function allowKyber(address[] tokenArr) public {
|
function allowKyber(address[] tokenArr) public {
|
||||||
for (uint i = 0; i < tokenArr.length; i++) {
|
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);
|
tokenFunctions.approve(getAddress("kyber"), 2**256 - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,7 +160,7 @@ contract MoatKyber is Trade {
|
||||||
if (tokenAddress == eth) {
|
if (tokenAddress == eth) {
|
||||||
msg.sender.transfer(amount);
|
msg.sender.transfer(amount);
|
||||||
} else {
|
} else {
|
||||||
token tokenFunctions = token(tokenAddress);
|
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||||
tokenFunctions.transfer(msg.sender, amount);
|
tokenFunctions.transfer(msg.sender, amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,9 @@
|
||||||
|
|
||||||
pragma solidity 0.4.24;
|
pragma solidity 0.4.24;
|
||||||
|
|
||||||
interface token {
|
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
|
||||||
function transfer(address receiver, uint amount) external returns (bool);
|
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
||||||
function approve(address spender, uint256 value) external returns (bool);
|
|
||||||
function transferFrom(address from, address to, uint amount) external returns (bool);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface AddressRegistry {
|
interface AddressRegistry {
|
||||||
function getAddr(string name) external returns(address);
|
function getAddr(string name) external returns(address);
|
||||||
|
@ -83,7 +81,7 @@ contract GlobalVar is Registry {
|
||||||
address public peth = 0xf4d791139cE033Ad35DB2B2201435fAd668B1b64;
|
address public peth = 0xf4d791139cE033Ad35DB2B2201435fAd668B1b64;
|
||||||
address public mkr = 0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD;
|
address public mkr = 0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD;
|
||||||
address public dai = 0xC4375B7De8af5a38a93548eb8453a498222C4fF2;
|
address public dai = 0xC4375B7De8af5a38a93548eb8453a498222C4fF2;
|
||||||
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
|
address public eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
|
|
||||||
address public cdpAddr = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2;
|
address public cdpAddr = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2;
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
|
@ -129,7 +127,7 @@ contract IssueLoan is GlobalVar {
|
||||||
function drawDAI(address borrower, uint daiDraw) public onlyUserOrResolver(borrower) {
|
function drawDAI(address borrower, uint daiDraw) public onlyUserOrResolver(borrower) {
|
||||||
loanMaster.draw(cdps[borrower], daiDraw);
|
loanMaster.draw(cdps[borrower], daiDraw);
|
||||||
uint fees = deductFees(daiDraw);
|
uint fees = deductFees(daiDraw);
|
||||||
token tokenFunctions = token(dai);
|
IERC20 tokenFunctions = IERC20(dai);
|
||||||
tokenFunctions.transfer(getAddress("resolver"), daiDraw - fees);
|
tokenFunctions.transfer(getAddress("resolver"), daiDraw - fees);
|
||||||
emit LoanedDAI(borrower, daiDraw, fees);
|
emit LoanedDAI(borrower, daiDraw, fees);
|
||||||
}
|
}
|
||||||
|
@ -143,7 +141,7 @@ contract IssueLoan is GlobalVar {
|
||||||
fees = moatRes.fees();
|
fees = moatRes.fees();
|
||||||
if (fees > 0) {
|
if (fees > 0) {
|
||||||
fees = volume / fees;
|
fees = volume / fees;
|
||||||
token tokenFunctions = token(dai);
|
IERC20 tokenFunctions = IERC20(dai);
|
||||||
tokenFunctions.transfer(getAddress("admin"), fees);
|
tokenFunctions.transfer(getAddress("admin"), fees);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,7 +169,7 @@ contract RepayLoan is IssueLoan {
|
||||||
}
|
}
|
||||||
|
|
||||||
function wipeDAI(address borrower, uint daiWipe) public {
|
function wipeDAI(address borrower, uint daiWipe) public {
|
||||||
token tokenFunction = token(dai);
|
IERC20 tokenFunction = IERC20(dai);
|
||||||
tokenFunction.transferFrom(msg.sender, address(this), daiWipe);
|
tokenFunction.transferFrom(msg.sender, address(this), daiWipe);
|
||||||
loanMaster.wipe(cdps[borrower], daiWipe);
|
loanMaster.wipe(cdps[borrower], daiWipe);
|
||||||
emit WipedDAI(borrower, daiWipe);
|
emit WipedDAI(borrower, daiWipe);
|
||||||
|
@ -203,13 +201,13 @@ contract BorrowTasks is RepayLoan {
|
||||||
}
|
}
|
||||||
|
|
||||||
function approveERC20() public {
|
function approveERC20() public {
|
||||||
token wethTkn = token(weth);
|
IERC20 wethTkn = IERC20(weth);
|
||||||
wethTkn.approve(cdpAddr, 2**256 - 1);
|
wethTkn.approve(cdpAddr, 2**256 - 1);
|
||||||
token pethTkn = token(peth);
|
IERC20 pethTkn = IERC20(peth);
|
||||||
pethTkn.approve(cdpAddr, 2**256 - 1);
|
pethTkn.approve(cdpAddr, 2**256 - 1);
|
||||||
token mkrTkn = token(mkr);
|
IERC20 mkrTkn = IERC20(mkr);
|
||||||
mkrTkn.approve(cdpAddr, 2**256 - 1);
|
mkrTkn.approve(cdpAddr, 2**256 - 1);
|
||||||
token daiTkn = token(dai);
|
IERC20 daiTkn = IERC20(dai);
|
||||||
daiTkn.approve(cdpAddr, 2**256 - 1);
|
daiTkn.approve(cdpAddr, 2**256 - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,7 +226,7 @@ contract MoatMaker is BorrowTasks {
|
||||||
if (tokenAddress == eth) {
|
if (tokenAddress == eth) {
|
||||||
msg.sender.transfer(amount);
|
msg.sender.transfer(amount);
|
||||||
} else {
|
} else {
|
||||||
token tokenFunctions = token(tokenAddress);
|
IERC20 tokenFunctions = IERC20(tokenAddress);
|
||||||
tokenFunctions.transfer(msg.sender, amount);
|
tokenFunctions.transfer(msg.sender, amount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user