Advertisement
Guest User

Untitled

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