Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainlink VRF V2 升级至 VRF V2.5 #816

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ artifacts
.history
**node_modules**
**cache**
**artifacts**
**artifacts**
.deps
52 changes: 29 additions & 23 deletions 39_Random/Random.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,44 @@
pragma solidity ^0.8.21;

import "https://github.com/AmazingAng/WTF-Solidity/blob/main/34_ERC721/ERC721.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";

contract Random is ERC721, VRFConsumerBaseV2{
contract Random is ERC721, VRFConsumerBaseV2Plus{
// NFT相关
uint256 public totalSupply = 100; // 总供给
uint256[100] public ids; // 用于计算可供mint的tokenId
uint256 public mintCount; // 已mint数量

// chainlink VRF参数

//VRFCoordinatorV2Interface
VRFCoordinatorV2Interface COORDINATOR;

/**
* 使用chainlink VRF,构造函数需要继承 VRFConsumerBaseV2
* 使用chainlink VRF,构造函数需要继承 VRFConsumerBaseV2Plus
* 不同链参数填的不一样
* 网络: Sepolia测试网
* Chainlink VRF Coordinator 地址: 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625
* LINK 代币地址: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* 30 gwei Key Hash: 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c
* Chainlink VRF Coordinator 地址: 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B
* LINK 代币地址: 0x779877a7b0d9e8603169ddbd7836e478b4624789
* 30 gwei Key Hash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae
* Minimum Confirmations 最小确认块数 : 3 (数字大安全性高,一般填12)
* callbackGasLimit gas限制 : 最大 2,500,000
* Maximum Random Values 一次可以得到的随机数个数 : 最大 500
*/
address vrfCoordinator = 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625;
bytes32 keyHash = 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c;
address vrfCoordinator = 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B;
bytes32 keyHash = 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae;
uint16 requestConfirmations = 3;
uint32 callbackGasLimit = 1_000_000;
uint32 numWords = 1;
uint64 subId;
//订阅ID类型已从VRF V2中的uint64变为VRF V2.5中的uint256
uint256 subId;
uint256 public requestId;

// 记录VRF申请标识对应的mint地址
mapping(uint256 => address) public requestToSender;

constructor(uint64 s_subId)
VRFConsumerBaseV2(vrfCoordinator)
constructor(uint256 s_subId)
VRFConsumerBaseV2Plus(vrfCoordinator)
ERC721("WTF Random", "WTF"){
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
subId = s_subId;
}

Expand Down Expand Up @@ -87,12 +85,20 @@ contract Random is ERC721, VRFConsumerBaseV2{
*/
function mintRandomVRF() public {
// 调用requestRandomness获取随机数
requestId = COORDINATOR.requestRandomWords(
keyHash,
subId,
requestConfirmations,
callbackGasLimit,
numWords
requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest(
{
keyHash:keyHash,
subId:subId,
requestConfirmations: requestConfirmations,
callbackGasLimit: callbackGasLimit,
numWords: numWords,
extraArgs: VRFV2PlusClient._argsToBytes(
//此为是否指定原生代币如ETH等,来支付VRF请求的费用,当为false表示使用LINK代币支付
VRFV2PlusClient.ExtraArgsV1({nativePayment: false})
)
}
)
);
requestToSender[requestId] = msg.sender;
}
Expand All @@ -101,8 +107,8 @@ contract Random is ERC721, VRFConsumerBaseV2{
* VRF的回调函数,由VRF Coordinator调用
* 消耗随机数的逻辑写在本函数中
*/
function fulfillRandomWords(uint256 requestId, uint256[] memory s_randomWords) internal override{
address sender = requestToSender[requestId]; // 从requestToSender中获取minter用户地址
function fulfillRandomWords(uint256 _requestId, uint256[] calldata s_randomWords) internal override{
address sender = requestToSender[_requestId]; // 从requestToSender中获取minter用户地址
uint256 tokenId = pickRandomUniqueId(s_randomWords[0]); // 利用VRF返回的随机数生成tokenId
_mint(sender, tokenId);
}
Expand Down
50 changes: 27 additions & 23 deletions 39_Random/RandomNumberConsumer.sol
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;

import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";

contract RandomNumberConsumer is VRFConsumerBaseV2{

//请求随机数需要调用VRFCoordinatorV2Interface接口
VRFCoordinatorV2Interface COORDINATOR;
contract RandomNumberConsumer is VRFConsumerBaseV2Plus{

// 申请后的subId
uint64 subId;
//订阅ID类型已从VRF V2中的uint64变为VRF V2.5中的uint256
uint256 subId;

//存放得到的 requestId 和 随机数
uint256 public requestId;
uint256[] public randomWords;

/**
* 使用chainlink VRF,构造函数需要继承 VRFConsumerBaseV2
* 使用chainlink VRF,构造函数需要继承 VRFConsumerBaseV2Plus
* 不同链参数填的不一样
* 具体可以看:https://docs.chain.link/vrf/v2/subscription/supported-networks
* 网络: Sepolia测试网
* Chainlink VRF Coordinator 地址: 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625
* LINK 代币地址: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* 30 gwei Key Hash: 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c
* Chainlink VRF Coordinator 地址: 0x9ddfaca8183c41ad55329bdeed9f6a8d53168b1b
* LINK 代币地址: 0x779877a7b0d9e8603169ddbd7836e478b4624789
* 30 gwei Key Hash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae
* Minimum Confirmations 最小确认块数 : 3 (数字大安全性高,一般填12)
* callbackGasLimit gas限制 : 最大 2,500,000
* Maximum Random Values 一次可以得到的随机数个数 : 最大 500
*/
address vrfCoordinator = 0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625;
bytes32 keyHash = 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c;
address vrfCoordinator = 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B;
bytes32 keyHash = 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae;
uint16 requestConfirmations = 3;
uint32 callbackGasLimit = 200_000;
uint32 numWords = 3;

constructor(uint64 s_subId) VRFConsumerBaseV2(vrfCoordinator){
COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
constructor(uint256 s_subId) VRFConsumerBaseV2Plus(vrfCoordinator){
subId = s_subId;
}

/**
* 向VRF合约申请随机数
*/
function requestRandomWords() external {
requestId = COORDINATOR.requestRandomWords(
keyHash,
subId,
requestConfirmations,
callbackGasLimit,
numWords
requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest(
{
keyHash:keyHash,
subId:subId,
requestConfirmations: requestConfirmations,
callbackGasLimit: callbackGasLimit,
numWords: numWords,
extraArgs: VRFV2PlusClient._argsToBytes(
//此为是否指定原生代币如ETH等,来支付VRF请求的费用,当为false表示使用LINK代币支付
VRFV2PlusClient.ExtraArgsV1({nativePayment: false})
)
}
)
);
}

/**
* VRF合约的回调函数,验证随机数有效之后会自动被调用
* 消耗随机数的逻辑写在这里
*/
function fulfillRandomWords(uint256 requestId, uint256[] memory s_randomWords) internal override {
function fulfillRandomWords(uint256 _requestId, uint256[] calldata s_randomWords) internal override {
randomWords = s_randomWords;
}

Expand Down
3 changes: 3 additions & 0 deletions 39_Random/VRFCoordinatorV2_5Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
Binary file modified 39_Random/img/39-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 39_Random/img/39-11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 39_Random/img/39-12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 39_Random/img/39-13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 39_Random/img/39-14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading