File size: 17,307 Bytes
dec6d5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
pragma solidity ^0.4.18;
contract Ownable {
address public owner;
address public newOwnerCandidate;
event OwnershipRequested(address indexed by, address indexed to);
event OwnershipTransferred(address indexed from, address indexed to);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyOwnerCandidate() {
require(msg.sender == newOwnerCandidate);
_;
}
function requestOwnershipTransfer(address _newOwnerCandidate) external onlyOwner {
require(_newOwnerCandidate != address(0));
newOwnerCandidate = _newOwnerCandidate;
OwnershipRequested(msg.sender, newOwnerCandidate);
}
function acceptOwnership() external onlyOwnerCandidate {
address previousOwner = owner;
owner = newOwnerCandidate;
newOwnerCandidate = address(0);
OwnershipTransferred(previousOwner, owner);
}
}
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
require(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a);
return c;
}
function max64(uint64 a, uint64 b) internal pure returns (uint64) {
return a >= b ? a : b;
}
function min64(uint64 a, uint64 b) internal pure returns (uint64) {
return a < b ? a : b;
}
function max256(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
function min256(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function toPower2(uint256 a) internal pure returns (uint256) {
return mul(a, a);
}
function sqrt(uint256 a) internal pure returns (uint256) {
uint256 c = (a + 1) / 2;
uint256 b = a;
while (c < b) {
b = c;
c = (a / c + c) / 2;
}
return b;
}
}
contract ERC20 {
uint public totalSupply;
function balanceOf(address _owner) constant public 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 constant returns (uint remaining);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract BasicToken is ERC20 {
using SafeMath for uint256;
uint256 public totalSupply;
mapping (address => mapping (address => uint256)) allowed;
mapping (address => uint256) balances;
event Approval(address indexed owner, address indexed spender, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
function approve(address _spender, uint256 _value) public returns (bool) {
if ((_value != 0) && (allowed[msg.sender][_spender] != 0)) {
revert();
}
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant public returns (uint256 remaining) {
return allowed[_owner][_spender];
}
function balanceOf(address _owner) constant public returns (uint256 balance) {
return balances[_owner];
}
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
uint256 _allowance = allowed[_from][msg.sender];
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = _allowance.sub(_value);
Transfer(_from, _to, _value);
return true;
}
}
contract ERC223Receiver {
function tokenFallback(address _sender, uint _value, bytes _data) external returns (bool ok);
}
contract ERC677 is ERC20 {
function transferAndCall(address to, uint value, bytes data) public returns (bool ok);
event TransferAndCall(address indexed from, address indexed to, uint value, bytes data);
}
contract Standard677Token is ERC677, BasicToken {
function transferAndCall(address _to, uint _value, bytes _data) public returns (bool) {
require(super.transfer(_to, _value));
TransferAndCall(msg.sender, _to, _value, _data);
if (isContract(_to)) return contractFallback(_to, _value, _data);
return true;
}
function contractFallback(address _to, uint _value, bytes _data) private returns (bool) {
ERC223Receiver receiver = ERC223Receiver(_to);
require(receiver.tokenFallback(msg.sender, _value, _data));
return true;
}
function isContract(address _addr) private constant returns (bool is_contract) {
uint length;
assembly { length := extcodesize(_addr) }
return length > 0;
}
}
contract TokenHolder is Ownable {
function transferAnyERC20Token(address _tokenAddress, uint256 _amount) public onlyOwner returns (bool success) {
return ERC20(_tokenAddress).transfer(owner, _amount);
}
}
contract ColuLocalCurrency is Ownable, Standard677Token, TokenHolder {
using SafeMath for uint256;
string public name;
string public symbol;
uint8 public decimals;
string public tokenURI;
event TokenURIChanged(string newTokenURI);
function ColuLocalCurrency(string _name, string _symbol, uint8 _decimals, uint256 _totalSupply, string _tokenURI) public {
require(_totalSupply != 0);
require(bytes(_name).length != 0);
require(bytes(_symbol).length != 0);
totalSupply = _totalSupply;
name = _name;
symbol = _symbol;
decimals = _decimals;
tokenURI = _tokenURI;
balances[msg.sender] = totalSupply;
}
function setTokenURI(string _tokenURI) public onlyOwner {
tokenURI = _tokenURI;
TokenURIChanged(_tokenURI);
}
}
contract Standard223Receiver is ERC223Receiver {
Tkn tkn;
struct Tkn {
address addr;
address sender;
uint256 value;
}
bool __isTokenFallback;
modifier tokenPayable {
require(__isTokenFallback);
_;
}
function tokenFallback(address _sender, uint _value, bytes _data) external returns (bool ok) {
if (!supportsToken(msg.sender)) {
return false;
}
tkn = Tkn(msg.sender, _sender, _value);
__isTokenFallback = true;
if (!address(this).delegatecall(_data)) {
__isTokenFallback = false;
return false;
}
__isTokenFallback = false;
return true;
}
function supportsToken(address token) public constant returns (bool);
}
contract TokenOwnable is Standard223Receiver, Ownable {
modifier onlyTokenOwner() {
require(tkn.sender == owner);
_;
}
}
contract EllipseMarketMaker is TokenOwnable {
uint256 public constant PRECISION = 10 ** 18;
ERC20 public token1;
ERC20 public token2;
uint256 public R1;
uint256 public R2;
uint256 public S1;
uint256 public S2;
bool public operational;
bool public openForPublic;
address public mmLib;
function EllipseMarketMaker(address _mmLib, address _token1, address _token2) public {
require(_mmLib != address(0));
bytes4 sig = 0x6dd23b5b;
uint256 argsSize = 3 * 32;
uint256 dataSize = 4 + argsSize;
bytes memory m_data = new bytes(dataSize);
assembly {
mstore(add(m_data, 0x20), sig)
mstore(add(m_data, 0x24), _mmLib)
mstore(add(m_data, 0x44), _token1)
mstore(add(m_data, 0x64), _token2)
}
require(_mmLib.delegatecall(m_data));
}
function supportsToken(address token) public constant returns (bool) {
return (token1 == token || token2 == token);
}
function() public {
address _mmLib = mmLib;
if (msg.data.length > 0) {
assembly {
calldatacopy(0xff, 0, calldatasize)
let retVal := delegatecall(gas, _mmLib, 0xff, calldatasize, 0, 0x20)
switch retVal case 0 { revert(0,0) } default { return(0, 0x20) }
}
}
}
}
contract MarketMaker is ERC223Receiver {
function getCurrentPrice() public constant returns (uint _price);
function change(address _fromToken, uint _amount, address _toToken) public returns (uint _returnAmount);
function change(address _fromToken, uint _amount, address _toToken, uint _minReturn) public returns (uint _returnAmount);
function change(address _toToken) public returns (uint _returnAmount);
function change(address _toToken, uint _minReturn) public returns (uint _returnAmount);
function quote(address _fromToken, uint _amount, address _toToken) public constant returns (uint _returnAmount);
function openForPublicTrade() public returns (bool success);
function isOpenForPublic() public returns (bool success);
event Change(address indexed fromToken, uint inAmount, address indexed toToken, uint returnAmount, address indexed account);
}
contract IEllipseMarketMaker is MarketMaker {
uint256 public constant PRECISION = 10 ** 18;
ERC20 public token1;
ERC20 public token2;
uint256 public R1;
uint256 public R2;
uint256 public S1;
uint256 public S2;
bool public operational;
bool public openForPublic;
address public mmLib;
function supportsToken(address token) public constant returns (bool);
function calcReserve(uint256 _R1, uint256 _S1, uint256 _S2) public pure returns (uint256);
function validateReserves() public view returns (bool);
function withdrawExcessReserves() public returns (uint256);
function initializeAfterTransfer() public returns (bool);
function initializeOnTransfer() public returns (bool);
function getPrice(uint256 _R1, uint256 _R2, uint256 _S1, uint256 _S2) public constant returns (uint256);
}
contract CurrencyFactory is Standard223Receiver, TokenHolder {
struct CurrencyStruct {
string name;
uint8 decimals;
uint256 totalSupply;
address owner;
address mmAddress;
}
mapping (address => CurrencyStruct) public currencyMap;
address public clnAddress;
address public mmLibAddress;
address[] public tokens;
event MarketOpen(address indexed marketMaker);
event TokenCreated(address indexed token, address indexed owner);
modifier tokenIssuerOnly(address token, address owner) {
require(currencyMap[token].owner == owner);
_;
}
modifier CLNOnly() {
require(msg.sender == clnAddress);
_;
}
modifier marketClosed(address _token) {
require(!MarketMaker(currencyMap[_token].mmAddress).isOpenForPublic());
_;
}
modifier marketOpen(address _token) {
require(MarketMaker(currencyMap[_token].mmAddress).isOpenForPublic());
_;
}
function CurrencyFactory(address _mmLib, address _clnAddress) public {
require(_mmLib != address(0));
require(_clnAddress != address(0));
mmLibAddress = _mmLib;
clnAddress = _clnAddress;
}
function createCurrency(string _name,
string _symbol,
uint8 _decimals,
uint256 _totalSupply,
string _tokenURI) public
returns (address) {
ColuLocalCurrency subToken = new ColuLocalCurrency(_name, _symbol, _decimals, _totalSupply, _tokenURI);
EllipseMarketMaker newMarketMaker = new EllipseMarketMaker(mmLibAddress, clnAddress, subToken);
require(subToken.transfer(newMarketMaker, _totalSupply));
require(IEllipseMarketMaker(newMarketMaker).initializeAfterTransfer());
currencyMap[subToken] = CurrencyStruct({ name: _name, decimals: _decimals, totalSupply: _totalSupply, mmAddress: newMarketMaker, owner: msg.sender});
tokens.push(subToken);
TokenCreated(subToken, msg.sender);
return subToken;
}
function createCurrency(string _name,
string _symbol,
uint8 _decimals,
uint256 _totalSupply) public
returns (address) {
return createCurrency(_name, _symbol, _decimals, _totalSupply, '');
}
function insertCLNtoMarketMaker(address _token,
uint256 _clnAmount) public
tokenIssuerOnly(_token, msg.sender)
returns (uint256 _subTokenAmount) {
require(_clnAmount > 0);
address marketMakerAddress = getMarketMakerAddressFromToken(_token);
require(ERC20(clnAddress).transferFrom(msg.sender, this, _clnAmount));
require(ERC20(clnAddress).approve(marketMakerAddress, _clnAmount));
_subTokenAmount = IEllipseMarketMaker(marketMakerAddress).change(clnAddress, _clnAmount, _token);
require(ERC20(_token).transfer(msg.sender, _subTokenAmount));
}
function insertCLNtoMarketMaker(address _token) public
tokenPayable
CLNOnly
tokenIssuerOnly(_token, tkn.sender)
returns (uint256 _subTokenAmount) {
address marketMakerAddress = getMarketMakerAddressFromToken(_token);
require(ERC20(clnAddress).approve(marketMakerAddress, tkn.value));
_subTokenAmount = IEllipseMarketMaker(marketMakerAddress).change(clnAddress, tkn.value, _token);
require(ERC20(_token).transfer(tkn.sender, _subTokenAmount));
}
function extractCLNfromMarketMaker(address _token,
uint256 _ccAmount) public
tokenIssuerOnly(_token, msg.sender)
returns (uint256 _clnTokenAmount) {
address marketMakerAddress = getMarketMakerAddressFromToken(_token);
require(ERC20(_token).transferFrom(msg.sender, this, _ccAmount));
require(ERC20(_token).approve(marketMakerAddress, _ccAmount));
_clnTokenAmount = IEllipseMarketMaker(marketMakerAddress).change(_token, _ccAmount, clnAddress);
require(ERC20(clnAddress).transfer(msg.sender, _clnTokenAmount));
}
function extractCLNfromMarketMaker() public
tokenPayable
tokenIssuerOnly(msg.sender, tkn.sender)
returns (uint256 _clnTokenAmount) {
address marketMakerAddress = getMarketMakerAddressFromToken(msg.sender);
require(ERC20(msg.sender).approve(marketMakerAddress, tkn.value));
_clnTokenAmount = IEllipseMarketMaker(marketMakerAddress).change(msg.sender, tkn.value, clnAddress);
require(ERC20(clnAddress).transfer(tkn.sender, _clnTokenAmount));
}
function openMarket(address _token) public
tokenIssuerOnly(_token, msg.sender)
returns (bool) {
address marketMakerAddress = getMarketMakerAddressFromToken(_token);
require(MarketMaker(marketMakerAddress).openForPublicTrade());
Ownable(marketMakerAddress).requestOwnershipTransfer(msg.sender);
Ownable(_token).requestOwnershipTransfer(msg.sender);
MarketOpen(marketMakerAddress);
return true;
}
function supportsToken(address _token) public constant returns (bool) {
return (clnAddress == _token || currencyMap[_token].totalSupply > 0);
}
function setTokenURI(address _token, string _tokenURI) public
tokenIssuerOnly(_token, msg.sender)
marketClosed(_token)
returns (bool) {
ColuLocalCurrency(_token).setTokenURI(_tokenURI);
return true;
}
function getMarketMakerAddressFromToken(address _token) public constant returns (address _marketMakerAddress) {
_marketMakerAddress = currencyMap[_token].mmAddress;
require(_marketMakerAddress != address(0));
}
} |