mirror of
https://github.com/Instadapp/InstaContract.git
synced 2024-07-29 22:47:45 +00:00
WIP, Upgrade to solidity 5
This commit is contained in:
parent
b6145dd90b
commit
20a21bc1a7
|
@ -1,8 +1,6 @@
|
||||||
pragma solidity ^0.4.24;
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
|
|
||||||
contract AddressRegistry {
|
contract AddressRegistry {
|
||||||
|
|
||||||
event AddressSet(string name, address addr);
|
event AddressSet(string name, address addr);
|
||||||
mapping(bytes32 => address) registry;
|
mapping(bytes32 => address) registry;
|
||||||
|
|
||||||
|
@ -10,18 +8,14 @@ contract AddressRegistry {
|
||||||
registry[keccak256(abi.encodePacked("admin"))] = msg.sender;
|
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))];
|
return registry[keccak256(abi.encodePacked(name))];
|
||||||
}
|
}
|
||||||
|
|
||||||
function setAddr(string name, address addr) public {
|
function setAddr(string memory name, address addr) public {
|
||||||
require(
|
require(msg.sender == getAddr("admin") || msg.sender == getAddr("owner"), "Permission Denied");
|
||||||
msg.sender == getAddr("admin") ||
|
|
||||||
msg.sender == getAddr("owner"),
|
|
||||||
"Permission Denied"
|
|
||||||
);
|
|
||||||
registry[keccak256(abi.encodePacked(name))] = addr;
|
registry[keccak256(abi.encodePacked(name))] = addr;
|
||||||
emit AddressSet(name, addr);
|
emit AddressSet(name, addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
pragma solidity 0.4.24;
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
/* solium-disable mixedcase */
|
/* solium-disable mixedcase */
|
||||||
contract Migrations {
|
contract Migrations {
|
||||||
|
@ -6,13 +6,10 @@ contract Migrations {
|
||||||
uint public last_completed_migration;
|
uint public last_completed_migration;
|
||||||
|
|
||||||
modifier restricted() {
|
modifier restricted() {
|
||||||
if (msg.sender == owner)
|
if (msg.sender == owner) _;
|
||||||
_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor()
|
constructor() public {
|
||||||
public
|
|
||||||
{
|
|
||||||
owner = msg.sender;
|
owner = msg.sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,4 +23,5 @@ contract Migrations {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* solium-enable mixedcase */
|
|
||||||
|
/* solium-enable mixedcase */
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
pragma solidity 0.4.24;
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title Ownable
|
* @title Ownable
|
||||||
|
@ -36,4 +35,4 @@ contract Ownable {
|
||||||
emit OwnershipTransferred(owner, newOwner);
|
emit OwnershipTransferred(owner, newOwner);
|
||||||
owner = newOwner;
|
owner = newOwner;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {}
|
|
||||||
|
|
||||||
// }
|
|
|
@ -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);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
|
@ -1,9 +1,7 @@
|
||||||
pragma solidity ^0.4.24;
|
pragma solidity ^0.5.0;
|
||||||
|
|
||||||
|
|
||||||
library SafeMath {
|
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) {
|
if (a == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -11,8 +9,8 @@ library SafeMath {
|
||||||
require(c / a == b, "Assertion Failed");
|
require(c / a == b, "Assertion Failed");
|
||||||
return c;
|
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");
|
require(b > 0, "Assertion Failed");
|
||||||
uint256 c = a / b;
|
uint256 c = a / b;
|
||||||
return c;
|
return c;
|
||||||
|
@ -20,17 +18,20 @@ library SafeMath {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface IERC20 {
|
interface IERC20 {
|
||||||
function balanceOf(address who) external view returns (uint256);
|
function balanceOf(address who) external view returns(uint256);
|
||||||
function transfer(address to, uint256 value) external returns (bool);
|
function transfer(address to, uint256 value) external returns(bool);
|
||||||
function approve(address spender, 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 transferFrom(address from, address to, uint256 value) external returns(bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface AddressRegistry {
|
interface AddressRegistry {
|
||||||
function getAddr(string name) external view returns(address);
|
function getAddr(string calldata name) external view returns(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface Kyber {
|
interface Kyber {
|
||||||
function trade(
|
function trade(
|
||||||
address src,
|
address src,
|
||||||
|
@ -40,26 +41,19 @@ interface Kyber {
|
||||||
uint maxDestAmount,
|
uint maxDestAmount,
|
||||||
uint minConversionRate,
|
uint minConversionRate,
|
||||||
address walletId
|
address walletId
|
||||||
) external payable returns (uint);
|
) external payable returns(uint);
|
||||||
|
|
||||||
function getExpectedRate(
|
function getExpectedRate(address src, address dest, uint srcQty) external view returns(uint, uint);
|
||||||
address src,
|
|
||||||
address dest,
|
|
||||||
uint srcQty
|
|
||||||
) external view returns (uint, uint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract Registry {
|
contract Registry {
|
||||||
address public addressRegistry;
|
address public addressRegistry;
|
||||||
modifier onlyAdmin() {
|
modifier onlyAdmin() {
|
||||||
require(
|
require(msg.sender == getAddress("admin"), "Permission Denied");
|
||||||
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);
|
AddressRegistry addrReg = AddressRegistry(addressRegistry);
|
||||||
return addrReg.getAddr(name);
|
return addrReg.getAddr(name);
|
||||||
}
|
}
|
||||||
|
@ -68,37 +62,21 @@ contract Registry {
|
||||||
|
|
||||||
|
|
||||||
contract Trade is Registry {
|
contract Trade is Registry {
|
||||||
|
|
||||||
using SafeMath for uint;
|
using SafeMath for uint;
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
|
|
||||||
event KyberTrade(
|
event KyberTrade(address src, uint srcAmt, address dest, uint destAmt, address beneficiary, uint minConversionRate, address affiliate);
|
||||||
address src,
|
|
||||||
uint srcAmt,
|
|
||||||
address dest,
|
|
||||||
uint destAmt,
|
|
||||||
address beneficiary,
|
|
||||||
uint minConversionRate,
|
|
||||||
address affiliate
|
|
||||||
);
|
|
||||||
|
|
||||||
function getExpectedPrice(
|
function getExpectedPrice(address src, address dest, uint srcAmt) public view returns(uint, uint) {
|
||||||
address src,
|
|
||||||
address dest,
|
|
||||||
uint srcAmt
|
|
||||||
) public view returns (uint, uint)
|
|
||||||
{
|
|
||||||
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
||||||
return kyberFunctions.getExpectedRate(
|
return kyberFunctions.getExpectedRate(src, dest, srcAmt);
|
||||||
src, dest, srcAmt
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function approveKyber(address[] tokenArr) public {
|
function approveKyber(address[] memory tokenArr) public {
|
||||||
address kyberProxy = getAddress("kyber");
|
address kyberProxy = getAddress("kyber");
|
||||||
for (uint i = 0; i < tokenArr.length; i++) {
|
for (uint i = 0; i < tokenArr.length; i++) {
|
||||||
IERC20 tokenFunctions = IERC20(tokenArr[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 srcAmt, // amount of token for sell
|
||||||
uint minConversionRate, // minimum slippage rate
|
uint minConversionRate, // minimum slippage rate
|
||||||
uint maxDestAmt // max amount of dest token
|
uint maxDestAmt // max amount of dest token
|
||||||
) public payable returns (uint destAmt)
|
) public payable returns(uint destAmt) {
|
||||||
{
|
|
||||||
|
|
||||||
address eth = getAddress("eth");
|
address eth = getAddress("eth");
|
||||||
uint ethQty = getToken(
|
uint ethQty = getToken(msg.sender, src, srcAmt, eth);
|
||||||
msg.sender, src, srcAmt, eth
|
|
||||||
);
|
|
||||||
|
|
||||||
// Interacting with Kyber Proxy Contract
|
// Interacting with Kyber Proxy Contract
|
||||||
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
||||||
destAmt = kyberFunctions.trade.value(ethQty)(
|
destAmt = kyberFunctions.trade.value(ethQty)(src, srcAmt, dest, msg.sender, maxDestAmt, minConversionRate, getAddress("admin"));
|
||||||
src,
|
|
||||||
srcAmt,
|
|
||||||
dest,
|
|
||||||
msg.sender,
|
|
||||||
maxDestAmt,
|
|
||||||
minConversionRate,
|
|
||||||
getAddress("admin")
|
|
||||||
);
|
|
||||||
|
|
||||||
// maxDestAmt usecase implementated
|
// maxDestAmt usecase implementated
|
||||||
if (src == eth && address(this).balance > 0) {
|
if (src == eth && address(this).balance > 0) {
|
||||||
msg.sender.transfer(address(this).balance);
|
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);
|
IERC20 srcTkn = IERC20(src);
|
||||||
uint srcBal = srcTkn.balanceOf(address(this));
|
uint srcBal = srcTkn.balanceOf(address(this));
|
||||||
if (srcBal > 0) {
|
if (srcBal > 0) {
|
||||||
|
@ -139,19 +106,11 @@ contract Trade is Registry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
emit KyberTrade(
|
emit KyberTrade(src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin"));
|
||||||
src, srcAmt, dest, destAmt, msg.sender, minConversionRate, getAddress("admin")
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getToken(
|
function getToken(address trader, address src, uint srcAmt, address eth) internal returns(uint ethQty) {
|
||||||
address trader,
|
|
||||||
address src,
|
|
||||||
uint srcAmt,
|
|
||||||
address eth
|
|
||||||
) internal returns (uint ethQty)
|
|
||||||
{
|
|
||||||
if (src == eth) {
|
if (src == eth) {
|
||||||
require(msg.value == srcAmt, "Invalid Operation");
|
require(msg.value == srcAmt, "Invalid Operation");
|
||||||
ethQty = srcAmt;
|
ethQty = srcAmt;
|
||||||
|
@ -166,11 +125,12 @@ contract Trade is Registry {
|
||||||
|
|
||||||
|
|
||||||
contract InstaKyber is Trade {
|
contract InstaKyber is Trade {
|
||||||
|
|
||||||
constructor(address rAddr) public {
|
constructor(address rAddr) public {
|
||||||
addressRegistry = rAddr;
|
addressRegistry = rAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
function () public payable {}
|
function() external payable {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
"bn.js": "^4.11.8",
|
"bn.js": "^4.11.8",
|
||||||
"dotenv": "^6.1.0",
|
"dotenv": "^6.1.0",
|
||||||
"openzeppelin-solidity": "^2.0.0",
|
"openzeppelin-solidity": "^2.0.0",
|
||||||
"prettier": "^1.14.3",
|
|
||||||
"truffle": "^5.0.0-beta.0",
|
"truffle": "^5.0.0-beta.0",
|
||||||
"webpack": "^4.23.1",
|
"webpack": "^4.23.1",
|
||||||
"truffle-hdwallet-provider": "^1.0.0-web3one.0"
|
"truffle-hdwallet-provider": "^1.0.0-web3one.0"
|
||||||
|
@ -59,6 +58,8 @@
|
||||||
"ganache-cli": "^6.1.8",
|
"ganache-cli": "^6.1.8",
|
||||||
"mocha-junit-reporter": "^1.18.0",
|
"mocha-junit-reporter": "^1.18.0",
|
||||||
"mocha-multi-reporters": "^1.1.7",
|
"mocha-multi-reporters": "^1.1.7",
|
||||||
|
"prettier": "^1.15.3",
|
||||||
|
"prettier-plugin-solidity-refactor": "^1.0.0-alpha.11",
|
||||||
"solidity-coverage": "0.5.11",
|
"solidity-coverage": "0.5.11",
|
||||||
"solium": "1.1.8"
|
"solium": "1.1.8"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user