SHARE
TWEET

Untitled

a guest Nov 17th, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^ 0.5.13;
  2. contract Pyramid{
  3.     // scaleFactor is used to convert Ether into bonds and vice-versa: they're of different
  4.     // orders of magnitude, hence the need to bridge between the two.
  5.     uint256 constant scaleFactor = 0x10000000000000000;
  6.  
  7.     int constant crr_n = 1;
  8.     int constant crr_d = 2;
  9.  
  10.     // One Configuration, Slope; Security vs Promise.
  11.     int constant public slope = -0x1337FA66607BADA55;
  12.  
  13.     // Typical values that we have to declare.
  14.     string constant public name = "Bonds";
  15.     string constant public symbol = "Bond";
  16.     uint8 constant public decimals = 12;
  17.  
  18.  
  19.     // Array between each address and their number of bonds.
  20.     mapping(address => uint256) public hodlBonds;
  21.     mapping(address => mapping(address => uint)) approvals;
  22.     // For calculating resolves minted
  23.     mapping(address => uint256) public average_ethSpent;
  24.     // For calculating hodl multiplier that factors into resolves minted
  25.     mapping(address => uint256) public average_buyInTimeSum;
  26.     // Array between each address and their number of resolves being staked.
  27.     mapping(address => uint256) public resolveWeight;
  28.  
  29.     // Array between each address and how much Ether has been paid out to it.
  30.     // Note that this is scaled by the scaleFactor variable.
  31.     mapping(address => int256) public payouts;
  32.  
  33.     // Variable tracking how many bonds are in existence overall.
  34.     uint256 public _totalSupply;
  35.  
  36.     // The total number of resolves being staked in this contract
  37.     uint256 public dissolvingResolves;
  38.     // The total number of resolves burned for a return of ETH(withdraw) or Bonds(reinvest)
  39.     uint256 public dissolved;
  40.  
  41.     // The ethereum locked up into bonds
  42.     uint public contractBalance;
  43.  
  44.     // Easing in the fee. Make the fee reasonable as the contract is scaling to the size of the ecosystem
  45.     uint256 public buySum;
  46.     uint256 public sellSum;
  47.  
  48.     // For calculating the hodl multiplier. Weighted average release time
  49.     uint public average_clockWeighted_releaseAmount;
  50.     uint public average_clockWeighted_releaseTimeSum;
  51.     // base time on when the contract was created
  52.     uint public genesis;
  53.  
  54.     // Something about invarience.
  55.     int256 earningsOffset;
  56.  
  57.     // Variable tracking how much Ether each token is currently worth.
  58.     // Note that this is scaled by the scaleFactor variable.
  59.     uint256 earningsPerResolve;
  60.  
  61.     //The resolve token contract
  62.     ResolveToken public resolveToken;
  63.  
  64.     constructor() public{
  65.         genesis = now;
  66.         resolveToken = new ResolveToken( address(this) );
  67.     }
  68.  
  69.     function totalSupply() public view returns (uint256) {
  70.         return _totalSupply;
  71.     }
  72.     // Returns the number of bonds currently held by _owner.
  73.     function balanceOf(address _owner) public view returns (uint256 balance) {
  74.         return hodlBonds[_owner];
  75.     }
  76.  
  77.     function fluxFee(uint paidAmount) public view returns (uint fee) {
  78.         if (dissolvingResolves == 0)
  79.             return 0;
  80.        
  81.         uint totalResolveSupply = resolveToken.totalSupply() - dissolved;
  82.         return paidAmount * dissolvingResolves / totalResolveSupply * sellSum / buySum;
  83.     }
  84.  
  85.     // Converts the Ether accrued as resolveEarnings back into bonds without having to
  86.     // withdraw it first. Saves on gas and potential price spike loss.
  87.     event Reinvest( address indexed addr, uint256 reinvested, uint256 dissolved, uint256 bonds, uint256 resolveTax);
  88.     function reinvestEarnings(uint amountFromEarnings) public returns(uint,uint){
  89.         address sender = msg.sender;
  90.         // Retrieve the resolveEarnings associated with the address the request came from.     
  91.         uint upScaleDivs = (uint)((int256)( earningsPerResolve * resolveWeight[sender] ) - payouts[sender]);
  92.         uint totalEarnings = upScaleDivs / scaleFactor;//resolveEarnings(sender);
  93.         require(amountFromEarnings <= totalEarnings, "the amount exceeds total earnings");
  94.         uint oldWeight = resolveWeight[sender];
  95.         resolveWeight[sender] = oldWeight * (totalEarnings - amountFromEarnings) / totalEarnings;
  96.         uint weightDiff = oldWeight * amountFromEarnings / totalEarnings;
  97.         dissolved += weightDiff;
  98.         dissolvingResolves -= weightDiff;
  99.        
  100.         // something about invariance
  101.         int withdrawnEarnings = (int)(upScaleDivs * amountFromEarnings / totalEarnings) - (int)(weightDiff*earningsPerResolve);
  102.         payouts[sender] += withdrawnEarnings;
  103.         // Increase the total amount that's been paid out to maintain invariance.
  104.         earningsOffset += withdrawnEarnings;
  105.  
  106.         // Assign balance to a new variable.
  107.         uint value_ = (uint) (amountFromEarnings);
  108.  
  109.         // If your resolveEarnings are worth less than 1 szabo, abort.
  110.         if (value_ < 0.000001 ether)
  111.             revert();
  112.  
  113.         // Calculate the fee
  114.         uint fee = fluxFee(value_);
  115.  
  116.         // The amount of Ether used to purchase new bonds for the caller
  117.         uint numEther = value_ - fee;
  118.         buySum += numEther;
  119.  
  120.         //resolve reward tracking stuff
  121.         uint currentTime = NOW();
  122.         average_ethSpent[sender] += numEther;
  123.         average_buyInTimeSum[sender] += currentTime * scaleFactor * numEther;
  124.  
  125.         // The number of bonds which can be purchased for numEther.
  126.         uint createdBonds = calculateBondsFromReinvest(numEther, amountFromEarnings);
  127.  
  128.         // the variable storing the amount to be paid to stakers
  129.         uint resolveFee;
  130.  
  131.         // Check if we have bonds in existence
  132.         if (_totalSupply > 0 && fee > 0) {
  133.             resolveFee = fee * scaleFactor;
  134.  
  135.             // Fee is distributed to all existing resolve stakers before the new bonds are purchased.
  136.             // rewardPerResolve is the amount(ETH) gained per resolve token from this purchase.
  137.             uint rewardPerResolve = resolveFee/dissolvingResolves;
  138.  
  139.             // The Ether value per token is increased proportionally.
  140.             earningsPerResolve += rewardPerResolve;
  141.         }
  142.  
  143.         // Add the createdBonds to the total supply.
  144.         _totalSupply += createdBonds;
  145.  
  146.         // Assign the bonds to the balance of the buyer.
  147.         hodlBonds[sender] += createdBonds;
  148.  
  149.         emit Reinvest(sender, value_, weightDiff, createdBonds, resolveFee);
  150.         return (createdBonds, weightDiff);
  151.     }
  152.  
  153.     // Sells your bonds for Ether
  154.     function sellAllBonds() public {
  155.         sell( balanceOf(msg.sender) );
  156.     }
  157.     function sellBonds(uint amount) public returns(uint,uint,uint){
  158.         uint balance = balanceOf(msg.sender);
  159.         require(balance >= amount, "Amount is more than balance");
  160.         (uint returned_eth, uint returned_resolves, uint initialInput_ETH) = sell(amount);
  161.         return (returned_eth, returned_resolves, initialInput_ETH);
  162.     }
  163.  
  164.     // Big red exit button to pull all of a holder's Ethereum value from the contract
  165.     function getMeOutOfHere() public {
  166.         sellAllBonds();
  167.         withdraw( resolveEarnings(msg.sender) );
  168.     }
  169.  
  170.     // Gatekeeper function to check if the amount of Ether being sent isn't too small
  171.     function fund() payable public returns(uint){
  172.         uint bought;
  173.         if (msg.value > 0.000001 ether) {
  174.             contractBalance += msg.value;
  175.             bought = buy();
  176.         } else {
  177.             revert();
  178.         }
  179.         return bought;
  180.     }
  181.  
  182.     // Function that returns the (dynamic) pricing for buys, sells and fee
  183.     function pricing(uint scale) public view returns (uint buyPrice, uint sellPrice, uint fee) {
  184.         uint buy_eth = scaleFactor * getPriceForBonds( scale, true) / ( scaleFactor - fluxFee(scaleFactor) ) ;
  185.         uint sell_eth = getPriceForBonds(scale, false);
  186.         sell_eth -= fluxFee(sell_eth);
  187.         return ( buy_eth, sell_eth, fluxFee(scale) );
  188.     }
  189.  
  190.     // For calculating the price
  191.     function getPriceForBonds(uint256 bonds, bool buy_or_sell) public view returns (uint256 price) {
  192.         uint reserveAmount = reserve();
  193.  
  194.         if(buy_or_sell){
  195.             uint x = fixedExp((fixedLog(_totalSupply + bonds) - slope) * crr_d/crr_n);
  196.             return x - reserveAmount;
  197.         }else{
  198.             uint x = fixedExp((fixedLog(_totalSupply - bonds) - slope) * crr_d/crr_n);
  199.             return reserveAmount - x;
  200.         }
  201.     }
  202.  
  203.     // Calculate the current resolveEarnings associated with the caller address. This is the net result
  204.     // of multiplying the number of resolves held by their current value in Ether and subtracting the
  205.     // Ether that has already been paid out.
  206.     function resolveEarnings(address _owner) public view returns (uint256 amount) {
  207.         return (uint256) ((int256)(earningsPerResolve * resolveWeight[_owner]) - payouts[_owner]) / scaleFactor;
  208.     }
  209.  
  210.     event Buy( address indexed addr, uint256 spent, uint256 bonds, uint256 resolveTax);
  211.     function buy() internal returns(uint){
  212.         address sender = msg.sender;
  213.         // Any transaction of less than 1 szabo is likely to be worth less than the gas used to send it.
  214.         if ( msg.value < 0.000001 ether )
  215.             revert();
  216.  
  217.         // Calculate the fee
  218.         uint fee = fluxFee(msg.value);
  219.  
  220.         // The amount of Ether used to purchase new bonds for the caller.
  221.         uint numEther = msg.value - fee;
  222.         buySum += numEther;
  223.  
  224.         //resolve reward tracking stuff
  225.         uint currentTime = NOW();
  226.         average_ethSpent[sender] += numEther;
  227.         average_buyInTimeSum[sender] += currentTime * scaleFactor * numEther;
  228.  
  229.         // The number of bonds which can be purchased for numEther.
  230.         uint createdBonds = getBondsForEther(numEther);
  231.  
  232.         // Add the createdBonds to the total supply.
  233.         _totalSupply += createdBonds;
  234.  
  235.         // Assign the bonds to the balance of the buyer.
  236.         hodlBonds[sender] += createdBonds;
  237.  
  238.         // Check if we have bonds in existence
  239.         uint resolveFee;
  240.         if (_totalSupply > 0 && fee > 0) {
  241.             resolveFee = fee * scaleFactor;
  242.  
  243.             // Fee is distributed to all existing resolve holders before the new bonds are purchased.
  244.             // rewardPerResolve is the amount gained per resolve token from this purchase.
  245.             uint rewardPerResolve = resolveFee/dissolvingResolves;
  246.  
  247.             // The Ether value per resolve is increased proportionally.
  248.             earningsPerResolve += rewardPerResolve;
  249.         }
  250.         emit Buy( sender, msg.value, createdBonds, resolveFee);
  251.         return createdBonds;
  252.     }
  253.     function NOW() public view returns(uint time){
  254.         return now - genesis;
  255.     }
  256.     function avgHodl() public view returns(uint hodlTime){
  257.         return average_clockWeighted_releaseTimeSum / average_clockWeighted_releaseAmount / scaleFactor;
  258.     }
  259.     function getReturnsForBonds(address addr, uint bondsReleased) public view returns(uint etherValue, uint mintedResolves, uint new_releaseTimeSum, uint new_releaseAmount, uint initialInput_ETH){
  260.         uint output_ETH = getEtherForBonds(bondsReleased);
  261.         uint input_ETH = average_ethSpent[addr] * bondsReleased / hodlBonds[addr];
  262.         // hodl multiplier. because if you don't hodl at all, you shouldn't be rewarded resolves.
  263.         // and the multiplier you get for hodling needs to be relative to the average hodl
  264.         uint buyInTime = average_buyInTimeSum[addr] / average_ethSpent[addr];
  265.         uint cashoutTime = NOW()*scaleFactor - buyInTime;
  266.         uint releaseTimeSum = average_clockWeighted_releaseTimeSum + cashoutTime*input_ETH/scaleFactor/*to give new life more weight--->*/*buyInTime;
  267.         uint releaseAmount = average_clockWeighted_releaseAmount + input_ETH/*to give new life more weight--->*/*buyInTime/scaleFactor;
  268.         uint avgCashoutTime = releaseTimeSum/releaseAmount;
  269.         return (output_ETH, input_ETH * cashoutTime / avgCashoutTime * input_ETH / output_ETH, releaseTimeSum, releaseAmount, input_ETH);
  270.     }
  271.     event Sell( address indexed addr, uint256 bondsSold, uint256 cashout, uint256 resolves, uint256 resolveTax, uint256 initialCash);
  272.     function sell(uint256 amount) internal returns(uint eth, uint resolves, uint initialInput){
  273.         address payable sender = msg.sender;
  274.         // Calculate the amount of Ether & Resolves that the holder's bonds sell for at the current sell price.
  275.         uint numEthersBeforeFee;
  276.         uint mintedResolves;
  277.         uint releaseTimeSum;
  278.         uint releaseAmount;
  279.         uint initialInput_ETH;
  280.         (numEthersBeforeFee,mintedResolves,releaseTimeSum,releaseAmount,initialInput_ETH) = getReturnsForBonds(sender, amount);
  281.  
  282.         // magic distribution
  283.         resolveToken.mint(sender, mintedResolves);
  284.  
  285.         // update weighted average cashout time
  286.         average_clockWeighted_releaseTimeSum = releaseTimeSum;
  287.         average_clockWeighted_releaseAmount = releaseAmount;
  288.  
  289.         // reduce the amount of "eth spent" based on the percentage of bonds being sold back into the contract
  290.         average_ethSpent[sender] = average_ethSpent[sender] * ( hodlBonds[sender] - amount) / hodlBonds[sender];
  291.         // reduce the "buyInTime" sum that's used for average buy in time
  292.         average_buyInTimeSum[sender] = average_buyInTimeSum[sender] * (hodlBonds[sender] - amount) / hodlBonds[sender];
  293.        
  294.         // calculate the fee
  295.         uint fee = fluxFee(numEthersBeforeFee);
  296.  
  297.         // Net Ether for the seller after the fee has been subtracted.
  298.         uint numEthers = numEthersBeforeFee - fee;
  299.  
  300.         //updating the numerator of the fee-easing factor
  301.         sellSum += initialInput_ETH;
  302.  
  303.         // Burn the bonds which were just sold from the total supply.
  304.         _totalSupply -= amount;
  305.  
  306.         // Remove the bonds from the balance of the buyer.
  307.         hodlBonds[sender] -= amount;
  308.  
  309.  
  310.         // Check if we have bonds in existence
  311.         uint resolveFee;
  312.         if (_totalSupply > 0 && dissolvingResolves > 0){
  313.             // Scale the Ether taken as the selling fee by the scaleFactor variable.
  314.             resolveFee = fee * scaleFactor;
  315.  
  316.             // Fee is distributed to all remaining resolve holders.
  317.             // rewardPerResolve is the amount gained per resolve thanks to this sell.
  318.             uint rewardPerResolve = resolveFee/dissolvingResolves;
  319.  
  320.             // The Ether value per resolve is increased proportionally.
  321.             earningsPerResolve += rewardPerResolve;
  322.         }
  323.        
  324.         // Send the ethereum to the address that requested the sell.
  325.         contractBalance -= numEthers;
  326.         sender.transfer(numEthers);
  327.         emit Sell( sender, amount, numEthers, mintedResolves, resolveFee, initialInput_ETH);
  328.         return (numEthers, mintedResolves, initialInput_ETH);
  329.     }
  330.  
  331.     // Dynamic value of Ether in reserve, according to the CRR requirement.
  332.     function reserve() public view returns (uint256 amount) {
  333.         return Common.subtract( balance(),  (uint256) ((int256) (earningsPerResolve * dissolvingResolves) - earningsOffset) / scaleFactor );
  334.     }
  335.     function balance() internal view returns (uint256 amount) {
  336.         // msg.value is the amount of Ether sent by the transaction.
  337.         return contractBalance - msg.value;
  338.     }
  339.  
  340.     // Calculates the number of bonds that can be bought for a given amount of Ether, according to the
  341.     // dynamic reserve and _totalSupply values (derived from the buy and sell prices).
  342.     function getBondsForEther(uint256 ethervalue) public view returns (uint256 bonds) {
  343.         return Common.subtract(fixedExp( fixedLog( reserve() + ethervalue ) * crr_n/crr_d + slope ) , _totalSupply);
  344.     }
  345.  
  346.     // Semantically similar to getBondsForEther, but subtracts the callers balance from the amount of Ether returned for conversion.
  347.     function calculateBondsFromReinvest(uint256 ethervalue, uint256 subvalue) public view returns (uint256 bondTokens) {
  348.         return Common.subtract(fixedExp(fixedLog(Common.subtract(reserve() , subvalue) + ethervalue)*crr_n/crr_d + slope) , _totalSupply);
  349.     }
  350.  
  351.     // Converts a number bonds into an Ether value.
  352.     function getEtherForBonds(uint256 bondTokens) public view returns (uint256 ethervalue) {
  353.         // How much reserve Ether do we have left in the contract?
  354.         uint reserveAmount = reserve();
  355.  
  356.         // If you're the Highlander (or bagholder), you get The Prize. Everything left in the remainder's vault.
  357.         if (bondTokens == _totalSupply)
  358.             return reserveAmount;
  359.  
  360.         // If there would be excess Ether left after the transaction this is called within, return the Ether
  361.         // corresponding to the equation in Dr Jochen Hoenicke's original Ponzi paper, which can be found
  362.         // at https://test.jochen-hoenicke.de/eth/ponzitoken/ in the third equation, with the CRR numerator
  363.         // and denominator altered to 1 and 2 respectively.
  364.         return Common.subtract(reserveAmount, fixedExp( ( fixedLog(_totalSupply-bondTokens)-slope ) * crr_d/crr_n) );
  365.     }
  366.  
  367.     // You don't care about these, but if you really do they're hex values for
  368.     // co-efficients used to simulate approximations of the log and exp functions.
  369.         int256  constant one        = 0x10000000000000000;
  370.         uint256 constant sqrt2      = 0x16a09e667f3bcc908;
  371.         uint256 constant sqrtdot5   = 0x0b504f333f9de6484;
  372.         int256  constant ln2        = 0x0b17217f7d1cf79ac;
  373.         int256  constant ln2_64dot5 = 0x2cb53f09f05cc627c8;
  374.         int256  constant c1         = 0x1ffffffffff9dac9b;
  375.         int256  constant c3         = 0x0aaaaaaac16877908;
  376.         int256  constant c5         = 0x0666664e5e9fa0c99;
  377.         int256  constant c7         = 0x049254026a7630acf;
  378.         int256  constant c9         = 0x038bd75ed37753d68;
  379.         int256  constant c11        = 0x03284a0c14610924f;
  380.  
  381.     // The polynomial R = c1*x + c3*x^3 + ... + c11 * x^11
  382.     // approximates the function log(1+x)-log(1-x)
  383.     // Hence R(s) = log((1+s)/(1-s)) = log(a)
  384.     function fixedLog(uint256 a) internal pure returns (int256 log) {
  385.         int32 scale = 0;
  386.         while (a > sqrt2) {
  387.             a /= 2;
  388.             scale++;
  389.         }
  390.         while (a <= sqrtdot5) {
  391.             a *= 2;
  392.             scale--;
  393.         }
  394.         int256 s = (((int256)(a) - one) * one) / ((int256)(a) + one);
  395.         int z = (s*s) / one;
  396.         return scale * ln2 +
  397.             (s*(c1 + (z*(c3 + (z*(c5 + (z*(c7 + (z*(c9 + (z*c11/one))
  398.                 /one))/one))/one))/one))/one);
  399.     }
  400.  
  401.     int256 constant c2 =  0x02aaaaaaaaa015db0;
  402.     int256 constant c4 = -0x000b60b60808399d1;
  403.     int256 constant c6 =  0x0000455956bccdd06;
  404.     int256 constant c8 = -0x000001b893ad04b3a;
  405.  
  406.     // The polynomial R = 2 + c2*x^2 + c4*x^4 + ...
  407.     // approximates the function x*(exp(x)+1)/(exp(x)-1)
  408.     // Hence exp(x) = (R(x)+x)/(R(x)-x)
  409.     function fixedExp(int256 a) internal pure returns (uint256 exp) {
  410.         int256 scale = (a + (ln2_64dot5)) / ln2 - 64;
  411.         a -= scale*ln2;
  412.         int256 z = (a*a) / one;
  413.         int256 R = ((int256)(2) * one) +
  414.             (z*(c2 + (z*(c4 + (z*(c6 + (z*c8/one))/one))/one))/one);
  415.         exp = (uint256) (((R + a) * one) / (R - a));
  416.         if (scale >= 0)
  417.             exp <<= scale;
  418.         else
  419.             exp >>= -scale;
  420.         return exp;
  421.     }
  422.  
  423.     // This allows you to buy bonds by sending Ether directly to the smart contract
  424.     // without including any transaction data (useful for, say, mobile wallet apps).
  425.     function () payable external {
  426.         // msg.value is the amount of Ether sent by the transaction.
  427.         if (msg.value > 0) {
  428.             fund();
  429.         } else {
  430.             withdraw( resolveEarnings(msg.sender) );
  431.         }
  432.     }
  433.  
  434.     // Allow contract to accept resolve tokens
  435.     event StakeResolves( address indexed addr, uint256 amountStaked, bytes _data );
  436.     function tokenFallback(address from, uint value, bytes calldata _data) external{
  437.         if(msg.sender == address(resolveToken) ){
  438.             resolveWeight[from] += value;
  439.             dissolvingResolves += value;
  440.  
  441.             // Then we update the payouts array for the "resolve shareholder" with this amount
  442.             int payoutDiff = (int256) (earningsPerResolve * value);
  443.             payouts[from] += payoutDiff;
  444.             earningsOffset += payoutDiff;
  445.  
  446.             emit StakeResolves(from, value, _data);
  447.         }else{
  448.             revert("no want");
  449.         }
  450.     }
  451.  
  452.  
  453.     // Withdraws resolveEarnings held by the caller sending the transaction, updates
  454.     // the requisite global variables, and transfers Ether back to the caller.
  455.     event Withdraw( address indexed addr, uint256 earnings, uint256 dissolve );
  456.     function withdraw(uint amount) public returns(uint){
  457.         address payable sender = msg.sender;
  458.         // Retrieve the resolveEarnings associated with the address the request came from.
  459.         uint upScaleDivs = (uint)((int256)( earningsPerResolve * resolveWeight[sender] ) - payouts[sender]);
  460.         uint totalEarnings = upScaleDivs / scaleFactor;//resolveEarnings(sender);
  461.         require( amount <= totalEarnings && amount > 0 );
  462.         uint oldWeight = resolveWeight[sender];
  463.         resolveWeight[sender] = oldWeight * ( totalEarnings - amount ) / totalEarnings;
  464.         uint weightDiff = oldWeight * amount / totalEarnings;
  465.         dissolved += weightDiff;
  466.         dissolvingResolves -= weightDiff;
  467.        
  468.         // something about invariance
  469.         int withdrawnEarnings = (int)(upScaleDivs * amount / totalEarnings) - (int)(weightDiff*earningsPerResolve);
  470.         payouts[sender] += withdrawnEarnings;
  471.         // Increase the total amount that's been paid out to maintain invariance.
  472.         earningsOffset += withdrawnEarnings;
  473.  
  474.         contractBalance -= amount;
  475.  
  476.         // Send the resolveEarnings to the address that requested the withdraw.
  477.         sender.transfer(amount);
  478.         emit Withdraw( sender, amount, weightDiff);
  479.         return weightDiff;
  480.     }
  481.     event PullResolves( address indexed addr, uint256 pulledResolves, uint256 forfeiture);
  482.     function pullResolves(uint amount) public{
  483.         address sender = msg.sender;
  484.         uint resolves = resolveWeight[ sender ];
  485.         require(amount <= resolves && amount > 0);
  486.         require(amount <= dissolvingResolves);//"you can't forfeit the last amount"
  487.  
  488.         uint allEarnings = (uint)((int256)(resolves * earningsPerResolve) - payouts[sender]);
  489.         uint forfeitedEarnings = allEarnings * amount / resolves;
  490.  
  491.         // Update the payout array so that the "resolve shareholder" cannot claim resolveEarnings on previous staked resolves.
  492.         payouts[sender] += (int256)(forfeitedEarnings) - (int256)(earningsPerResolve * amount);
  493.         earningsOffset -= (int256)(earningsPerResolve * amount);
  494.  
  495.         resolveWeight[sender] -= amount;
  496.         dissolvingResolves -= amount;
  497.         // The Ether value per token is increased proportionally.
  498.         earningsPerResolve += forfeitedEarnings / dissolvingResolves;
  499.         resolveToken.transfer( sender, amount );
  500.         emit PullResolves( sender, amount, forfeitedEarnings / scaleFactor);
  501.     }
  502.  
  503.     // Function that is called when a user or another contract wants to transfer funds .
  504.     function transfer(address _to, uint _value, bytes memory _data) public returns (bool success) {
  505.         if (balanceOf(msg.sender) < _value) revert();
  506.         if(Common.isContract(_to)) {
  507.             return transferToContract(_to, _value, _data);
  508.         }else{
  509.             return transferToAddress(_to, _value, _data);
  510.         }
  511.     }
  512.  
  513.     // Standard function transfer similar to ERC20 transfer with no _data .
  514.     // Added due to backwards compatibility reasons .
  515.     function transfer(address _to, uint _value) public returns (bool success) {
  516.         if (balanceOf(msg.sender) < _value) revert();
  517.         //standard function transfer similar to ERC20 transfer with no _data
  518.         //added due to backwards compatibility reasons
  519.         bytes memory empty;
  520.         if(Common.isContract(_to)){
  521.             return transferToContract(_to, _value, empty);
  522.         }else{
  523.             return transferToAddress(_to, _value, empty);
  524.         }
  525.     }
  526.  
  527.     //function that is called when transaction target is an address
  528.     function transferToAddress(address _to, uint _value, bytes memory _data) private returns (bool success) {
  529.         moveTokens(msg.sender,_to,_value);
  530.         emit Transfer(msg.sender, _to, _value, _data);
  531.         return true;
  532.     }
  533.  
  534.     //function that is called when transaction target is a contract
  535.     function transferToContract(address _to, uint _value, bytes memory _data) private returns (bool success) {
  536.         address sender = msg.sender;
  537.         moveTokens(sender, _to, _value);
  538.         ERC223ReceivingContract reciever = ERC223ReceivingContract(_to);
  539.         reciever.tokenFallback(sender, _value, _data);
  540.         emit Transfer(sender, _to, _value, _data);
  541.         return true;
  542.     }
  543.  
  544.     function moveTokens(address _from, address _to, uint _amount) private{
  545.  
  546.         uint totalBonds = hodlBonds[_from];
  547.         require(_amount <= totalBonds && _amount > 0);
  548.         uint ethSpent = average_ethSpent[_from] * _amount / totalBonds;
  549.         uint buyInTimeSum = average_buyInTimeSum[_from] * _amount / totalBonds;
  550.         average_ethSpent[_from] -= ethSpent;
  551.         average_buyInTimeSum[_from] -= buyInTimeSum;
  552.         hodlBonds[_from] -= _amount;
  553.         average_ethSpent[_to] += ethSpent;
  554.         average_buyInTimeSum[_to] += buyInTimeSum;
  555.         hodlBonds[_to] += _amount;
  556.     }
  557.  
  558.     function allowance(address src, address guy) public view returns (uint) {
  559.         return approvals[src][guy];
  560.     }
  561.  
  562.     function transferFrom(address src, address dst, uint wad) public returns (bool){
  563.         address sender = msg.sender;
  564.         require(approvals[src][sender] >=  wad, "That amount is not approved");
  565.         require(hodlBonds[src] >=  wad, "That amount is not available from this wallet");
  566.         if (src != sender) {
  567.             approvals[src][sender] -=  wad;
  568.         }
  569.         moveTokens(src,dst,wad);
  570.  
  571.         bytes memory empty;
  572.         emit Transfer(src, dst, wad, empty);
  573.  
  574.         return true;
  575.     }
  576.  
  577.     function approve(address guy, uint wad) public returns (bool) {
  578.         approvals[msg.sender][guy] = wad;
  579.  
  580.         emit Approval(msg.sender, guy, wad);
  581.  
  582.         return true;
  583.     }
  584.     event Transfer(
  585.         address indexed from,
  586.         address indexed to,
  587.         uint256 amount,
  588.         bytes data
  589.     );
  590.     event Approval(address indexed src, address indexed guy, uint wad);
  591. }
  592.  
  593. contract ERC223ReceivingContract{
  594.     function tokenFallback(address _from, uint _value, bytes calldata _data) external;
  595. }
  596.  
  597. contract ResolveToken{
  598.  
  599.  
  600.     mapping(address => uint) balances;
  601.     mapping(address => mapping(address => uint)) approvals;
  602.  
  603.     string public name = "Halo";
  604.     string public symbol = "`Halo";
  605.     uint8 constant public decimals = 18;
  606.     uint256 private _totalSupply;
  607.     address public hourglass;
  608.  
  609.     constructor(address _hourglass) public{
  610.         hourglass = _hourglass;
  611.     }
  612.  
  613.     modifier hourglassOnly{
  614.       require(msg.sender == hourglass);
  615.       _;
  616.     }
  617.  
  618.     event Transfer(
  619.         address indexed from,
  620.         address indexed to,
  621.         uint256 amount,
  622.         bytes data
  623.     );
  624.  
  625.     event Mint(
  626.         address indexed addr,
  627.         uint256 amount
  628.     );
  629.  
  630.     function totalSupply() public view returns (uint256) {
  631.         return _totalSupply;
  632.     }
  633.  
  634.     function mint(address _address, uint _value) external hourglassOnly(){
  635.         balances[_address] += _value;
  636.         _totalSupply += _value;
  637.         emit Mint(_address, _value);
  638.     }
  639.  
  640.     // Function that is called when a user or another contract wants to transfer funds .
  641.     function transfer(address _to, uint _value, bytes memory _data) public returns (bool success) {
  642.         if (balanceOf(msg.sender) < _value) revert();
  643.         if(Common.isContract(_to)) {
  644.             return transferToContract(_to, _value, _data);
  645.         }else{
  646.             return transferToAddress(_to, _value, _data);
  647.         }
  648.     }
  649.  
  650.     // Standard function transfer similar to ERC20 transfer with no _data .
  651.     // Added due to backwards compatibility reasons .
  652.     function transfer(address _to, uint _value) public returns (bool success) {
  653.         if (balanceOf(msg.sender) < _value) revert();
  654.         //standard function transfer similar to ERC20 transfer with no _data
  655.         //added due to backwards compatibility reasons
  656.         bytes memory empty;
  657.         if(Common.isContract(_to)){
  658.             return transferToContract(_to, _value, empty);
  659.         }else{
  660.             return transferToAddress(_to, _value, empty);
  661.         }
  662.     }
  663.  
  664.     //function that is called when transaction target is an address
  665.     function transferToAddress(address _to, uint _value, bytes memory _data) private returns (bool success) {
  666.         moveTokens(msg.sender,_to,_value);
  667.         emit Transfer(msg.sender, _to, _value, _data);
  668.         return true;
  669.     }
  670.  
  671.     //function that is called when transaction target is a contract
  672.     function transferToContract(address _to, uint _value, bytes memory _data) private returns (bool success) {
  673.         address sender = msg.sender;
  674.         moveTokens(sender, _to, _value);
  675.         ERC223ReceivingContract reciever = ERC223ReceivingContract(_to);
  676.         reciever.tokenFallback(sender, _value, _data);
  677.         emit Transfer(sender, _to, _value, _data);
  678.         return true;
  679.     }
  680.  
  681.     function moveTokens(address _from, address _to, uint _amount) private{
  682.         balances[_from] -= _amount;
  683.         balances[_to] += _amount;
  684.     }
  685.  
  686.     function balanceOf(address _owner) public view returns (uint balance) {
  687.         return balances[_owner];
  688.     }
  689.  
  690.     function allowance(address src, address guy) public view returns (uint) {
  691.         return approvals[src][guy];
  692.     }
  693.  
  694.     function transferFrom(address src, address dst, uint wad) public returns (bool){
  695.         address sender = msg.sender;
  696.         require(approvals[src][sender] >=  wad, "That amount is not approved");
  697.         require(balances[src] >=  wad, "That amount is not available from this wallet");
  698.         if (src != sender) {
  699.             approvals[src][sender] -=  wad;
  700.         }
  701.         moveTokens(src,dst,wad);
  702.  
  703.         bytes memory empty;
  704.         emit Transfer(src, dst, wad, empty);
  705.  
  706.         return true;
  707.     }
  708.  
  709.     function approve(address guy, uint wad) public returns (bool) {
  710.         approvals[msg.sender][guy] = wad;
  711.  
  712.         emit Approval(msg.sender, guy, wad);
  713.  
  714.         return true;
  715.     }
  716.  
  717.     event Approval(address indexed src, address indexed guy, uint wad);
  718. }
  719.  
  720. /**
  721.  * @title Common
  722.  * @dev Math operations with safety checks that throw on error
  723.  */
  724. library Common {
  725.     /**
  726.     * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
  727.     */
  728.     function subtract(uint256 a, uint256 b) internal pure returns (uint256) {
  729.         assert(b <= a);
  730.         return a - b;
  731.     }
  732.  
  733.     //assemble the given address bytecode. If bytecode exists then the _addr is a contract.
  734.     function isContract(address _addr) public view returns (bool is_contract) {
  735.         uint length;
  736.         assembly {
  737.             //retrieve the size of the code on target address, this needs assembly
  738.             length := extcodesize(_addr)
  739.         }
  740.         if(length>0) {
  741.             return true;
  742.         }else {
  743.             return false;
  744.         }
  745.     }
  746. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top