Advertisement
Guest User

Attempted Basic Contract API

a guest
Feb 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.28 KB | None | 0 0
  1. pragma solidity ^ 0.4 .18;
  2. contract SoulPyramid{
  3.  
  4. uint256 constant scaleFactor = 0x10000000000000000;
  5. int constant crr_n = 1; // CRR numerator
  6. int constant crr_d = 2; // CRR denominator
  7. int constant price_coeff = -0x296ABF784A358468C;
  8. string constant public name = "Soulecule Pyramid";
  9. string constant public symbol = "SOULPYR";
  10. uint8 constant public decimals = 18;
  11. mapping(address => uint256) public tokenBalance;
  12. mapping(address => int256) public payouts;
  13. uint256 public totalSupply;
  14. int256 totalPayouts;
  15. uint256 earningsPerToken;
  16. uint256 public contractBalance;
  17. gemsToken souleculeGemsSource;
  18.  
  19.  
  20. function SoulPyramid() public{
  21. souleculeGemsSource = gemsToken();
  22. }
  23.  
  24. /*function createSoul(uint _value, address _wallet) internal {
  25. souleculeGemsSource.generateSoul(_value, _wallet);
  26. }*/
  27.  
  28. function totalSupply() constant returns(uint _totalSupply) {
  29. _totalSupply = totalSupply;
  30. }
  31.  
  32. function balanceOf(address _owner) public constant returns(uint256 balance) {
  33. return tokenBalance[_owner];
  34. }
  35.  
  36. function withdraw() public {
  37. var balance = dividends(msg.sender);
  38. payouts[msg.sender] += (int256)(balance * scaleFactor);
  39. totalPayouts += (int256)(balance * scaleFactor);
  40. contractBalance = sub(contractBalance, balance);
  41. msg.sender.transfer(balance);
  42. }
  43.  
  44. function reinvestDividends() public {
  45. var balance = dividends(msg.sender);
  46. payouts[msg.sender] += (int256)(balance * scaleFactor);
  47. totalPayouts += (int256)(balance * scaleFactor);
  48. uint value_ = (uint)(balance);
  49. if (value_ < 0.000001 ether || value_ > 1000000 ether)
  50. revert();
  51. var sender = msg.sender;
  52. var res = reserve() - balance;
  53. var fee = div(value_, 10);
  54. var numEther = value_ - fee;
  55.  
  56. var numTokens = calculateDividendTokens(numEther, balance);
  57.  
  58.  
  59. var buyerFee = fee * scaleFactor;
  60. if (totalSupply > 0) {
  61. var bonusCoEff =
  62. (scaleFactor - (res + numEther) * numTokens * scaleFactor / (totalSupply + numTokens) / numEther) *
  63. (uint)(crr_d) / (uint)(crr_d - crr_n);
  64. var holderReward = fee * bonusCoEff;
  65.  
  66. buyerFee -= holderReward;
  67.  
  68. var rewardPerShare = holderReward / totalSupply;
  69. earningsPerToken += rewardPerShare;
  70. }
  71.  
  72. totalSupply = add(totalSupply, numTokens);
  73. tokenBalance[sender] = add(tokenBalance[sender], numTokens);
  74. var payoutDiff = (int256)((earningsPerToken * numTokens) - buyerFee);
  75.  
  76. payouts[sender] += payoutDiff;
  77. totalPayouts += payoutDiff;
  78.  
  79. }
  80.  
  81. function sellMyTokens() public {
  82. var balance = balanceOf(msg.sender);
  83. //createSoul(balance, msg.sender);
  84. sell(balance);
  85. }
  86.  
  87. function getMeOutOfHere() public {
  88. sellMyTokens();
  89. withdraw();
  90. }
  91.  
  92. function mineSoul() public {
  93. sellMyTokens();
  94. reinvestDividends();
  95. }
  96.  
  97. function fund() payable public {
  98.  
  99. if (msg.value > 0.000001 ether) {
  100. contractBalance = add(contractBalance, msg.value);
  101. buy();
  102. } else {
  103. revert();
  104. }
  105. }
  106.  
  107. function buyPrice() public constant returns(uint) {
  108. return getTokensForEther(1 finney);
  109. }
  110.  
  111. function sellPrice() public constant returns(uint) {
  112. var eth = getEtherForTokens(1 finney);
  113. var fee = div(eth, 10);
  114. return eth - fee;
  115. }
  116.  
  117. function dividends(address _owner) public constant returns(uint256 amount) {
  118. return (uint256)((int256)(earningsPerToken * tokenBalance[_owner]) - payouts[_owner]) / scaleFactor;
  119. }
  120.  
  121. function withdrawOld(address to) public {
  122.  
  123. var balance = dividends(msg.sender);
  124.  
  125. payouts[msg.sender] += (int256)(balance * scaleFactor);
  126.  
  127. totalPayouts += (int256)(balance * scaleFactor);
  128.  
  129. contractBalance = sub(contractBalance, balance);
  130. to.transfer(balance);
  131. }
  132.  
  133. function balance() internal constant returns(uint256 amount) {
  134. return contractBalance - msg.value;
  135. }
  136.  
  137. function buy() internal {
  138. if (msg.value < 0.000001 ether || msg.value > 1000000 ether)
  139. revert();
  140. var sender = msg.sender;
  141. var fee = div(msg.value, 10);
  142. var numEther = msg.value - fee;
  143. var numTokens = getTokensForEther(numEther);
  144.  
  145. var buyerFee = fee * scaleFactor;
  146. if (totalSupply > 0) {
  147. var bonusCoEff =
  148. (scaleFactor - (reserve() + numEther) * numTokens * scaleFactor / (totalSupply + numTokens) / numEther) *
  149. (uint)(crr_d) / (uint)(crr_d - crr_n);
  150. var holderReward = fee * bonusCoEff;
  151.  
  152. buyerFee -= holderReward;
  153.  
  154. var rewardPerShare = holderReward / totalSupply;
  155. earningsPerToken += rewardPerShare;
  156.  
  157. }
  158. totalSupply = add(totalSupply, numTokens);
  159.  
  160. tokenBalance[sender] = add(tokenBalance[sender], numTokens);
  161. var payoutDiff = (int256)((earningsPerToken * numTokens) - buyerFee);
  162.  
  163. payouts[sender] += payoutDiff;
  164. totalPayouts += payoutDiff;
  165.  
  166. }
  167.  
  168. function sell(uint256 amount) internal {
  169. var numEthersBeforeFee = getEtherForTokens(amount);
  170. var fee = div(numEthersBeforeFee, 10);
  171.  
  172. var numEthers = numEthersBeforeFee; //- fee;
  173.  
  174. totalSupply = sub(totalSupply, amount);
  175.  
  176. tokenBalance[msg.sender] = sub(tokenBalance[msg.sender], amount);
  177.  
  178. var payoutDiff = (int256)(earningsPerToken * amount + (numEthers * scaleFactor));
  179.  
  180. payouts[msg.sender] -= payoutDiff;
  181.  
  182. totalPayouts -= payoutDiff;
  183.  
  184. if (totalSupply > 0) {
  185. var etherFee = fee * scaleFactor;
  186.  
  187. var rewardPerShare = etherFee / totalSupply;
  188.  
  189. earningsPerToken = add(earningsPerToken, rewardPerShare);
  190. }
  191. }
  192.  
  193. function reserve() internal constant returns(uint256 amount) {
  194. return sub(balance(),
  195. ((uint256)((int256)(earningsPerToken * totalSupply) - totalPayouts) / scaleFactor));
  196. }
  197.  
  198. function getTokensForEther(uint256 ethervalue) public constant returns(uint256 tokens) {
  199. return sub(fixedExp(fixedLog(reserve() + ethervalue) * crr_n / crr_d + price_coeff), totalSupply);
  200. }
  201.  
  202. function calculateDividendTokens(uint256 ethervalue, uint256 subvalue) public constant returns(uint256 tokens) {
  203. return sub(fixedExp(fixedLog(reserve() - subvalue + ethervalue) * crr_n / crr_d + price_coeff), totalSupply);
  204. }
  205.  
  206. function getEtherForTokens(uint256 tokens) public constant returns(uint256 ethervalue) {
  207. var reserveAmount = reserve();
  208.  
  209. if (tokens == totalSupply)
  210. return reserveAmount;
  211. return sub(reserveAmount, fixedExp((fixedLog(totalSupply - tokens) - price_coeff) * crr_d / crr_n));
  212. }
  213.  
  214. int256 constant one = 0x10000000000000000;
  215. uint256 constant sqrt2 = 0x16a09e667f3bcc908;
  216. uint256 constant sqrtdot5 = 0x0b504f333f9de6484;
  217. int256 constant ln2 = 0x0b17217f7d1cf79ac;
  218. int256 constant ln2_64dot5 = 0x2cb53f09f05cc627c8;
  219. int256 constant c1 = 0x1ffffffffff9dac9b;
  220. int256 constant c3 = 0x0aaaaaaac16877908;
  221. int256 constant c5 = 0x0666664e5e9fa0c99;
  222. int256 constant c7 = 0x049254026a7630acf;
  223. int256 constant c9 = 0x038bd75ed37753d68;
  224. int256 constant c11 = 0x03284a0c14610924f;
  225.  
  226. function fixedLog(uint256 a) internal pure returns(int256 log) {
  227. int32 scale = 0;
  228. while (a > sqrt2) {
  229. a /= 2;
  230. scale++;
  231. }
  232. while (a <= sqrtdot5) {
  233. a *= 2;
  234. scale--;
  235. }
  236. int256 s = (((int256)(a) - one) * one) / ((int256)(a) + one);
  237. var z = (s * s) / one;
  238. return scale * ln2 +
  239. (s * (c1 + (z * (c3 + (z * (c5 + (z * (c7 + (z * (c9 + (z * c11 / one)) /
  240. one)) / one)) / one)) / one)) / one);
  241. }
  242.  
  243. int256 constant c2 = 0x02aaaaaaaaa015db0;
  244. int256 constant c4 = -0x000b60b60808399d1;
  245. int256 constant c6 = 0x0000455956bccdd06;
  246. int256 constant c8 = -0x000001b893ad04b3a;
  247.  
  248. function fixedExp(int256 a) internal pure returns(uint256 exp) {
  249. int256 scale = (a + (ln2_64dot5)) / ln2 - 64;
  250. a -= scale * ln2;
  251. int256 z = (a * a) / one;
  252. int256 R = ((int256)(2) * one) +
  253. (z * (c2 + (z * (c4 + (z * (c6 + (z * c8 / one)) / one)) / one)) / one);
  254. exp = (uint256)(((R + a) * one) / (R - a));
  255. if (scale >= 0)
  256. exp <<= scale;
  257. else
  258. exp >>= -scale;
  259. return exp;
  260. }
  261.  
  262.  
  263. function mul(uint256 a, uint256 b) internal pure returns(uint256) {
  264. if (a == 0) {
  265. return 0;
  266. }
  267. uint256 c = a * b;
  268. assert(c / a == b);
  269. return c;
  270. }
  271.  
  272. function div(uint256 a, uint256 b) internal pure returns(uint256) {
  273. uint256 c = a / b;
  274. return c;
  275. }
  276.  
  277. function sub(uint256 a, uint256 b) internal pure returns(uint256) {
  278. assert(b <= a);
  279. return a - b;
  280. }
  281.  
  282. function add(uint256 a, uint256 b) internal pure returns(uint256) {
  283. uint256 c = a + b;
  284. assert(c >= a);
  285. return c;
  286. }
  287.  
  288. function() payable public {
  289. if (msg.value > 0) {
  290. fund();
  291. } else {
  292. withdrawOld(msg.sender);
  293. }
  294. }
  295. }
  296.  
  297.  
  298.  
  299. contract gemsToken {
  300. string constant public name = "Soul Gem";
  301. string constant public symbol = "SOULGEM";
  302. uint8 constant public decimals = 18;
  303.  
  304. uint private __totalSupply = 0;
  305. mapping (address => uint) private __balanceOf;
  306. mapping (address => mapping (address => uint)) private __allowances;
  307.  
  308. function gemsToken(){
  309. }
  310.  
  311. function totalSupply() constant returns (uint _totalSupply) {
  312. _totalSupply = __totalSupply;
  313. }
  314.  
  315. function generateSoul(uint _value, address _wallet) public {
  316. __balanceOf[_wallet] += _value;
  317. __totalSupply += _value;
  318. }
  319. function balanceOf(address _addr) constant returns (uint balance) {
  320. return __balanceOf[_addr];
  321. }
  322.  
  323. function transfer(address _to, uint _value) returns (bool success) {
  324. if (_value > 0 && _value <= balanceOf(msg.sender)) {
  325. __balanceOf[msg.sender] -= _value;
  326. __balanceOf[_to] += _value;
  327. return true;
  328. }
  329. return false;
  330. }
  331.  
  332. function transferFrom(address _from, address _to, uint _value) returns (bool success) {
  333. if (__allowances[_from][msg.sender] > 0 &&
  334. _value > 0 &&
  335. __allowances[_from][msg.sender] >= _value &&
  336. __balanceOf[_from] >= _value) {
  337. __balanceOf[_from] -= _value;
  338. __balanceOf[_to] += _value;
  339.  
  340. __allowances[_from][msg.sender] -= _value;
  341. return true;
  342. }
  343. return false;
  344. }
  345.  
  346. function approve(address _spender, uint _value) returns (bool success) {
  347. __allowances[msg.sender][_spender] = _value;
  348. return true;
  349. }
  350.  
  351. function allowance(address _owner, address _spender) constant returns (uint remaining) {
  352. return __allowances[_owner][_spender];
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement