Refactored and simplified protocol codes.

This commit is contained in:
Sowmayjain 2018-10-29 14:28:37 +05:30
parent 20404b567b
commit 3ac321bace
2 changed files with 125 additions and 120 deletions

View File

@ -1,142 +1,147 @@
// pragma solidity ^0.4.24;
pragma solidity ^0.4.24;
// import "openzeppelin-solidity/contracts/math/SafeMath.sol";
// import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
// interface AddressRegistry {
// function getAddr(string name) external view returns(address);
// function isApprovedResolver(address user) external view returns(bool);
// }
interface AddressRegistry {
function getAddr(string name) external view returns(address);
}
// interface Kyber {
// function trade(
// address src,
// uint srcAmount,
// address dest,
// address destAddress,
// uint maxDestAmount,
// uint minConversionRate,
// address walletId
// ) external payable returns (uint);
// }
interface Kyber {
function trade(
address src,
uint srcAmount,
address dest,
address destAddress,
uint maxDestAmount,
uint minConversionRate,
address walletId
) external payable returns (uint);
}
// contract Registry {
// address public addressRegistry;
// modifier onlyAdmin() {
// require(
// msg.sender == getAddress("admin"),
// "Permission Denied"
// );
// _;
// }
// function getAddress(string name) internal view returns(address addr) {
// AddressRegistry addrReg = AddressRegistry(addressRegistry);
// return addrReg.getAddr(name);
// }
// }
contract Registry {
address public addressRegistry;
modifier onlyAdmin() {
require(
msg.sender == getAddress("admin"),
"Permission Denied"
);
_;
}
function getAddress(string name) internal view returns(address addr) {
AddressRegistry addrReg = AddressRegistry(addressRegistry);
return addrReg.getAddr(name);
}
}
// contract Trade is Registry {
contract Trade is Registry {
// event KyberTrade(
// address src,
// uint srcAmt,
// address dest,
// uint destAmt,
// address beneficiary,
// uint fees,
// uint slipRate,
// address affiliate
// );
uint public fees;
// // ropsten network
// address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
// address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
event KyberTrade(
address src,
uint srcAmt,
address dest,
uint destAmt,
address beneficiary,
uint fees,
uint slipRate,
address affiliate
);
// function executeTrade(
// address trader,
// address src,
// address dest,
// uint srcAmt,
// uint slipRate
// ) public payable onlyUserOrResolver(trader) returns (uint destAmt)
// {
// ropsten network
address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
// fetchToken(trader, src, srcAmt);
// uint fees = deductFees(src, srcAmt);
function executeTrade(
address src,
address dest,
uint srcAmt,
uint slipRate
) public payable returns (uint destAmt)
{
// Kyber kyberFunctions = Kyber(kyberAddr);
// destAmt = kyberFunctions.trade.value(msg.value)(
// src,
// srcAmt - fees,
// dest,
// trader,
// 2**256 - 1,
// slipRate,
// getAddress("admin")
// );
fetchToken(src, srcAmt);
uint feecut = deductFees(src, srcAmt);
// emit KyberTrade(
// src,
// srcAmt,
// dest,
// destAmt,
// trader,
// fees,
// slipRate,
// getAddress("admin")
// );
Kyber kyberFunctions = Kyber(kyberAddr);
destAmt = kyberFunctions.trade.value(msg.value)(
src,
srcAmt - feecut,
dest,
msg.sender,
2**256 - 1,
slipRate,
getAddress("admin")
);
// }
emit KyberTrade(
src,
srcAmt,
dest,
destAmt,
msg.sender,
feecut,
slipRate,
getAddress("admin")
);
// function fetchToken(address trader, address src, uint srcAmt) internal {
// if (src != eth) {
// IERC20 tokenFunctions = IERC20(src);
// tokenFunctions.transferFrom(trader, address(this), srcAmt);
// }
// }
}
// function deductFees(address src, uint volume) internal returns(uint fees) {
// Resolver moatRes = Resolver(getAddress("resolver"));
// fees = moatRes.fees();
// if (fees > 0) {
// fees = volume / fees;
// if (src == eth) {
// getAddress("admin").transfer(fees);
// } else {
// IERC20 tokenFunctions = IERC20(src);
// tokenFunctions.transfer(getAddress("admin"), fees);
// }
// }
// }
function fetchToken(address src, uint srcAmt) internal {
if (src != eth) {
IERC20 tokenFunctions = IERC20(src);
tokenFunctions.transferFrom(msg.sender, address(this), srcAmt);
}
}
// function allowKyber(address[] tokenArr) public {
// for (uint i = 0; i < tokenArr.length; i++) {
// IERC20 tokenFunctions = IERC20(tokenArr[i]);
// tokenFunctions.approve(getAddress("kyber"), 2**256 - 1);
// }
// }
function deductFees(address src, uint volume) internal returns(uint brokerage) {
if (fees > 0) {
brokerage = volume / fees;
if (src == eth) {
getAddress("admin").transfer(brokerage);
} else {
IERC20 tokenFunctions = IERC20(src);
tokenFunctions.transfer(getAddress("admin"), brokerage);
}
}
}
// }
function allowKyber(address[] tokenArr) public {
for (uint i = 0; i < tokenArr.length; i++) {
IERC20 tokenFunctions = IERC20(tokenArr[i]);
tokenFunctions.approve(getAddress("kyber"), 2**256 - 1);
}
}
}
// contract MoatKyber is Trade {
contract MoatKyber is Trade {
// constructor(address rAddr) public {
// addressRegistry = rAddr;
// }
constructor(address rAddr) public {
addressRegistry = rAddr;
}
// function () public payable {}
function () public payable {}
// function collectAsset(address tokenAddress, uint amount) public onlyAdmin {
// if (tokenAddress == eth) {
// msg.sender.transfer(amount);
// } else {
// IERC20 tokenFunctions = IERC20(tokenAddress);
// tokenFunctions.transfer(msg.sender, amount);
// }
// }
function collectAsset(address tokenAddress, uint amount) public onlyAdmin {
if (tokenAddress == eth) {
msg.sender.transfer(amount);
} else {
IERC20 tokenFunctions = IERC20(tokenAddress);
tokenFunctions.transfer(msg.sender, amount);
}
}
// }
function setFees(uint cut) public onlyAdmin { // 200 means 0.5%
fees = cut;
}
}

View File

@ -94,10 +94,10 @@ contract IssueLoan is GlobalVar {
function drawDAI(uint daiDraw) public {
require(!freezed, "Operation Disabled");
loanMaster.draw(cdps[msg.sender], daiDraw);
uint fees = deductFees(daiDraw);
uint feecut = deductFees(daiDraw);
IERC20 tokenFunctions = IERC20(dai);
tokenFunctions.transfer(msg.sender, daiDraw - fees);
emit LoanedDAI(msg.sender, daiDraw, fees);
tokenFunctions.transfer(msg.sender, daiDraw - feecut);
emit LoanedDAI(msg.sender, daiDraw, feecut);
}
function deductFees(uint volume) internal returns(uint brokerage) {