Guest User

Untitled

a guest
Sep 8th, 2021
17
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity 0.6.11;
  2.  
  3. import "github.com/provable-things/ethereum-api/blob/master/provableAPI_0.6.sol";
  4.  
  5. contract BettingSystem is usingProvable {
  6.  
  7. address payable owner;
  8.  
  9. constructor() public payable {
  10. owner = msg.sender;
  11. }
  12.  
  13. event LogMessage(string description);
  14.  
  15. mapping(string => address payable[]) winnerAddresses;
  16. mapping(string => mapping(address => uint256)) winnerAmounts;
  17. mapping(string => string) betUrls;
  18. mapping(string => address payable[]) betAddresses;
  19. mapping(string => string[]) betResults;
  20. mapping(string => uint256[]) betAmounts;
  21. mapping(string => uint) betDeadlines;
  22. mapping(bytes32 => string) betQueries;
  23. mapping(bytes32 => bool) finishedQueries;
  24.  
  25. function createBet(string calldata id, string calldata url, uint64 deadline, uint32 delay) external payable {
  26. require(bytes(id).length > 5 && bytes(betUrls[id]).length == 0 && bytes(url).length > 18 && deadline > block.timestamp);
  27. betUrls[id] = url;
  28. betDeadlines[id] = deadline;
  29. scheduleBet(id, url, delay);
  30. }
  31.  
  32. function placeBet(string calldata id, string calldata result) external payable {
  33. require(bytes(id).length > 5 && bytes(betUrls[id]).length != 0 && betDeadlines[id] > block.timestamp);
  34. betAddresses[id].push(msg.sender);
  35. betAmounts[id].push(msg.value);
  36. betResults[id].push(result);
  37. }
  38.  
  39. function scheduleBet(string calldata betId, string calldata betUrl, uint32 betDelay) internal {
  40. if (provable_getPrice("URL") > address(this).balance) {
  41. revertBet(betId);
  42. emit LogMessage("Provable query was NOT sent, please add some ETH to cover for the query fee");
  43. } else {
  44. bytes32 queryId = provable_query(betDelay, "URL", betUrl);
  45. LogMessage("Provable query was sent, standing by for the answer..");
  46. betQueries[queryId] = betId;
  47. }
  48. }
  49.  
  50. function __callback(bytes32 queryId, string calldata queryResult) override public {
  51. emit LogMessage("Executing callback.");
  52. require(msg.sender == provable_cbAddress() && !finishedQueries[queryId]);
  53. finishedQueries[queryId] = true;
  54. string memory betId = betQueries[queryId];
  55. uint256 loserPool = 0;
  56. uint256 winnerPool = 0;
  57.  
  58. for (uint i = 0; i < betAddresses[betId].length; i++) {
  59. address payable player = betAddresses[betId][i];
  60. uint256 amount = betAmounts[betId][i];
  61. string memory result = betResults[betId][i];
  62. if (strcmp(result, queryResult)) {
  63. winnerAddresses[betId].push(player);
  64. winnerAmounts[betId][player] += amount;
  65. winnerPool += amount;
  66. } else {
  67. loserPool += amount;
  68. }
  69. }
  70.  
  71. if (winnerPool == 0) {
  72. for (uint i = 0; i < betAddresses[betId].length; i++) {
  73. address payable player = betAddresses[betId][i];
  74. uint256 refund = betAmounts[betId][i];
  75. uint256 fee = refund / 20;
  76. refund -= fee;
  77. owner.transfer(fee);
  78. player.transfer(refund);
  79. }
  80. } else {
  81. for (uint i = 0; i < winnerAddresses[betId].length; i++) {
  82. address payable winner = winnerAddresses[betId][i];
  83. uint256 winnerAmount = winnerAmounts[betId][winner];
  84. uint256 fraction = winnerPool / winnerAmount;
  85. uint256 reward = loserPool / fraction + winnerAmount;
  86. uint256 fee = reward / 100;
  87. reward -= fee;
  88. owner.transfer(fee);
  89. winner.transfer(reward);
  90. }
  91. }
  92. }
  93.  
  94. function memcmp(bytes memory a, bytes memory b) internal pure returns(bool){
  95. return (a.length == b.length) && (keccak256(a) == keccak256(b));
  96. }
  97.  
  98. function strcmp(string memory a, string memory b) internal pure returns(bool){
  99. return memcmp(bytes(a), bytes(b));
  100. }
  101.  
  102. function revertBet(string calldata id) internal {
  103. betUrls[id] = "";
  104. betDeadlines[id] = 0;
  105. }
  106. }
RAW Paste Data