Cách tích hợp Chainlink VRF
Chainlinker /
Để tích hợp Chainlink VRF vào dự án của bạn, bạn sẽ cần theo các bước sau. Lưu ý rằng ví dụ này sẽ sử dụng Ethereum với Solidity nhưng quy trình tương tự có thể áp dụng cho các blockchain khác hỗ trợ Chainlink:
1. Thiết Lập Môi Trường Phát Triển
Cài đặt Truffle (hoặc một framework phát triển khác như Hardhat):
npm install -g truffle
Tạo một dự án Truffle:
truffle init
2. Cài Đặt Chainlink Contracts
Thêm các hợp đồng của Chainlink vào dự án:
npm install @chainlink/contracts
3. Viết Smart Contract
Tạo một file Solidity mới trong thư mục contracts, ví dụ: RandomNumberConsumer.sol
. Dưới đây là một ví dụ cơ bản:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
contract RandomNumberConsumer is VRFConsumerBaseV2 {
VRFCoordinatorV2Interface COORDINATOR;
// Your subscription ID.
uint64 s_subscriptionId;
// 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 = 0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15;
// Depends on the number of requested values that you want sent to the
// fulfillRandomWords() function. Test and adjust
// this limit based on the network that you select, gas price, and complexity of the callback.
uint32 callbackGasLimit = 100000;
// The default is 3, but you can set this higher.
uint16 requestConfirmations = 3;
// For this example, we will request 1 random value
uint32 numWords = 1;
uint256[] public randomWords;
uint256 public requestId;
constructor(uint64 subscriptionId) VRFConsumerBaseV2(0x6168499c0cFfCaCD319c818142124B7A15E857ab) {
COORDINATOR = VRFCoordinatorV2Interface(0x6168499c0cFfCaCD319c818142124B7A15E857ab);
s_subscriptionId = subscriptionId;
}
// Assumes the subscription is funded sufficiently.
function requestRandomWords() external {
requestId = COORDINATOR.requestRandomWords(
keyHash,
s_subscriptionId,
requestConfirmations,
callbackGasLimit,
numWords
);
}
function fulfillRandomWords(uint256, uint256[] memory _randomWords) internal override {
randomWords = _randomWords;
}
// To get the random number after it has been generated
function getRandomNumber() public view returns (uint256) {
require(randomWords.length > 0, "No random words have been generated yet.");
return randomWords[0];
}
}
4. Cấu Hình Truffle và Triển Khai Hợp Đồng
- Cấu hình
truffle-config.js
với các thông tin mạng mà bạn muốn triển khai hợp đồng (ví dụ: Rinkeby, Mumbai, hoặc mạng Ethereum chính). - Tạo một tài khoản Chainlink Subscription và lấy subscriptionId. Bạn sẽ cần này để khởi tạo hợp đồng của bạn.
- Triển khai hợp đồng:
truffle migrate --network <your-network>
5. Tương tác với Hợp Đồng
Sử dụng Truffle Console hoặc một dApp front-end để gọi phương thức requestRandomWords()
để yêu cầu một số ngẫu nhiên. Sau đó, bạn có thể gọi getRandomNumber()
để lấy số ngẫu nhiên đã được tạo.
6. Xử lý Phí và Quản lý Subscription
Đảm bảo rằng subscription của bạn có đủ LINK để trả cho các yêu cầu VRF. Bạn có thể cần phải chuyển LINK vào subscription của bạn thông qua giao diện Chainlink
.
Lưu ý:
Key Hash và Địa Chỉ Coordinator: Các giá trị này khác nhau tùy thuộc vào mạng bạn đang triển khai. Bạn cần kiểm tra tài liệu Chainlink để lấy các giá trị chính xác cho mạng của mình.
Security: Smart Contract sử dụng VRF phải được kiểm tra kỹ lưỡng về bảo mật, vì giá trị ngẫu nhiên có thể ảnh hưởng trực tiếp đến logic của ứng dụng của bạn.
Performance: Điều chỉnh callbackGasLimit
và requestConfirmations
dựa trên nhu cầu cụ thể của bạn để đảm bảo hiệu suất tốt mà không lãng phí tài nguyên.
Đây là một cách tiếp cận cơ bản để tích hợp Chainlink VRF. Các dự án phức tạp hơn có thể yêu cầu thêm các phương pháp bảo mật, kiểm tra hiệu suất, và quản lý rủi ro.