Transaction Hash:
Block:
9743275 at Mar-25-2020 10:55:40 PM +UTC
Transaction Fee:
0.001737549 ETH
$6.25
Gas Used:
386,122 Gas / 4.5 Gwei
Emitted Events:
24 |
SNT.Transfer( _from=KyberReserve, _to=KyberNetwork, _amount=910151532755244089505 )
|
25 |
KyberReserve.TradeExecute( origin=KyberNetwork, src=0xEeeeeEee...eeeeeEEeE, srcAmount=100580000000000000, destToken=SNT, destAmount=910151532755244089505, destAddress=KyberNetwork )
|
26 |
SNT.Transfer( _from=KyberNetwork, _to=[Sender] 0x35b0d1c1ce7d56e37041df650f94eba6f80dd1b5, _amount=910151532755244089505 )
|
27 |
FeeBurner.AssignFeeToWallet( reserve=KyberReserve, wallet=0xdE63aef6...4dB1a42Fb, walletFee=22073748068126471 )
|
28 |
FeeBurner.AssignBurnFees( reserve=KyberReserve, burnFee=51505412158961768 )
|
29 |
KyberNetwork.KyberTrade( trader=[Sender] 0x35b0d1c1ce7d56e37041df650f94eba6f80dd1b5, src=0xEeeeeEee...eeeeeEEeE, dest=SNT, srcAmount=100580000000000000, dstAmount=910151532755244089505, destAddress=[Sender] 0x35b0d1c1ce7d56e37041df650f94eba6f80dd1b5, ethWeiValue=100580000000000000, reserve1=0x00000000...000000000, reserve2=KyberReserve, hint=0x5045524D )
|
30 |
KyberNetworkProxy.ExecuteTrade( trader=[Sender] 0x35b0d1c1ce7d56e37041df650f94eba6f80dd1b5, src=0xEeeeeEee...eeeeeEEeE, dest=SNT, actualSrcAmount=100580000000000000, actualDestAmount=910151532755244089505 )
|
Account State Difference:
Address | Before | After | State Difference | ||
---|---|---|---|---|---|
0x35B0D1C1...6f80Dd1B5 |
0.104581872685223551 Eth
Nonce: 7
|
0.002264323685223551 Eth
Nonce: 8
| 0.102317549 | ||
0x63825c17...bD36A0D8F | (Kyber: Reserve) | 4,566.033298935984327944 Eth | 4,566.133878935984327944 Eth | 0.10058 | |
0x65bF64Ff...77ced86Cd | (Kyber: Contract) | ||||
0x744d70FD...3DF805B9E | |||||
0x798AbDA6...d3d11191B | (Kyber: Conversion Rates) | ||||
0x8007aa43...E5fF626d1 | (Kyber: Fee Burner) | ||||
0xf64f9720...b875B7295
Miner
| (ICanMining.ru) | 0.778871561310048745 Eth | 0.780609110310048745 Eth | 0.001737549 |
Execution Trace
ETH 0.10058
KyberNetworkProxy.tradeWithHint( src=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, srcAmount=100580000000000000, dest=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, destAddress=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5, maxDestAmount=57896044618658097711785492504343953926634992332820282019728792003956564819968, minConversionRate=8596579400651024367616, walletId=0xdE63aef60307655405835DA74BA02CE4dB1a42Fb, hint=0x5045524D ) => ( 910151532755244089505 )
-
SNT.balanceOf( _owner=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5 ) => ( balance=2141666430757924939800 )
ETH 0.10058
KyberNetwork.tradeWithHint( trader=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5, src=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, srcAmount=100580000000000000, dest=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, destAddress=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5, maxDestAmount=57896044618658097711785492504343953926634992332820282019728792003956564819968, minConversionRate=8596579400651024367616, walletId=0xdE63aef60307655405835DA74BA02CE4dB1a42Fb, hint=0x5045524D ) => ( 910151532755244089505 )
KyberReserve.getConversionRate( src=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, dest=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, srcQty=100580000000000000, blockNumber=9743275 ) => ( 9049030948053729265319 )
-
ConversionRates.getRate( token=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, currentBlockNumber=9743275, buy=True, qty=100580000000000000 ) => ( 9049030948053729265319 )
-
SNT.balanceOf( _owner=0x63825c174ab367968EC60f061753D3bbD36A0D8F ) => ( balance=135894857799388358052827 )
-
SanityRates.getSanityRate( src=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, dest=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E ) => ( 9934073873385712995574 )
-
ETH 0.10058
KyberReserve.trade( srcToken=0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE, srcAmount=100580000000000000, destToken=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, destAddress=0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd, conversionRate=9049030948053729265319, validate=True ) => ( True )
-
ConversionRates.recordImbalance( token=0x744d70FDBE2Ba4CF95131626614a1763DF805B9E, buyAmount=910151532755244089505, rateUpdateBlock=0, currentBlock=9743275 )
SNT.transfer( _to=0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd, _amount=910151532755244089505 ) => ( success=True )
-
SNTPlaceHolder.onTransfer( _from=0x63825c174ab367968EC60f061753D3bbD36A0D8F, 0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd, 910151532755244089505 ) => ( True )
-
-
SNT.transfer( _to=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5, _amount=910151532755244089505 ) => ( success=True )
-
SNTPlaceHolder.onTransfer( _from=0x65bF64Ff5f51272f729BDcD7AcFB00677ced86Cd, 0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5, 910151532755244089505 ) => ( True )
-
-
FeeBurner.handleFees( tradeWeiAmount=100580000000000000, reserve=0x63825c174ab367968EC60f061753D3bbD36A0D8F, wallet=0xdE63aef60307655405835DA74BA02CE4dB1a42Fb ) => ( True )
-
SNT.balanceOf( _owner=0x35B0D1C1cE7d56e37041DF650f94eBA6f80Dd1B5 ) => ( balance=3051817963513169029305 )
tradeWithHint[KyberNetworkProxy (ln:462)]
getBalance[KyberNetworkProxy (ln:480)]
getBalance[KyberNetworkProxy (ln:481)]
transferFrom[KyberNetworkProxy (ln:486)]
value[KyberNetworkProxy (ln:489)]
calculateTradeOutcome[KyberNetworkProxy (ln:501)]
getBalance[KyberNetworkProxy (ln:568)]
getBalance[KyberNetworkProxy (ln:569)]
calcRateFromQty[KyberNetworkProxy (ln:578)]
getDecimalsSafe[KyberNetworkProxy (ln:581)]
getDecimalsSafe[KyberNetworkProxy (ln:582)]
ExecuteTrade[KyberNetworkProxy (ln:513)]
File 1 of 8: KyberNetworkProxy
File 2 of 8: KyberReserve
File 3 of 8: KyberNetwork
File 4 of 8: SNT
File 5 of 8: FeeBurner
File 6 of 8: ConversionRates
File 7 of 8: SanityRates
File 8 of 8: SNTPlaceHolder
pragma solidity 0.4.18; // File: contracts/ERC20Interface.sol // https://github.com/ethereum/EIPs/issues/20 interface ERC20 { function totalSupply() public view returns (uint supply); function balanceOf(address _owner) public view returns (uint balance); function transfer(address _to, uint _value) public returns (bool success); function transferFrom(address _from, address _to, uint _value) public returns (bool success); function approve(address _spender, uint _value) public returns (bool success); function allowance(address _owner, address _spender) public view returns (uint remaining); function decimals() public view returns(uint digits); event Approval(address indexed _owner, address indexed _spender, uint _value); } // File: contracts/KyberNetworkInterface.sol /// @title Kyber Network interface interface KyberNetworkInterface { function maxGasPrice() public view returns(uint); function getUserCapInWei(address user) public view returns(uint); function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint); function enabled() public view returns(bool); function info(bytes32 id) public view returns(uint); function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) public view returns (uint expectedRate, uint slippageRate); function tradeWithHint(address trader, ERC20 src, uint srcAmount, ERC20 dest, address destAddress, uint maxDestAmount, uint minConversionRate, address walletId, bytes hint) public payable returns(uint); } // File: contracts/KyberNetworkProxyInterface.sol /// @title Kyber Network interface interface KyberNetworkProxyInterface { function maxGasPrice() public view returns(uint); function getUserCapInWei(address user) public view returns(uint); function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint); function enabled() public view returns(bool); function info(bytes32 id) public view returns(uint); function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) public view returns (uint expectedRate, uint slippageRate); function tradeWithHint(ERC20 src, uint srcAmount, ERC20 dest, address destAddress, uint maxDestAmount, uint minConversionRate, address walletId, bytes hint) public payable returns(uint); } // File: contracts/SimpleNetworkInterface.sol /// @title simple interface for Kyber Network interface SimpleNetworkInterface { function swapTokenToToken(ERC20 src, uint srcAmount, ERC20 dest, uint minConversionRate) public returns(uint); function swapEtherToToken(ERC20 token, uint minConversionRate) public payable returns(uint); function swapTokenToEther(ERC20 token, uint srcAmount, uint minConversionRate) public returns(uint); } // File: contracts/Utils.sol /// @title Kyber constants contract contract Utils { ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee); uint constant internal PRECISION = (10**18); uint constant internal MAX_QTY = (10**28); // 10B tokens uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH uint constant internal MAX_DECIMALS = 18; uint constant internal ETH_DECIMALS = 18; mapping(address=>uint) internal decimals; function setDecimals(ERC20 token) internal { if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS; else decimals[token] = token.decimals(); } function getDecimals(ERC20 token) internal view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access uint tokenDecimals = decimals[token]; // technically, there might be token with decimals 0 // moreover, very possible that old tokens have decimals 0 // these tokens will just have higher gas fees. if(tokenDecimals == 0) return token.decimals(); return tokenDecimals; } function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(srcQty <= MAX_QTY); require(rate <= MAX_RATE); if (dstDecimals >= srcDecimals) { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION; } else { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals))); } } function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(dstQty <= MAX_QTY); require(rate <= MAX_RATE); //source quantity is rounded up. to avoid dest quantity being too low. uint numerator; uint denominator; if (srcDecimals >= dstDecimals) { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals))); denominator = rate; } else { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty); denominator = (rate * (10**(dstDecimals - srcDecimals))); } return (numerator + denominator - 1) / denominator; //avoid rounding down errors } } // File: contracts/Utils2.sol contract Utils2 is Utils { /// @dev get the balance of a user. /// @param token The token type /// @return The balance function getBalance(ERC20 token, address user) public view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return user.balance; else return token.balanceOf(user); } function getDecimalsSafe(ERC20 token) internal returns(uint) { if (decimals[token] == 0) { setDecimals(token); } return decimals[token]; } function calcDestAmount(ERC20 src, ERC20 dest, uint srcAmount, uint rate) internal view returns(uint) { return calcDstQty(srcAmount, getDecimals(src), getDecimals(dest), rate); } function calcSrcAmount(ERC20 src, ERC20 dest, uint destAmount, uint rate) internal view returns(uint) { return calcSrcQty(destAmount, getDecimals(src), getDecimals(dest), rate); } function calcRateFromQty(uint srcAmount, uint destAmount, uint srcDecimals, uint dstDecimals) internal pure returns(uint) { require(srcAmount <= MAX_QTY); require(destAmount <= MAX_QTY); if (dstDecimals >= srcDecimals) { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); return (destAmount * PRECISION / ((10 ** (dstDecimals - srcDecimals)) * srcAmount)); } else { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); return (destAmount * PRECISION * (10 ** (srcDecimals - dstDecimals)) / srcAmount); } } } // File: contracts/PermissionGroups.sol contract PermissionGroups { address public admin; address public pendingAdmin; mapping(address=>bool) internal operators; mapping(address=>bool) internal alerters; address[] internal operatorsGroup; address[] internal alertersGroup; uint constant internal MAX_GROUP_SIZE = 50; function PermissionGroups() public { admin = msg.sender; } modifier onlyAdmin() { require(msg.sender == admin); _; } modifier onlyOperator() { require(operators[msg.sender]); _; } modifier onlyAlerter() { require(alerters[msg.sender]); _; } function getOperators () external view returns(address[]) { return operatorsGroup; } function getAlerters () external view returns(address[]) { return alertersGroup; } event TransferAdminPending(address pendingAdmin); /** * @dev Allows the current admin to set the pendingAdmin address. * @param newAdmin The address to transfer ownership to. */ function transferAdmin(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(pendingAdmin); pendingAdmin = newAdmin; } /** * @dev Allows the current admin to set the admin in one tx. Useful initial deployment. * @param newAdmin The address to transfer ownership to. */ function transferAdminQuickly(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(newAdmin); AdminClaimed(newAdmin, admin); admin = newAdmin; } event AdminClaimed( address newAdmin, address previousAdmin); /** * @dev Allows the pendingAdmin address to finalize the change admin process. */ function claimAdmin() public { require(pendingAdmin == msg.sender); AdminClaimed(pendingAdmin, admin); admin = pendingAdmin; pendingAdmin = address(0); } event AlerterAdded (address newAlerter, bool isAdd); function addAlerter(address newAlerter) public onlyAdmin { require(!alerters[newAlerter]); // prevent duplicates. require(alertersGroup.length < MAX_GROUP_SIZE); AlerterAdded(newAlerter, true); alerters[newAlerter] = true; alertersGroup.push(newAlerter); } function removeAlerter (address alerter) public onlyAdmin { require(alerters[alerter]); alerters[alerter] = false; for (uint i = 0; i < alertersGroup.length; ++i) { if (alertersGroup[i] == alerter) { alertersGroup[i] = alertersGroup[alertersGroup.length - 1]; alertersGroup.length--; AlerterAdded(alerter, false); break; } } } event OperatorAdded(address newOperator, bool isAdd); function addOperator(address newOperator) public onlyAdmin { require(!operators[newOperator]); // prevent duplicates. require(operatorsGroup.length < MAX_GROUP_SIZE); OperatorAdded(newOperator, true); operators[newOperator] = true; operatorsGroup.push(newOperator); } function removeOperator (address operator) public onlyAdmin { require(operators[operator]); operators[operator] = false; for (uint i = 0; i < operatorsGroup.length; ++i) { if (operatorsGroup[i] == operator) { operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1]; operatorsGroup.length -= 1; OperatorAdded(operator, false); break; } } } } // File: contracts/Withdrawable.sol /** * @title Contracts that should be able to recover tokens or ethers * @author Ilan Doron * @dev This allows to recover any tokens or Ethers received in a contract. * This will prevent any accidental loss of tokens. */ contract Withdrawable is PermissionGroups { event TokenWithdraw(ERC20 token, uint amount, address sendTo); /** * @dev Withdraw all ERC20 compatible tokens * @param token ERC20 The address of the token contract */ function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin { require(token.transfer(sendTo, amount)); TokenWithdraw(token, amount, sendTo); } event EtherWithdraw(uint amount, address sendTo); /** * @dev Withdraw Ethers */ function withdrawEther(uint amount, address sendTo) external onlyAdmin { sendTo.transfer(amount); EtherWithdraw(amount, sendTo); } } // File: contracts/KyberNetworkProxy.sol //////////////////////////////////////////////////////////////////////////////////////////////////////// /// @title Kyber Network proxy for main contract contract KyberNetworkProxy is KyberNetworkProxyInterface, SimpleNetworkInterface, Withdrawable, Utils2 { KyberNetworkInterface public kyberNetworkContract; function KyberNetworkProxy(address _admin) public { require(_admin != address(0)); admin = _admin; } /// @notice use token address ETH_TOKEN_ADDRESS for ether /// @dev makes a trade between src and dest token and send dest token to destAddress /// @param src Src token /// @param srcAmount amount of src tokens /// @param dest Destination token /// @param destAddress Address to send tokens to /// @param maxDestAmount A limit on the amount of dest tokens /// @param minConversionRate The minimal conversion rate. If actual rate is lower, trade is canceled. /// @param walletId is the wallet ID to send part of the fees /// @return amount of actual dest tokens function trade( ERC20 src, uint srcAmount, ERC20 dest, address destAddress, uint maxDestAmount, uint minConversionRate, address walletId ) public payable returns(uint) { bytes memory hint; return tradeWithHint( src, srcAmount, dest, destAddress, maxDestAmount, minConversionRate, walletId, hint ); } /// @dev makes a trade between src and dest token and send dest tokens to msg sender /// @param src Src token /// @param srcAmount amount of src tokens /// @param dest Destination token /// @param minConversionRate The minimal conversion rate. If actual rate is lower, trade is canceled. /// @return amount of actual dest tokens function swapTokenToToken( ERC20 src, uint srcAmount, ERC20 dest, uint minConversionRate ) public returns(uint) { bytes memory hint; return tradeWithHint( src, srcAmount, dest, msg.sender, MAX_QTY, minConversionRate, 0, hint ); } /// @dev makes a trade from Ether to token. Sends token to msg sender /// @param token Destination token /// @param minConversionRate The minimal conversion rate. If actual rate is lower, trade is canceled. /// @return amount of actual dest tokens function swapEtherToToken(ERC20 token, uint minConversionRate) public payable returns(uint) { bytes memory hint; return tradeWithHint( ETH_TOKEN_ADDRESS, msg.value, token, msg.sender, MAX_QTY, minConversionRate, 0, hint ); } /// @dev makes a trade from token to Ether, sends Ether to msg sender /// @param token Src token /// @param srcAmount amount of src tokens /// @param minConversionRate The minimal conversion rate. If actual rate is lower, trade is canceled. /// @return amount of actual dest tokens function swapTokenToEther(ERC20 token, uint srcAmount, uint minConversionRate) public returns(uint) { bytes memory hint; return tradeWithHint( token, srcAmount, ETH_TOKEN_ADDRESS, msg.sender, MAX_QTY, minConversionRate, 0, hint ); } struct UserBalance { uint srcBalance; uint destBalance; } event ExecuteTrade(address indexed trader, ERC20 src, ERC20 dest, uint actualSrcAmount, uint actualDestAmount); /// @notice use token address ETH_TOKEN_ADDRESS for ether /// @dev makes a trade between src and dest token and send dest token to destAddress /// @param src Src token /// @param srcAmount amount of src tokens /// @param dest Destination token /// @param destAddress Address to send tokens to /// @param maxDestAmount A limit on the amount of dest tokens /// @param minConversionRate The minimal conversion rate. If actual rate is lower, trade is canceled. /// @param walletId is the wallet ID to send part of the fees /// @param hint will give hints for the trade. /// @return amount of actual dest tokens function tradeWithHint( ERC20 src, uint srcAmount, ERC20 dest, address destAddress, uint maxDestAmount, uint minConversionRate, address walletId, bytes hint ) public payable returns(uint) { require(src == ETH_TOKEN_ADDRESS || msg.value == 0); UserBalance memory userBalanceBefore; userBalanceBefore.srcBalance = getBalance(src, msg.sender); userBalanceBefore.destBalance = getBalance(dest, destAddress); if (src == ETH_TOKEN_ADDRESS) { userBalanceBefore.srcBalance += msg.value; } else { require(src.transferFrom(msg.sender, kyberNetworkContract, srcAmount)); } uint reportedDestAmount = kyberNetworkContract.tradeWithHint.value(msg.value)( msg.sender, src, srcAmount, dest, destAddress, maxDestAmount, minConversionRate, walletId, hint ); TradeOutcome memory tradeOutcome = calculateTradeOutcome( userBalanceBefore.srcBalance, userBalanceBefore.destBalance, src, dest, destAddress ); require(reportedDestAmount == tradeOutcome.userDeltaDestAmount); require(tradeOutcome.userDeltaDestAmount <= maxDestAmount); require(tradeOutcome.actualRate >= minConversionRate); ExecuteTrade(msg.sender, src, dest, tradeOutcome.userDeltaSrcAmount, tradeOutcome.userDeltaDestAmount); return tradeOutcome.userDeltaDestAmount; } event KyberNetworkSet(address newNetworkContract, address oldNetworkContract); function setKyberNetworkContract(KyberNetworkInterface _kyberNetworkContract) public onlyAdmin { require(_kyberNetworkContract != address(0)); KyberNetworkSet(_kyberNetworkContract, kyberNetworkContract); kyberNetworkContract = _kyberNetworkContract; } function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) public view returns(uint expectedRate, uint slippageRate) { return kyberNetworkContract.getExpectedRate(src, dest, srcQty); } function getUserCapInWei(address user) public view returns(uint) { return kyberNetworkContract.getUserCapInWei(user); } function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint) { return kyberNetworkContract.getUserCapInTokenWei(user, token); } function maxGasPrice() public view returns(uint) { return kyberNetworkContract.maxGasPrice(); } function enabled() public view returns(bool) { return kyberNetworkContract.enabled(); } function info(bytes32 field) public view returns(uint) { return kyberNetworkContract.info(field); } struct TradeOutcome { uint userDeltaSrcAmount; uint userDeltaDestAmount; uint actualRate; } function calculateTradeOutcome (uint srcBalanceBefore, uint destBalanceBefore, ERC20 src, ERC20 dest, address destAddress) internal returns(TradeOutcome outcome) { uint userSrcBalanceAfter; uint userDestBalanceAfter; userSrcBalanceAfter = getBalance(src, msg.sender); userDestBalanceAfter = getBalance(dest, destAddress); //protect from underflow require(userDestBalanceAfter > destBalanceBefore); require(srcBalanceBefore > userSrcBalanceAfter); outcome.userDeltaDestAmount = userDestBalanceAfter - destBalanceBefore; outcome.userDeltaSrcAmount = srcBalanceBefore - userSrcBalanceAfter; outcome.actualRate = calcRateFromQty( outcome.userDeltaSrcAmount, outcome.userDeltaDestAmount, getDecimalsSafe(src), getDecimalsSafe(dest) ); } }
File 2 of 8: KyberReserve
pragma solidity 0.4.18; contract Utils { ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee); uint constant internal PRECISION = (10**18); uint constant internal MAX_QTY = (10**28); // 10B tokens uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH uint constant internal MAX_DECIMALS = 18; uint constant internal ETH_DECIMALS = 18; mapping(address=>uint) internal decimals; function setDecimals(ERC20 token) internal { if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS; else decimals[token] = token.decimals(); } function getDecimals(ERC20 token) internal view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access uint tokenDecimals = decimals[token]; // technically, there might be token with decimals 0 // moreover, very possible that old tokens have decimals 0 // these tokens will just have higher gas fees. if(tokenDecimals == 0) return token.decimals(); return tokenDecimals; } function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(srcQty <= MAX_QTY); require(rate <= MAX_RATE); if (dstDecimals >= srcDecimals) { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION; } else { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals))); } } function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(dstQty <= MAX_QTY); require(rate <= MAX_RATE); //source quantity is rounded up. to avoid dest quantity being too low. uint numerator; uint denominator; if (srcDecimals >= dstDecimals) { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals))); denominator = rate; } else { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty); denominator = (rate * (10**(dstDecimals - srcDecimals))); } return (numerator + denominator - 1) / denominator; //avoid rounding down errors } } contract PermissionGroups { address public admin; address public pendingAdmin; mapping(address=>bool) internal operators; mapping(address=>bool) internal alerters; address[] internal operatorsGroup; address[] internal alertersGroup; uint constant internal MAX_GROUP_SIZE = 50; function PermissionGroups() public { admin = msg.sender; } modifier onlyAdmin() { require(msg.sender == admin); _; } modifier onlyOperator() { require(operators[msg.sender]); _; } modifier onlyAlerter() { require(alerters[msg.sender]); _; } function getOperators () external view returns(address[]) { return operatorsGroup; } function getAlerters () external view returns(address[]) { return alertersGroup; } event TransferAdminPending(address pendingAdmin); /** * @dev Allows the current admin to set the pendingAdmin address. * @param newAdmin The address to transfer ownership to. */ function transferAdmin(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(pendingAdmin); pendingAdmin = newAdmin; } /** * @dev Allows the current admin to set the admin in one tx. Useful initial deployment. * @param newAdmin The address to transfer ownership to. */ function transferAdminQuickly(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(newAdmin); AdminClaimed(newAdmin, admin); admin = newAdmin; } event AdminClaimed( address newAdmin, address previousAdmin); /** * @dev Allows the pendingAdmin address to finalize the change admin process. */ function claimAdmin() public { require(pendingAdmin == msg.sender); AdminClaimed(pendingAdmin, admin); admin = pendingAdmin; pendingAdmin = address(0); } event AlerterAdded (address newAlerter, bool isAdd); function addAlerter(address newAlerter) public onlyAdmin { require(!alerters[newAlerter]); // prevent duplicates. require(alertersGroup.length < MAX_GROUP_SIZE); AlerterAdded(newAlerter, true); alerters[newAlerter] = true; alertersGroup.push(newAlerter); } function removeAlerter (address alerter) public onlyAdmin { require(alerters[alerter]); alerters[alerter] = false; for (uint i = 0; i < alertersGroup.length; ++i) { if (alertersGroup[i] == alerter) { alertersGroup[i] = alertersGroup[alertersGroup.length - 1]; alertersGroup.length--; AlerterAdded(alerter, false); break; } } } event OperatorAdded(address newOperator, bool isAdd); function addOperator(address newOperator) public onlyAdmin { require(!operators[newOperator]); // prevent duplicates. require(operatorsGroup.length < MAX_GROUP_SIZE); OperatorAdded(newOperator, true); operators[newOperator] = true; operatorsGroup.push(newOperator); } function removeOperator (address operator) public onlyAdmin { require(operators[operator]); operators[operator] = false; for (uint i = 0; i < operatorsGroup.length; ++i) { if (operatorsGroup[i] == operator) { operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1]; operatorsGroup.length -= 1; OperatorAdded(operator, false); break; } } } } interface ConversionRatesInterface { function recordImbalance( ERC20 token, int buyAmount, uint rateUpdateBlock, uint currentBlock ) public; function getRate(ERC20 token, uint currentBlockNumber, bool buy, uint qty) public view returns(uint); } interface ERC20 { function totalSupply() public view returns (uint supply); function balanceOf(address _owner) public view returns (uint balance); function transfer(address _to, uint _value) public returns (bool success); function transferFrom(address _from, address _to, uint _value) public returns (bool success); function approve(address _spender, uint _value) public returns (bool success); function allowance(address _owner, address _spender) public view returns (uint remaining); function decimals() public view returns(uint digits); event Approval(address indexed _owner, address indexed _spender, uint _value); } interface KyberReserveInterface { function trade( ERC20 srcToken, uint srcAmount, ERC20 destToken, address destAddress, uint conversionRate, bool validate ) public payable returns(bool); function getConversionRate(ERC20 src, ERC20 dest, uint srcQty, uint blockNumber) public view returns(uint); } interface SanityRatesInterface { function getSanityRate(ERC20 src, ERC20 dest) public view returns(uint); } contract Withdrawable is PermissionGroups { event TokenWithdraw(ERC20 token, uint amount, address sendTo); /** * @dev Withdraw all ERC20 compatible tokens * @param token ERC20 The address of the token contract */ function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin { require(token.transfer(sendTo, amount)); TokenWithdraw(token, amount, sendTo); } event EtherWithdraw(uint amount, address sendTo); /** * @dev Withdraw Ethers */ function withdrawEther(uint amount, address sendTo) external onlyAdmin { sendTo.transfer(amount); EtherWithdraw(amount, sendTo); } } contract KyberReserve is KyberReserveInterface, Withdrawable, Utils { address public kyberNetwork; bool public tradeEnabled; ConversionRatesInterface public conversionRatesContract; SanityRatesInterface public sanityRatesContract; mapping(bytes32=>bool) public approvedWithdrawAddresses; // sha3(token,address)=>bool function KyberReserve(address _kyberNetwork, ConversionRatesInterface _ratesContract, address _admin) public { require(_admin != address(0)); require(_ratesContract != address(0)); require(_kyberNetwork != address(0)); kyberNetwork = _kyberNetwork; conversionRatesContract = _ratesContract; admin = _admin; tradeEnabled = true; } event DepositToken(ERC20 token, uint amount); function() public payable { DepositToken(ETH_TOKEN_ADDRESS, msg.value); } event TradeExecute( address indexed origin, address src, uint srcAmount, address destToken, uint destAmount, address destAddress ); function trade( ERC20 srcToken, uint srcAmount, ERC20 destToken, address destAddress, uint conversionRate, bool validate ) public payable returns(bool) { require(tradeEnabled); require(msg.sender == kyberNetwork); require(doTrade(srcToken, srcAmount, destToken, destAddress, conversionRate, validate)); return true; } event TradeEnabled(bool enable); function enableTrade() public onlyAdmin returns(bool) { tradeEnabled = true; TradeEnabled(true); return true; } function disableTrade() public onlyAlerter returns(bool) { tradeEnabled = false; TradeEnabled(false); return true; } event WithdrawAddressApproved(ERC20 token, address addr, bool approve); function approveWithdrawAddress(ERC20 token, address addr, bool approve) public onlyAdmin { approvedWithdrawAddresses[keccak256(token, addr)] = approve; WithdrawAddressApproved(token, addr, approve); setDecimals(token); } event WithdrawFunds(ERC20 token, uint amount, address destination); function withdraw(ERC20 token, uint amount, address destination) public onlyOperator returns(bool) { require(approvedWithdrawAddresses[keccak256(token, destination)]); if (token == ETH_TOKEN_ADDRESS) { destination.transfer(amount); } else { require(token.transfer(destination, amount)); } WithdrawFunds(token, amount, destination); return true; } event SetContractAddresses(address network, address rate, address sanity); function setContracts(address _kyberNetwork, ConversionRatesInterface _conversionRates, SanityRatesInterface _sanityRates) public onlyAdmin { require(_kyberNetwork != address(0)); require(_conversionRates != address(0)); kyberNetwork = _kyberNetwork; conversionRatesContract = _conversionRates; sanityRatesContract = _sanityRates; SetContractAddresses(kyberNetwork, conversionRatesContract, sanityRatesContract); } //////////////////////////////////////////////////////////////////////////// /// status functions /////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// function getBalance(ERC20 token) public view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return this.balance; else return token.balanceOf(this); } function getDestQty(ERC20 src, ERC20 dest, uint srcQty, uint rate) public view returns(uint) { uint dstDecimals = getDecimals(dest); uint srcDecimals = getDecimals(src); return calcDstQty(srcQty, srcDecimals, dstDecimals, rate); } function getSrcQty(ERC20 src, ERC20 dest, uint dstQty, uint rate) public view returns(uint) { uint dstDecimals = getDecimals(dest); uint srcDecimals = getDecimals(src); return calcSrcQty(dstQty, srcDecimals, dstDecimals, rate); } function getConversionRate(ERC20 src, ERC20 dest, uint srcQty, uint blockNumber) public view returns(uint) { ERC20 token; bool buy; if (!tradeEnabled) return 0; if (ETH_TOKEN_ADDRESS == src) { buy = true; token = dest; } else if (ETH_TOKEN_ADDRESS == dest) { buy = false; token = src; } else { return 0; // pair is not listed } uint rate = conversionRatesContract.getRate(token, blockNumber, buy, srcQty); uint destQty = getDestQty(src, dest, srcQty, rate); if (getBalance(dest) < destQty) return 0; if (sanityRatesContract != address(0)) { uint sanityRate = sanityRatesContract.getSanityRate(src, dest); if (rate > sanityRate) return 0; } return rate; } /// @dev do a trade /// @param srcToken Src token /// @param srcAmount Amount of src token /// @param destToken Destination token /// @param destAddress Destination address to send tokens to /// @param validate If true, additional validations are applicable /// @return true iff trade is successful function doTrade( ERC20 srcToken, uint srcAmount, ERC20 destToken, address destAddress, uint conversionRate, bool validate ) internal returns(bool) { // can skip validation if done at kyber network level if (validate) { require(conversionRate > 0); if (srcToken == ETH_TOKEN_ADDRESS) require(msg.value == srcAmount); else require(msg.value == 0); } uint destAmount = getDestQty(srcToken, destToken, srcAmount, conversionRate); // sanity check require(destAmount > 0); // add to imbalance ERC20 token; int buy; if (srcToken == ETH_TOKEN_ADDRESS) { buy = int(destAmount); token = destToken; } else { buy = -1 * int(srcAmount); token = srcToken; } conversionRatesContract.recordImbalance( token, buy, 0, block.number ); // collect src tokens if (srcToken != ETH_TOKEN_ADDRESS) { require(srcToken.transferFrom(msg.sender, this, srcAmount)); } // send dest tokens if (destToken == ETH_TOKEN_ADDRESS) { destAddress.transfer(destAmount); } else { require(destToken.transfer(destAddress, destAmount)); } TradeExecute(msg.sender, srcToken, srcAmount, destToken, destAmount, destAddress); return true; } }
File 3 of 8: KyberNetwork
{"ERC20Interface.sol":{"content":"pragma solidity 0.4.18;\n\n\n// https://github.com/ethereum/EIPs/issues/20\ninterface ERC20 {\n function totalSupply() public view returns (uint supply);\n function balanceOf(address _owner) public view returns (uint balance);\n function transfer(address _to, uint _value) public returns (bool success);\n function transferFrom(address _from, address _to, uint _value) public returns (bool success);\n function approve(address _spender, uint _value) public returns (bool success);\n function allowance(address _owner, address _spender) public view returns (uint remaining);\n function decimals() public view returns(uint digits);\n event Approval(address indexed _owner, address indexed _spender, uint _value);\n}\n"},"ExpectedRateInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n\ninterface ExpectedRateInterface {\n function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty, bool usePermissionless) public view\n returns (uint expectedRate, uint slippageRate);\n}\n"},"FeeBurnerInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\ninterface FeeBurnerInterface {\n function handleFees (uint tradeWeiAmount, address reserve, address wallet) public returns(bool);\n function setReserveData(address reserve, uint feesInBps, address kncWallet) public;\n}\n"},"KyberNetwork.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\nimport \"./KyberReserveInterface.sol\";\nimport \"./KyberNetworkInterface.sol\";\nimport \"./Withdrawable.sol\";\nimport \"./Utils3.sol\";\nimport \"./WhiteListInterface.sol\";\nimport \"./ExpectedRateInterface.sol\";\nimport \"./FeeBurnerInterface.sol\";\n\n\n/**\n * @title Helps contracts guard against reentrancy attacks.\n */\ncontract ReentrancyGuard {\n\n /// @dev counter to allow mutex lock with only one SSTORE operation\n uint256 private guardCounter = 1;\n\n /**\n * @dev Prevents a function from calling itself, directly or indirectly.\n * Calling one `nonReentrant` function from\n * another is not supported. Instead, you can implement a\n * `private` function doing the actual work, and an `external`\n * wrapper marked as `nonReentrant`.\n */\n modifier nonReentrant() {\n guardCounter += 1;\n uint256 localCounter = guardCounter;\n _;\n require(localCounter == guardCounter);\n }\n}\n\n\n////////////////////////////////////////////////////////////////////////////////////////////////////////\n/// @title Kyber Network main contract\ncontract KyberNetwork is Withdrawable, Utils3, KyberNetworkInterface, ReentrancyGuard {\n\n bytes public constant PERM_HINT = \"PERM\";\n uint public constant PERM_HINT_GET_RATE = 1 \u003c\u003c 255; // for get rate. bit mask hint.\n\n uint public negligibleRateDiff = 10; // basic rate steps will be in 0.01%\n KyberReserveInterface[] public reserves;\n mapping(address=\u003eReserveType) public reserveType;\n WhiteListInterface public whiteListContract;\n ExpectedRateInterface public expectedRateContract;\n FeeBurnerInterface public feeBurnerContract;\n address public kyberNetworkProxyContract;\n uint public maxGasPriceValue = 50 * 1000 * 1000 * 1000; // 50 gwei\n bool public isEnabled = false; // network is enabled\n mapping(bytes32=\u003euint) public infoFields; // this is only a UI field for external app.\n\n mapping(address=\u003eaddress[]) public reservesPerTokenSrc; //reserves supporting token to eth\n mapping(address=\u003eaddress[]) public reservesPerTokenDest;//reserves support eth to token\n\n enum ReserveType {NONE, PERMISSIONED, PERMISSIONLESS}\n bytes internal constant EMPTY_HINT = \"\";\n\n function KyberNetwork(address _admin) public {\n require(_admin != address(0));\n admin = _admin;\n }\n\n event EtherReceival(address indexed sender, uint amount);\n\n /* solhint-disable no-complex-fallback */\n function() public payable {\n EtherReceival(msg.sender, msg.value);\n }\n /* solhint-enable no-complex-fallback */\n\n struct TradeInput {\n address trader;\n ERC20 src;\n uint srcAmount;\n ERC20 dest;\n address destAddress;\n uint maxDestAmount;\n uint minConversionRate;\n address walletId;\n bytes hint;\n }\n\n function tradeWithHint(\n address trader,\n ERC20 src,\n uint srcAmount,\n ERC20 dest,\n address destAddress,\n uint maxDestAmount,\n uint minConversionRate,\n address walletId,\n bytes hint\n )\n public\n nonReentrant\n payable\n returns(uint)\n {\n require(msg.sender == kyberNetworkProxyContract);\n require((hint.length == 0) || (hint.length == 4));\n\n TradeInput memory tradeInput;\n\n tradeInput.trader = trader;\n tradeInput.src = src;\n tradeInput.srcAmount = srcAmount;\n tradeInput.dest = dest;\n tradeInput.destAddress = destAddress;\n tradeInput.maxDestAmount = maxDestAmount;\n tradeInput.minConversionRate = minConversionRate;\n tradeInput.walletId = walletId;\n tradeInput.hint = hint;\n\n return trade(tradeInput);\n }\n\n event AddReserveToNetwork(KyberReserveInterface indexed reserve, bool add, bool isPermissionless);\n\n /// @notice can be called only by operator\n /// @dev add or deletes a reserve to/from the network.\n /// @param reserve The reserve address.\n /// @param isPermissionless is the new reserve from permissionless type.\n function addReserve(KyberReserveInterface reserve, bool isPermissionless) public onlyOperator\n returns(bool)\n {\n require(reserveType[reserve] == ReserveType.NONE);\n reserves.push(reserve);\n\n reserveType[reserve] = isPermissionless ? ReserveType.PERMISSIONLESS : ReserveType.PERMISSIONED;\n\n AddReserveToNetwork(reserve, true, isPermissionless);\n\n return true;\n }\n\n event RemoveReserveFromNetwork(KyberReserveInterface reserve);\n\n /// @notice can be called only by operator\n /// @dev removes a reserve from Kyber network.\n /// @param reserve The reserve address.\n /// @param index in reserve array.\n function removeReserve(KyberReserveInterface reserve, uint index) public onlyOperator\n returns(bool)\n {\n\n require(reserveType[reserve] != ReserveType.NONE);\n require(reserves[index] == reserve);\n\n reserveType[reserve] = ReserveType.NONE;\n reserves[index] = reserves[reserves.length - 1];\n reserves.length--;\n\n RemoveReserveFromNetwork(reserve);\n\n return true;\n }\n\n event ListReservePairs(address indexed reserve, ERC20 src, ERC20 dest, bool add);\n\n /// @notice can be called only by operator\n /// @dev allow or prevent a specific reserve to trade a pair of tokens\n /// @param reserve The reserve address.\n /// @param token token address\n /// @param ethToToken will it support ether to token trade\n /// @param tokenToEth will it support token to ether trade\n /// @param add If true then list this pair, otherwise unlist it.\n function listPairForReserve(address reserve, ERC20 token, bool ethToToken, bool tokenToEth, bool add)\n public\n onlyOperator\n returns(bool)\n {\n require(reserveType[reserve] != ReserveType.NONE);\n\n if (ethToToken) {\n listPairs(reserve, token, false, add);\n\n ListReservePairs(reserve, ETH_TOKEN_ADDRESS, token, add);\n }\n\n if (tokenToEth) {\n listPairs(reserve, token, true, add);\n\n if (add) {\n require(token.approve(reserve, 2**255)); // approve infinity\n } else {\n require(token.approve(reserve, 0));\n }\n\n ListReservePairs(reserve, token, ETH_TOKEN_ADDRESS, add);\n }\n\n setDecimals(token);\n\n return true;\n }\n\n event WhiteListContractSet(WhiteListInterface newContract, WhiteListInterface currentContract);\n\n ///@param whiteList can be empty\n function setWhiteList(WhiteListInterface whiteList) public onlyAdmin {\n WhiteListContractSet(whiteList, whiteListContract);\n whiteListContract = whiteList;\n }\n\n event ExpectedRateContractSet(ExpectedRateInterface newContract, ExpectedRateInterface currentContract);\n\n function setExpectedRate(ExpectedRateInterface expectedRate) public onlyAdmin {\n require(expectedRate != address(0));\n\n ExpectedRateContractSet(expectedRate, expectedRateContract);\n expectedRateContract = expectedRate;\n }\n\n event FeeBurnerContractSet(FeeBurnerInterface newContract, FeeBurnerInterface currentContract);\n\n function setFeeBurner(FeeBurnerInterface feeBurner) public onlyAdmin {\n require(feeBurner != address(0));\n\n FeeBurnerContractSet(feeBurner, feeBurnerContract);\n feeBurnerContract = feeBurner;\n }\n\n event KyberNetwrokParamsSet(uint maxGasPrice, uint negligibleRateDiff);\n\n function setParams(\n uint _maxGasPrice,\n uint _negligibleRateDiff\n )\n public\n onlyAdmin\n {\n require(_negligibleRateDiff \u003c= 100 * 100); // at most 100%\n\n maxGasPriceValue = _maxGasPrice;\n negligibleRateDiff = _negligibleRateDiff;\n KyberNetwrokParamsSet(maxGasPriceValue, negligibleRateDiff);\n }\n\n event KyberNetworkSetEnable(bool isEnabled);\n\n function setEnable(bool _enable) public onlyAdmin {\n if (_enable) {\n require(feeBurnerContract != address(0));\n require(expectedRateContract != address(0));\n require(kyberNetworkProxyContract != address(0));\n }\n isEnabled = _enable;\n\n KyberNetworkSetEnable(isEnabled);\n }\n\n function setInfo(bytes32 field, uint value) public onlyOperator {\n infoFields[field] = value;\n }\n\n event KyberProxySet(address proxy, address sender);\n\n function setKyberProxy(address networkProxy) public onlyAdmin {\n require(networkProxy != address(0));\n kyberNetworkProxyContract = networkProxy;\n KyberProxySet(kyberNetworkProxyContract, msg.sender);\n }\n\n /// @dev returns number of reserves\n /// @return number of reserves\n function getNumReserves() public view returns(uint) {\n return reserves.length;\n }\n\n /// @notice should be called off chain\n /// @dev get an array of all reserves\n /// @return An array of all reserves\n function getReserves() public view returns(KyberReserveInterface[]) {\n return reserves;\n }\n\n function maxGasPrice() public view returns(uint) {\n return maxGasPriceValue;\n }\n\n function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty)\n public view\n returns(uint expectedRate, uint slippageRate)\n {\n require(expectedRateContract != address(0));\n if (src == dest) return (0,0);\n bool includePermissionless = true;\n\n if (srcQty \u0026 PERM_HINT_GET_RATE \u003e 0) {\n includePermissionless = false;\n srcQty = srcQty \u0026 ~PERM_HINT_GET_RATE;\n }\n\n return expectedRateContract.getExpectedRate(src, dest, srcQty, includePermissionless);\n }\n\n function getExpectedRateOnlyPermission(ERC20 src, ERC20 dest, uint srcQty)\n public view\n returns(uint expectedRate, uint slippageRate)\n {\n require(expectedRateContract != address(0));\n if (src == dest) return (0,0);\n return expectedRateContract.getExpectedRate(src, dest, srcQty, false);\n }\n\n function getUserCapInWei(address user) public view returns(uint) {\n if (whiteListContract == address(0)) return (2 ** 255);\n return whiteListContract.getUserCapInWei(user);\n }\n\n function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint) {\n //future feature\n user;\n token;\n require(false);\n }\n\n struct BestRateResult {\n uint rate;\n address reserve1;\n address reserve2;\n uint weiAmount;\n uint rateSrcToEth;\n uint rateEthToDest;\n uint destAmount;\n }\n\n /// @notice use token address ETH_TOKEN_ADDRESS for ether\n /// @dev best conversion rate for a pair of tokens, if number of reserves have small differences. randomize\n /// @param src Src token\n /// @param dest Destination token\n /// @return obsolete - used to return best reserve index. not relevant anymore for this API.\n function findBestRate(ERC20 src, ERC20 dest, uint srcAmount) public view returns(uint obsolete, uint rate) {\n BestRateResult memory result = findBestRateTokenToToken(src, dest, srcAmount, EMPTY_HINT);\n return(0, result.rate);\n }\n\n function findBestRateOnlyPermission(ERC20 src, ERC20 dest, uint srcAmount)\n public\n view\n returns(uint obsolete, uint rate)\n {\n BestRateResult memory result = findBestRateTokenToToken(src, dest, srcAmount, PERM_HINT);\n return(0, result.rate);\n }\n\n function enabled() public view returns(bool) {\n return isEnabled;\n }\n\n function info(bytes32 field) public view returns(uint) {\n return infoFields[field];\n }\n\n /* solhint-disable code-complexity */\n // Regarding complexity. Below code follows the required algorithm for choosing a reserve.\n // It has been tested, reviewed and found to be clear enough.\n //@dev this function always src or dest are ether. can\u0027t do token to token\n function searchBestRate(ERC20 src, ERC20 dest, uint srcAmount, bool usePermissionless)\n public\n view\n returns(address, uint)\n {\n uint bestRate = 0;\n uint bestReserve = 0;\n uint numRelevantReserves = 0;\n\n //return 1 for ether to ether\n if (src == dest) return (reserves[bestReserve], PRECISION);\n\n address[] memory reserveArr;\n\n reserveArr = src == ETH_TOKEN_ADDRESS ? reservesPerTokenDest[dest] : reservesPerTokenSrc[src];\n\n if (reserveArr.length == 0) return (reserves[bestReserve], bestRate);\n\n uint[] memory rates = new uint[](reserveArr.length);\n uint[] memory reserveCandidates = new uint[](reserveArr.length);\n\n for (uint i = 0; i \u003c reserveArr.length; i++) {\n //list all reserves that have this token.\n if (!usePermissionless \u0026\u0026 reserveType[reserveArr[i]] == ReserveType.PERMISSIONLESS) {\n continue;\n }\n\n rates[i] = (KyberReserveInterface(reserveArr[i])).getConversionRate(src, dest, srcAmount, block.number);\n\n if (rates[i] \u003e bestRate) {\n //best rate is highest rate\n bestRate = rates[i];\n }\n }\n\n if (bestRate \u003e 0) {\n uint smallestRelevantRate = (bestRate * 10000) / (10000 + negligibleRateDiff);\n\n for (i = 0; i \u003c reserveArr.length; i++) {\n if (rates[i] \u003e= smallestRelevantRate) {\n reserveCandidates[numRelevantReserves++] = i;\n }\n }\n\n if (numRelevantReserves \u003e 1) {\n //when encountering small rate diff from bestRate. draw from relevant reserves\n bestReserve = reserveCandidates[uint(block.blockhash(block.number-1)) % numRelevantReserves];\n } else {\n bestReserve = reserveCandidates[0];\n }\n\n bestRate = rates[bestReserve];\n }\n\n return (reserveArr[bestReserve], bestRate);\n }\n /* solhint-enable code-complexity */\n\n function getReservesRates(ERC20 token, uint optionalAmount) public view\n returns(address[] buyReserves, uint[] buyRates, address[] sellReserves, uint[] sellRates)\n {\n uint amount = optionalAmount \u003e 0 ? optionalAmount : 1000;\n ERC20 ETH = ETH_TOKEN_ADDRESS;\n\n buyReserves = reservesPerTokenDest[token];\n buyRates = new uint[](buyReserves.length);\n\n for (uint i = 0; i \u003c buyReserves.length; i++) {\n buyRates[i] = (KyberReserveInterface(buyReserves[i])).getConversionRate(ETH, token, amount, block.number);\n }\n\n sellReserves = reservesPerTokenSrc[token];\n sellRates = new uint[](sellReserves.length);\n\n for (i = 0; i \u003c sellReserves.length; i++) {\n sellRates[i] = (KyberReserveInterface(sellReserves[i])).getConversionRate(token, ETH, amount, block.number);\n }\n }\n\n function findBestRateTokenToToken(ERC20 src, ERC20 dest, uint srcAmount, bytes hint) internal view\n returns(BestRateResult result)\n {\n //by default we use permission less reserves\n bool usePermissionless = true;\n\n // if hint in first 4 bytes == \u0027PERM\u0027 only permissioned reserves will be used.\n if ((hint.length \u003e= 4) \u0026\u0026 (keccak256(hint[0], hint[1], hint[2], hint[3]) == keccak256(PERM_HINT))) {\n usePermissionless = false;\n }\n\n uint srcDecimals = getDecimals(src);\n uint destDecimals = getDecimals(dest);\n\n (result.reserve1, result.rateSrcToEth) =\n searchBestRate(src, ETH_TOKEN_ADDRESS, srcAmount, usePermissionless);\n\n result.weiAmount = calcDestAmountWithDecimals(srcDecimals, ETH_DECIMALS, srcAmount, result.rateSrcToEth);\n //if weiAmount is zero, return zero rate to avoid revert in ETH -\u003e token call\n if (result.weiAmount == 0) {\n result.rate = 0;\n return;\n }\n \n (result.reserve2, result.rateEthToDest) =\n searchBestRate(ETH_TOKEN_ADDRESS, dest, result.weiAmount, usePermissionless);\n\n result.destAmount = calcDestAmountWithDecimals(ETH_DECIMALS, destDecimals, result.weiAmount, result.rateEthToDest);\n\n result.rate = calcRateFromQty(srcAmount, result.destAmount, srcDecimals, destDecimals);\n }\n\n function listPairs(address reserve, ERC20 token, bool isTokenToEth, bool add) internal {\n uint i;\n address[] storage reserveArr = reservesPerTokenDest[token];\n\n if (isTokenToEth) {\n reserveArr = reservesPerTokenSrc[token];\n }\n\n for (i = 0; i \u003c reserveArr.length; i++) {\n if (reserve == reserveArr[i]) {\n if (add) {\n break; //already added\n } else {\n //remove\n reserveArr[i] = reserveArr[reserveArr.length - 1];\n reserveArr.length--;\n break;\n }\n }\n }\n\n if (add \u0026\u0026 i == reserveArr.length) {\n //if reserve wasn\u0027t found add it\n reserveArr.push(reserve);\n }\n }\n\n event KyberTrade(address indexed trader, ERC20 src, ERC20 dest, uint srcAmount, uint dstAmount,\n address destAddress, uint ethWeiValue, address reserve1, address reserve2, bytes hint);\n\n /* solhint-disable function-max-lines */\n // Most of the lines here are functions calls spread over multiple lines. We find this function readable enough\n /// @notice use token address ETH_TOKEN_ADDRESS for ether\n /// @dev trade api for kyber network.\n /// @param tradeInput structure of trade inputs\n function trade(TradeInput tradeInput) internal returns(uint) {\n require(isEnabled);\n require(tx.gasprice \u003c= maxGasPriceValue);\n require(validateTradeInput(tradeInput.src, tradeInput.srcAmount, tradeInput.dest, tradeInput.destAddress));\n\n BestRateResult memory rateResult =\n findBestRateTokenToToken(tradeInput.src, tradeInput.dest, tradeInput.srcAmount, tradeInput.hint);\n\n require(rateResult.rate \u003e 0);\n require(rateResult.rate \u003c MAX_RATE);\n require(rateResult.rate \u003e= tradeInput.minConversionRate);\n\n uint actualDestAmount;\n uint weiAmount;\n uint actualSrcAmount;\n\n (actualSrcAmount, weiAmount, actualDestAmount) = calcActualAmounts(tradeInput.src,\n tradeInput.dest,\n tradeInput.srcAmount,\n tradeInput.maxDestAmount,\n rateResult);\n\n require(getUserCapInWei(tradeInput.trader) \u003e= weiAmount);\n require(handleChange(tradeInput.src, tradeInput.srcAmount, actualSrcAmount, tradeInput.trader));\n\n require(doReserveTrade( //src to ETH\n tradeInput.src,\n actualSrcAmount,\n ETH_TOKEN_ADDRESS,\n this,\n weiAmount,\n KyberReserveInterface(rateResult.reserve1),\n rateResult.rateSrcToEth,\n true));\n\n require(doReserveTrade( //Eth to dest\n ETH_TOKEN_ADDRESS,\n weiAmount,\n tradeInput.dest,\n tradeInput.destAddress,\n actualDestAmount,\n KyberReserveInterface(rateResult.reserve2),\n rateResult.rateEthToDest,\n true));\n\n if (tradeInput.src != ETH_TOKEN_ADDRESS) //\"fake\" trade. (ether to ether) - don\u0027t burn.\n require(feeBurnerContract.handleFees(weiAmount, rateResult.reserve1, tradeInput.walletId));\n if (tradeInput.dest != ETH_TOKEN_ADDRESS) //\"fake\" trade. (ether to ether) - don\u0027t burn.\n require(feeBurnerContract.handleFees(weiAmount, rateResult.reserve2, tradeInput.walletId));\n\n KyberTrade({\n trader: tradeInput.trader,\n src: tradeInput.src,\n dest: tradeInput.dest,\n srcAmount: actualSrcAmount,\n dstAmount: actualDestAmount,\n destAddress: tradeInput.destAddress,\n ethWeiValue: weiAmount,\n reserve1: (tradeInput.src == ETH_TOKEN_ADDRESS) ? address(0) : rateResult.reserve1,\n reserve2: (tradeInput.dest == ETH_TOKEN_ADDRESS) ? address(0) : rateResult.reserve2,\n hint: tradeInput.hint\n });\n\n return actualDestAmount;\n }\n /* solhint-enable function-max-lines */\n\n function calcActualAmounts (ERC20 src, ERC20 dest, uint srcAmount, uint maxDestAmount, BestRateResult rateResult)\n internal view returns(uint actualSrcAmount, uint weiAmount, uint actualDestAmount)\n {\n if (rateResult.destAmount \u003e maxDestAmount) {\n actualDestAmount = maxDestAmount;\n weiAmount = calcSrcAmount(ETH_TOKEN_ADDRESS, dest, actualDestAmount, rateResult.rateEthToDest);\n actualSrcAmount = calcSrcAmount(src, ETH_TOKEN_ADDRESS, weiAmount, rateResult.rateSrcToEth);\n require(actualSrcAmount \u003c= srcAmount);\n } else {\n actualDestAmount = rateResult.destAmount;\n actualSrcAmount = srcAmount;\n weiAmount = rateResult.weiAmount;\n }\n }\n\n /// @notice use token address ETH_TOKEN_ADDRESS for ether\n /// @dev do one trade with a reserve\n /// @param src Src token\n /// @param amount amount of src tokens\n /// @param dest Destination token\n /// @param destAddress Address to send tokens to\n /// @param reserve Reserve to use\n /// @param validate If true, additional validations are applicable\n /// @return true if trade is successful\n function doReserveTrade(\n ERC20 src,\n uint amount,\n ERC20 dest,\n address destAddress,\n uint expectedDestAmount,\n KyberReserveInterface reserve,\n uint conversionRate,\n bool validate\n )\n internal\n returns(bool)\n {\n uint callValue = 0;\n\n if (src == dest) {\n //this is for a \"fake\" trade when both src and dest are ethers.\n if (destAddress != (address(this)))\n destAddress.transfer(amount);\n return true;\n }\n\n if (src == ETH_TOKEN_ADDRESS) {\n callValue = amount;\n }\n\n // reserve sends tokens/eth to network. network sends it to destination\n require(reserve.trade.value(callValue)(src, amount, dest, this, conversionRate, validate));\n\n if (destAddress != address(this)) {\n //for token to token dest address is network. and Ether / token already here...\n if (dest == ETH_TOKEN_ADDRESS) {\n destAddress.transfer(expectedDestAmount);\n } else {\n require(dest.transfer(destAddress, expectedDestAmount));\n }\n }\n\n return true;\n }\n\n /// when user sets max dest amount we could have too many source tokens == change. so we send it back to user.\n function handleChange (ERC20 src, uint srcAmount, uint requiredSrcAmount, address trader) internal returns (bool) {\n\n if (requiredSrcAmount \u003c srcAmount) {\n //if there is \"change\" send back to trader\n if (src == ETH_TOKEN_ADDRESS) {\n trader.transfer(srcAmount - requiredSrcAmount);\n } else {\n require(src.transfer(trader, (srcAmount - requiredSrcAmount)));\n }\n }\n\n return true;\n }\n\n /// @notice use token address ETH_TOKEN_ADDRESS for ether\n /// @dev checks that user sent ether/tokens to contract before trade\n /// @param src Src token\n /// @param srcAmount amount of src tokens\n /// @return true if tradeInput is valid\n function validateTradeInput(ERC20 src, uint srcAmount, ERC20 dest, address destAddress)\n internal\n view\n returns(bool)\n {\n require(srcAmount \u003c= MAX_QTY);\n require(srcAmount != 0);\n require(destAddress != address(0));\n require(src != dest);\n\n if (src == ETH_TOKEN_ADDRESS) {\n require(msg.value == srcAmount);\n } else {\n require(msg.value == 0);\n //funds should have been moved to this contract already.\n require(src.balanceOf(this) \u003e= srcAmount);\n }\n\n return true;\n }\n}\n"},"KyberNetworkInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n\n/// @title Kyber Network interface\ninterface KyberNetworkInterface {\n function maxGasPrice() public view returns(uint);\n function getUserCapInWei(address user) public view returns(uint);\n function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint);\n function enabled() public view returns(bool);\n function info(bytes32 id) public view returns(uint);\n\n function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) public view\n returns (uint expectedRate, uint slippageRate);\n\n function tradeWithHint(address trader, ERC20 src, uint srcAmount, ERC20 dest, address destAddress,\n uint maxDestAmount, uint minConversionRate, address walletId, bytes hint) public payable returns(uint);\n}\n"},"KyberReserveInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n/// @title Kyber Reserve contract\ninterface KyberReserveInterface {\n\n function trade(\n ERC20 srcToken,\n uint srcAmount,\n ERC20 destToken,\n address destAddress,\n uint conversionRate,\n bool validate\n )\n public\n payable\n returns(bool);\n\n function getConversionRate(ERC20 src, ERC20 dest, uint srcQty, uint blockNumber) public view returns(uint);\n}\n"},"PermissionGroups.sol":{"content":"pragma solidity 0.4.18;\n\n\ncontract PermissionGroups {\n\n address public admin;\n address public pendingAdmin;\n mapping(address=\u003ebool) internal operators;\n mapping(address=\u003ebool) internal alerters;\n address[] internal operatorsGroup;\n address[] internal alertersGroup;\n uint constant internal MAX_GROUP_SIZE = 50;\n\n function PermissionGroups() public {\n admin = msg.sender;\n }\n\n modifier onlyAdmin() {\n require(msg.sender == admin);\n _;\n }\n\n modifier onlyOperator() {\n require(operators[msg.sender]);\n _;\n }\n\n modifier onlyAlerter() {\n require(alerters[msg.sender]);\n _;\n }\n\n function getOperators () external view returns(address[]) {\n return operatorsGroup;\n }\n\n function getAlerters () external view returns(address[]) {\n return alertersGroup;\n }\n\n event TransferAdminPending(address pendingAdmin);\n\n /**\n * @dev Allows the current admin to set the pendingAdmin address.\n * @param newAdmin The address to transfer ownership to.\n */\n function transferAdmin(address newAdmin) public onlyAdmin {\n require(newAdmin != address(0));\n TransferAdminPending(pendingAdmin);\n pendingAdmin = newAdmin;\n }\n\n /**\n * @dev Allows the current admin to set the admin in one tx. Useful initial deployment.\n * @param newAdmin The address to transfer ownership to.\n */\n function transferAdminQuickly(address newAdmin) public onlyAdmin {\n require(newAdmin != address(0));\n TransferAdminPending(newAdmin);\n AdminClaimed(newAdmin, admin);\n admin = newAdmin;\n }\n\n event AdminClaimed( address newAdmin, address previousAdmin);\n\n /**\n * @dev Allows the pendingAdmin address to finalize the change admin process.\n */\n function claimAdmin() public {\n require(pendingAdmin == msg.sender);\n AdminClaimed(pendingAdmin, admin);\n admin = pendingAdmin;\n pendingAdmin = address(0);\n }\n\n event AlerterAdded (address newAlerter, bool isAdd);\n\n function addAlerter(address newAlerter) public onlyAdmin {\n require(!alerters[newAlerter]); // prevent duplicates.\n require(alertersGroup.length \u003c MAX_GROUP_SIZE);\n\n AlerterAdded(newAlerter, true);\n alerters[newAlerter] = true;\n alertersGroup.push(newAlerter);\n }\n\n function removeAlerter (address alerter) public onlyAdmin {\n require(alerters[alerter]);\n alerters[alerter] = false;\n\n for (uint i = 0; i \u003c alertersGroup.length; ++i) {\n if (alertersGroup[i] == alerter) {\n alertersGroup[i] = alertersGroup[alertersGroup.length - 1];\n alertersGroup.length--;\n AlerterAdded(alerter, false);\n break;\n }\n }\n }\n\n event OperatorAdded(address newOperator, bool isAdd);\n\n function addOperator(address newOperator) public onlyAdmin {\n require(!operators[newOperator]); // prevent duplicates.\n require(operatorsGroup.length \u003c MAX_GROUP_SIZE);\n\n OperatorAdded(newOperator, true);\n operators[newOperator] = true;\n operatorsGroup.push(newOperator);\n }\n\n function removeOperator (address operator) public onlyAdmin {\n require(operators[operator]);\n operators[operator] = false;\n\n for (uint i = 0; i \u003c operatorsGroup.length; ++i) {\n if (operatorsGroup[i] == operator) {\n operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1];\n operatorsGroup.length -= 1;\n OperatorAdded(operator, false);\n break;\n }\n }\n }\n}\n"},"Utils.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n\n/// @title Kyber constants contract\ncontract Utils {\n\n ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);\n uint constant internal PRECISION = (10**18);\n uint constant internal MAX_QTY = (10**28); // 10B tokens\n uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH\n uint constant internal MAX_DECIMALS = 18;\n uint constant internal ETH_DECIMALS = 18;\n mapping(address=\u003euint) internal decimals;\n\n function setDecimals(ERC20 token) internal {\n if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS;\n else decimals[token] = token.decimals();\n }\n\n function getDecimals(ERC20 token) internal view returns(uint) {\n if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access\n uint tokenDecimals = decimals[token];\n // technically, there might be token with decimals 0\n // moreover, very possible that old tokens have decimals 0\n // these tokens will just have higher gas fees.\n if(tokenDecimals == 0) return token.decimals();\n\n return tokenDecimals;\n }\n\n function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {\n require(srcQty \u003c= MAX_QTY);\n require(rate \u003c= MAX_RATE);\n\n if (dstDecimals \u003e= srcDecimals) {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION;\n } else {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals)));\n }\n }\n\n function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {\n require(dstQty \u003c= MAX_QTY);\n require(rate \u003c= MAX_RATE);\n \n //source quantity is rounded up. to avoid dest quantity being too low.\n uint numerator;\n uint denominator;\n if (srcDecimals \u003e= dstDecimals) {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals)));\n denominator = rate;\n } else {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n numerator = (PRECISION * dstQty);\n denominator = (rate * (10**(dstDecimals - srcDecimals)));\n }\n return (numerator + denominator - 1) / denominator; //avoid rounding down errors\n }\n}\n"},"Utils2.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./Utils.sol\";\n\n\ncontract Utils2 is Utils {\n\n /// @dev get the balance of a user.\n /// @param token The token type\n /// @return The balance\n function getBalance(ERC20 token, address user) public view returns(uint) {\n if (token == ETH_TOKEN_ADDRESS)\n return user.balance;\n else\n return token.balanceOf(user);\n }\n\n function getDecimalsSafe(ERC20 token) internal returns(uint) {\n\n if (decimals[token] == 0) {\n setDecimals(token);\n }\n\n return decimals[token];\n }\n\n function calcDestAmount(ERC20 src, ERC20 dest, uint srcAmount, uint rate) internal view returns(uint) {\n return calcDstQty(srcAmount, getDecimals(src), getDecimals(dest), rate);\n }\n\n function calcSrcAmount(ERC20 src, ERC20 dest, uint destAmount, uint rate) internal view returns(uint) {\n return calcSrcQty(destAmount, getDecimals(src), getDecimals(dest), rate);\n }\n\n function calcRateFromQty(uint srcAmount, uint destAmount, uint srcDecimals, uint dstDecimals)\n internal pure returns(uint)\n {\n require(srcAmount \u003c= MAX_QTY);\n require(destAmount \u003c= MAX_QTY);\n\n if (dstDecimals \u003e= srcDecimals) {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n return (destAmount * PRECISION / ((10 ** (dstDecimals - srcDecimals)) * srcAmount));\n } else {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n return (destAmount * PRECISION * (10 ** (srcDecimals - dstDecimals)) / srcAmount);\n }\n }\n}\n"},"Utils3.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./Utils2.sol\";\n\n\ncontract Utils3 is Utils2 {\n\n function calcDestAmountWithDecimals(uint srcDecimals, uint destDecimals, uint srcAmount, uint rate) internal pure returns(uint) {\n return calcDstQty(srcAmount, srcDecimals, destDecimals, rate);\n }\n\n}\n"},"WhiteListInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\ncontract WhiteListInterface {\n function getUserCapInWei(address user) external view returns (uint userCapWei);\n}\n"},"Withdrawable.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\nimport \"./PermissionGroups.sol\";\n\n\n/**\n * @title Contracts that should be able to recover tokens or ethers\n * @author Ilan Doron\n * @dev This allows to recover any tokens or Ethers received in a contract.\n * This will prevent any accidental loss of tokens.\n */\ncontract Withdrawable is PermissionGroups {\n\n event TokenWithdraw(ERC20 token, uint amount, address sendTo);\n\n /**\n * @dev Withdraw all ERC20 compatible tokens\n * @param token ERC20 The address of the token contract\n */\n function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin {\n require(token.transfer(sendTo, amount));\n TokenWithdraw(token, amount, sendTo);\n }\n\n event EtherWithdraw(uint amount, address sendTo);\n\n /**\n * @dev Withdraw Ethers\n */\n function withdrawEther(uint amount, address sendTo) external onlyAdmin {\n sendTo.transfer(amount);\n EtherWithdraw(amount, sendTo);\n }\n}\n"}}
File 4 of 8: SNT
pragma solidity ^0.4.11; /// @dev `Owned` is a base level contract that assigns an `owner` that can be /// later changed contract Owned { /// @dev `owner` is the only address that can call a function with this /// modifier modifier onlyOwner() { require(msg.sender == owner); _; } address public owner; /// @notice The Constructor assigns the message sender to be `owner` function Owned() { owner = msg.sender; } address public newOwner; /// @notice `owner` can step down and assign some other address to this role /// @param _newOwner The address of the new owner. 0x0 can be used to create /// an unowned neutral vault, however that cannot be undone function changeOwner(address _newOwner) onlyOwner { newOwner = _newOwner; } function acceptOwnership() { if (msg.sender == newOwner) { owner = newOwner; } } } // Abstract contract for the full ERC 20 Token standard // https://github.com/ethereum/EIPs/issues/20 contract ERC20Token { /* This is a slight change to the ERC20 base standard. function totalSupply() constant returns (uint256 supply); is replaced with: uint256 public totalSupply; This automatically creates a getter function for the totalSupply. This is moved to the base contract since public getter functions are not currently recognised as an implementation of the matching abstract function by the compiler. */ /// total amount of tokens uint256 public totalSupply; /// @param _owner The address from which the balance will be retrieved /// @return The balance function balanceOf(address _owner) constant returns (uint256 balance); /// @notice send `_value` token to `_to` from `msg.sender` /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transfer(address _to, uint256 _value) returns (bool success); /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from` /// @param _from The address of the sender /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transferFrom(address _from, address _to, uint256 _value) returns (bool success); /// @notice `msg.sender` approves `_spender` to spend `_value` tokens /// @param _spender The address of the account able to transfer the tokens /// @param _value The amount of tokens to be approved for transfer /// @return Whether the approval was successful or not function approve(address _spender, uint256 _value) returns (bool success); /// @param _owner The address of the account owning tokens /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens allowed to spent function allowance(address _owner, address _spender) constant returns (uint256 remaining); event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); } /** * Math operations with safety checks */ library SafeMath { function mul(uint a, uint b) internal returns (uint) { uint c = a * b; assert(a == 0 || c / a == b); return c; } function div(uint a, uint b) internal returns (uint) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint a, uint b) internal returns (uint) { assert(b <= a); return a - b; } function add(uint a, uint b) internal returns (uint) { uint c = a + b; assert(c >= a); return c; } function max64(uint64 a, uint64 b) internal constant returns (uint64) { return a >= b ? a : b; } function min64(uint64 a, uint64 b) internal constant returns (uint64) { return a < b ? a : b; } function max256(uint256 a, uint256 b) internal constant returns (uint256) { return a >= b ? a : b; } function min256(uint256 a, uint256 b) internal constant returns (uint256) { return a < b ? a : b; } } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title DynamicCeiling Contract /// @author Jordi Baylina /// @dev This contract calculates the ceiling from a series of curves. /// These curves are committed first and revealed later. /// All the curves must be in increasing order and the last curve is marked /// as the last one. /// This contract allows to hide and reveal the ceiling at will of the owner. contract DynamicCeiling is Owned { using SafeMath for uint256; struct Curve { bytes32 hash; // Absolute limit for this curve uint256 limit; // The funds remaining to be collected are divided by `slopeFactor` smooth ceiling // with a long tail where big and small buyers can take part. uint256 slopeFactor; // This keeps the curve flat at this number, until funds to be collected is less than this uint256 collectMinimum; } address public contribution; Curve[] public curves; uint256 public currentIndex; uint256 public revealedCurves; bool public allRevealed; /// @dev `contribution` is the only address that can call a function with this /// modifier modifier onlyContribution { require(msg.sender == contribution); _; } function DynamicCeiling(address _owner, address _contribution) { owner = _owner; contribution = _contribution; } /// @notice This should be called by the creator of the contract to commit /// all the curves. /// @param _curveHashes Array of hashes of each curve. Each hash is calculated /// by the `calculateHash` method. More hashes than actual curves can be /// committed in order to hide also the number of curves. /// The remaining hashes can be just random numbers. function setHiddenCurves(bytes32[] _curveHashes) public onlyOwner { require(curves.length == 0); curves.length = _curveHashes.length; for (uint256 i = 0; i < _curveHashes.length; i = i.add(1)) { curves[i].hash = _curveHashes[i]; } } /// @notice Anybody can reveal the next curve if he knows it. /// @param _limit Ceiling cap. /// (must be greater or equal to the previous one). /// @param _last `true` if it's the last curve. /// @param _salt Random number used to commit the curve function revealCurve(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum, bool _last, bytes32 _salt) public { require(!allRevealed); require(curves[revealedCurves].hash == calculateHash(_limit, _slopeFactor, _collectMinimum, _last, _salt)); require(_limit != 0 && _slopeFactor != 0 && _collectMinimum != 0); if (revealedCurves > 0) { require(_limit >= curves[revealedCurves.sub(1)].limit); } curves[revealedCurves].limit = _limit; curves[revealedCurves].slopeFactor = _slopeFactor; curves[revealedCurves].collectMinimum = _collectMinimum; revealedCurves = revealedCurves.add(1); if (_last) allRevealed = true; } /// @notice Reveal multiple curves at once function revealMulti(uint256[] _limits, uint256[] _slopeFactors, uint256[] _collectMinimums, bool[] _lasts, bytes32[] _salts) public { // Do not allow none and needs to be same length for all parameters require(_limits.length != 0 && _limits.length == _slopeFactors.length && _limits.length == _collectMinimums.length && _limits.length == _lasts.length && _limits.length == _salts.length); for (uint256 i = 0; i < _limits.length; i = i.add(1)) { revealCurve(_limits[i], _slopeFactors[i], _collectMinimums[i], _lasts[i], _salts[i]); } } /// @notice Move to curve, used as a failsafe function moveTo(uint256 _index) public onlyOwner { require(_index < revealedCurves && // No more curves _index == currentIndex.add(1)); // Only move one index at a time currentIndex = _index; } /// @return Return the funds to collect for the current point on the curve /// (or 0 if no curves revealed yet) function toCollect(uint256 collected) public onlyContribution returns (uint256) { if (revealedCurves == 0) return 0; // Move to the next curve if (collected >= curves[currentIndex].limit) { // Catches `limit == 0` uint256 nextIndex = currentIndex.add(1); if (nextIndex >= revealedCurves) return 0; // No more curves currentIndex = nextIndex; if (collected >= curves[currentIndex].limit) return 0; // Catches `limit == 0` } // Everything left to collect from this limit uint256 difference = curves[currentIndex].limit.sub(collected); // Current point on the curve uint256 collect = difference.div(curves[currentIndex].slopeFactor); // Prevents paying too much fees vs to be collected; breaks long tail if (collect <= curves[currentIndex].collectMinimum) { if (difference > curves[currentIndex].collectMinimum) { return curves[currentIndex].collectMinimum; } else { return difference; } } else { return collect; } } /// @notice Calculates the hash of a curve. /// @param _limit Ceiling cap. /// @param _last `true` if it's the last curve. /// @param _salt Random number that will be needed to reveal this curve. /// @return The calculated hash of this curve to be used in the `setHiddenCurves` method function calculateHash(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum, bool _last, bytes32 _salt) public constant returns (bytes32) { return keccak256(_limit, _slopeFactor, _collectMinimum, _last, _salt); } /// @return Return the total number of curves committed /// (can be larger than the number of actual curves on the curve to hide /// the real number of curves) function nCurves() public constant returns (uint256) { return curves.length; } } /* Copyright 2016, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title MiniMeToken Contract /// @author Jordi Baylina /// @dev This token contract's goal is to make it easy for anyone to clone this /// token using the token distribution at a given block, this will allow DAO's /// and DApps to upgrade their features in a decentralized manner without /// affecting the original token /// @dev It is ERC20 compliant, but still needs to under go further testing. /// @dev The token controller contract must implement these functions contract TokenController { /// @notice Called when `_owner` sends ether to the MiniMe Token contract /// @param _owner The address that sent the ether to create tokens /// @return True if the ether is accepted, false if it throws function proxyPayment(address _owner) payable returns(bool); /// @notice Notifies the controller about a token transfer allowing the /// controller to react if desired /// @param _from The origin of the transfer /// @param _to The destination of the transfer /// @param _amount The amount of the transfer /// @return False if the controller does not authorize the transfer function onTransfer(address _from, address _to, uint _amount) returns(bool); /// @notice Notifies the controller about an approval allowing the /// controller to react if desired /// @param _owner The address that calls `approve()` /// @param _spender The spender in the `approve()` call /// @param _amount The amount in the `approve()` call /// @return False if the controller does not authorize the approval function onApprove(address _owner, address _spender, uint _amount) returns(bool); } contract Controlled { /// @notice The address of the controller is the only address that can call /// a function with this modifier modifier onlyController { if (msg.sender != controller) throw; _; } address public controller; function Controlled() { controller = msg.sender;} /// @notice Changes the controller of the contract /// @param _newController The new controller of the contract function changeController(address _newController) onlyController { controller = _newController; } } contract ApproveAndCallFallBack { function receiveApproval(address from, uint256 _amount, address _token, bytes _data); } /// @dev The actual token contract, the default controller is the msg.sender /// that deploys the contract, so usually this token will be deployed by a /// token controller contract, which Giveth will call a "Campaign" contract MiniMeToken is Controlled { string public name; //The Token's name: e.g. DigixDAO Tokens uint8 public decimals; //Number of decimals of the smallest unit string public symbol; //An identifier: e.g. REP string public version = 'MMT_0.1'; //An arbitrary versioning scheme /// @dev `Checkpoint` is the structure that attaches a block number to a /// given value, the block number attached is the one that last changed the /// value struct Checkpoint { // `fromBlock` is the block number that the value was generated from uint128 fromBlock; // `value` is the amount of tokens at a specific block number uint128 value; } // `parentToken` is the Token address that was cloned to produce this token; // it will be 0x0 for a token that was not cloned MiniMeToken public parentToken; // `parentSnapShotBlock` is the block number from the Parent Token that was // used to determine the initial distribution of the Clone Token uint public parentSnapShotBlock; // `creationBlock` is the block number that the Clone Token was created uint public creationBlock; // `balances` is the map that tracks the balance of each address, in this // contract when the balance changes the block number that the change // occurred is also included in the map mapping (address => Checkpoint[]) balances; // `allowed` tracks any extra transfer rights as in all ERC20 tokens mapping (address => mapping (address => uint256)) allowed; // Tracks the history of the `totalSupply` of the token Checkpoint[] totalSupplyHistory; // Flag that determines if the token is transferable or not. bool public transfersEnabled; // The factory used to create new clone tokens MiniMeTokenFactory public tokenFactory; //////////////// // Constructor //////////////// /// @notice Constructor to create a MiniMeToken /// @param _tokenFactory The address of the MiniMeTokenFactory contract that /// will create the Clone token contracts, the token factory needs to be /// deployed first /// @param _parentToken Address of the parent token, set to 0x0 if it is a /// new token /// @param _parentSnapShotBlock Block of the parent token that will /// determine the initial distribution of the clone token, set to 0 if it /// is a new token /// @param _tokenName Name of the new token /// @param _decimalUnits Number of decimals of the new token /// @param _tokenSymbol Token Symbol for the new token /// @param _transfersEnabled If true, tokens will be able to be transferred function MiniMeToken( address _tokenFactory, address _parentToken, uint _parentSnapShotBlock, string _tokenName, uint8 _decimalUnits, string _tokenSymbol, bool _transfersEnabled ) { tokenFactory = MiniMeTokenFactory(_tokenFactory); name = _tokenName; // Set the name decimals = _decimalUnits; // Set the decimals symbol = _tokenSymbol; // Set the symbol parentToken = MiniMeToken(_parentToken); parentSnapShotBlock = _parentSnapShotBlock; transfersEnabled = _transfersEnabled; creationBlock = getBlockNumber(); } /////////////////// // ERC20 Methods /////////////////// /// @notice Send `_amount` tokens to `_to` from `msg.sender` /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return Whether the transfer was successful or not function transfer(address _to, uint256 _amount) returns (bool success) { if (!transfersEnabled) throw; return doTransfer(msg.sender, _to, _amount); } /// @notice Send `_amount` tokens to `_to` from `_from` on the condition it /// is approved by `_from` /// @param _from The address holding the tokens being transferred /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful function transferFrom(address _from, address _to, uint256 _amount ) returns (bool success) { // The controller of this contract can move tokens around at will, // this is important to recognize! Confirm that you trust the // controller of this contract, which in most situations should be // another open source smart contract or 0x0 if (msg.sender != controller) { if (!transfersEnabled) throw; // The standard ERC 20 transferFrom functionality if (allowed[_from][msg.sender] < _amount) return false; allowed[_from][msg.sender] -= _amount; } return doTransfer(_from, _to, _amount); } /// @dev This is the actual transfer function in the token contract, it can /// only be called by other functions in this contract. /// @param _from The address holding the tokens being transferred /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful function doTransfer(address _from, address _to, uint _amount ) internal returns(bool) { if (_amount == 0) { return true; } if (parentSnapShotBlock >= getBlockNumber()) throw; // Do not allow transfer to 0x0 or the token contract itself if ((_to == 0) || (_to == address(this))) throw; // If the amount being transfered is more than the balance of the // account the transfer returns false var previousBalanceFrom = balanceOfAt(_from, getBlockNumber()); if (previousBalanceFrom < _amount) { return false; } // Alerts the token controller of the transfer if (isContract(controller)) { if (!TokenController(controller).onTransfer(_from, _to, _amount)) throw; } // First update the balance array with the new value for the address // sending the tokens updateValueAtNow(balances[_from], previousBalanceFrom - _amount); // Then update the balance array with the new value for the address // receiving the tokens var previousBalanceTo = balanceOfAt(_to, getBlockNumber()); if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow updateValueAtNow(balances[_to], previousBalanceTo + _amount); // An event to make the transfer easy to find on the blockchain Transfer(_from, _to, _amount); return true; } /// @param _owner The address that's balance is being requested /// @return The balance of `_owner` at the current block function balanceOf(address _owner) constant returns (uint256 balance) { return balanceOfAt(_owner, getBlockNumber()); } /// @notice `msg.sender` approves `_spender` to spend `_amount` tokens on /// its behalf. This is a modified version of the ERC20 approve function /// to be a little bit safer /// @param _spender The address of the account able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the approval was successful function approve(address _spender, uint256 _amount) returns (bool success) { if (!transfersEnabled) throw; // To change the approve amount you first have to reduce the addresses` // allowance to zero by calling `approve(_spender,0)` if it is not // already 0 to mitigate the race condition described here: // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 if ((_amount!=0) && (allowed[msg.sender][_spender] !=0)) throw; // Alerts the token controller of the approve function call if (isContract(controller)) { if (!TokenController(controller).onApprove(msg.sender, _spender, _amount)) throw; } allowed[msg.sender][_spender] = _amount; Approval(msg.sender, _spender, _amount); return true; } /// @dev This function makes it easy to read the `allowed[]` map /// @param _owner The address of the account that owns the token /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens of _owner that _spender is allowed /// to spend function allowance(address _owner, address _spender ) constant returns (uint256 remaining) { return allowed[_owner][_spender]; } /// @notice `msg.sender` approves `_spender` to send `_amount` tokens on /// its behalf, and then a function is triggered in the contract that is /// being approved, `_spender`. This allows users to use their tokens to /// interact with contracts in one function call instead of two /// @param _spender The address of the contract able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the function call was successful function approveAndCall(address _spender, uint256 _amount, bytes _extraData ) returns (bool success) { if (!approve(_spender, _amount)) throw; ApproveAndCallFallBack(_spender).receiveApproval( msg.sender, _amount, this, _extraData ); return true; } /// @dev This function makes it easy to get the total number of tokens /// @return The total number of tokens function totalSupply() constant returns (uint) { return totalSupplyAt(getBlockNumber()); } //////////////// // Query balance and totalSupply in History //////////////// /// @dev Queries the balance of `_owner` at a specific `_blockNumber` /// @param _owner The address from which the balance will be retrieved /// @param _blockNumber The block number when the balance is queried /// @return The balance at `_blockNumber` function balanceOfAt(address _owner, uint _blockNumber) constant returns (uint) { // These next few lines are used when the balance of the token is // requested before a check point was ever created for this token, it // requires that the `parentToken.balanceOfAt` be queried at the // genesis block for that token as this contains initial balance of // this token if ((balances[_owner].length == 0) || (balances[_owner][0].fromBlock > _blockNumber)) { if (address(parentToken) != 0) { return parentToken.balanceOfAt(_owner, min(_blockNumber, parentSnapShotBlock)); } else { // Has no parent return 0; } // This will return the expected balance during normal situations } else { return getValueAt(balances[_owner], _blockNumber); } } /// @notice Total amount of tokens at a specific `_blockNumber`. /// @param _blockNumber The block number when the totalSupply is queried /// @return The total amount of tokens at `_blockNumber` function totalSupplyAt(uint _blockNumber) constant returns(uint) { // These next few lines are used when the totalSupply of the token is // requested before a check point was ever created for this token, it // requires that the `parentToken.totalSupplyAt` be queried at the // genesis block for this token as that contains totalSupply of this // token at this block number. if ((totalSupplyHistory.length == 0) || (totalSupplyHistory[0].fromBlock > _blockNumber)) { if (address(parentToken) != 0) { return parentToken.totalSupplyAt(min(_blockNumber, parentSnapShotBlock)); } else { return 0; } // This will return the expected totalSupply during normal situations } else { return getValueAt(totalSupplyHistory, _blockNumber); } } //////////////// // Clone Token Method //////////////// /// @notice Creates a new clone token with the initial distribution being /// this token at `_snapshotBlock` /// @param _cloneTokenName Name of the clone token /// @param _cloneDecimalUnits Number of decimals of the smallest unit /// @param _cloneTokenSymbol Symbol of the clone token /// @param _snapshotBlock Block when the distribution of the parent token is /// copied to set the initial distribution of the new clone token; /// if the block is zero than the actual block, the current block is used /// @param _transfersEnabled True if transfers are allowed in the clone /// @return The address of the new MiniMeToken Contract function createCloneToken( string _cloneTokenName, uint8 _cloneDecimalUnits, string _cloneTokenSymbol, uint _snapshotBlock, bool _transfersEnabled ) returns(address) { if (_snapshotBlock == 0) _snapshotBlock = getBlockNumber(); MiniMeToken cloneToken = tokenFactory.createCloneToken( this, _snapshotBlock, _cloneTokenName, _cloneDecimalUnits, _cloneTokenSymbol, _transfersEnabled ); cloneToken.changeController(msg.sender); // An event to make the token easy to find on the blockchain NewCloneToken(address(cloneToken), _snapshotBlock); return address(cloneToken); } //////////////// // Generate and destroy tokens //////////////// /// @notice Generates `_amount` tokens that are assigned to `_owner` /// @param _owner The address that will be assigned the new tokens /// @param _amount The quantity of tokens generated /// @return True if the tokens are generated correctly function generateTokens(address _owner, uint _amount ) onlyController returns (bool) { uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount); var previousBalanceTo = balanceOf(_owner); if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow updateValueAtNow(balances[_owner], previousBalanceTo + _amount); Transfer(0, _owner, _amount); return true; } /// @notice Burns `_amount` tokens from `_owner` /// @param _owner The address that will lose the tokens /// @param _amount The quantity of tokens to burn /// @return True if the tokens are burned correctly function destroyTokens(address _owner, uint _amount ) onlyController returns (bool) { uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); if (curTotalSupply < _amount) throw; updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount); var previousBalanceFrom = balanceOf(_owner); if (previousBalanceFrom < _amount) throw; updateValueAtNow(balances[_owner], previousBalanceFrom - _amount); Transfer(_owner, 0, _amount); return true; } //////////////// // Enable tokens transfers //////////////// /// @notice Enables token holders to transfer their tokens freely if true /// @param _transfersEnabled True if transfers are allowed in the clone function enableTransfers(bool _transfersEnabled) onlyController { transfersEnabled = _transfersEnabled; } //////////////// // Internal helper functions to query and set a value in a snapshot array //////////////// /// @dev `getValueAt` retrieves the number of tokens at a given block number /// @param checkpoints The history of values being queried /// @param _block The block number to retrieve the value at /// @return The number of tokens being queried function getValueAt(Checkpoint[] storage checkpoints, uint _block ) constant internal returns (uint) { if (checkpoints.length == 0) return 0; // Shortcut for the actual value if (_block >= checkpoints[checkpoints.length-1].fromBlock) return checkpoints[checkpoints.length-1].value; if (_block < checkpoints[0].fromBlock) return 0; // Binary search of the value in the array uint min = 0; uint max = checkpoints.length-1; while (max > min) { uint mid = (max + min + 1)/ 2; if (checkpoints[mid].fromBlock<=_block) { min = mid; } else { max = mid-1; } } return checkpoints[min].value; } /// @dev `updateValueAtNow` used to update the `balances` map and the /// `totalSupplyHistory` /// @param checkpoints The history of data being updated /// @param _value The new number of tokens function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value ) internal { if ((checkpoints.length == 0) || (checkpoints[checkpoints.length -1].fromBlock < getBlockNumber())) { Checkpoint newCheckPoint = checkpoints[ checkpoints.length++ ]; newCheckPoint.fromBlock = uint128(getBlockNumber()); newCheckPoint.value = uint128(_value); } else { Checkpoint oldCheckPoint = checkpoints[checkpoints.length-1]; oldCheckPoint.value = uint128(_value); } } /// @dev Internal function to determine if an address is a contract /// @param _addr The address being queried /// @return True if `_addr` is a contract function isContract(address _addr) constant internal returns(bool) { uint size; if (_addr == 0) return false; assembly { size := extcodesize(_addr) } return size>0; } /// @dev Helper function to return a min betwen the two uints function min(uint a, uint b) internal returns (uint) { return a < b ? a : b; } /// @notice The fallback function: If the contract's controller has not been /// set to 0, then the `proxyPayment` method is called which relays the /// ether and creates tokens as described in the token controller contract function () payable { if (isContract(controller)) { if (! TokenController(controller).proxyPayment.value(msg.value)(msg.sender)) throw; } else { throw; } } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) onlyController { if (_token == 0x0) { controller.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint balance = token.balanceOf(this); token.transfer(controller, balance); ClaimedTokens(_token, controller, balance); } //////////////// // Events //////////////// event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount); event Transfer(address indexed _from, address indexed _to, uint256 _amount); event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock); event Approval( address indexed _owner, address indexed _spender, uint256 _amount ); } //////////////// // MiniMeTokenFactory //////////////// /// @dev This contract is used to generate clone contracts from a contract. /// In solidity this is the way to create a contract from a contract of the /// same class contract MiniMeTokenFactory { /// @notice Update the DApp by creating a new token with new functionalities /// the msg.sender becomes the controller of this clone token /// @param _parentToken Address of the token being cloned /// @param _snapshotBlock Block of the parent token that will /// determine the initial distribution of the clone token /// @param _tokenName Name of the new token /// @param _decimalUnits Number of decimals of the new token /// @param _tokenSymbol Token Symbol for the new token /// @param _transfersEnabled If true, tokens will be able to be transferred /// @return The address of the new token contract function createCloneToken( address _parentToken, uint _snapshotBlock, string _tokenName, uint8 _decimalUnits, string _tokenSymbol, bool _transfersEnabled ) returns (MiniMeToken) { MiniMeToken newToken = new MiniMeToken( this, _parentToken, _snapshotBlock, _tokenName, _decimalUnits, _tokenSymbol, _transfersEnabled ); newToken.changeController(msg.sender); return newToken; } } /* Copyright 2017, Jarrad Hope (Status Research & Development GmbH) */ contract SNT is MiniMeToken { // @dev SNT constructor just parametrizes the MiniMeIrrevocableVestedToken constructor function SNT(address _tokenFactory) MiniMeToken( _tokenFactory, 0x0, // no parent token 0, // no snapshot block number from parent "Status Network Token", // Token name 18, // Decimals "SNT", // Symbol true // Enable transfers ) {} } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title StatusContribution Contract /// @author Jordi Baylina /// @dev This contract will be the SNT controller during the contribution period. /// This contract will determine the rules during this period. /// Final users will generally not interact directly with this contract. ETH will /// be sent to the SNT token contract. The ETH is sent to this contract and from here, /// ETH is sent to the contribution walled and SNTs are mined according to the defined /// rules. contract StatusContribution is Owned, TokenController { using SafeMath for uint256; uint256 constant public failSafeLimit = 300000 ether; uint256 constant public maxGuaranteedLimit = 30000 ether; uint256 constant public exchangeRate = 10000; uint256 constant public maxGasPrice = 50000000000; uint256 constant public maxCallFrequency = 100; MiniMeToken public SGT; MiniMeToken public SNT; uint256 public startBlock; uint256 public endBlock; address public destEthDevs; address public destTokensDevs; address public destTokensReserve; uint256 public maxSGTSupply; address public destTokensSgt; DynamicCeiling public dynamicCeiling; address public sntController; mapping (address => uint256) public guaranteedBuyersLimit; mapping (address => uint256) public guaranteedBuyersBought; uint256 public totalGuaranteedCollected; uint256 public totalNormalCollected; uint256 public finalizedBlock; uint256 public finalizedTime; mapping (address => uint256) public lastCallBlock; bool public paused; modifier initialized() { require(address(SNT) != 0x0); _; } modifier contributionOpen() { require(getBlockNumber() >= startBlock && getBlockNumber() <= endBlock && finalizedBlock == 0 && address(SNT) != 0x0); _; } modifier notPaused() { require(!paused); _; } function StatusContribution() { paused = false; } /// @notice This method should be called by the owner before the contribution /// period starts This initializes most of the parameters /// @param _snt Address of the SNT token contract /// @param _sntController Token controller for the SNT that will be transferred after /// the contribution finalizes. /// @param _startBlock Block when the contribution period starts /// @param _endBlock The last block that the contribution period is active /// @param _dynamicCeiling Address of the contract that controls the ceiling /// @param _destEthDevs Destination address where the contribution ether is sent /// @param _destTokensReserve Address where the tokens for the reserve are sent /// @param _destTokensSgt Address of the exchanger SGT-SNT where the SNT are sent /// to be distributed to the SGT holders. /// @param _destTokensDevs Address where the tokens for the dev are sent /// @param _sgt Address of the SGT token contract /// @param _maxSGTSupply Quantity of SGT tokens that would represent 10% of status. function initialize( address _snt, address _sntController, uint256 _startBlock, uint256 _endBlock, address _dynamicCeiling, address _destEthDevs, address _destTokensReserve, address _destTokensSgt, address _destTokensDevs, address _sgt, uint256 _maxSGTSupply ) public onlyOwner { // Initialize only once require(address(SNT) == 0x0); SNT = MiniMeToken(_snt); require(SNT.totalSupply() == 0); require(SNT.controller() == address(this)); require(SNT.decimals() == 18); // Same amount of decimals as ETH require(_sntController != 0x0); sntController = _sntController; require(_startBlock >= getBlockNumber()); require(_startBlock < _endBlock); startBlock = _startBlock; endBlock = _endBlock; require(_dynamicCeiling != 0x0); dynamicCeiling = DynamicCeiling(_dynamicCeiling); require(_destEthDevs != 0x0); destEthDevs = _destEthDevs; require(_destTokensReserve != 0x0); destTokensReserve = _destTokensReserve; require(_destTokensSgt != 0x0); destTokensSgt = _destTokensSgt; require(_destTokensDevs != 0x0); destTokensDevs = _destTokensDevs; require(_sgt != 0x0); SGT = MiniMeToken(_sgt); require(_maxSGTSupply >= MiniMeToken(SGT).totalSupply()); maxSGTSupply = _maxSGTSupply; } /// @notice Sets the limit for a guaranteed address. All the guaranteed addresses /// will be able to get SNTs during the contribution period with his own /// specific limit. /// This method should be called by the owner after the initialization /// and before the contribution starts. /// @param _th Guaranteed address /// @param _limit Limit for the guaranteed address. function setGuaranteedAddress(address _th, uint256 _limit) public initialized onlyOwner { require(getBlockNumber() < startBlock); require(_limit > 0 && _limit <= maxGuaranteedLimit); guaranteedBuyersLimit[_th] = _limit; GuaranteedAddress(_th, _limit); } /// @notice If anybody sends Ether directly to this contract, consider he is /// getting SNTs. function () public payable notPaused { proxyPayment(msg.sender); } ////////// // MiniMe Controller functions ////////// /// @notice This method will generally be called by the SNT token contract to /// acquire SNTs. Or directly from third parties that want to acquire SNTs in /// behalf of a token holder. /// @param _th SNT holder where the SNTs will be minted. function proxyPayment(address _th) public payable notPaused initialized contributionOpen returns (bool) { require(_th != 0x0); if (guaranteedBuyersLimit[_th] > 0) { buyGuaranteed(_th); } else { buyNormal(_th); } return true; } function onTransfer(address, address, uint256) public returns (bool) { return false; } function onApprove(address, address, uint256) public returns (bool) { return false; } function buyNormal(address _th) internal { require(tx.gasprice <= maxGasPrice); // Antispam mechanism address caller; if (msg.sender == address(SNT)) { caller = _th; } else { caller = msg.sender; } // Do not allow contracts to game the system require(!isContract(caller)); require(getBlockNumber().sub(lastCallBlock[caller]) >= maxCallFrequency); lastCallBlock[caller] = getBlockNumber(); uint256 toCollect = dynamicCeiling.toCollect(totalNormalCollected); uint256 toFund; if (msg.value <= toCollect) { toFund = msg.value; } else { toFund = toCollect; } totalNormalCollected = totalNormalCollected.add(toFund); doBuy(_th, toFund, false); } function buyGuaranteed(address _th) internal { uint256 toCollect = guaranteedBuyersLimit[_th]; uint256 toFund; if (guaranteedBuyersBought[_th].add(msg.value) > toCollect) { toFund = toCollect.sub(guaranteedBuyersBought[_th]); } else { toFund = msg.value; } guaranteedBuyersBought[_th] = guaranteedBuyersBought[_th].add(toFund); totalGuaranteedCollected = totalGuaranteedCollected.add(toFund); doBuy(_th, toFund, true); } function doBuy(address _th, uint256 _toFund, bool _guaranteed) internal { assert(msg.value >= _toFund); // Not needed, but double check. assert(totalCollected() <= failSafeLimit); if (_toFund > 0) { uint256 tokensGenerated = _toFund.mul(exchangeRate); assert(SNT.generateTokens(_th, tokensGenerated)); destEthDevs.transfer(_toFund); NewSale(_th, _toFund, tokensGenerated, _guaranteed); } uint256 toReturn = msg.value.sub(_toFund); if (toReturn > 0) { // If the call comes from the Token controller, // then we return it to the token Holder. // Otherwise we return to the sender. if (msg.sender == address(SNT)) { _th.transfer(toReturn); } else { msg.sender.transfer(toReturn); } } } // NOTE on Percentage format // Right now, Solidity does not support decimal numbers. (This will change very soon) // So in this contract we use a representation of a percentage that consist in // expressing the percentage in "x per 10**18" // This format has a precision of 16 digits for a percent. // Examples: // 3% = 3*(10**16) // 100% = 100*(10**16) = 10**18 // // To get a percentage of a value we do it by first multiplying it by the percentage in (x per 10^18) // and then divide it by 10**18 // // Y * X(in x per 10**18) // X% of Y = ------------------------- // 100(in x per 10**18) // /// @notice This method will can be called by the owner before the contribution period /// end or by anybody after the `endBlock`. This method finalizes the contribution period /// by creating the remaining tokens and transferring the controller to the configured /// controller. function finalize() public initialized { require(getBlockNumber() >= startBlock); require(msg.sender == owner || getBlockNumber() > endBlock); require(finalizedBlock == 0); // Do not allow termination until all curves revealed. require(dynamicCeiling.allRevealed()); // Allow premature finalization if final limit is reached if (getBlockNumber() <= endBlock) { var (,lastLimit,,) = dynamicCeiling.curves(dynamicCeiling.revealedCurves().sub(1)); require(totalNormalCollected >= lastLimit); } finalizedBlock = getBlockNumber(); finalizedTime = now; uint256 percentageToSgt; if (SGT.totalSupply() >= maxSGTSupply) { percentageToSgt = percent(10); // 10% } else { // // SGT.totalSupply() // percentageToSgt = 10% * ------------------- // maxSGTSupply // percentageToSgt = percent(10).mul(SGT.totalSupply()).div(maxSGTSupply); } uint256 percentageToDevs = percent(20); // 20% // // % To Contributors = 41% + (10% - % to SGT holders) // uint256 percentageToContributors = percent(41).add(percent(10).sub(percentageToSgt)); uint256 percentageToReserve = percent(29); // SNT.totalSupply() -> Tokens minted during the contribution // totalTokens -> Total tokens that should be after the allocation // of devTokens, sgtTokens and reserve // percentageToContributors -> Which percentage should go to the // contribution participants // (x per 10**18 format) // percent(100) -> 100% in (x per 10**18 format) // // percentageToContributors // SNT.totalSupply() = -------------------------- * totalTokens => // percent(100) // // // percent(100) // => totalTokens = ---------------------------- * SNT.totalSupply() // percentageToContributors // uint256 totalTokens = SNT.totalSupply().mul(percent(100)).div(percentageToContributors); // Generate tokens for SGT Holders. // // percentageToReserve // reserveTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensReserve, totalTokens.mul(percentageToReserve).div(percent(100)))); // // percentageToSgt // sgtTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensSgt, totalTokens.mul(percentageToSgt).div(percent(100)))); // // percentageToDevs // devTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensDevs, totalTokens.mul(percentageToDevs).div(percent(100)))); SNT.changeController(sntController); Finalized(); } function percent(uint256 p) internal returns (uint256) { return p.mul(10**16); } /// @dev Internal function to determine if an address is a contract /// @param _addr The address being queried /// @return True if `_addr` is a contract function isContract(address _addr) constant internal returns (bool) { if (_addr == 0) return false; uint256 size; assembly { size := extcodesize(_addr) } return (size > 0); } ////////// // Constant functions ////////// /// @return Total tokens issued in weis. function tokensIssued() public constant returns (uint256) { return SNT.totalSupply(); } /// @return Total Ether collected. function totalCollected() public constant returns (uint256) { return totalNormalCollected.add(totalGuaranteedCollected); } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { if (SNT.controller() == address(this)) { SNT.claimTokens(_token); } if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } /// @notice Pauses the contribution if there is any issue function pauseContribution() onlyOwner { paused = true; } /// @notice Resumes the contribution function resumeContribution() onlyOwner { paused = false; } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event NewSale(address indexed _th, uint256 _amount, uint256 _tokens, bool _guaranteed); event GuaranteedAddress(address indexed _th, uint256 _limit); event Finalized(); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title ContributionWallet Contract /// @author Jordi Baylina /// @dev This contract will be hold the Ether during the contribution period. /// The idea of this contract is to avoid recycling Ether during the contribution /// period. So all the ETH collected will be locked here until the contribution /// period ends // @dev Contract to hold sale raised funds during the sale period. // Prevents attack in which the Aragon Multisig sends raised ether // to the sale contract to mint tokens to itself, and getting the // funds back immediately. contract ContributionWallet { // Public variables address public multisig; uint256 public endBlock; StatusContribution public contribution; // @dev Constructor initializes public variables // @param _multisig The address of the multisig that will receive the funds // @param _endBlock Block after which the multisig can request the funds // @param _contribution Address of the StatusContribution contract function ContributionWallet(address _multisig, uint256 _endBlock, address _contribution) { require(_multisig != 0x0); require(_contribution != 0x0); require(_endBlock != 0 && _endBlock <= 4000000); multisig = _multisig; endBlock = _endBlock; contribution = StatusContribution(_contribution); } // @dev Receive all sent funds without any further logic function () public payable {} // @dev Withdraw function sends all the funds to the wallet if conditions are correct function withdraw() public { require(msg.sender == multisig); // Only the multisig can request it require(block.number > endBlock || // Allow after end block contribution.finalizedBlock() != 0); // Allow when sale is finalized multisig.transfer(this.balance); } } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title DevTokensHolder Contract /// @author Jordi Baylina /// @dev This contract will hold the tokens of the developers. /// Tokens will not be able to be collected until 6 months after the contribution /// period ends. And it will be increasing linearly until 2 years. // collectable tokens // | _/-------- vestedTokens rect // | _/ // | _/ // | _/ // | _/ // | _/ // | _/ // | _/ // | | // | . | // | . | // | . | // +===+======+--------------+----------> time // Contrib 6 Months 24 Months // End contract DevTokensHolder is Owned { using SafeMath for uint256; uint256 collectedTokens; StatusContribution contribution; MiniMeToken snt; function DevTokensHolder(address _owner, address _contribution, address _snt) { owner = _owner; contribution = StatusContribution(_contribution); snt = MiniMeToken(_snt); } /// @notice The Dev (Owner) will call this method to extract the tokens function collectTokens() public onlyOwner { uint256 balance = snt.balanceOf(address(this)); uint256 total = collectedTokens.add(balance); uint256 finalizedTime = contribution.finalizedTime(); require(finalizedTime > 0 && getTime() > finalizedTime.add(months(6))); uint256 canExtract = total.mul(getTime().sub(finalizedTime)).div(months(24)); canExtract = canExtract.sub(collectedTokens); if (canExtract > balance) { canExtract = balance; } collectedTokens = collectedTokens.add(canExtract); assert(snt.transfer(owner, canExtract)); TokensWithdrawn(owner, canExtract); } function months(uint256 m) internal returns (uint256) { return m.mul(30 days); } function getTime() internal returns (uint256) { return now; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { require(_token != address(snt)); if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event TokensWithdrawn(address indexed _holder, uint256 _amount); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title SGTExchanger Contract /// @author Jordi Baylina /// @dev This contract will be used to distribute SNT between SGT holders. /// SGT token is not transferable, and we just keep an accounting between all tokens /// deposited and the tokens collected. /// The controllerShip of SGT should be transferred to this contract before the /// contribution period starts. contract SGTExchanger is TokenController, Owned { using SafeMath for uint256; mapping (address => uint256) public collected; uint256 public totalCollected; MiniMeToken public sgt; MiniMeToken public snt; StatusContribution public statusContribution; function SGTExchanger(address _sgt, address _snt, address _statusContribution) { sgt = MiniMeToken(_sgt); snt = MiniMeToken(_snt); statusContribution = StatusContribution(_statusContribution); } /// @notice This method should be called by the SGT holders to collect their /// corresponding SNTs function collect() public { uint256 finalizedBlock = statusContribution.finalizedBlock(); require(finalizedBlock != 0); require(getBlockNumber() > finalizedBlock); uint256 total = totalCollected.add(snt.balanceOf(address(this))); uint256 balance = sgt.balanceOfAt(msg.sender, finalizedBlock); // First calculate how much correspond to him uint256 amount = total.mul(balance).div(sgt.totalSupplyAt(finalizedBlock)); // And then subtract the amount already collected amount = amount.sub(collected[msg.sender]); require(amount > 0); // Notify the user that there are no tokens to exchange totalCollected = totalCollected.add(amount); collected[msg.sender] = collected[msg.sender].add(amount); assert(snt.transfer(msg.sender, amount)); TokensCollected(msg.sender, amount); } function proxyPayment(address) public payable returns (bool) { throw; } function onTransfer(address, address, uint256) public returns (bool) { return false; } function onApprove(address, address, uint256) public returns (bool) { return false; } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Method ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { require(_token != address(snt)); if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event TokensCollected(address indexed _holder, uint256 _amount); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title SNTPlaceholder Contract /// @author Jordi Baylina /// @dev The SNTPlaceholder contract will take control over the SNT after the contribution /// is finalized and before the Status Network is deployed. /// The contract allows for SNT transfers and transferFrom and implements the /// logic for transferring control of the token to the network when the offering /// asks it to do so. contract SNTPlaceHolder is TokenController, Owned { using SafeMath for uint256; MiniMeToken public snt; StatusContribution public contribution; uint256 public activationTime; address public sgtExchanger; /// @notice Constructor /// @param _owner Trusted owner for this contract. /// @param _snt SNT token contract address /// @param _contribution StatusContribution contract address /// @param _sgtExchanger SGT-SNT Exchange address. (During the first week /// only this exchanger will be able to move tokens) function SNTPlaceHolder(address _owner, address _snt, address _contribution, address _sgtExchanger) { owner = _owner; snt = MiniMeToken(_snt); contribution = StatusContribution(_contribution); sgtExchanger = _sgtExchanger; } /// @notice The owner of this contract can change the controller of the SNT token /// Please, be sure that the owner is a trusted agent or 0x0 address. /// @param _newController The address of the new controller function changeController(address _newController) public onlyOwner { snt.changeController(_newController); ControllerChanged(_newController); } ////////// // MiniMe Controller Interface functions ////////// // In between the offering and the network. Default settings for allowing token transfers. function proxyPayment(address) public payable returns (bool) { return false; } function onTransfer(address _from, address, uint256) public returns (bool) { return transferable(_from); } function onApprove(address _from, address, uint256) public returns (bool) { return transferable(_from); } function transferable(address _from) internal returns (bool) { // Allow the exchanger to work from the beginning if (activationTime == 0) { uint256 f = contribution.finalizedTime(); if (f > 0) { activationTime = f.add(1 weeks); } else { return false; } } return (getTime() > activationTime) || (_from == sgtExchanger); } ////////// // Testing specific methods ////////// /// @notice This function is overrided by the test Mocks. function getTime() internal returns (uint256) { return now; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { if (snt.controller() == address(this)) { snt.claimTokens(_token); } if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event ControllerChanged(address indexed _newController); }
File 5 of 8: FeeBurner
{"ERC20Interface.sol":{"content":"pragma solidity 0.4.18;\n\n\n// https://github.com/ethereum/EIPs/issues/20\ninterface ERC20 {\n function totalSupply() public view returns (uint supply);\n function balanceOf(address _owner) public view returns (uint balance);\n function transfer(address _to, uint _value) public returns (bool success);\n function transferFrom(address _from, address _to, uint _value) public returns (bool success);\n function approve(address _spender, uint _value) public returns (bool success);\n function allowance(address _owner, address _spender) public view returns (uint remaining);\n function decimals() public view returns(uint digits);\n event Approval(address indexed _owner, address indexed _spender, uint _value);\n}\n"},"FeeBurner.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./FeeBurnerInterface.sol\";\nimport \"./Withdrawable.sol\";\nimport \"./Utils3.sol\";\nimport \"./KyberNetworkInterface.sol\";\n\n\ninterface BurnableToken {\n function transferFrom(address _from, address _to, uint _value) public returns (bool);\n function burnFrom(address _from, uint256 _value) public returns (bool);\n}\n\n\ncontract FeeBurner is Withdrawable, FeeBurnerInterface, Utils3 {\n\n mapping(address=\u003euint) public reserveFeesInBps;\n mapping(address=\u003eaddress) public reserveKNCWallet; //wallet holding knc per reserve. from here burn and send fees.\n mapping(address=\u003euint) public walletFeesInBps; // wallet that is the source of tx is entitled so some fees.\n mapping(address=\u003euint) public reserveFeeToBurn;\n mapping(address=\u003euint) public feePayedPerReserve; // track burned fees and sent wallet fees per reserve.\n mapping(address=\u003emapping(address=\u003euint)) public reserveFeeToWallet;\n address public taxWallet;\n uint public taxFeeBps = 0; // burned fees are taxed. % out of burned fees.\n\n BurnableToken public knc;\n KyberNetworkInterface public kyberNetwork;\n uint public kncPerEthRatePrecision = 600 * PRECISION; //--\u003e 1 ether = 600 knc tokens\n\n function FeeBurner(\n address _admin,\n BurnableToken _kncToken,\n KyberNetworkInterface _kyberNetwork,\n uint _initialKncToEthRatePrecision\n )\n public\n {\n require(_admin != address(0));\n require(_kncToken != address(0));\n require(_kyberNetwork != address(0));\n require(_initialKncToEthRatePrecision != 0);\n\n kyberNetwork = _kyberNetwork;\n admin = _admin;\n knc = _kncToken;\n kncPerEthRatePrecision = _initialKncToEthRatePrecision;\n }\n\n event ReserveDataSet(address reserve, uint feeInBps, address kncWallet);\n\n function setReserveData(address reserve, uint feesInBps, address kncWallet) public onlyOperator {\n require(feesInBps \u003c 100); // make sure it is always \u003c 1%\n require(kncWallet != address(0));\n reserveFeesInBps[reserve] = feesInBps;\n reserveKNCWallet[reserve] = kncWallet;\n ReserveDataSet(reserve, feesInBps, kncWallet);\n }\n\n event WalletFeesSet(address wallet, uint feesInBps);\n\n function setWalletFees(address wallet, uint feesInBps) public onlyAdmin {\n require(feesInBps \u003c 10000); // under 100%\n walletFeesInBps[wallet] = feesInBps;\n WalletFeesSet(wallet, feesInBps);\n }\n\n event TaxFeesSet(uint feesInBps);\n\n function setTaxInBps(uint _taxFeeBps) public onlyAdmin {\n require(_taxFeeBps \u003c 10000); // under 100%\n taxFeeBps = _taxFeeBps;\n TaxFeesSet(_taxFeeBps);\n }\n\n event TaxWalletSet(address taxWallet);\n\n function setTaxWallet(address _taxWallet) public onlyAdmin {\n require(_taxWallet != address(0));\n taxWallet = _taxWallet;\n TaxWalletSet(_taxWallet);\n }\n\n event KNCRateSet(uint ethToKncRatePrecision, uint kyberEthKnc, uint kyberKncEth, address updater);\n\n function setKNCRate() public {\n //query kyber for knc rate sell and buy\n uint kyberEthKncRate;\n uint kyberKncEthRate;\n (kyberEthKncRate, ) = kyberNetwork.getExpectedRate(ETH_TOKEN_ADDRESS, ERC20(knc), (10 ** 18));\n (kyberKncEthRate, ) = kyberNetwork.getExpectedRate(ERC20(knc), ETH_TOKEN_ADDRESS, (10 ** 18));\n\n //check \"reasonable\" spread == diff not too big. rate wasn\u0027t tampered.\n require(kyberEthKncRate * kyberKncEthRate \u003c PRECISION ** 2 * 2);\n require(kyberEthKncRate * kyberKncEthRate \u003e PRECISION ** 2 / 2);\n\n require(kyberEthKncRate \u003c= MAX_RATE);\n kncPerEthRatePrecision = kyberEthKncRate;\n KNCRateSet(kncPerEthRatePrecision, kyberEthKncRate, kyberKncEthRate, msg.sender);\n }\n\n event AssignFeeToWallet(address reserve, address wallet, uint walletFee);\n event AssignBurnFees(address reserve, uint burnFee);\n\n function handleFees(uint tradeWeiAmount, address reserve, address wallet) public returns(bool) {\n require(msg.sender == address(kyberNetwork));\n require(tradeWeiAmount \u003c= MAX_QTY);\n\n // MAX_DECIMALS = 18 = KNC_DECIMALS, use this value instead of calling getDecimals() to save gas\n uint kncAmount = calcDestAmountWithDecimals(ETH_DECIMALS, MAX_DECIMALS, tradeWeiAmount, kncPerEthRatePrecision);\n uint fee = kncAmount * reserveFeesInBps[reserve] / 10000;\n\n uint walletFee = fee * walletFeesInBps[wallet] / 10000;\n require(fee \u003e= walletFee);\n uint feeToBurn = fee - walletFee;\n\n if (walletFee \u003e 0) {\n reserveFeeToWallet[reserve][wallet] += walletFee;\n AssignFeeToWallet(reserve, wallet, walletFee);\n }\n\n if (feeToBurn \u003e 0) {\n AssignBurnFees(reserve, feeToBurn);\n reserveFeeToBurn[reserve] += feeToBurn;\n }\n\n return true;\n }\n\n event BurnAssignedFees(address indexed reserve, address sender, uint quantity);\n\n event SendTaxFee(address indexed reserve, address sender, address taxWallet, uint quantity);\n\n // this function is callable by anyone\n function burnReserveFees(address reserve) public {\n uint burnAmount = reserveFeeToBurn[reserve];\n uint taxToSend = 0;\n require(burnAmount \u003e 2);\n reserveFeeToBurn[reserve] = 1; // leave 1 twei to avoid spikes in gas fee\n if (taxWallet != address(0) \u0026\u0026 taxFeeBps != 0) {\n taxToSend = (burnAmount - 1) * taxFeeBps / 10000;\n require(burnAmount - 1 \u003e taxToSend);\n burnAmount -= taxToSend;\n if (taxToSend \u003e 0) {\n require(knc.transferFrom(reserveKNCWallet[reserve], taxWallet, taxToSend));\n SendTaxFee(reserve, msg.sender, taxWallet, taxToSend);\n }\n }\n require(knc.burnFrom(reserveKNCWallet[reserve], burnAmount - 1));\n\n //update reserve \"payments\" so far\n feePayedPerReserve[reserve] += (taxToSend + burnAmount - 1);\n\n BurnAssignedFees(reserve, msg.sender, (burnAmount - 1));\n }\n\n event SendWalletFees(address indexed wallet, address reserve, address sender);\n\n // this function is callable by anyone\n function sendFeeToWallet(address wallet, address reserve) public {\n uint feeAmount = reserveFeeToWallet[reserve][wallet];\n require(feeAmount \u003e 1);\n reserveFeeToWallet[reserve][wallet] = 1; // leave 1 twei to avoid spikes in gas fee\n require(knc.transferFrom(reserveKNCWallet[reserve], wallet, feeAmount - 1));\n\n feePayedPerReserve[reserve] += (feeAmount - 1);\n SendWalletFees(wallet, reserve, msg.sender);\n }\n}\n"},"FeeBurnerInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\ninterface FeeBurnerInterface {\n function handleFees (uint tradeWeiAmount, address reserve, address wallet) public returns(bool);\n function setReserveData(address reserve, uint feesInBps, address kncWallet) public;\n}\n"},"KyberNetworkInterface.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n\n/// @title Kyber Network interface\ninterface KyberNetworkInterface {\n function maxGasPrice() public view returns(uint);\n function getUserCapInWei(address user) public view returns(uint);\n function getUserCapInTokenWei(address user, ERC20 token) public view returns(uint);\n function enabled() public view returns(bool);\n function info(bytes32 id) public view returns(uint);\n\n function getExpectedRate(ERC20 src, ERC20 dest, uint srcQty) public view\n returns (uint expectedRate, uint slippageRate);\n\n function tradeWithHint(address trader, ERC20 src, uint srcAmount, ERC20 dest, address destAddress,\n uint maxDestAmount, uint minConversionRate, address walletId, bytes hint) public payable returns(uint);\n}\n"},"PermissionGroups.sol":{"content":"pragma solidity 0.4.18;\n\n\ncontract PermissionGroups {\n\n address public admin;\n address public pendingAdmin;\n mapping(address=\u003ebool) internal operators;\n mapping(address=\u003ebool) internal alerters;\n address[] internal operatorsGroup;\n address[] internal alertersGroup;\n uint constant internal MAX_GROUP_SIZE = 50;\n\n function PermissionGroups() public {\n admin = msg.sender;\n }\n\n modifier onlyAdmin() {\n require(msg.sender == admin);\n _;\n }\n\n modifier onlyOperator() {\n require(operators[msg.sender]);\n _;\n }\n\n modifier onlyAlerter() {\n require(alerters[msg.sender]);\n _;\n }\n\n function getOperators () external view returns(address[]) {\n return operatorsGroup;\n }\n\n function getAlerters () external view returns(address[]) {\n return alertersGroup;\n }\n\n event TransferAdminPending(address pendingAdmin);\n\n /**\n * @dev Allows the current admin to set the pendingAdmin address.\n * @param newAdmin The address to transfer ownership to.\n */\n function transferAdmin(address newAdmin) public onlyAdmin {\n require(newAdmin != address(0));\n TransferAdminPending(pendingAdmin);\n pendingAdmin = newAdmin;\n }\n\n /**\n * @dev Allows the current admin to set the admin in one tx. Useful initial deployment.\n * @param newAdmin The address to transfer ownership to.\n */\n function transferAdminQuickly(address newAdmin) public onlyAdmin {\n require(newAdmin != address(0));\n TransferAdminPending(newAdmin);\n AdminClaimed(newAdmin, admin);\n admin = newAdmin;\n }\n\n event AdminClaimed( address newAdmin, address previousAdmin);\n\n /**\n * @dev Allows the pendingAdmin address to finalize the change admin process.\n */\n function claimAdmin() public {\n require(pendingAdmin == msg.sender);\n AdminClaimed(pendingAdmin, admin);\n admin = pendingAdmin;\n pendingAdmin = address(0);\n }\n\n event AlerterAdded (address newAlerter, bool isAdd);\n\n function addAlerter(address newAlerter) public onlyAdmin {\n require(!alerters[newAlerter]); // prevent duplicates.\n require(alertersGroup.length \u003c MAX_GROUP_SIZE);\n\n AlerterAdded(newAlerter, true);\n alerters[newAlerter] = true;\n alertersGroup.push(newAlerter);\n }\n\n function removeAlerter (address alerter) public onlyAdmin {\n require(alerters[alerter]);\n alerters[alerter] = false;\n\n for (uint i = 0; i \u003c alertersGroup.length; ++i) {\n if (alertersGroup[i] == alerter) {\n alertersGroup[i] = alertersGroup[alertersGroup.length - 1];\n alertersGroup.length--;\n AlerterAdded(alerter, false);\n break;\n }\n }\n }\n\n event OperatorAdded(address newOperator, bool isAdd);\n\n function addOperator(address newOperator) public onlyAdmin {\n require(!operators[newOperator]); // prevent duplicates.\n require(operatorsGroup.length \u003c MAX_GROUP_SIZE);\n\n OperatorAdded(newOperator, true);\n operators[newOperator] = true;\n operatorsGroup.push(newOperator);\n }\n\n function removeOperator (address operator) public onlyAdmin {\n require(operators[operator]);\n operators[operator] = false;\n\n for (uint i = 0; i \u003c operatorsGroup.length; ++i) {\n if (operatorsGroup[i] == operator) {\n operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1];\n operatorsGroup.length -= 1;\n OperatorAdded(operator, false);\n break;\n }\n }\n }\n}\n"},"Utils.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\n\n\n/// @title Kyber constants contract\ncontract Utils {\n\n ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);\n uint constant internal PRECISION = (10**18);\n uint constant internal MAX_QTY = (10**28); // 10B tokens\n uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH\n uint constant internal MAX_DECIMALS = 18;\n uint constant internal ETH_DECIMALS = 18;\n mapping(address=\u003euint) internal decimals;\n\n function setDecimals(ERC20 token) internal {\n if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS;\n else decimals[token] = token.decimals();\n }\n\n function getDecimals(ERC20 token) internal view returns(uint) {\n if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access\n uint tokenDecimals = decimals[token];\n // technically, there might be token with decimals 0\n // moreover, very possible that old tokens have decimals 0\n // these tokens will just have higher gas fees.\n if(tokenDecimals == 0) return token.decimals();\n\n return tokenDecimals;\n }\n\n function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {\n require(srcQty \u003c= MAX_QTY);\n require(rate \u003c= MAX_RATE);\n\n if (dstDecimals \u003e= srcDecimals) {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION;\n } else {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals)));\n }\n }\n\n function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) {\n require(dstQty \u003c= MAX_QTY);\n require(rate \u003c= MAX_RATE);\n \n //source quantity is rounded up. to avoid dest quantity being too low.\n uint numerator;\n uint denominator;\n if (srcDecimals \u003e= dstDecimals) {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals)));\n denominator = rate;\n } else {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n numerator = (PRECISION * dstQty);\n denominator = (rate * (10**(dstDecimals - srcDecimals)));\n }\n return (numerator + denominator - 1) / denominator; //avoid rounding down errors\n }\n}\n"},"Utils2.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./Utils.sol\";\n\n\ncontract Utils2 is Utils {\n\n /// @dev get the balance of a user.\n /// @param token The token type\n /// @return The balance\n function getBalance(ERC20 token, address user) public view returns(uint) {\n if (token == ETH_TOKEN_ADDRESS)\n return user.balance;\n else\n return token.balanceOf(user);\n }\n\n function getDecimalsSafe(ERC20 token) internal returns(uint) {\n\n if (decimals[token] == 0) {\n setDecimals(token);\n }\n\n return decimals[token];\n }\n\n function calcDestAmount(ERC20 src, ERC20 dest, uint srcAmount, uint rate) internal view returns(uint) {\n return calcDstQty(srcAmount, getDecimals(src), getDecimals(dest), rate);\n }\n\n function calcSrcAmount(ERC20 src, ERC20 dest, uint destAmount, uint rate) internal view returns(uint) {\n return calcSrcQty(destAmount, getDecimals(src), getDecimals(dest), rate);\n }\n\n function calcRateFromQty(uint srcAmount, uint destAmount, uint srcDecimals, uint dstDecimals)\n internal pure returns(uint)\n {\n require(srcAmount \u003c= MAX_QTY);\n require(destAmount \u003c= MAX_QTY);\n\n if (dstDecimals \u003e= srcDecimals) {\n require((dstDecimals - srcDecimals) \u003c= MAX_DECIMALS);\n return (destAmount * PRECISION / ((10 ** (dstDecimals - srcDecimals)) * srcAmount));\n } else {\n require((srcDecimals - dstDecimals) \u003c= MAX_DECIMALS);\n return (destAmount * PRECISION * (10 ** (srcDecimals - dstDecimals)) / srcAmount);\n }\n }\n}\n"},"Utils3.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./Utils2.sol\";\n\n\ncontract Utils3 is Utils2 {\n\n function calcDestAmountWithDecimals(uint srcDecimals, uint destDecimals, uint srcAmount, uint rate) internal pure returns(uint) {\n return calcDstQty(srcAmount, srcDecimals, destDecimals, rate);\n }\n\n}\n"},"Withdrawable.sol":{"content":"pragma solidity 0.4.18;\n\n\nimport \"./ERC20Interface.sol\";\nimport \"./PermissionGroups.sol\";\n\n\n/**\n * @title Contracts that should be able to recover tokens or ethers\n * @author Ilan Doron\n * @dev This allows to recover any tokens or Ethers received in a contract.\n * This will prevent any accidental loss of tokens.\n */\ncontract Withdrawable is PermissionGroups {\n\n event TokenWithdraw(ERC20 token, uint amount, address sendTo);\n\n /**\n * @dev Withdraw all ERC20 compatible tokens\n * @param token ERC20 The address of the token contract\n */\n function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin {\n require(token.transfer(sendTo, amount));\n TokenWithdraw(token, amount, sendTo);\n }\n\n event EtherWithdraw(uint amount, address sendTo);\n\n /**\n * @dev Withdraw Ethers\n */\n function withdrawEther(uint amount, address sendTo) external onlyAdmin {\n sendTo.transfer(amount);\n EtherWithdraw(amount, sendTo);\n }\n}\n"}}
File 6 of 8: ConversionRates
pragma solidity 0.4.18; interface ConversionRatesInterface { function recordImbalance( ERC20 token, int buyAmount, uint rateUpdateBlock, uint currentBlock ) public; function getRate(ERC20 token, uint currentBlockNumber, bool buy, uint qty) public view returns(uint); } interface ERC20 { function totalSupply() public view returns (uint supply); function balanceOf(address _owner) public view returns (uint balance); function transfer(address _to, uint _value) public returns (bool success); function transferFrom(address _from, address _to, uint _value) public returns (bool success); function approve(address _spender, uint _value) public returns (bool success); function allowance(address _owner, address _spender) public view returns (uint remaining); function decimals() public view returns(uint digits); event Approval(address indexed _owner, address indexed _spender, uint _value); } contract PermissionGroups { address public admin; address public pendingAdmin; mapping(address=>bool) internal operators; mapping(address=>bool) internal alerters; address[] internal operatorsGroup; address[] internal alertersGroup; uint constant internal MAX_GROUP_SIZE = 50; function PermissionGroups() public { admin = msg.sender; } modifier onlyAdmin() { require(msg.sender == admin); _; } modifier onlyOperator() { require(operators[msg.sender]); _; } modifier onlyAlerter() { require(alerters[msg.sender]); _; } function getOperators () external view returns(address[]) { return operatorsGroup; } function getAlerters () external view returns(address[]) { return alertersGroup; } event TransferAdminPending(address pendingAdmin); /** * @dev Allows the current admin to set the pendingAdmin address. * @param newAdmin The address to transfer ownership to. */ function transferAdmin(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(pendingAdmin); pendingAdmin = newAdmin; } /** * @dev Allows the current admin to set the admin in one tx. Useful initial deployment. * @param newAdmin The address to transfer ownership to. */ function transferAdminQuickly(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(newAdmin); AdminClaimed(newAdmin, admin); admin = newAdmin; } event AdminClaimed( address newAdmin, address previousAdmin); /** * @dev Allows the pendingAdmin address to finalize the change admin process. */ function claimAdmin() public { require(pendingAdmin == msg.sender); AdminClaimed(pendingAdmin, admin); admin = pendingAdmin; pendingAdmin = address(0); } event AlerterAdded (address newAlerter, bool isAdd); function addAlerter(address newAlerter) public onlyAdmin { require(!alerters[newAlerter]); // prevent duplicates. require(alertersGroup.length < MAX_GROUP_SIZE); AlerterAdded(newAlerter, true); alerters[newAlerter] = true; alertersGroup.push(newAlerter); } function removeAlerter (address alerter) public onlyAdmin { require(alerters[alerter]); alerters[alerter] = false; for (uint i = 0; i < alertersGroup.length; ++i) { if (alertersGroup[i] == alerter) { alertersGroup[i] = alertersGroup[alertersGroup.length - 1]; alertersGroup.length--; AlerterAdded(alerter, false); break; } } } event OperatorAdded(address newOperator, bool isAdd); function addOperator(address newOperator) public onlyAdmin { require(!operators[newOperator]); // prevent duplicates. require(operatorsGroup.length < MAX_GROUP_SIZE); OperatorAdded(newOperator, true); operators[newOperator] = true; operatorsGroup.push(newOperator); } function removeOperator (address operator) public onlyAdmin { require(operators[operator]); operators[operator] = false; for (uint i = 0; i < operatorsGroup.length; ++i) { if (operatorsGroup[i] == operator) { operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1]; operatorsGroup.length -= 1; OperatorAdded(operator, false); break; } } } } contract Withdrawable is PermissionGroups { event TokenWithdraw(ERC20 token, uint amount, address sendTo); /** * @dev Withdraw all ERC20 compatible tokens * @param token ERC20 The address of the token contract */ function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin { require(token.transfer(sendTo, amount)); TokenWithdraw(token, amount, sendTo); } event EtherWithdraw(uint amount, address sendTo); /** * @dev Withdraw Ethers */ function withdrawEther(uint amount, address sendTo) external onlyAdmin { sendTo.transfer(amount); EtherWithdraw(amount, sendTo); } } contract VolumeImbalanceRecorder is Withdrawable { uint constant internal SLIDING_WINDOW_SIZE = 5; uint constant internal POW_2_64 = 2 ** 64; struct TokenControlInfo { uint minimalRecordResolution; // can be roughly 1 cent uint maxPerBlockImbalance; // in twei resolution uint maxTotalImbalance; // max total imbalance (between rate updates) // before halting trade } mapping(address => TokenControlInfo) internal tokenControlInfo; struct TokenImbalanceData { int lastBlockBuyUnitsImbalance; uint lastBlock; int totalBuyUnitsImbalance; uint lastRateUpdateBlock; } mapping(address => mapping(uint=>uint)) public tokenImbalanceData; function VolumeImbalanceRecorder(address _admin) public { require(_admin != address(0)); admin = _admin; } function setTokenControlInfo( ERC20 token, uint minimalRecordResolution, uint maxPerBlockImbalance, uint maxTotalImbalance ) public onlyAdmin { tokenControlInfo[token] = TokenControlInfo( minimalRecordResolution, maxPerBlockImbalance, maxTotalImbalance ); } function getTokenControlInfo(ERC20 token) public view returns(uint, uint, uint) { return (tokenControlInfo[token].minimalRecordResolution, tokenControlInfo[token].maxPerBlockImbalance, tokenControlInfo[token].maxTotalImbalance); } function addImbalance( ERC20 token, int buyAmount, uint rateUpdateBlock, uint currentBlock ) internal { uint currentBlockIndex = currentBlock % SLIDING_WINDOW_SIZE; int recordedBuyAmount = int(buyAmount / int(tokenControlInfo[token].minimalRecordResolution)); int prevImbalance = 0; TokenImbalanceData memory currentBlockData = decodeTokenImbalanceData(tokenImbalanceData[token][currentBlockIndex]); // first scenario - this is not the first tx in the current block if (currentBlockData.lastBlock == currentBlock) { if (uint(currentBlockData.lastRateUpdateBlock) == rateUpdateBlock) { // just increase imbalance currentBlockData.lastBlockBuyUnitsImbalance += recordedBuyAmount; currentBlockData.totalBuyUnitsImbalance += recordedBuyAmount; } else { // imbalance was changed in the middle of the block prevImbalance = getImbalanceInRange(token, rateUpdateBlock, currentBlock); currentBlockData.totalBuyUnitsImbalance = int(prevImbalance) + recordedBuyAmount; currentBlockData.lastBlockBuyUnitsImbalance += recordedBuyAmount; currentBlockData.lastRateUpdateBlock = uint(rateUpdateBlock); } } else { // first tx in the current block int currentBlockImbalance; (prevImbalance, currentBlockImbalance) = getImbalanceSinceRateUpdate(token, rateUpdateBlock, currentBlock); currentBlockData.lastBlockBuyUnitsImbalance = recordedBuyAmount; currentBlockData.lastBlock = uint(currentBlock); currentBlockData.lastRateUpdateBlock = uint(rateUpdateBlock); currentBlockData.totalBuyUnitsImbalance = int(prevImbalance) + recordedBuyAmount; } tokenImbalanceData[token][currentBlockIndex] = encodeTokenImbalanceData(currentBlockData); } function setGarbageToVolumeRecorder(ERC20 token) internal { for (uint i = 0; i < SLIDING_WINDOW_SIZE; i++) { tokenImbalanceData[token][i] = 0x1; } } function getImbalanceInRange(ERC20 token, uint startBlock, uint endBlock) internal view returns(int buyImbalance) { // check the imbalance in the sliding window require(startBlock <= endBlock); buyImbalance = 0; for (uint windowInd = 0; windowInd < SLIDING_WINDOW_SIZE; windowInd++) { TokenImbalanceData memory perBlockData = decodeTokenImbalanceData(tokenImbalanceData[token][windowInd]); if (perBlockData.lastBlock <= endBlock && perBlockData.lastBlock >= startBlock) { buyImbalance += int(perBlockData.lastBlockBuyUnitsImbalance); } } } function getImbalanceSinceRateUpdate(ERC20 token, uint rateUpdateBlock, uint currentBlock) internal view returns(int buyImbalance, int currentBlockImbalance) { buyImbalance = 0; currentBlockImbalance = 0; uint latestBlock = 0; int imbalanceInRange = 0; uint startBlock = rateUpdateBlock; uint endBlock = currentBlock; for (uint windowInd = 0; windowInd < SLIDING_WINDOW_SIZE; windowInd++) { TokenImbalanceData memory perBlockData = decodeTokenImbalanceData(tokenImbalanceData[token][windowInd]); if (perBlockData.lastBlock <= endBlock && perBlockData.lastBlock >= startBlock) { imbalanceInRange += perBlockData.lastBlockBuyUnitsImbalance; } if (perBlockData.lastRateUpdateBlock != rateUpdateBlock) continue; if (perBlockData.lastBlock < latestBlock) continue; latestBlock = perBlockData.lastBlock; buyImbalance = perBlockData.totalBuyUnitsImbalance; if (uint(perBlockData.lastBlock) == currentBlock) { currentBlockImbalance = perBlockData.lastBlockBuyUnitsImbalance; } } if (buyImbalance == 0) { buyImbalance = imbalanceInRange; } } function getImbalance(ERC20 token, uint rateUpdateBlock, uint currentBlock) internal view returns(int totalImbalance, int currentBlockImbalance) { int resolution = int(tokenControlInfo[token].minimalRecordResolution); (totalImbalance, currentBlockImbalance) = getImbalanceSinceRateUpdate( token, rateUpdateBlock, currentBlock); totalImbalance *= resolution; currentBlockImbalance *= resolution; } function getMaxPerBlockImbalance(ERC20 token) internal view returns(uint) { return tokenControlInfo[token].maxPerBlockImbalance; } function getMaxTotalImbalance(ERC20 token) internal view returns(uint) { return tokenControlInfo[token].maxTotalImbalance; } function encodeTokenImbalanceData(TokenImbalanceData data) internal pure returns(uint) { // check for overflows require(data.lastBlockBuyUnitsImbalance < int(POW_2_64 / 2)); require(data.lastBlockBuyUnitsImbalance > int(-1 * int(POW_2_64) / 2)); require(data.lastBlock < POW_2_64); require(data.totalBuyUnitsImbalance < int(POW_2_64 / 2)); require(data.totalBuyUnitsImbalance > int(-1 * int(POW_2_64) / 2)); require(data.lastRateUpdateBlock < POW_2_64); // do encoding uint result = uint(data.lastBlockBuyUnitsImbalance) & (POW_2_64 - 1); result |= data.lastBlock * POW_2_64; result |= (uint(data.totalBuyUnitsImbalance) & (POW_2_64 - 1)) * POW_2_64 * POW_2_64; result |= data.lastRateUpdateBlock * POW_2_64 * POW_2_64 * POW_2_64; return result; } function decodeTokenImbalanceData(uint input) internal pure returns(TokenImbalanceData) { TokenImbalanceData memory data; data.lastBlockBuyUnitsImbalance = int(int64(input & (POW_2_64 - 1))); data.lastBlock = uint(uint64((input / POW_2_64) & (POW_2_64 - 1))); data.totalBuyUnitsImbalance = int(int64((input / (POW_2_64 * POW_2_64)) & (POW_2_64 - 1))); data.lastRateUpdateBlock = uint(uint64((input / (POW_2_64 * POW_2_64 * POW_2_64)))); return data; } } contract Utils { ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee); uint constant internal PRECISION = (10**18); uint constant internal MAX_QTY = (10**28); // 10B tokens uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH uint constant internal MAX_DECIMALS = 18; uint constant internal ETH_DECIMALS = 18; mapping(address=>uint) internal decimals; function setDecimals(ERC20 token) internal { if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS; else decimals[token] = token.decimals(); } function getDecimals(ERC20 token) internal view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access uint tokenDecimals = decimals[token]; // technically, there might be token with decimals 0 // moreover, very possible that old tokens have decimals 0 // these tokens will just have higher gas fees. if(tokenDecimals == 0) return token.decimals(); return tokenDecimals; } function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(srcQty <= MAX_QTY); require(rate <= MAX_RATE); if (dstDecimals >= srcDecimals) { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION; } else { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals))); } } function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(dstQty <= MAX_QTY); require(rate <= MAX_RATE); //source quantity is rounded up. to avoid dest quantity being too low. uint numerator; uint denominator; if (srcDecimals >= dstDecimals) { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals))); denominator = rate; } else { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty); denominator = (rate * (10**(dstDecimals - srcDecimals))); } return (numerator + denominator - 1) / denominator; //avoid rounding down errors } } contract ConversionRates is ConversionRatesInterface, VolumeImbalanceRecorder, Utils { // bps - basic rate steps. one step is 1 / 10000 of the rate. struct StepFunction { int[] x; // quantity for each step. Quantity of each step includes previous steps. int[] y; // rate change per quantity step in bps. } struct TokenData { bool listed; // was added to reserve bool enabled; // whether trade is enabled // position in the compact data uint compactDataArrayIndex; uint compactDataFieldIndex; // rate data. base and changes according to quantity and reserve balance. // generally speaking. Sell rate is 1 / buy rate i.e. the buy in the other direction. uint baseBuyRate; // in PRECISION units. see KyberConstants uint baseSellRate; // PRECISION units. without (sell / buy) spread it is 1 / baseBuyRate StepFunction buyRateQtyStepFunction; // in bps. higher quantity - bigger the rate. StepFunction sellRateQtyStepFunction;// in bps. higher the qua StepFunction buyRateImbalanceStepFunction; // in BPS. higher reserve imbalance - bigger the rate. StepFunction sellRateImbalanceStepFunction; } /* this is the data for tokenRatesCompactData but solidity compiler optimizer is sub-optimal, and cannot write this structure in a single storage write so we represent it as bytes32 and do the byte tricks ourselves. struct TokenRatesCompactData { bytes14 buy; // change buy rate of token from baseBuyRate in 10 bps bytes14 sell; // change sell rate of token from baseSellRate in 10 bps uint32 blockNumber; } */ uint public validRateDurationInBlocks = 10; // rates are valid for this amount of blocks ERC20[] internal listedTokens; mapping(address=>TokenData) internal tokenData; bytes32[] internal tokenRatesCompactData; uint public numTokensInCurrentCompactData = 0; address public reserveContract; uint constant internal NUM_TOKENS_IN_COMPACT_DATA = 14; uint constant internal BYTES_14_OFFSET = (2 ** (8 * NUM_TOKENS_IN_COMPACT_DATA)); uint constant internal MAX_STEPS_IN_FUNCTION = 10; int constant internal MAX_BPS_ADJUSTMENT = 10 ** 11; // 1B % int constant internal MIN_BPS_ADJUSTMENT = -100 * 100; // cannot go down by more than 100% function ConversionRates(address _admin) public VolumeImbalanceRecorder(_admin) { } // solhint-disable-line no-empty-blocks function addToken(ERC20 token) public onlyAdmin { require(!tokenData[token].listed); tokenData[token].listed = true; listedTokens.push(token); if (numTokensInCurrentCompactData == 0) { tokenRatesCompactData.length++; // add new structure } tokenData[token].compactDataArrayIndex = tokenRatesCompactData.length - 1; tokenData[token].compactDataFieldIndex = numTokensInCurrentCompactData; numTokensInCurrentCompactData = (numTokensInCurrentCompactData + 1) % NUM_TOKENS_IN_COMPACT_DATA; setGarbageToVolumeRecorder(token); setDecimals(token); } function setCompactData(bytes14[] buy, bytes14[] sell, uint blockNumber, uint[] indices) public onlyOperator { require(buy.length == sell.length); require(indices.length == buy.length); require(blockNumber <= 0xFFFFFFFF); uint bytes14Offset = BYTES_14_OFFSET; for (uint i = 0; i < indices.length; i++) { require(indices[i] < tokenRatesCompactData.length); uint data = uint(buy[i]) | uint(sell[i]) * bytes14Offset | (blockNumber * (bytes14Offset * bytes14Offset)); tokenRatesCompactData[indices[i]] = bytes32(data); } } function setBaseRate( ERC20[] tokens, uint[] baseBuy, uint[] baseSell, bytes14[] buy, bytes14[] sell, uint blockNumber, uint[] indices ) public onlyOperator { require(tokens.length == baseBuy.length); require(tokens.length == baseSell.length); require(sell.length == buy.length); require(sell.length == indices.length); for (uint ind = 0; ind < tokens.length; ind++) { require(tokenData[tokens[ind]].listed); tokenData[tokens[ind]].baseBuyRate = baseBuy[ind]; tokenData[tokens[ind]].baseSellRate = baseSell[ind]; } setCompactData(buy, sell, blockNumber, indices); } function setQtyStepFunction( ERC20 token, int[] xBuy, int[] yBuy, int[] xSell, int[] ySell ) public onlyOperator { require(xBuy.length == yBuy.length); require(xSell.length == ySell.length); require(xBuy.length <= MAX_STEPS_IN_FUNCTION); require(xSell.length <= MAX_STEPS_IN_FUNCTION); require(tokenData[token].listed); tokenData[token].buyRateQtyStepFunction = StepFunction(xBuy, yBuy); tokenData[token].sellRateQtyStepFunction = StepFunction(xSell, ySell); } function setImbalanceStepFunction( ERC20 token, int[] xBuy, int[] yBuy, int[] xSell, int[] ySell ) public onlyOperator { require(xBuy.length == yBuy.length); require(xSell.length == ySell.length); require(xBuy.length <= MAX_STEPS_IN_FUNCTION); require(xSell.length <= MAX_STEPS_IN_FUNCTION); require(tokenData[token].listed); tokenData[token].buyRateImbalanceStepFunction = StepFunction(xBuy, yBuy); tokenData[token].sellRateImbalanceStepFunction = StepFunction(xSell, ySell); } function setValidRateDurationInBlocks(uint duration) public onlyAdmin { validRateDurationInBlocks = duration; } function enableTokenTrade(ERC20 token) public onlyAdmin { require(tokenData[token].listed); require(tokenControlInfo[token].minimalRecordResolution != 0); tokenData[token].enabled = true; } function disableTokenTrade(ERC20 token) public onlyAlerter { require(tokenData[token].listed); tokenData[token].enabled = false; } function setReserveAddress(address reserve) public onlyAdmin { reserveContract = reserve; } function recordImbalance( ERC20 token, int buyAmount, uint rateUpdateBlock, uint currentBlock ) public { require(msg.sender == reserveContract); if (rateUpdateBlock == 0) rateUpdateBlock = getRateUpdateBlock(token); return addImbalance(token, buyAmount, rateUpdateBlock, currentBlock); } /* solhint-disable function-max-lines */ function getRate(ERC20 token, uint currentBlockNumber, bool buy, uint qty) public view returns(uint) { // check if trade is enabled if (!tokenData[token].enabled) return 0; if (tokenControlInfo[token].minimalRecordResolution == 0) return 0; // token control info not set // get rate update block bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex]; uint updateRateBlock = getLast4Bytes(compactData); if (currentBlockNumber >= updateRateBlock + validRateDurationInBlocks) return 0; // rate is expired // check imbalance int totalImbalance; int blockImbalance; (totalImbalance, blockImbalance) = getImbalance(token, updateRateBlock, currentBlockNumber); // calculate actual rate int imbalanceQty; int extraBps; int8 rateUpdate; uint rate; if (buy) { // start with base rate rate = tokenData[token].baseBuyRate; // add rate update rateUpdate = getRateByteFromCompactData(compactData, token, true); extraBps = int(rateUpdate) * 10; rate = addBps(rate, extraBps); // compute token qty qty = getTokenQty(token, rate, qty); imbalanceQty = int(qty); totalImbalance += imbalanceQty; // add qty overhead extraBps = executeStepFunction(tokenData[token].buyRateQtyStepFunction, int(qty)); rate = addBps(rate, extraBps); // add imbalance overhead extraBps = executeStepFunction(tokenData[token].buyRateImbalanceStepFunction, totalImbalance); rate = addBps(rate, extraBps); } else { // start with base rate rate = tokenData[token].baseSellRate; // add rate update rateUpdate = getRateByteFromCompactData(compactData, token, false); extraBps = int(rateUpdate) * 10; rate = addBps(rate, extraBps); // compute token qty imbalanceQty = -1 * int(qty); totalImbalance += imbalanceQty; // add qty overhead extraBps = executeStepFunction(tokenData[token].sellRateQtyStepFunction, int(qty)); rate = addBps(rate, extraBps); // add imbalance overhead extraBps = executeStepFunction(tokenData[token].sellRateImbalanceStepFunction, totalImbalance); rate = addBps(rate, extraBps); } if (abs(totalImbalance) >= getMaxTotalImbalance(token)) return 0; if (abs(blockImbalance + imbalanceQty) >= getMaxPerBlockImbalance(token)) return 0; return rate; } /* solhint-enable function-max-lines */ function getBasicRate(ERC20 token, bool buy) public view returns(uint) { if (buy) return tokenData[token].baseBuyRate; else return tokenData[token].baseSellRate; } function getCompactData(ERC20 token) public view returns(uint, uint, byte, byte) { require(tokenData[token].listed); uint arrayIndex = tokenData[token].compactDataArrayIndex; uint fieldOffset = tokenData[token].compactDataFieldIndex; return ( arrayIndex, fieldOffset, byte(getRateByteFromCompactData(tokenRatesCompactData[arrayIndex], token, true)), byte(getRateByteFromCompactData(tokenRatesCompactData[arrayIndex], token, false)) ); } function getTokenBasicData(ERC20 token) public view returns(bool, bool) { return (tokenData[token].listed, tokenData[token].enabled); } /* solhint-disable code-complexity */ function getStepFunctionData(ERC20 token, uint command, uint param) public view returns(int) { if (command == 0) return int(tokenData[token].buyRateQtyStepFunction.x.length); if (command == 1) return tokenData[token].buyRateQtyStepFunction.x[param]; if (command == 2) return int(tokenData[token].buyRateQtyStepFunction.y.length); if (command == 3) return tokenData[token].buyRateQtyStepFunction.y[param]; if (command == 4) return int(tokenData[token].sellRateQtyStepFunction.x.length); if (command == 5) return tokenData[token].sellRateQtyStepFunction.x[param]; if (command == 6) return int(tokenData[token].sellRateQtyStepFunction.y.length); if (command == 7) return tokenData[token].sellRateQtyStepFunction.y[param]; if (command == 8) return int(tokenData[token].buyRateImbalanceStepFunction.x.length); if (command == 9) return tokenData[token].buyRateImbalanceStepFunction.x[param]; if (command == 10) return int(tokenData[token].buyRateImbalanceStepFunction.y.length); if (command == 11) return tokenData[token].buyRateImbalanceStepFunction.y[param]; if (command == 12) return int(tokenData[token].sellRateImbalanceStepFunction.x.length); if (command == 13) return tokenData[token].sellRateImbalanceStepFunction.x[param]; if (command == 14) return int(tokenData[token].sellRateImbalanceStepFunction.y.length); if (command == 15) return tokenData[token].sellRateImbalanceStepFunction.y[param]; revert(); } /* solhint-enable code-complexity */ function getRateUpdateBlock(ERC20 token) public view returns(uint) { bytes32 compactData = tokenRatesCompactData[tokenData[token].compactDataArrayIndex]; return getLast4Bytes(compactData); } function getListedTokens() public view returns(ERC20[]) { return listedTokens; } function getTokenQty(ERC20 token, uint ethQty, uint rate) internal view returns(uint) { uint dstDecimals = getDecimals(token); uint srcDecimals = ETH_DECIMALS; return calcDstQty(ethQty, srcDecimals, dstDecimals, rate); } function getLast4Bytes(bytes32 b) internal pure returns(uint) { // cannot trust compiler with not turning bit operations into EXP opcode return uint(b) / (BYTES_14_OFFSET * BYTES_14_OFFSET); } function getRateByteFromCompactData(bytes32 data, ERC20 token, bool buy) internal view returns(int8) { uint fieldOffset = tokenData[token].compactDataFieldIndex; uint byteOffset; if (buy) byteOffset = 32 - NUM_TOKENS_IN_COMPACT_DATA + fieldOffset; else byteOffset = 4 + fieldOffset; return int8(data[byteOffset]); } function executeStepFunction(StepFunction f, int x) internal pure returns(int) { uint len = f.y.length; for (uint ind = 0; ind < len; ind++) { if (x <= f.x[ind]) return f.y[ind]; } return f.y[len-1]; } function addBps(uint rate, int bps) internal pure returns(uint) { require(rate <= MAX_RATE); require(bps >= MIN_BPS_ADJUSTMENT); require(bps <= MAX_BPS_ADJUSTMENT); uint maxBps = 100 * 100; return (rate * uint(int(maxBps) + bps)) / maxBps; } function abs(int x) internal pure returns(uint) { if (x < 0) return uint(-1 * x); else return uint(x); } }
File 7 of 8: SanityRates
pragma solidity 0.4.18; interface ERC20 { function totalSupply() public view returns (uint supply); function balanceOf(address _owner) public view returns (uint balance); function transfer(address _to, uint _value) public returns (bool success); function transferFrom(address _from, address _to, uint _value) public returns (bool success); function approve(address _spender, uint _value) public returns (bool success); function allowance(address _owner, address _spender) public view returns (uint remaining); function decimals() public view returns(uint digits); event Approval(address indexed _owner, address indexed _spender, uint _value); } contract PermissionGroups { address public admin; address public pendingAdmin; mapping(address=>bool) internal operators; mapping(address=>bool) internal alerters; address[] internal operatorsGroup; address[] internal alertersGroup; uint constant internal MAX_GROUP_SIZE = 50; function PermissionGroups() public { admin = msg.sender; } modifier onlyAdmin() { require(msg.sender == admin); _; } modifier onlyOperator() { require(operators[msg.sender]); _; } modifier onlyAlerter() { require(alerters[msg.sender]); _; } function getOperators () external view returns(address[]) { return operatorsGroup; } function getAlerters () external view returns(address[]) { return alertersGroup; } event TransferAdminPending(address pendingAdmin); /** * @dev Allows the current admin to set the pendingAdmin address. * @param newAdmin The address to transfer ownership to. */ function transferAdmin(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(pendingAdmin); pendingAdmin = newAdmin; } /** * @dev Allows the current admin to set the admin in one tx. Useful initial deployment. * @param newAdmin The address to transfer ownership to. */ function transferAdminQuickly(address newAdmin) public onlyAdmin { require(newAdmin != address(0)); TransferAdminPending(newAdmin); AdminClaimed(newAdmin, admin); admin = newAdmin; } event AdminClaimed( address newAdmin, address previousAdmin); /** * @dev Allows the pendingAdmin address to finalize the change admin process. */ function claimAdmin() public { require(pendingAdmin == msg.sender); AdminClaimed(pendingAdmin, admin); admin = pendingAdmin; pendingAdmin = address(0); } event AlerterAdded (address newAlerter, bool isAdd); function addAlerter(address newAlerter) public onlyAdmin { require(!alerters[newAlerter]); // prevent duplicates. require(alertersGroup.length < MAX_GROUP_SIZE); AlerterAdded(newAlerter, true); alerters[newAlerter] = true; alertersGroup.push(newAlerter); } function removeAlerter (address alerter) public onlyAdmin { require(alerters[alerter]); alerters[alerter] = false; for (uint i = 0; i < alertersGroup.length; ++i) { if (alertersGroup[i] == alerter) { alertersGroup[i] = alertersGroup[alertersGroup.length - 1]; alertersGroup.length--; AlerterAdded(alerter, false); break; } } } event OperatorAdded(address newOperator, bool isAdd); function addOperator(address newOperator) public onlyAdmin { require(!operators[newOperator]); // prevent duplicates. require(operatorsGroup.length < MAX_GROUP_SIZE); OperatorAdded(newOperator, true); operators[newOperator] = true; operatorsGroup.push(newOperator); } function removeOperator (address operator) public onlyAdmin { require(operators[operator]); operators[operator] = false; for (uint i = 0; i < operatorsGroup.length; ++i) { if (operatorsGroup[i] == operator) { operatorsGroup[i] = operatorsGroup[operatorsGroup.length - 1]; operatorsGroup.length -= 1; OperatorAdded(operator, false); break; } } } } contract Utils { ERC20 constant internal ETH_TOKEN_ADDRESS = ERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee); uint constant internal PRECISION = (10**18); uint constant internal MAX_QTY = (10**28); // 10B tokens uint constant internal MAX_RATE = (PRECISION * 10**6); // up to 1M tokens per ETH uint constant internal MAX_DECIMALS = 18; uint constant internal ETH_DECIMALS = 18; mapping(address=>uint) internal decimals; function setDecimals(ERC20 token) internal { if (token == ETH_TOKEN_ADDRESS) decimals[token] = ETH_DECIMALS; else decimals[token] = token.decimals(); } function getDecimals(ERC20 token) internal view returns(uint) { if (token == ETH_TOKEN_ADDRESS) return ETH_DECIMALS; // save storage access uint tokenDecimals = decimals[token]; // technically, there might be token with decimals 0 // moreover, very possible that old tokens have decimals 0 // these tokens will just have higher gas fees. if(tokenDecimals == 0) return token.decimals(); return tokenDecimals; } function calcDstQty(uint srcQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(srcQty <= MAX_QTY); require(rate <= MAX_RATE); if (dstDecimals >= srcDecimals) { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); return (srcQty * rate * (10**(dstDecimals - srcDecimals))) / PRECISION; } else { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); return (srcQty * rate) / (PRECISION * (10**(srcDecimals - dstDecimals))); } } function calcSrcQty(uint dstQty, uint srcDecimals, uint dstDecimals, uint rate) internal pure returns(uint) { require(dstQty <= MAX_QTY); require(rate <= MAX_RATE); //source quantity is rounded up. to avoid dest quantity being too low. uint numerator; uint denominator; if (srcDecimals >= dstDecimals) { require((srcDecimals - dstDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty * (10**(srcDecimals - dstDecimals))); denominator = rate; } else { require((dstDecimals - srcDecimals) <= MAX_DECIMALS); numerator = (PRECISION * dstQty); denominator = (rate * (10**(dstDecimals - srcDecimals))); } return (numerator + denominator - 1) / denominator; //avoid rounding down errors } } contract Withdrawable is PermissionGroups { event TokenWithdraw(ERC20 token, uint amount, address sendTo); /** * @dev Withdraw all ERC20 compatible tokens * @param token ERC20 The address of the token contract */ function withdrawToken(ERC20 token, uint amount, address sendTo) external onlyAdmin { require(token.transfer(sendTo, amount)); TokenWithdraw(token, amount, sendTo); } event EtherWithdraw(uint amount, address sendTo); /** * @dev Withdraw Ethers */ function withdrawEther(uint amount, address sendTo) external onlyAdmin { sendTo.transfer(amount); EtherWithdraw(amount, sendTo); } } interface SanityRatesInterface { function getSanityRate(ERC20 src, ERC20 dest) public view returns(uint); } contract SanityRates is SanityRatesInterface, Withdrawable, Utils { mapping(address=>uint) public tokenRate; mapping(address=>uint) public reasonableDiffInBps; function SanityRates(address _admin) public { require(_admin != address(0)); admin = _admin; } function setReasonableDiff(ERC20[] srcs, uint[] diff) public onlyAdmin { require(srcs.length == diff.length); for (uint i = 0; i < srcs.length; i++) { require(diff[i] <= 100 * 100); reasonableDiffInBps[srcs[i]] = diff[i]; } } function setSanityRates(ERC20[] srcs, uint[] rates) public onlyOperator { require(srcs.length == rates.length); for (uint i = 0; i < srcs.length; i++) { require(rates[i] <= MAX_RATE); tokenRate[srcs[i]] = rates[i]; } } function getSanityRate(ERC20 src, ERC20 dest) public view returns(uint) { if (src != ETH_TOKEN_ADDRESS && dest != ETH_TOKEN_ADDRESS) return 0; uint rate; address token; if (src == ETH_TOKEN_ADDRESS) { rate = (PRECISION*PRECISION)/tokenRate[dest]; token = dest; } else { rate = tokenRate[src]; token = src; } return rate * (10000 + reasonableDiffInBps[token])/10000; } }
File 8 of 8: SNTPlaceHolder
pragma solidity ^0.4.11; /// @dev `Owned` is a base level contract that assigns an `owner` that can be /// later changed contract Owned { /// @dev `owner` is the only address that can call a function with this /// modifier modifier onlyOwner() { require(msg.sender == owner); _; } address public owner; /// @notice The Constructor assigns the message sender to be `owner` function Owned() { owner = msg.sender; } address public newOwner; /// @notice `owner` can step down and assign some other address to this role /// @param _newOwner The address of the new owner. 0x0 can be used to create /// an unowned neutral vault, however that cannot be undone function changeOwner(address _newOwner) onlyOwner { newOwner = _newOwner; } function acceptOwnership() { if (msg.sender == newOwner) { owner = newOwner; } } } // Abstract contract for the full ERC 20 Token standard // https://github.com/ethereum/EIPs/issues/20 contract ERC20Token { /* This is a slight change to the ERC20 base standard. function totalSupply() constant returns (uint256 supply); is replaced with: uint256 public totalSupply; This automatically creates a getter function for the totalSupply. This is moved to the base contract since public getter functions are not currently recognised as an implementation of the matching abstract function by the compiler. */ /// total amount of tokens uint256 public totalSupply; /// @param _owner The address from which the balance will be retrieved /// @return The balance function balanceOf(address _owner) constant returns (uint256 balance); /// @notice send `_value` token to `_to` from `msg.sender` /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transfer(address _to, uint256 _value) returns (bool success); /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from` /// @param _from The address of the sender /// @param _to The address of the recipient /// @param _value The amount of token to be transferred /// @return Whether the transfer was successful or not function transferFrom(address _from, address _to, uint256 _value) returns (bool success); /// @notice `msg.sender` approves `_spender` to spend `_value` tokens /// @param _spender The address of the account able to transfer the tokens /// @param _value The amount of tokens to be approved for transfer /// @return Whether the approval was successful or not function approve(address _spender, uint256 _value) returns (bool success); /// @param _owner The address of the account owning tokens /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens allowed to spent function allowance(address _owner, address _spender) constant returns (uint256 remaining); event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); } /** * Math operations with safety checks */ library SafeMath { function mul(uint a, uint b) internal returns (uint) { uint c = a * b; assert(a == 0 || c / a == b); return c; } function div(uint a, uint b) internal returns (uint) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint a, uint b) internal returns (uint) { assert(b <= a); return a - b; } function add(uint a, uint b) internal returns (uint) { uint c = a + b; assert(c >= a); return c; } function max64(uint64 a, uint64 b) internal constant returns (uint64) { return a >= b ? a : b; } function min64(uint64 a, uint64 b) internal constant returns (uint64) { return a < b ? a : b; } function max256(uint256 a, uint256 b) internal constant returns (uint256) { return a >= b ? a : b; } function min256(uint256 a, uint256 b) internal constant returns (uint256) { return a < b ? a : b; } } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title DynamicCeiling Contract /// @author Jordi Baylina /// @dev This contract calculates the ceiling from a series of curves. /// These curves are committed first and revealed later. /// All the curves must be in increasing order and the last curve is marked /// as the last one. /// This contract allows to hide and reveal the ceiling at will of the owner. contract DynamicCeiling is Owned { using SafeMath for uint256; struct Curve { bytes32 hash; // Absolute limit for this curve uint256 limit; // The funds remaining to be collected are divided by `slopeFactor` smooth ceiling // with a long tail where big and small buyers can take part. uint256 slopeFactor; // This keeps the curve flat at this number, until funds to be collected is less than this uint256 collectMinimum; } address public contribution; Curve[] public curves; uint256 public currentIndex; uint256 public revealedCurves; bool public allRevealed; /// @dev `contribution` is the only address that can call a function with this /// modifier modifier onlyContribution { require(msg.sender == contribution); _; } function DynamicCeiling(address _owner, address _contribution) { owner = _owner; contribution = _contribution; } /// @notice This should be called by the creator of the contract to commit /// all the curves. /// @param _curveHashes Array of hashes of each curve. Each hash is calculated /// by the `calculateHash` method. More hashes than actual curves can be /// committed in order to hide also the number of curves. /// The remaining hashes can be just random numbers. function setHiddenCurves(bytes32[] _curveHashes) public onlyOwner { require(curves.length == 0); curves.length = _curveHashes.length; for (uint256 i = 0; i < _curveHashes.length; i = i.add(1)) { curves[i].hash = _curveHashes[i]; } } /// @notice Anybody can reveal the next curve if he knows it. /// @param _limit Ceiling cap. /// (must be greater or equal to the previous one). /// @param _last `true` if it's the last curve. /// @param _salt Random number used to commit the curve function revealCurve(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum, bool _last, bytes32 _salt) public { require(!allRevealed); require(curves[revealedCurves].hash == calculateHash(_limit, _slopeFactor, _collectMinimum, _last, _salt)); require(_limit != 0 && _slopeFactor != 0 && _collectMinimum != 0); if (revealedCurves > 0) { require(_limit >= curves[revealedCurves.sub(1)].limit); } curves[revealedCurves].limit = _limit; curves[revealedCurves].slopeFactor = _slopeFactor; curves[revealedCurves].collectMinimum = _collectMinimum; revealedCurves = revealedCurves.add(1); if (_last) allRevealed = true; } /// @notice Reveal multiple curves at once function revealMulti(uint256[] _limits, uint256[] _slopeFactors, uint256[] _collectMinimums, bool[] _lasts, bytes32[] _salts) public { // Do not allow none and needs to be same length for all parameters require(_limits.length != 0 && _limits.length == _slopeFactors.length && _limits.length == _collectMinimums.length && _limits.length == _lasts.length && _limits.length == _salts.length); for (uint256 i = 0; i < _limits.length; i = i.add(1)) { revealCurve(_limits[i], _slopeFactors[i], _collectMinimums[i], _lasts[i], _salts[i]); } } /// @notice Move to curve, used as a failsafe function moveTo(uint256 _index) public onlyOwner { require(_index < revealedCurves && // No more curves _index == currentIndex.add(1)); // Only move one index at a time currentIndex = _index; } /// @return Return the funds to collect for the current point on the curve /// (or 0 if no curves revealed yet) function toCollect(uint256 collected) public onlyContribution returns (uint256) { if (revealedCurves == 0) return 0; // Move to the next curve if (collected >= curves[currentIndex].limit) { // Catches `limit == 0` uint256 nextIndex = currentIndex.add(1); if (nextIndex >= revealedCurves) return 0; // No more curves currentIndex = nextIndex; if (collected >= curves[currentIndex].limit) return 0; // Catches `limit == 0` } // Everything left to collect from this limit uint256 difference = curves[currentIndex].limit.sub(collected); // Current point on the curve uint256 collect = difference.div(curves[currentIndex].slopeFactor); // Prevents paying too much fees vs to be collected; breaks long tail if (collect <= curves[currentIndex].collectMinimum) { if (difference > curves[currentIndex].collectMinimum) { return curves[currentIndex].collectMinimum; } else { return difference; } } else { return collect; } } /// @notice Calculates the hash of a curve. /// @param _limit Ceiling cap. /// @param _last `true` if it's the last curve. /// @param _salt Random number that will be needed to reveal this curve. /// @return The calculated hash of this curve to be used in the `setHiddenCurves` method function calculateHash(uint256 _limit, uint256 _slopeFactor, uint256 _collectMinimum, bool _last, bytes32 _salt) public constant returns (bytes32) { return keccak256(_limit, _slopeFactor, _collectMinimum, _last, _salt); } /// @return Return the total number of curves committed /// (can be larger than the number of actual curves on the curve to hide /// the real number of curves) function nCurves() public constant returns (uint256) { return curves.length; } } /* Copyright 2016, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title MiniMeToken Contract /// @author Jordi Baylina /// @dev This token contract's goal is to make it easy for anyone to clone this /// token using the token distribution at a given block, this will allow DAO's /// and DApps to upgrade their features in a decentralized manner without /// affecting the original token /// @dev It is ERC20 compliant, but still needs to under go further testing. /// @dev The token controller contract must implement these functions contract TokenController { /// @notice Called when `_owner` sends ether to the MiniMe Token contract /// @param _owner The address that sent the ether to create tokens /// @return True if the ether is accepted, false if it throws function proxyPayment(address _owner) payable returns(bool); /// @notice Notifies the controller about a token transfer allowing the /// controller to react if desired /// @param _from The origin of the transfer /// @param _to The destination of the transfer /// @param _amount The amount of the transfer /// @return False if the controller does not authorize the transfer function onTransfer(address _from, address _to, uint _amount) returns(bool); /// @notice Notifies the controller about an approval allowing the /// controller to react if desired /// @param _owner The address that calls `approve()` /// @param _spender The spender in the `approve()` call /// @param _amount The amount in the `approve()` call /// @return False if the controller does not authorize the approval function onApprove(address _owner, address _spender, uint _amount) returns(bool); } contract Controlled { /// @notice The address of the controller is the only address that can call /// a function with this modifier modifier onlyController { if (msg.sender != controller) throw; _; } address public controller; function Controlled() { controller = msg.sender;} /// @notice Changes the controller of the contract /// @param _newController The new controller of the contract function changeController(address _newController) onlyController { controller = _newController; } } contract ApproveAndCallFallBack { function receiveApproval(address from, uint256 _amount, address _token, bytes _data); } /// @dev The actual token contract, the default controller is the msg.sender /// that deploys the contract, so usually this token will be deployed by a /// token controller contract, which Giveth will call a "Campaign" contract MiniMeToken is Controlled { string public name; //The Token's name: e.g. DigixDAO Tokens uint8 public decimals; //Number of decimals of the smallest unit string public symbol; //An identifier: e.g. REP string public version = 'MMT_0.1'; //An arbitrary versioning scheme /// @dev `Checkpoint` is the structure that attaches a block number to a /// given value, the block number attached is the one that last changed the /// value struct Checkpoint { // `fromBlock` is the block number that the value was generated from uint128 fromBlock; // `value` is the amount of tokens at a specific block number uint128 value; } // `parentToken` is the Token address that was cloned to produce this token; // it will be 0x0 for a token that was not cloned MiniMeToken public parentToken; // `parentSnapShotBlock` is the block number from the Parent Token that was // used to determine the initial distribution of the Clone Token uint public parentSnapShotBlock; // `creationBlock` is the block number that the Clone Token was created uint public creationBlock; // `balances` is the map that tracks the balance of each address, in this // contract when the balance changes the block number that the change // occurred is also included in the map mapping (address => Checkpoint[]) balances; // `allowed` tracks any extra transfer rights as in all ERC20 tokens mapping (address => mapping (address => uint256)) allowed; // Tracks the history of the `totalSupply` of the token Checkpoint[] totalSupplyHistory; // Flag that determines if the token is transferable or not. bool public transfersEnabled; // The factory used to create new clone tokens MiniMeTokenFactory public tokenFactory; //////////////// // Constructor //////////////// /// @notice Constructor to create a MiniMeToken /// @param _tokenFactory The address of the MiniMeTokenFactory contract that /// will create the Clone token contracts, the token factory needs to be /// deployed first /// @param _parentToken Address of the parent token, set to 0x0 if it is a /// new token /// @param _parentSnapShotBlock Block of the parent token that will /// determine the initial distribution of the clone token, set to 0 if it /// is a new token /// @param _tokenName Name of the new token /// @param _decimalUnits Number of decimals of the new token /// @param _tokenSymbol Token Symbol for the new token /// @param _transfersEnabled If true, tokens will be able to be transferred function MiniMeToken( address _tokenFactory, address _parentToken, uint _parentSnapShotBlock, string _tokenName, uint8 _decimalUnits, string _tokenSymbol, bool _transfersEnabled ) { tokenFactory = MiniMeTokenFactory(_tokenFactory); name = _tokenName; // Set the name decimals = _decimalUnits; // Set the decimals symbol = _tokenSymbol; // Set the symbol parentToken = MiniMeToken(_parentToken); parentSnapShotBlock = _parentSnapShotBlock; transfersEnabled = _transfersEnabled; creationBlock = getBlockNumber(); } /////////////////// // ERC20 Methods /////////////////// /// @notice Send `_amount` tokens to `_to` from `msg.sender` /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return Whether the transfer was successful or not function transfer(address _to, uint256 _amount) returns (bool success) { if (!transfersEnabled) throw; return doTransfer(msg.sender, _to, _amount); } /// @notice Send `_amount` tokens to `_to` from `_from` on the condition it /// is approved by `_from` /// @param _from The address holding the tokens being transferred /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful function transferFrom(address _from, address _to, uint256 _amount ) returns (bool success) { // The controller of this contract can move tokens around at will, // this is important to recognize! Confirm that you trust the // controller of this contract, which in most situations should be // another open source smart contract or 0x0 if (msg.sender != controller) { if (!transfersEnabled) throw; // The standard ERC 20 transferFrom functionality if (allowed[_from][msg.sender] < _amount) return false; allowed[_from][msg.sender] -= _amount; } return doTransfer(_from, _to, _amount); } /// @dev This is the actual transfer function in the token contract, it can /// only be called by other functions in this contract. /// @param _from The address holding the tokens being transferred /// @param _to The address of the recipient /// @param _amount The amount of tokens to be transferred /// @return True if the transfer was successful function doTransfer(address _from, address _to, uint _amount ) internal returns(bool) { if (_amount == 0) { return true; } if (parentSnapShotBlock >= getBlockNumber()) throw; // Do not allow transfer to 0x0 or the token contract itself if ((_to == 0) || (_to == address(this))) throw; // If the amount being transfered is more than the balance of the // account the transfer returns false var previousBalanceFrom = balanceOfAt(_from, getBlockNumber()); if (previousBalanceFrom < _amount) { return false; } // Alerts the token controller of the transfer if (isContract(controller)) { if (!TokenController(controller).onTransfer(_from, _to, _amount)) throw; } // First update the balance array with the new value for the address // sending the tokens updateValueAtNow(balances[_from], previousBalanceFrom - _amount); // Then update the balance array with the new value for the address // receiving the tokens var previousBalanceTo = balanceOfAt(_to, getBlockNumber()); if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow updateValueAtNow(balances[_to], previousBalanceTo + _amount); // An event to make the transfer easy to find on the blockchain Transfer(_from, _to, _amount); return true; } /// @param _owner The address that's balance is being requested /// @return The balance of `_owner` at the current block function balanceOf(address _owner) constant returns (uint256 balance) { return balanceOfAt(_owner, getBlockNumber()); } /// @notice `msg.sender` approves `_spender` to spend `_amount` tokens on /// its behalf. This is a modified version of the ERC20 approve function /// to be a little bit safer /// @param _spender The address of the account able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the approval was successful function approve(address _spender, uint256 _amount) returns (bool success) { if (!transfersEnabled) throw; // To change the approve amount you first have to reduce the addresses` // allowance to zero by calling `approve(_spender,0)` if it is not // already 0 to mitigate the race condition described here: // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 if ((_amount!=0) && (allowed[msg.sender][_spender] !=0)) throw; // Alerts the token controller of the approve function call if (isContract(controller)) { if (!TokenController(controller).onApprove(msg.sender, _spender, _amount)) throw; } allowed[msg.sender][_spender] = _amount; Approval(msg.sender, _spender, _amount); return true; } /// @dev This function makes it easy to read the `allowed[]` map /// @param _owner The address of the account that owns the token /// @param _spender The address of the account able to transfer the tokens /// @return Amount of remaining tokens of _owner that _spender is allowed /// to spend function allowance(address _owner, address _spender ) constant returns (uint256 remaining) { return allowed[_owner][_spender]; } /// @notice `msg.sender` approves `_spender` to send `_amount` tokens on /// its behalf, and then a function is triggered in the contract that is /// being approved, `_spender`. This allows users to use their tokens to /// interact with contracts in one function call instead of two /// @param _spender The address of the contract able to transfer the tokens /// @param _amount The amount of tokens to be approved for transfer /// @return True if the function call was successful function approveAndCall(address _spender, uint256 _amount, bytes _extraData ) returns (bool success) { if (!approve(_spender, _amount)) throw; ApproveAndCallFallBack(_spender).receiveApproval( msg.sender, _amount, this, _extraData ); return true; } /// @dev This function makes it easy to get the total number of tokens /// @return The total number of tokens function totalSupply() constant returns (uint) { return totalSupplyAt(getBlockNumber()); } //////////////// // Query balance and totalSupply in History //////////////// /// @dev Queries the balance of `_owner` at a specific `_blockNumber` /// @param _owner The address from which the balance will be retrieved /// @param _blockNumber The block number when the balance is queried /// @return The balance at `_blockNumber` function balanceOfAt(address _owner, uint _blockNumber) constant returns (uint) { // These next few lines are used when the balance of the token is // requested before a check point was ever created for this token, it // requires that the `parentToken.balanceOfAt` be queried at the // genesis block for that token as this contains initial balance of // this token if ((balances[_owner].length == 0) || (balances[_owner][0].fromBlock > _blockNumber)) { if (address(parentToken) != 0) { return parentToken.balanceOfAt(_owner, min(_blockNumber, parentSnapShotBlock)); } else { // Has no parent return 0; } // This will return the expected balance during normal situations } else { return getValueAt(balances[_owner], _blockNumber); } } /// @notice Total amount of tokens at a specific `_blockNumber`. /// @param _blockNumber The block number when the totalSupply is queried /// @return The total amount of tokens at `_blockNumber` function totalSupplyAt(uint _blockNumber) constant returns(uint) { // These next few lines are used when the totalSupply of the token is // requested before a check point was ever created for this token, it // requires that the `parentToken.totalSupplyAt` be queried at the // genesis block for this token as that contains totalSupply of this // token at this block number. if ((totalSupplyHistory.length == 0) || (totalSupplyHistory[0].fromBlock > _blockNumber)) { if (address(parentToken) != 0) { return parentToken.totalSupplyAt(min(_blockNumber, parentSnapShotBlock)); } else { return 0; } // This will return the expected totalSupply during normal situations } else { return getValueAt(totalSupplyHistory, _blockNumber); } } //////////////// // Clone Token Method //////////////// /// @notice Creates a new clone token with the initial distribution being /// this token at `_snapshotBlock` /// @param _cloneTokenName Name of the clone token /// @param _cloneDecimalUnits Number of decimals of the smallest unit /// @param _cloneTokenSymbol Symbol of the clone token /// @param _snapshotBlock Block when the distribution of the parent token is /// copied to set the initial distribution of the new clone token; /// if the block is zero than the actual block, the current block is used /// @param _transfersEnabled True if transfers are allowed in the clone /// @return The address of the new MiniMeToken Contract function createCloneToken( string _cloneTokenName, uint8 _cloneDecimalUnits, string _cloneTokenSymbol, uint _snapshotBlock, bool _transfersEnabled ) returns(address) { if (_snapshotBlock == 0) _snapshotBlock = getBlockNumber(); MiniMeToken cloneToken = tokenFactory.createCloneToken( this, _snapshotBlock, _cloneTokenName, _cloneDecimalUnits, _cloneTokenSymbol, _transfersEnabled ); cloneToken.changeController(msg.sender); // An event to make the token easy to find on the blockchain NewCloneToken(address(cloneToken), _snapshotBlock); return address(cloneToken); } //////////////// // Generate and destroy tokens //////////////// /// @notice Generates `_amount` tokens that are assigned to `_owner` /// @param _owner The address that will be assigned the new tokens /// @param _amount The quantity of tokens generated /// @return True if the tokens are generated correctly function generateTokens(address _owner, uint _amount ) onlyController returns (bool) { uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount); var previousBalanceTo = balanceOf(_owner); if (previousBalanceTo + _amount < previousBalanceTo) throw; // Check for overflow updateValueAtNow(balances[_owner], previousBalanceTo + _amount); Transfer(0, _owner, _amount); return true; } /// @notice Burns `_amount` tokens from `_owner` /// @param _owner The address that will lose the tokens /// @param _amount The quantity of tokens to burn /// @return True if the tokens are burned correctly function destroyTokens(address _owner, uint _amount ) onlyController returns (bool) { uint curTotalSupply = getValueAt(totalSupplyHistory, getBlockNumber()); if (curTotalSupply < _amount) throw; updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount); var previousBalanceFrom = balanceOf(_owner); if (previousBalanceFrom < _amount) throw; updateValueAtNow(balances[_owner], previousBalanceFrom - _amount); Transfer(_owner, 0, _amount); return true; } //////////////// // Enable tokens transfers //////////////// /// @notice Enables token holders to transfer their tokens freely if true /// @param _transfersEnabled True if transfers are allowed in the clone function enableTransfers(bool _transfersEnabled) onlyController { transfersEnabled = _transfersEnabled; } //////////////// // Internal helper functions to query and set a value in a snapshot array //////////////// /// @dev `getValueAt` retrieves the number of tokens at a given block number /// @param checkpoints The history of values being queried /// @param _block The block number to retrieve the value at /// @return The number of tokens being queried function getValueAt(Checkpoint[] storage checkpoints, uint _block ) constant internal returns (uint) { if (checkpoints.length == 0) return 0; // Shortcut for the actual value if (_block >= checkpoints[checkpoints.length-1].fromBlock) return checkpoints[checkpoints.length-1].value; if (_block < checkpoints[0].fromBlock) return 0; // Binary search of the value in the array uint min = 0; uint max = checkpoints.length-1; while (max > min) { uint mid = (max + min + 1)/ 2; if (checkpoints[mid].fromBlock<=_block) { min = mid; } else { max = mid-1; } } return checkpoints[min].value; } /// @dev `updateValueAtNow` used to update the `balances` map and the /// `totalSupplyHistory` /// @param checkpoints The history of data being updated /// @param _value The new number of tokens function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value ) internal { if ((checkpoints.length == 0) || (checkpoints[checkpoints.length -1].fromBlock < getBlockNumber())) { Checkpoint newCheckPoint = checkpoints[ checkpoints.length++ ]; newCheckPoint.fromBlock = uint128(getBlockNumber()); newCheckPoint.value = uint128(_value); } else { Checkpoint oldCheckPoint = checkpoints[checkpoints.length-1]; oldCheckPoint.value = uint128(_value); } } /// @dev Internal function to determine if an address is a contract /// @param _addr The address being queried /// @return True if `_addr` is a contract function isContract(address _addr) constant internal returns(bool) { uint size; if (_addr == 0) return false; assembly { size := extcodesize(_addr) } return size>0; } /// @dev Helper function to return a min betwen the two uints function min(uint a, uint b) internal returns (uint) { return a < b ? a : b; } /// @notice The fallback function: If the contract's controller has not been /// set to 0, then the `proxyPayment` method is called which relays the /// ether and creates tokens as described in the token controller contract function () payable { if (isContract(controller)) { if (! TokenController(controller).proxyPayment.value(msg.value)(msg.sender)) throw; } else { throw; } } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) onlyController { if (_token == 0x0) { controller.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint balance = token.balanceOf(this); token.transfer(controller, balance); ClaimedTokens(_token, controller, balance); } //////////////// // Events //////////////// event ClaimedTokens(address indexed _token, address indexed _controller, uint _amount); event Transfer(address indexed _from, address indexed _to, uint256 _amount); event NewCloneToken(address indexed _cloneToken, uint _snapshotBlock); event Approval( address indexed _owner, address indexed _spender, uint256 _amount ); } //////////////// // MiniMeTokenFactory //////////////// /// @dev This contract is used to generate clone contracts from a contract. /// In solidity this is the way to create a contract from a contract of the /// same class contract MiniMeTokenFactory { /// @notice Update the DApp by creating a new token with new functionalities /// the msg.sender becomes the controller of this clone token /// @param _parentToken Address of the token being cloned /// @param _snapshotBlock Block of the parent token that will /// determine the initial distribution of the clone token /// @param _tokenName Name of the new token /// @param _decimalUnits Number of decimals of the new token /// @param _tokenSymbol Token Symbol for the new token /// @param _transfersEnabled If true, tokens will be able to be transferred /// @return The address of the new token contract function createCloneToken( address _parentToken, uint _snapshotBlock, string _tokenName, uint8 _decimalUnits, string _tokenSymbol, bool _transfersEnabled ) returns (MiniMeToken) { MiniMeToken newToken = new MiniMeToken( this, _parentToken, _snapshotBlock, _tokenName, _decimalUnits, _tokenSymbol, _transfersEnabled ); newToken.changeController(msg.sender); return newToken; } } /* Copyright 2017, Jarrad Hope (Status Research & Development GmbH) */ contract SNT is MiniMeToken { // @dev SNT constructor just parametrizes the MiniMeIrrevocableVestedToken constructor function SNT(address _tokenFactory) MiniMeToken( _tokenFactory, 0x0, // no parent token 0, // no snapshot block number from parent "Status Network Token", // Token name 18, // Decimals "SNT", // Symbol true // Enable transfers ) {} } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title StatusContribution Contract /// @author Jordi Baylina /// @dev This contract will be the SNT controller during the contribution period. /// This contract will determine the rules during this period. /// Final users will generally not interact directly with this contract. ETH will /// be sent to the SNT token contract. The ETH is sent to this contract and from here, /// ETH is sent to the contribution walled and SNTs are mined according to the defined /// rules. contract StatusContribution is Owned, TokenController { using SafeMath for uint256; uint256 constant public failSafeLimit = 300000 ether; uint256 constant public maxGuaranteedLimit = 30000 ether; uint256 constant public exchangeRate = 10000; uint256 constant public maxGasPrice = 50000000000; uint256 constant public maxCallFrequency = 100; MiniMeToken public SGT; MiniMeToken public SNT; uint256 public startBlock; uint256 public endBlock; address public destEthDevs; address public destTokensDevs; address public destTokensReserve; uint256 public maxSGTSupply; address public destTokensSgt; DynamicCeiling public dynamicCeiling; address public sntController; mapping (address => uint256) public guaranteedBuyersLimit; mapping (address => uint256) public guaranteedBuyersBought; uint256 public totalGuaranteedCollected; uint256 public totalNormalCollected; uint256 public finalizedBlock; uint256 public finalizedTime; mapping (address => uint256) public lastCallBlock; bool public paused; modifier initialized() { require(address(SNT) != 0x0); _; } modifier contributionOpen() { require(getBlockNumber() >= startBlock && getBlockNumber() <= endBlock && finalizedBlock == 0 && address(SNT) != 0x0); _; } modifier notPaused() { require(!paused); _; } function StatusContribution() { paused = false; } /// @notice This method should be called by the owner before the contribution /// period starts This initializes most of the parameters /// @param _snt Address of the SNT token contract /// @param _sntController Token controller for the SNT that will be transferred after /// the contribution finalizes. /// @param _startBlock Block when the contribution period starts /// @param _endBlock The last block that the contribution period is active /// @param _dynamicCeiling Address of the contract that controls the ceiling /// @param _destEthDevs Destination address where the contribution ether is sent /// @param _destTokensReserve Address where the tokens for the reserve are sent /// @param _destTokensSgt Address of the exchanger SGT-SNT where the SNT are sent /// to be distributed to the SGT holders. /// @param _destTokensDevs Address where the tokens for the dev are sent /// @param _sgt Address of the SGT token contract /// @param _maxSGTSupply Quantity of SGT tokens that would represent 10% of status. function initialize( address _snt, address _sntController, uint256 _startBlock, uint256 _endBlock, address _dynamicCeiling, address _destEthDevs, address _destTokensReserve, address _destTokensSgt, address _destTokensDevs, address _sgt, uint256 _maxSGTSupply ) public onlyOwner { // Initialize only once require(address(SNT) == 0x0); SNT = MiniMeToken(_snt); require(SNT.totalSupply() == 0); require(SNT.controller() == address(this)); require(SNT.decimals() == 18); // Same amount of decimals as ETH require(_sntController != 0x0); sntController = _sntController; require(_startBlock >= getBlockNumber()); require(_startBlock < _endBlock); startBlock = _startBlock; endBlock = _endBlock; require(_dynamicCeiling != 0x0); dynamicCeiling = DynamicCeiling(_dynamicCeiling); require(_destEthDevs != 0x0); destEthDevs = _destEthDevs; require(_destTokensReserve != 0x0); destTokensReserve = _destTokensReserve; require(_destTokensSgt != 0x0); destTokensSgt = _destTokensSgt; require(_destTokensDevs != 0x0); destTokensDevs = _destTokensDevs; require(_sgt != 0x0); SGT = MiniMeToken(_sgt); require(_maxSGTSupply >= MiniMeToken(SGT).totalSupply()); maxSGTSupply = _maxSGTSupply; } /// @notice Sets the limit for a guaranteed address. All the guaranteed addresses /// will be able to get SNTs during the contribution period with his own /// specific limit. /// This method should be called by the owner after the initialization /// and before the contribution starts. /// @param _th Guaranteed address /// @param _limit Limit for the guaranteed address. function setGuaranteedAddress(address _th, uint256 _limit) public initialized onlyOwner { require(getBlockNumber() < startBlock); require(_limit > 0 && _limit <= maxGuaranteedLimit); guaranteedBuyersLimit[_th] = _limit; GuaranteedAddress(_th, _limit); } /// @notice If anybody sends Ether directly to this contract, consider he is /// getting SNTs. function () public payable notPaused { proxyPayment(msg.sender); } ////////// // MiniMe Controller functions ////////// /// @notice This method will generally be called by the SNT token contract to /// acquire SNTs. Or directly from third parties that want to acquire SNTs in /// behalf of a token holder. /// @param _th SNT holder where the SNTs will be minted. function proxyPayment(address _th) public payable notPaused initialized contributionOpen returns (bool) { require(_th != 0x0); if (guaranteedBuyersLimit[_th] > 0) { buyGuaranteed(_th); } else { buyNormal(_th); } return true; } function onTransfer(address, address, uint256) public returns (bool) { return false; } function onApprove(address, address, uint256) public returns (bool) { return false; } function buyNormal(address _th) internal { require(tx.gasprice <= maxGasPrice); // Antispam mechanism address caller; if (msg.sender == address(SNT)) { caller = _th; } else { caller = msg.sender; } // Do not allow contracts to game the system require(!isContract(caller)); require(getBlockNumber().sub(lastCallBlock[caller]) >= maxCallFrequency); lastCallBlock[caller] = getBlockNumber(); uint256 toCollect = dynamicCeiling.toCollect(totalNormalCollected); uint256 toFund; if (msg.value <= toCollect) { toFund = msg.value; } else { toFund = toCollect; } totalNormalCollected = totalNormalCollected.add(toFund); doBuy(_th, toFund, false); } function buyGuaranteed(address _th) internal { uint256 toCollect = guaranteedBuyersLimit[_th]; uint256 toFund; if (guaranteedBuyersBought[_th].add(msg.value) > toCollect) { toFund = toCollect.sub(guaranteedBuyersBought[_th]); } else { toFund = msg.value; } guaranteedBuyersBought[_th] = guaranteedBuyersBought[_th].add(toFund); totalGuaranteedCollected = totalGuaranteedCollected.add(toFund); doBuy(_th, toFund, true); } function doBuy(address _th, uint256 _toFund, bool _guaranteed) internal { assert(msg.value >= _toFund); // Not needed, but double check. assert(totalCollected() <= failSafeLimit); if (_toFund > 0) { uint256 tokensGenerated = _toFund.mul(exchangeRate); assert(SNT.generateTokens(_th, tokensGenerated)); destEthDevs.transfer(_toFund); NewSale(_th, _toFund, tokensGenerated, _guaranteed); } uint256 toReturn = msg.value.sub(_toFund); if (toReturn > 0) { // If the call comes from the Token controller, // then we return it to the token Holder. // Otherwise we return to the sender. if (msg.sender == address(SNT)) { _th.transfer(toReturn); } else { msg.sender.transfer(toReturn); } } } // NOTE on Percentage format // Right now, Solidity does not support decimal numbers. (This will change very soon) // So in this contract we use a representation of a percentage that consist in // expressing the percentage in "x per 10**18" // This format has a precision of 16 digits for a percent. // Examples: // 3% = 3*(10**16) // 100% = 100*(10**16) = 10**18 // // To get a percentage of a value we do it by first multiplying it by the percentage in (x per 10^18) // and then divide it by 10**18 // // Y * X(in x per 10**18) // X% of Y = ------------------------- // 100(in x per 10**18) // /// @notice This method will can be called by the owner before the contribution period /// end or by anybody after the `endBlock`. This method finalizes the contribution period /// by creating the remaining tokens and transferring the controller to the configured /// controller. function finalize() public initialized { require(getBlockNumber() >= startBlock); require(msg.sender == owner || getBlockNumber() > endBlock); require(finalizedBlock == 0); // Do not allow termination until all curves revealed. require(dynamicCeiling.allRevealed()); // Allow premature finalization if final limit is reached if (getBlockNumber() <= endBlock) { var (,lastLimit,,) = dynamicCeiling.curves(dynamicCeiling.revealedCurves().sub(1)); require(totalNormalCollected >= lastLimit); } finalizedBlock = getBlockNumber(); finalizedTime = now; uint256 percentageToSgt; if (SGT.totalSupply() >= maxSGTSupply) { percentageToSgt = percent(10); // 10% } else { // // SGT.totalSupply() // percentageToSgt = 10% * ------------------- // maxSGTSupply // percentageToSgt = percent(10).mul(SGT.totalSupply()).div(maxSGTSupply); } uint256 percentageToDevs = percent(20); // 20% // // % To Contributors = 41% + (10% - % to SGT holders) // uint256 percentageToContributors = percent(41).add(percent(10).sub(percentageToSgt)); uint256 percentageToReserve = percent(29); // SNT.totalSupply() -> Tokens minted during the contribution // totalTokens -> Total tokens that should be after the allocation // of devTokens, sgtTokens and reserve // percentageToContributors -> Which percentage should go to the // contribution participants // (x per 10**18 format) // percent(100) -> 100% in (x per 10**18 format) // // percentageToContributors // SNT.totalSupply() = -------------------------- * totalTokens => // percent(100) // // // percent(100) // => totalTokens = ---------------------------- * SNT.totalSupply() // percentageToContributors // uint256 totalTokens = SNT.totalSupply().mul(percent(100)).div(percentageToContributors); // Generate tokens for SGT Holders. // // percentageToReserve // reserveTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensReserve, totalTokens.mul(percentageToReserve).div(percent(100)))); // // percentageToSgt // sgtTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensSgt, totalTokens.mul(percentageToSgt).div(percent(100)))); // // percentageToDevs // devTokens = ----------------------- * totalTokens // percentage(100) // assert(SNT.generateTokens( destTokensDevs, totalTokens.mul(percentageToDevs).div(percent(100)))); SNT.changeController(sntController); Finalized(); } function percent(uint256 p) internal returns (uint256) { return p.mul(10**16); } /// @dev Internal function to determine if an address is a contract /// @param _addr The address being queried /// @return True if `_addr` is a contract function isContract(address _addr) constant internal returns (bool) { if (_addr == 0) return false; uint256 size; assembly { size := extcodesize(_addr) } return (size > 0); } ////////// // Constant functions ////////// /// @return Total tokens issued in weis. function tokensIssued() public constant returns (uint256) { return SNT.totalSupply(); } /// @return Total Ether collected. function totalCollected() public constant returns (uint256) { return totalNormalCollected.add(totalGuaranteedCollected); } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { if (SNT.controller() == address(this)) { SNT.claimTokens(_token); } if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } /// @notice Pauses the contribution if there is any issue function pauseContribution() onlyOwner { paused = true; } /// @notice Resumes the contribution function resumeContribution() onlyOwner { paused = false; } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event NewSale(address indexed _th, uint256 _amount, uint256 _tokens, bool _guaranteed); event GuaranteedAddress(address indexed _th, uint256 _limit); event Finalized(); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title ContributionWallet Contract /// @author Jordi Baylina /// @dev This contract will be hold the Ether during the contribution period. /// The idea of this contract is to avoid recycling Ether during the contribution /// period. So all the ETH collected will be locked here until the contribution /// period ends // @dev Contract to hold sale raised funds during the sale period. // Prevents attack in which the Aragon Multisig sends raised ether // to the sale contract to mint tokens to itself, and getting the // funds back immediately. contract ContributionWallet { // Public variables address public multisig; uint256 public endBlock; StatusContribution public contribution; // @dev Constructor initializes public variables // @param _multisig The address of the multisig that will receive the funds // @param _endBlock Block after which the multisig can request the funds // @param _contribution Address of the StatusContribution contract function ContributionWallet(address _multisig, uint256 _endBlock, address _contribution) { require(_multisig != 0x0); require(_contribution != 0x0); require(_endBlock != 0 && _endBlock <= 4000000); multisig = _multisig; endBlock = _endBlock; contribution = StatusContribution(_contribution); } // @dev Receive all sent funds without any further logic function () public payable {} // @dev Withdraw function sends all the funds to the wallet if conditions are correct function withdraw() public { require(msg.sender == multisig); // Only the multisig can request it require(block.number > endBlock || // Allow after end block contribution.finalizedBlock() != 0); // Allow when sale is finalized multisig.transfer(this.balance); } } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title DevTokensHolder Contract /// @author Jordi Baylina /// @dev This contract will hold the tokens of the developers. /// Tokens will not be able to be collected until 6 months after the contribution /// period ends. And it will be increasing linearly until 2 years. // collectable tokens // | _/-------- vestedTokens rect // | _/ // | _/ // | _/ // | _/ // | _/ // | _/ // | _/ // | | // | . | // | . | // | . | // +===+======+--------------+----------> time // Contrib 6 Months 24 Months // End contract DevTokensHolder is Owned { using SafeMath for uint256; uint256 collectedTokens; StatusContribution contribution; MiniMeToken snt; function DevTokensHolder(address _owner, address _contribution, address _snt) { owner = _owner; contribution = StatusContribution(_contribution); snt = MiniMeToken(_snt); } /// @notice The Dev (Owner) will call this method to extract the tokens function collectTokens() public onlyOwner { uint256 balance = snt.balanceOf(address(this)); uint256 total = collectedTokens.add(balance); uint256 finalizedTime = contribution.finalizedTime(); require(finalizedTime > 0 && getTime() > finalizedTime.add(months(6))); uint256 canExtract = total.mul(getTime().sub(finalizedTime)).div(months(24)); canExtract = canExtract.sub(collectedTokens); if (canExtract > balance) { canExtract = balance; } collectedTokens = collectedTokens.add(canExtract); assert(snt.transfer(owner, canExtract)); TokensWithdrawn(owner, canExtract); } function months(uint256 m) internal returns (uint256) { return m.mul(30 days); } function getTime() internal returns (uint256) { return now; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { require(_token != address(snt)); if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event TokensWithdrawn(address indexed _holder, uint256 _amount); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title SGTExchanger Contract /// @author Jordi Baylina /// @dev This contract will be used to distribute SNT between SGT holders. /// SGT token is not transferable, and we just keep an accounting between all tokens /// deposited and the tokens collected. /// The controllerShip of SGT should be transferred to this contract before the /// contribution period starts. contract SGTExchanger is TokenController, Owned { using SafeMath for uint256; mapping (address => uint256) public collected; uint256 public totalCollected; MiniMeToken public sgt; MiniMeToken public snt; StatusContribution public statusContribution; function SGTExchanger(address _sgt, address _snt, address _statusContribution) { sgt = MiniMeToken(_sgt); snt = MiniMeToken(_snt); statusContribution = StatusContribution(_statusContribution); } /// @notice This method should be called by the SGT holders to collect their /// corresponding SNTs function collect() public { uint256 finalizedBlock = statusContribution.finalizedBlock(); require(finalizedBlock != 0); require(getBlockNumber() > finalizedBlock); uint256 total = totalCollected.add(snt.balanceOf(address(this))); uint256 balance = sgt.balanceOfAt(msg.sender, finalizedBlock); // First calculate how much correspond to him uint256 amount = total.mul(balance).div(sgt.totalSupplyAt(finalizedBlock)); // And then subtract the amount already collected amount = amount.sub(collected[msg.sender]); require(amount > 0); // Notify the user that there are no tokens to exchange totalCollected = totalCollected.add(amount); collected[msg.sender] = collected[msg.sender].add(amount); assert(snt.transfer(msg.sender, amount)); TokensCollected(msg.sender, amount); } function proxyPayment(address) public payable returns (bool) { throw; } function onTransfer(address, address, uint256) public returns (bool) { return false; } function onApprove(address, address, uint256) public returns (bool) { return false; } ////////// // Testing specific methods ////////// /// @notice This function is overridden by the test Mocks. function getBlockNumber() internal constant returns (uint256) { return block.number; } ////////// // Safety Method ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { require(_token != address(snt)); if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event TokensCollected(address indexed _holder, uint256 _amount); } /* Copyright 2017, Jordi Baylina This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /// @title SNTPlaceholder Contract /// @author Jordi Baylina /// @dev The SNTPlaceholder contract will take control over the SNT after the contribution /// is finalized and before the Status Network is deployed. /// The contract allows for SNT transfers and transferFrom and implements the /// logic for transferring control of the token to the network when the offering /// asks it to do so. contract SNTPlaceHolder is TokenController, Owned { using SafeMath for uint256; MiniMeToken public snt; StatusContribution public contribution; uint256 public activationTime; address public sgtExchanger; /// @notice Constructor /// @param _owner Trusted owner for this contract. /// @param _snt SNT token contract address /// @param _contribution StatusContribution contract address /// @param _sgtExchanger SGT-SNT Exchange address. (During the first week /// only this exchanger will be able to move tokens) function SNTPlaceHolder(address _owner, address _snt, address _contribution, address _sgtExchanger) { owner = _owner; snt = MiniMeToken(_snt); contribution = StatusContribution(_contribution); sgtExchanger = _sgtExchanger; } /// @notice The owner of this contract can change the controller of the SNT token /// Please, be sure that the owner is a trusted agent or 0x0 address. /// @param _newController The address of the new controller function changeController(address _newController) public onlyOwner { snt.changeController(_newController); ControllerChanged(_newController); } ////////// // MiniMe Controller Interface functions ////////// // In between the offering and the network. Default settings for allowing token transfers. function proxyPayment(address) public payable returns (bool) { return false; } function onTransfer(address _from, address, uint256) public returns (bool) { return transferable(_from); } function onApprove(address _from, address, uint256) public returns (bool) { return transferable(_from); } function transferable(address _from) internal returns (bool) { // Allow the exchanger to work from the beginning if (activationTime == 0) { uint256 f = contribution.finalizedTime(); if (f > 0) { activationTime = f.add(1 weeks); } else { return false; } } return (getTime() > activationTime) || (_from == sgtExchanger); } ////////// // Testing specific methods ////////// /// @notice This function is overrided by the test Mocks. function getTime() internal returns (uint256) { return now; } ////////// // Safety Methods ////////// /// @notice This method can be used by the controller to extract mistakenly /// sent tokens to this contract. /// @param _token The address of the token contract that you want to recover /// set to 0 in case you want to extract ether. function claimTokens(address _token) public onlyOwner { if (snt.controller() == address(this)) { snt.claimTokens(_token); } if (_token == 0x0) { owner.transfer(this.balance); return; } ERC20Token token = ERC20Token(_token); uint256 balance = token.balanceOf(this); token.transfer(owner, balance); ClaimedTokens(_token, owner, balance); } event ClaimedTokens(address indexed _token, address indexed _controller, uint256 _amount); event ControllerChanged(address indexed _newController); }