From 20a21bc1a7be625b5fd8510ba1f5942d226dbfe1 Mon Sep 17 00:00:00 2001 From: Ravindra Kumar Date: Sun, 23 Dec 2018 12:27:00 +0530 Subject: [PATCH] WIP, Upgrade to solidity 5 --- contracts/InstaAddress.sol | 16 ++-- contracts/Migrations.sol | 12 ++- contracts/Ownable.sol | 5 +- contracts/bin/MoatAsset.sol | 116 ----------------------------- contracts/bin/MoatResolver.sol | 58 --------------- contracts/protocols/InstaKyber.sol | 106 ++++++++------------------ package.json | 3 +- 7 files changed, 47 insertions(+), 269 deletions(-) delete mode 100644 contracts/bin/MoatAsset.sol delete mode 100644 contracts/bin/MoatResolver.sol diff --git a/contracts/InstaAddress.sol b/contracts/InstaAddress.sol index da61740..d5e69b9 100644 --- a/contracts/InstaAddress.sol +++ b/contracts/InstaAddress.sol @@ -1,8 +1,6 @@ -pragma solidity ^0.4.24; - +pragma solidity ^0.5.0; contract AddressRegistry { - event AddressSet(string name, address addr); mapping(bytes32 => address) registry; @@ -10,18 +8,14 @@ contract AddressRegistry { registry[keccak256(abi.encodePacked("admin"))] = msg.sender; } - function getAddr(string name) public view returns(address) { + function getAddr(string memory name) public view returns(address) { return registry[keccak256(abi.encodePacked(name))]; } - function setAddr(string name, address addr) public { - require( - msg.sender == getAddr("admin") || - msg.sender == getAddr("owner"), - "Permission Denied" - ); + function setAddr(string memory name, address addr) public { + require(msg.sender == getAddr("admin") || msg.sender == getAddr("owner"), "Permission Denied"); registry[keccak256(abi.encodePacked(name))] = addr; emit AddressSet(name, addr); } -} \ No newline at end of file +} diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol index bf44803..4ff98a0 100644 --- a/contracts/Migrations.sol +++ b/contracts/Migrations.sol @@ -1,4 +1,4 @@ -pragma solidity 0.4.24; +pragma solidity ^0.5.0; /* solium-disable mixedcase */ contract Migrations { @@ -6,13 +6,10 @@ contract Migrations { uint public last_completed_migration; modifier restricted() { - if (msg.sender == owner) - _; + if (msg.sender == owner) _; } - constructor() - public - { + constructor() public { owner = msg.sender; } @@ -26,4 +23,5 @@ contract Migrations { } } -/* solium-enable mixedcase */ \ No newline at end of file + +/* solium-enable mixedcase */ diff --git a/contracts/Ownable.sol b/contracts/Ownable.sol index 18dfea2..b2e9bfa 100644 --- a/contracts/Ownable.sol +++ b/contracts/Ownable.sol @@ -1,5 +1,4 @@ -pragma solidity 0.4.24; - +pragma solidity ^0.5.0; /** * @title Ownable @@ -36,4 +35,4 @@ contract Ownable { emit OwnershipTransferred(owner, newOwner); owner = newOwner; } -} \ No newline at end of file +} diff --git a/contracts/bin/MoatAsset.sol b/contracts/bin/MoatAsset.sol deleted file mode 100644 index 3184ae0..0000000 --- a/contracts/bin/MoatAsset.sol +++ /dev/null @@ -1,116 +0,0 @@ -// // withdraw the extra assets other than global balance (in case anyone donated for free) and then no need for seperate brokerage calculation -// // IMPORTANT CHECK - decimals() - how the balance of tokens with less than 18 decimals are stored. Factor it. -// // update the balance along with "transferAssets" functions and also check the for onlyAllowedResolver -// // transfer assets to different address (create 2 different mappings) - 48 hour time to transfer all - send email for this - -// pragma solidity ^0.4.24; - -// 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); -// } - - -// contract Registry { - -// address public registryAddress; -// AddressRegistry addrReg = AddressRegistry(registryAddress); - -// modifier onlyAllowedResolver(address user) { -// require( -// addrReg.isApprovedResolver(user), -// "Permission Denied" -// ); -// _; -// } - -// function getAddress(string name) internal view returns(address addr) { -// addr = addrReg.getAddr(name); -// require(addr != address(0), "Invalid Address"); -// } - -// } - - -// contract AssetDB is Registry { - -// using SafeMath for uint; -// using SafeMath for uint256; - -// mapping(address => mapping(address => uint)) balances; -// address eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; - -// function getBalance( -// address assetHolder, -// address tokenAddr -// ) public view returns (uint256 balance) -// { -// balance = balances[assetHolder][tokenAddr]; -// } - -// function deposit(address tknAddr, uint amount) public payable { -// if (msg.value > 0) { -// balances[msg.sender][eth] = balances[msg.sender][eth].add(msg.value); -// } else { -// IERC20 tokenFunctions = IERC20(tknAddr); -// tokenFunctions.transferFrom(msg.sender, address(this), amount); -// balances[msg.sender][tknAddr] = balances[msg.sender][tknAddr].add(amount); -// } -// } - -// function withdraw(address tknAddr, uint amount) public { -// require(balances[msg.sender][tknAddr] >= amount, "Insufficient Balance"); -// balances[msg.sender][tknAddr] = balances[msg.sender][tknAddr].sub(amount); -// if (tknAddr == eth) { -// msg.sender.transfer(amount); -// } else { -// IERC20 tokenFunctions = IERC20(tknAddr); -// tokenFunctions.transfer(msg.sender, amount); -// } -// } - -// function updateBalance( -// address tokenAddr, -// uint amount, -// bool credit, -// address user -// ) public onlyAllowedResolver(user) -// { -// if (credit) { -// balances[user][tokenAddr] = balances[user][tokenAddr].add(amount); -// } else { -// balances[user][tokenAddr] = balances[user][tokenAddr].sub(amount); -// } -// } - -// function transferAssets( -// address tokenAddress, -// uint amount, -// address sendTo, -// address user -// ) public onlyAllowedResolver(user) -// { -// if (tokenAddress == eth) { -// sendTo.transfer(amount); -// } else { -// IERC20 tokenFunctions = IERC20(tokenAddress); -// tokenFunctions.transfer(sendTo, amount); -// } -// balances[user][tokenAddress] = balances[user][tokenAddress].sub(amount); -// } - -// } - - -// contract MoatAsset is AssetDB { - -// constructor(address rAddr) public { -// registryAddress = rAddr; -// } - -// function () public payable {} - -// } diff --git a/contracts/bin/MoatResolver.sol b/contracts/bin/MoatResolver.sol deleted file mode 100644 index 5c2bbb4..0000000 --- a/contracts/bin/MoatResolver.sol +++ /dev/null @@ -1,58 +0,0 @@ -// pragma solidity ^0.4.24; - -// import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; - -// interface AddressRegistry { -// function getAddr(string name) external view returns(address); -// } - - -// contract Registry { - -// address public registryAddress; -// modifier onlyAdmin() { -// require( -// msg.sender == getAddress("admin"), -// "Permission Denied" -// ); -// _; -// } - -// function getAddress(string name) internal view returns(address addr) { -// AddressRegistry addrReg = AddressRegistry(registryAddress); -// addr = addrReg.getAddr(name); -// require(addr != address(0), "Invalid Address"); -// } - -// } - - -// contract FeeDetail is Registry { - -// uint public fees; -// function setFees(uint cut) public onlyAdmin { // 200 means 0.5% -// fees = cut; -// } - -// } - - -// contract MoatResolver is FeeDetail { - -// function () public payable {} - -// constructor(address rAddr, uint cut) public { // 200 means 0.5% -// registryAddress = rAddr; -// setFees(cut); -// } - -// function collectAsset(address tokenAddress, uint amount) public onlyAdmin { -// if (tokenAddress == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) { -// msg.sender.transfer(amount); -// } else { -// IERC20 tokenFunctions = IERC20(tokenAddress); -// tokenFunctions.transfer(msg.sender, amount); -// } -// } - -// } diff --git a/contracts/protocols/InstaKyber.sol b/contracts/protocols/InstaKyber.sol index beb1a9e..ebb6d20 100644 --- a/contracts/protocols/InstaKyber.sol +++ b/contracts/protocols/InstaKyber.sol @@ -1,9 +1,7 @@ -pragma solidity ^0.4.24; - +pragma solidity ^0.5.0; library SafeMath { - - function mul(uint256 a, uint256 b) internal pure returns (uint256) { + function mul(uint256 a, uint256 b) internal pure returns(uint256) { if (a == 0) { return 0; } @@ -11,8 +9,8 @@ library SafeMath { require(c / a == b, "Assertion Failed"); return c; } - - function div(uint256 a, uint256 b) internal pure returns (uint256) { + + function div(uint256 a, uint256 b) internal pure returns(uint256) { require(b > 0, "Assertion Failed"); uint256 c = a / b; return c; @@ -20,17 +18,20 @@ library SafeMath { } + interface IERC20 { - function balanceOf(address who) external view returns (uint256); - function transfer(address to, uint256 value) external returns (bool); - function approve(address spender, uint256 value) external returns (bool); - function transferFrom(address from, address to, uint256 value) external returns (bool); + function balanceOf(address who) external view returns(uint256); + function transfer(address to, uint256 value) external returns(bool); + function approve(address spender, uint256 value) external returns(bool); + function transferFrom(address from, address to, uint256 value) external returns(bool); } + interface AddressRegistry { - function getAddr(string name) external view returns(address); + function getAddr(string calldata name) external view returns(address); } + interface Kyber { function trade( address src, @@ -40,26 +41,19 @@ interface Kyber { uint maxDestAmount, uint minConversionRate, address walletId - ) external payable returns (uint); + ) external payable returns(uint); - function getExpectedRate( - address src, - address dest, - uint srcQty - ) external view returns (uint, uint); + function getExpectedRate(address src, address dest, uint srcQty) external view returns(uint, uint); } contract Registry { address public addressRegistry; modifier onlyAdmin() { - require( - msg.sender == getAddress("admin"), - "Permission Denied" - ); + require(msg.sender == getAddress("admin"), "Permission Denied"); _; } - function getAddress(string name) internal view returns(address) { + function getAddress(string memory name) internal view returns(address) { AddressRegistry addrReg = AddressRegistry(addressRegistry); return addrReg.getAddr(name); } @@ -68,37 +62,21 @@ contract Registry { contract Trade is Registry { - using SafeMath for uint; using SafeMath for uint256; - event KyberTrade( - address src, - uint srcAmt, - address dest, - uint destAmt, - address beneficiary, - uint minConversionRate, - address affiliate - ); + event KyberTrade(address src, uint srcAmt, address dest, uint destAmt, address beneficiary, uint minConversionRate, address affiliate); - function getExpectedPrice( - address src, - address dest, - uint srcAmt - ) public view returns (uint, uint) - { + function getExpectedPrice(address src, address dest, uint srcAmt) public view returns(uint, uint) { Kyber kyberFunctions = Kyber(getAddress("kyber")); - return kyberFunctions.getExpectedRate( - src, dest, srcAmt - ); + return kyberFunctions.getExpectedRate(src, dest, srcAmt); } - function approveKyber(address[] tokenArr) public { + function approveKyber(address[] memory tokenArr) public { address kyberProxy = getAddress("kyber"); for (uint i = 0; i < tokenArr.length; i++) { IERC20 tokenFunctions = IERC20(tokenArr[i]); - tokenFunctions.approve(kyberProxy, 2**256 - 1); + tokenFunctions.approve(kyberProxy, 2 ** 256 - 1); } } @@ -108,30 +86,19 @@ contract Trade is Registry { uint srcAmt, // amount of token for sell uint minConversionRate, // minimum slippage rate uint maxDestAmt // max amount of dest token - ) public payable returns (uint destAmt) - { - + ) public payable returns(uint destAmt) { address eth = getAddress("eth"); - uint ethQty = getToken( - msg.sender, src, srcAmt, eth - ); - + uint ethQty = getToken(msg.sender, src, srcAmt, eth); + // Interacting with Kyber Proxy Contract Kyber kyberFunctions = Kyber(getAddress("kyber")); - destAmt = kyberFunctions.trade.value(ethQty)( - src, - srcAmt, - dest, - msg.sender, - maxDestAmt, - minConversionRate, - getAddress("admin") - ); + destAmt = kyberFunctions.trade.value(ethQty)(src, srcAmt, dest, msg.sender, maxDestAmt, minConversionRate, getAddress("admin")); // maxDestAmt usecase implementated if (src == eth && address(this).balance > 0) { msg.sender.transfer(address(this).balance); - } else if (src != eth) { // as there is no balanceOf of eth + } else if (src != eth) { + // as there is no balanceOf of eth IERC20 srcTkn = IERC20(src); uint srcBal = srcTkn.balanceOf(address(this)); if (srcBal > 0) { @@ -139,19 +106,11 @@ contract Trade is Registry { } } - emit KyberTrade( - src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin") - ); + emit KyberTrade(src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin")); } - function getToken( - address trader, - address src, - uint srcAmt, - address eth - ) internal returns (uint ethQty) - { + function getToken(address trader, address src, uint srcAmt, address eth) internal returns(uint ethQty) { if (src == eth) { require(msg.value == srcAmt, "Invalid Operation"); ethQty = srcAmt; @@ -166,11 +125,12 @@ contract Trade is Registry { contract InstaKyber is Trade { - constructor(address rAddr) public { addressRegistry = rAddr; } - function () public payable {} + function() external payable { -} \ No newline at end of file + } + +} diff --git a/package.json b/package.json index 4de1a90..6829df9 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "bn.js": "^4.11.8", "dotenv": "^6.1.0", "openzeppelin-solidity": "^2.0.0", - "prettier": "^1.14.3", "truffle": "^5.0.0-beta.0", "webpack": "^4.23.1", "truffle-hdwallet-provider": "^1.0.0-web3one.0" @@ -59,6 +58,8 @@ "ganache-cli": "^6.1.8", "mocha-junit-reporter": "^1.18.0", "mocha-multi-reporters": "^1.1.7", + "prettier": "^1.15.3", + "prettier-plugin-solidity-refactor": "^1.0.0-alpha.11", "solidity-coverage": "0.5.11", "solium": "1.1.8" }