SHARE
TWEET

Untitled

a guest Oct 18th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^ 0.5.11;
  2. contract ColorToken{
  3.     BondContract public bondContract;
  4.     ResolveContract public resolveContract;
  5.     address lastGateway;
  6.     address communityResolve;
  7.     address THIS = address(this);
  8.  
  9.     string public name = "Color Token";
  10.     string public symbol = "RGB";
  11.     uint8 constant public decimals = 18;
  12.     uint _totalSupply;
  13.     uint public _totalBonds;
  14.     uint masternodeFee = 10; // 10%
  15.  
  16.     mapping(address => address payable) proxy;
  17.     PyramidProxy[] proxies;
  18.     mapping(address => address) proxyOwner;
  19.  
  20.     mapping(address => uint256) redBonds;
  21.     mapping(address => uint256) greenBonds;
  22.     mapping(address => uint256) blueBonds;
  23.  
  24.     mapping(address => uint256) redResolves;
  25.     mapping(address => uint256) greenResolves;
  26.     mapping(address => uint256) blueResolves;
  27.  
  28.     mapping(address => mapping(address => uint)) approvals;
  29.  
  30.     mapping(address => address) gateway;
  31.     mapping(address => uint256) public pocket;
  32.     mapping(address => uint256) public upline;
  33.     mapping(address => address) public minecart;
  34.  
  35.     mapping(address => address) public votingFor;
  36.     mapping(address => uint256) public votesFor;
  37.    
  38.     constructor(address _bondContract) public{
  39.         bondContract = BondContract( _bondContract );
  40.         resolveContract = ResolveContract( bondContract.getResolveContract() );
  41.         communityResolve = msg.sender;
  42.         lastGateway = THIS;
  43.     }
  44.  
  45.     function totalSupply() public view returns (uint256) {
  46.         return _totalSupply;
  47.     }
  48.     function balanceOf(address addr) public view returns (uint balance) {
  49.         return PyramidProxy( proxy[addr] ).getBalance();
  50.     }
  51.     function max1(uint x) internal pure returns (uint){
  52.         if(x>1e12)
  53.             return 1e12;
  54.         else
  55.             return x;
  56.     }
  57.     /*function proxy(address addr) returns(PyramidProxy){
  58.         proxies[proxyID[addr]]
  59.     }*/
  60.     function ensureProxy(address addr) internal{
  61.         if( proxy[addr] == 0x0000000000000000000000000000000000000000){
  62.             //PyramidProxy p = new PyramidProxy( this, bondContract );
  63.             proxies.push( new PyramidProxy( this, bondContract ) );
  64.             proxy[addr] = address( proxies[proxies.length-1] );
  65.             proxyOwner[ proxy[addr] ] = addr;
  66.         }
  67.     }
  68.     event Buy( address indexed addr, uint256 spent, uint256 bonds, uint red, uint green, uint blue);
  69.     function buy(uint _red, uint _green, uint _blue) payable public returns(uint bondsCreated){
  70.         return buy( msg.value, _red, _green, _blue, true);
  71.     }
  72.     function buy(uint ETH, uint _red, uint _green, uint _blue, bool EMIT) payable public returns(uint bondsCreated){
  73.         address sender = msg.sender;
  74.         ensureProxy(sender);
  75.         _red = max1(_red);
  76.         _green = max1(_green);
  77.         _blue = max1(_blue);
  78.         uint fee = ETH / masternodeFee;
  79.         uint eth4Bonds = ETH - fee;
  80.  
  81.         address payable proxyAddr = proxyAddress(sender);
  82.         proxyAddr.transfer(eth4Bonds);
  83.         uint createdBonds = PyramidProxy( proxyAddr ).buy();/**/
  84.         _totalBonds += createdBonds;
  85.         bondsAddColor(sender,createdBonds, _red, _green, _blue);
  86.  
  87.         pocket[ gateway[sender] ] += fee/2;
  88.         upline[ gateway[sender] ] += (fee/2)-(fee/2)/5;
  89.         pocket[ THIS ] += (fee/2)/5;
  90.         pushMinecart();
  91.  
  92.         if( EMIT ){
  93.             emit Buy( sender, ETH, createdBonds, _red, _green,  _blue);
  94.         }
  95.  
  96.         if( bondBalance(sender) > 10000*1e12 ){
  97.             lastGateway = sender;  
  98.         }
  99.         return createdBonds;/**/
  100.     }
  101.  
  102.     function pushMinecart() public{
  103.         pushMinecart(msg.sender);
  104.     }
  105.  
  106.     function pushMinecart(address addr) public{
  107.         if(gateway[addr] == 0x0000000000000000000000000000000000000000 || bondBalance( gateway[addr] ) < 10000*1e12){
  108.             gateway[addr] = lastGateway;
  109.         }
  110.         if( minecart[addr] == THIS || minecart[addr] == 0x0000000000000000000000000000000000000000){
  111.             minecart[addr] = addr;
  112.         }else{
  113.             minecart[addr] = gateway[ minecart[addr] ];
  114.         }
  115.         address dropOff = minecart[addr];
  116.         uint up = upline[ dropOff ];
  117.         pocket[ dropOff ] +=  up/5;
  118.         upline[ gateway[dropOff] ] += up - up/5 ;
  119.         upline[ dropOff ] = 0;/**/
  120.     }
  121.  
  122.     event Sell( address indexed addr, uint256 bondsSold, uint256 resolves, uint red, uint green, uint blue);
  123.     function sell(uint amountToSell) public{
  124.         address sender = sender;
  125.         uint bondsBefore = bondBalance(sender);
  126.         (uint _red, uint _green, uint _blue) = RGB_bondRatio();
  127.         uint mintedResolves = PyramidProxy( proxyAddress(sender) ).sell(amountToSell);
  128.         _totalBonds -= amountToSell;
  129.         /*uint _red = redBonds[sender] / bondsBefore;
  130.         uint _green = greenBonds[sender] / bondsBefore;
  131.         uint _blue = blueBonds[sender] / bondsBefore;*/
  132.         resolvesAddColor(sender, mintedResolves, _red, _green, _blue);
  133.         votesFor[ votingFor[sender] ] += mintedResolves;
  134.         _totalSupply += mintedResolves;
  135.         emit Sell(sender, amountToSell, mintedResolves, _red, _green, _blue );
  136.  
  137.         bondsThinColor(msg.sender, bondsBefore - amountToSell, bondsBefore);
  138.         pushMinecart();
  139.     }
  140.     function stake(uint amountToStake) public{
  141.         address sender = msg.sender;
  142.         PyramidProxy( proxyAddress(sender) ).stake( amountToStake );
  143.         colorShift(sender, address(bondContract), amountToStake );
  144.         pushMinecart();
  145.     }
  146.     function unstake(uint amountToUnstake) public{
  147.         address sender = msg.sender;
  148.         PyramidProxy(proxy[sender]).unstake( amountToUnstake );
  149.         colorShift(address(bondContract), sender, amountToUnstake );
  150.         pushMinecart();
  151.     }/**/
  152.     function reinvest(uint amount) public{
  153.         address sender = msg.sender;
  154.         //address proxyAddr = proxyAddress( sender );
  155.         //uint bal = bondContract.balanceOf( proxyAddr );
  156.         /*uint red = redBonds[sender] / bal;
  157.         uint green = greenBonds[sender] / bal;
  158.         uint blue = blueBonds[sender] / bal;*/
  159.         (uint red, uint green, uint blue) = RGB_bondRatio();
  160.  
  161.         uint createdBonds;
  162.         uint dissolvedResolves;
  163.         (createdBonds, dissolvedResolves) = PyramidProxy( proxyAddress(sender) ).reinvest(amount);
  164.        
  165.         createdBonds += buy( pocket[sender], red, green, blue, false);
  166.         _totalBonds += createdBonds;
  167.         pocket[sender] = 0;
  168.  
  169.         bondsAddColor(sender, createdBonds, red, green, blue);
  170.         // update core contract's Resolve color
  171.         address pyrAddr = address(bondContract);
  172.         uint currentResolves = resolveContract.balanceOf( pyrAddr );
  173.         resolvesThinColor(pyrAddr, currentResolves, currentResolves + dissolvedResolves);
  174.         pushMinecart();
  175.     }
  176.     function withdraw(uint amount) public{
  177.         address payable sender = msg.sender;
  178.         uint dissolvedResolves = PyramidProxy(proxy[sender]).withdraw(amount);
  179.         uint earned = pocket[sender];
  180.         pocket[sender] = 0;
  181.         sender.transfer( earned );
  182.         // update core contract's Resolve color
  183.         address pyrAddr = address(bondContract);
  184.         uint currentResolves = resolveContract.balanceOf( pyrAddr );
  185.         resolvesThinColor(pyrAddr, currentResolves, currentResolves + dissolvedResolves);
  186.         pushMinecart();
  187.     }
  188.  
  189.     function proxyAddress(address addr) public view returns(address payable addressOfProxxy){
  190.         return address(proxy[addr]);//address( proxies[ proxyID[addr] ]  );
  191.     }
  192.     function getProxyOwner(address proxyAddr) public view returns(address ownerAddress){
  193.         return proxyOwner[proxyAddr];
  194.     }
  195.     function unbindResolves(uint amount) public {
  196.         address sender = msg.sender;
  197.         uint totalResolves = resolveContract.balanceOf( proxyAddress(sender) );
  198.         resolvesThinColor( sender, totalResolves - amount, totalResolves);
  199.         PyramidProxy( proxyAddress(sender) ).transfer(sender, amount);
  200.     }
  201.     function setVotingFor(address candidate) public {
  202.         address sender = msg.sender;
  203.         //Contracts can't vote for anyone. Because then people would just evenly split the pool fund most of the time
  204.         require( !isContract(sender) );//This could be enhanced, but this is a barebones demonstration of the powhr of resolve tokens
  205.         uint voteWeight = balanceOf(sender);
  206.         votesFor[ votingFor[ sender ] ] -= voteWeight;
  207.         votingFor[ sender ] = candidate;
  208.         votesFor[ candidate ] += voteWeight;
  209.     }
  210.     function assertNewCommunityResolve(address candidate) public {
  211.         if( votesFor[candidate] > votesFor[communityResolve] ){
  212.             communityResolve = candidate;
  213.         }
  214.     }
  215.  
  216.     function GET_FUNDED() public{
  217.         if(msg.sender == communityResolve){
  218.             uint money_gotten = pocket[ THIS ];
  219.             pocket[ THIS ] = 0;
  220.             msg.sender.transfer(money_gotten);
  221.             pushMinecart();
  222.         }
  223.     }
  224.  
  225.     // Function that is called when a user or another contract wants to transfer funds .
  226.     function transfer(address _to, uint _value, bytes memory _data) public returns (bool success) {
  227.         if( balanceOf(msg.sender) < _value ) revert();
  228.         if( isContract(_to) ){
  229.             return transferToContract(_to, _value, _data);
  230.         }else{
  231.             return transferToAddress(_to, _value, _data);
  232.         }
  233.     }
  234.  
  235.     // Standard function transfer similar to ERC20 transfer with no _data .
  236.     // Added due to backwards compatibility reasons .
  237.     function transfer(address _to, uint _value) public returns (bool success) {
  238.         if (balanceOf(msg.sender) < _value) revert();
  239.         //standard function transfer similar to ERC20 transfer with no _data
  240.         //added due to backwards compatibility reasons
  241.         bytes memory empty;
  242.         if(isContract(_to)){
  243.             return transferToContract(_to, _value, empty);
  244.         }else{
  245.             return transferToAddress(_to, _value, empty);
  246.         }
  247.     }
  248.  
  249.     //assemble the given address bytecode. If bytecode exists then the _addr is a contract.
  250.     function isContract(address _addr) public view returns (bool is_contract) {
  251.         uint length = 0;
  252.         assembly {
  253.             //retrieve the size of the code on target address, this needs assembly
  254.             length := extcodesize(_addr)
  255.         }
  256.         if(length>0) {
  257.             return true;
  258.         }else {
  259.             return false;
  260.         }
  261.     }
  262.  
  263.     //function that is called when transaction target is an address
  264.     function transferToAddress(address _to, uint _value, bytes memory _data) private returns (bool success) {
  265.         moveTokens(msg.sender,_to,_value);
  266.         return true;
  267.     }
  268.  
  269.     //function that is called when transaction target is a contract
  270.     function transferToContract(address _to, uint _value, bytes memory _data) private returns (bool success) {
  271.         moveTokens(msg.sender,_to,_value);
  272.         ERC223ReceivingContract reciever = ERC223ReceivingContract(_to);
  273.         reciever.tokenFallback(msg.sender, _value, _data);
  274.         return true;
  275.     }
  276.  
  277.     function moveTokens(address _from, address _to, uint _amount) internal{
  278.         colorShift(_from, _to, _amount);
  279.         ensureProxy(_to);
  280.        
  281.         votesFor[ votingFor[_from] ] -= _amount;
  282.         votesFor[ votingFor[_to] ] += _amount;
  283.  
  284.         PyramidProxy(proxy[_from]).transfer( proxyAddress(_to), _amount );
  285.     }
  286.     function RGB_scale(uint r, uint g, uint b, uint numerator, uint denominator) internal pure returns(uint,uint,uint){
  287.         return (r * numerator / denominator, g * numerator / denominator, b * numerator / denominator );
  288.     }
  289.     function colorShift(address _from, address _to, uint _amount) internal{
  290.         //uint bal = proxy[_from].getBalance();
  291.         /*uint red_ratio = redResolves[_from] * _amount / bal;
  292.         uint green_ratio = greenResolves[_from] * _amount / bal;
  293.         uint blue_ratio = blueResolves[_from] * _amount / bal;*/
  294.         (uint red_ratio, uint green_ratio, uint blue_ratio) = RGB_scale(redResolves[_from], greenResolves[_from], blueResolves[_from], _amount, PyramidProxy(proxy[_from]).getBalance() );
  295.         redResolves[_from] -= red_ratio;
  296.         greenResolves[_from] -= green_ratio;
  297.         blueResolves[_from] -= blue_ratio;
  298.         redResolves[_to] += red_ratio;
  299.         greenResolves[_to] += green_ratio;
  300.         blueResolves[_to] += blue_ratio;
  301.     }
  302.  
  303.     function allowance(address src, address guy) public view returns (uint) {
  304.         return approvals[src][guy];
  305.     }
  306.  
  307.     function transferFrom(address src, address dst, uint wad) public returns (bool){
  308.         address sender = msg.sender;
  309.         require(approvals[src][sender] >=  wad);
  310.         require(PyramidProxy(proxy[src]).getBalance() >=  wad);
  311.         if (src != sender) {
  312.             approvals[src][sender] -=  wad;
  313.         }
  314.         moveTokens(src,dst,wad);
  315.  
  316.         return true;
  317.     }
  318.     event Approval(address indexed src, address indexed guy, uint wad);
  319.         address sender = msg.sender;
  320.     function approve(address guy, uint wad) public returns (bool) {
  321.         approvals[sender][guy] = wad;
  322.  
  323.         emit Approval(sender, guy, wad);
  324.  
  325.         return true;
  326.     }
  327.  
  328.     function resolvesAddColor(address addr, uint amount , uint red, uint green, uint blue) internal{
  329.         redResolves[addr] += red * amount;
  330.         greenResolves[addr] += green * amount;
  331.         blueResolves[addr] += blue * amount;
  332.     }
  333.     function bondsAddColor(address addr, uint amount , uint red, uint green, uint blue) internal{
  334.         redBonds[addr] += red * amount;
  335.         greenBonds[addr] += green * amount;
  336.         blueBonds[addr] += blue * amount;
  337.     }
  338.     function resolvesThinColor(address addr, uint newWeight, uint oldWeight) internal{
  339.         (redResolves[addr], greenResolves[addr], blueResolves[addr]) = RGB_scale(redResolves[addr], greenResolves[addr], blueResolves[addr], newWeight, oldWeight);
  340.         /*redResolves[addr] = redResolves[addr] * newWeight / oldWeight;
  341.         greenResolves[addr] = greenResolves[addr] * newWeight / oldWeight;
  342.         blueResolves[addr] = blueResolves[addr] * newWeight / oldWeight;    */
  343.     }
  344.     function bondsThinColor(address addr, uint newWeight, uint oldWeight) internal{
  345.         (redBonds[addr], greenBonds[addr], blueBonds[addr]) = RGB_scale(redBonds[addr], greenBonds[addr], blueBonds[addr], newWeight, oldWeight);
  346.         /*redBonds[addr] = redBonds[addr] * newWeight / oldWeight;
  347.         greenBonds[addr] = greenBonds[addr] * newWeight / oldWeight;
  348.         blueBonds[addr] = blueBonds[addr] * newWeight / oldWeight;  */
  349.     }
  350.     function RGB_bondRatio() public view returns(uint,uint,uint){
  351.         //uint bonds = bondBalance(msg.sender);
  352.         return RGB_bondRatio(msg.sender);//(redBonds[sender]/bonds, greenBonds[sender]/bonds, blueBonds[sender]/bonds);
  353.     }
  354.     function RGB_bondRatio(address addr) public view returns(uint,uint,uint){
  355.         uint bonds = bondBalance(addr);
  356.         return (redBonds[sender]/bonds, greenBonds[sender]/bonds, blueBonds[sender]/bonds);
  357.     }
  358.     function RGB_resolveRatio(address addr) public view returns(uint,uint,uint){
  359.         uint resolves = balanceOf(addr);
  360.         return (redResolves[sender]/resolves, greenResolves[sender]/resolves, blueResolves[sender]/resolves);
  361.     }
  362.     function () payable external {
  363.         if (msg.value > 0) {
  364.             //address sender = msg.sender;
  365.             //uint totalHoldings = bondBalance(msg.sender);
  366.             (uint _red , uint _green, uint _blue) = RGB_bondRatio();
  367.             /*redBonds[sender]/totalHoldings;
  368.              = greenBonds[sender]/totalHoldings;
  369.              = blueBonds[sender]/totalHoldings;*/
  370.             buy(_red, _green, _blue);
  371.         } else {
  372.             withdraw( bondContract.resolveEarnings( proxyAddress(msg.sender) ) );
  373.         }
  374.     }
  375.  
  376.     function bondBalance(address addr) public view returns(uint){
  377.         return bondContract.balanceOf( proxyAddress(addr) );
  378.     }
  379.     /*event BondTransfer(address from, address to, uint amount, uint red, uint green, uint blue);
  380.     function bondTransfer(address to, uint amount) public{
  381.         address sender = msg.sender;
  382.         uint sendersFormerTotalBonds = bondBalance(sender);
  383.         bondsThinColor( sender, sendersFormerTotalBonds - amount, sendersFormerTotalBonds);
  384.         (uint r, uint g, uint b) = RGB_bondRatio(sender);
  385.         bondsAddColor( to, amount, r, g, b );
  386.         PyramidProxy(proxy[sender]).bondTransfer( proxyAddress(to) , amount);
  387.         emit BondTransfer(sender, to, amount, r, g, b);
  388.     }*/
  389. }
  390.  
  391.  
  392. contract BondContract{
  393.     function balanceOf(address _owner) public view returns (uint256 balance);
  394.     function sellBonds(uint amount) public returns(uint,uint);
  395.     function getResolveContract() public view returns(address);
  396.     function pullResolves(uint amount) public;
  397.     function reinvestEarnings(uint amountFromEarnings) public returns(uint,uint);
  398.     function withdraw(uint amount) public returns(uint);
  399.     function fund() payable public returns(uint);
  400.     function resolveEarnings(address _owner) public view returns (uint256 amount);
  401.     //function bondTransfer( address to, uint amount ) public;
  402. }
  403.  
  404. contract ResolveContract{
  405.     function balanceOf(address _owner) public view returns (uint256 balance);
  406.     function transfer(address _to, uint _value) public returns (bool success);
  407. }
  408.  
  409. contract PyramidProxy{
  410.     ColorToken router;
  411.     BondContract public bondContract;
  412.     ResolveContract public resolveContract;
  413.     uint ETH;
  414.     address THIS = address(this);
  415.  
  416.     constructor(ColorToken _router, BondContract _BondContract) public{
  417.         router = _router;
  418.         bondContract = _BondContract;
  419.         resolveContract = ResolveContract( _BondContract.getResolveContract() );
  420.     }
  421.  
  422.     modifier routerOnly{
  423.         require(msg.sender == address(router));
  424.         _;
  425.     }
  426.     function () payable external{
  427.         //ETH += msg.value;
  428.     }
  429.  
  430.     function buy() public routerOnly() returns(uint){
  431.         //uint _ETH = ETH;
  432.         //ETH = 0;
  433.         return bondContract.fund.value( THIS.balance )();
  434.     }
  435.     function cash2Owner() internal{
  436.         address payable owner = address(uint160( router.getProxyOwner( THIS ) ) );
  437.         //uint _ETH = ETH;
  438.         //ETH = 0;
  439.         owner.transfer( THIS.balance );
  440.     }
  441.     function getBalance() public view returns (uint balance) {
  442.         address bC = address(bondContract);
  443.         if( THIS == bC ){
  444.             return resolveContract.balanceOf( bC );
  445.         }else{
  446.             return resolveContract.balanceOf( THIS );  
  447.         }
  448.     }
  449.     /*function resolveEarnings() internal view returns(uint){
  450.         return bondContract.resolveEarnings( THIS );
  451.     }*/
  452.     function reinvest(uint amount) public routerOnly() returns(uint,uint){
  453.         return bondContract.reinvestEarnings( amount );
  454.     }
  455.     function withdraw(uint amount) public routerOnly() returns(uint){
  456.         uint dissolvedResolves = bondContract.withdraw( amount );
  457.         cash2Owner();
  458.         return dissolvedResolves;
  459.     }
  460.     function sell(uint amount) public routerOnly() returns (uint){
  461.         uint resolves;
  462.         (,resolves) = bondContract.sellBonds(amount);
  463.         cash2Owner();
  464.         return resolves;
  465.     }
  466.    
  467.     function stake(uint amount) public routerOnly(){
  468.         resolveContract.transfer( address(bondContract), amount );
  469.     }
  470.     function transfer(address addr, uint amount) public routerOnly(){
  471.         resolveContract.transfer( addr, amount );
  472.     }
  473.     /*function bondTransfer(address to, uint amount) public routerOnly(){
  474.         bondContract.bondTransfer( to, amount );
  475.     }*/
  476.     function unstake(uint amount) public routerOnly(){
  477.         bondContract.pullResolves( amount );
  478.     }
  479. }
  480.  
  481. contract ERC223ReceivingContract{
  482.     function tokenFallback(address _from, uint _value, bytes calldata _data) external;
  483. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top