Created
April 3, 2022 03:41
-
-
Save korrio/6366c099216f83a88b895d21385c591f to your computer and use it in GitHub Desktop.
GameConsumer.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
// An example of a consumer contract that relies on a subscription for funding. | |
pragma solidity ^0.8.7; | |
import "@chainlink/contracts/src/v0.8/interfaces/LinkTokenInterface.sol"; | |
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol"; | |
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol"; | |
library SafeMath { | |
/** | |
* @dev Returns the addition of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
uint256 c = a + b; | |
if (c < a) return (false, 0); | |
return (true, c); | |
} | |
/** | |
* @dev Returns the substraction of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b > a) return (false, 0); | |
return (true, a - b); | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, with an overflow flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
if (a == 0) return (true, 0); | |
uint256 c = a * b; | |
if (c / a != b) return (false, 0); | |
return (true, c); | |
} | |
/** | |
* @dev Returns the division of two unsigned integers, with a division by zero flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b == 0) return (false, 0); | |
return (true, a / b); | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag. | |
* | |
* _Available since v3.4._ | |
*/ | |
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) { | |
if (b == 0) return (false, 0); | |
return (true, a % b); | |
} | |
/** | |
* @dev Returns the addition of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `+` operator. | |
* | |
* Requirements: | |
* | |
* - Addition cannot overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b <= a, "SafeMath: subtraction overflow"); | |
return a - b; | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `*` operator. | |
* | |
* Requirements: | |
* | |
* - Multiplication cannot overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) return 0; | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers, reverting on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b > 0, "SafeMath: division by zero"); | |
return a / b; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* reverting when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b > 0, "SafeMath: modulo by zero"); | |
return a % b; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
* overflow (when the result is negative). | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {trySub}. | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b <= a, errorMessage); | |
return a - b; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers, reverting with custom message on | |
* division by zero. The result is rounded towards zero. | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {tryDiv}. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b > 0, errorMessage); | |
return a / b; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* reverting with custom message when dividing by zero. | |
* | |
* CAUTION: This function is deprecated because it requires allocating memory for the error | |
* message unnecessarily. For custom revert reasons use {tryMod}. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { | |
require(b > 0, errorMessage); | |
return a % b; | |
} | |
} | |
contract GameConsumeRandom is VRFConsumerBaseV2 { | |
VRFCoordinatorV2Interface COORDINATOR; | |
LinkTokenInterface LINKTOKEN; | |
// Your subscription ID. | |
uint64 s_subscriptionId; | |
// Rinkeby coordinator. For other networks, | |
// see https://docs.chain.link/docs/vrf-contracts/#configurations | |
address vrfCoordinator = 0x6168499c0cFfCaCD319c818142124B7A15E857ab; | |
// Rinkeby LINK token contract. For other networks, | |
// see https://docs.chain.link/docs/vrf-contracts/#configurations | |
address link = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709; | |
// The gas lane to use, which specifies the maximum gas price to bump to. | |
// For a list of available gas lanes on each network, | |
// see https://docs.chain.link/docs/vrf-contracts/#configurations | |
bytes32 keyHash = 0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc; | |
// Depends on the number of requested values that you want sent to the | |
// fulfillRandomWords() function. Storing each word costs about 20,000 gas, | |
// so 100,000 is a safe default for this example contract. Test and adjust | |
// this limit based on the network that you select, the size of the request, | |
// and the processing of the callback request in the fulfillRandomWords() | |
// function. | |
uint32 callbackGasLimit = 100000; | |
// The default is 3, but you can set this higher. | |
uint16 requestConfirmations = 3; | |
// For this example, retrieve 2 random values in one request. | |
// Cannot exceed VRFCoordinatorV2.MAX_NUM_WORDS. | |
uint32 numWords = 2; | |
uint256[] public s_randomWords; | |
uint256 public s_requestId; | |
address s_owner; | |
constructor(uint64 subscriptionId) VRFConsumerBaseV2(vrfCoordinator) { | |
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator); | |
LINKTOKEN = LinkTokenInterface(link); | |
s_owner = msg.sender; | |
s_subscriptionId = subscriptionId; | |
} | |
// Assumes the subscription is funded sufficiently. | |
function draw() external onlyOwner returns (uint8[6] memory) { | |
require(s_requestId != 0, "Please random first"); | |
// Will revert if subscription is not set and funded. | |
s_requestId = COORDINATOR.requestRandomWords( | |
keyHash, | |
s_subscriptionId, | |
requestConfirmations, | |
callbackGasLimit, | |
numWords | |
); | |
uint8[6] memory huay = drawNewHuay(0,s_requestId); | |
return huay; | |
} | |
function drawNewDecentralizedHuay() public view returns (uint8[6] memory) { | |
require(s_requestId != 0, "Please random first"); | |
uint8[6] memory huay = drawNewHuay(0,s_requestId); | |
return huay; | |
} | |
function drawNewHuay(uint tokenId, uint256 _externalRandomNumber) public view returns (uint8[6] memory) { | |
uint8 _maxNumber = 9; | |
bytes32 _blockhash = blockhash(block.number - 1); | |
return [ | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, msg.sender , tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.number, tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.timestamp, tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.timestamp % 15, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.timestamp % 20, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.timestamp % 100, _externalRandomNumber))),_maxNumber),1)) | |
]; | |
} | |
function fulfillRandomWords( | |
uint256, /* requestId */ | |
uint256[] memory randomWords | |
) internal override { | |
s_randomWords = randomWords; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == s_owner); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment