Guest User

Untitled

a guest
Sep 17th, 2021
23
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SPDX-License-Identifier: GPL-3.0
  2.  
  3. pragma solidity 0.6.11;
  4.  
  5. import "github.com/provable-things/ethereum-api/blob/master/provableAPI_0.6.sol";
  6.  
  7. contract BettingSystem is usingProvable {
  8.  
  9. address payable owner;
  10.  
  11. event LogMessage(string);
  12.  
  13.  
  14. constructor() public payable {
  15. owner = msg.sender;
  16. }
  17.  
  18. function createBet(string calldata betId, string calldata url, uint64 deadline, uint64 schedule) public payable {
  19. require(deadline <= schedule);
  20. if (createdBets[betId]) {
  21. emit LogMessage("ERROR: Duplicated BetId");
  22. return;
  23. }
  24. scheduleBet(betId, url, deadline, schedule);
  25. }
  26.  
  27. // BetId -> Result -> Total pooled per result
  28. mapping(string => mapping(string => uint256)) resultPools;
  29.  
  30. // BetId -> Total pooled
  31. mapping(string => uint256) betPools;
  32.  
  33. // BetId -> User -> Result -> User bet per result
  34. mapping(string => mapping(address => mapping(string => uint256))) resultAmounts;
  35.  
  36.  
  37. function placeBet(string calldata betId, string calldata result) public payable {
  38. require(betDeadlines[betId] > block.timestamp && createdBets[betId] && !finishedBets[betId]);
  39. resultPools[betId][result] += msg.value;
  40. betPools[betId] += msg.value;
  41. resultAmounts[betId][msg.sender][result] += msg.value;
  42. }
  43.  
  44.  
  45. mapping(string => mapping(address => bool)) claimedBets;
  46. function reclaimBet(string calldata betId) public {
  47. require(finishedBets[betId] && !claimedBets[betId][msg.sender]);
  48. claimedBets[betId][msg.sender] = true;
  49. string memory result = betResults[betId];
  50. uint256 userBet = resultAmounts[betId][msg.sender][result];
  51. if (userBet < 2) {
  52. return;
  53. }
  54. uint256 winnerPool = resultPools[betId][result];
  55. uint256 loserPool = betPools[betId] - winnerPool;
  56. uint256 reward = loserPool / (winnerPool / userBet) + userBet;
  57. uint256 fee = (reward / 100) + 2;
  58. reward -= fee;
  59. if (reward != 0) {
  60. msg.sender.transfer(reward);
  61. }
  62. owner.transfer(fee / 2);
  63. }
  64.  
  65. // BetId -> Deadline to place new bets
  66. mapping(string => uint64) betDeadlines;
  67. // BetId -> Scheduled time to run
  68. mapping(string => uint64) betSchedules;
  69.  
  70. mapping(string => bool) createdBets;
  71. mapping(bytes32 => string) queryBets;
  72.  
  73. function scheduleBet(string calldata betId, string calldata url, uint64 deadline, uint64 schedule) internal {
  74. if (provable_getPrice("URL") > msg.value) {
  75. emit LogMessage("ERROR: Bet creation not successful due to lack of funds.");
  76. return;
  77. } else {
  78. bytes32 queryId;
  79. if (deadline == 0) {
  80. provable_query("URL", url);
  81. } else {
  82. provable_query(schedule, "URL", url);
  83. }
  84. queryBets[queryId] = betId;
  85. betSchedules[betId] = schedule;
  86. betDeadlines[betId] = deadline;
  87. createdBets[betId] = true;
  88. }
  89. }
  90.  
  91. mapping(string => bool) finishedBets;
  92. mapping(string => string) betResults;
  93. function __callback(bytes32 queryId, string memory result) override public {
  94. emit LogMessage("Callback executed");
  95. require(msg.sender == provable_cbAddress());
  96. string memory betId = queryBets[queryId];
  97. betResults[betId] = result;
  98. finishedBets[betId] = true;
  99. }
  100. }
RAW Paste Data