Advertisement
Guest User

fundraiser

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