jackalchenxu / jackalchenxu

my blog
MIT License
4 stars 0 forks source link

DailyProgress_0808 #22

Open jackalchenxu opened 2 years ago

jackalchenxu commented 2 years ago

ChainLink提供VRF服务的介绍

目前ChainLink VRF只在以太坊上有支持

V2协议规定了几个角色:

所以这样的模型,把支付者, 使用者,管理者的角色分开了,能支持客户灵活的业务逻辑

Chainlink还有每次使用VRF的gas和费用的limit的更详细的规定,这里就不讲述了。

使用VRF也很简单,在以太坊上部署一个合约,可以参考ChainLink的demo, 我添加一些简单的描述:

// 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/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";

/**
 * 切勿在生产环境中使用.
 */

contract VRFv2Consumer is VRFConsumerBaseV2 {
  VRFCoordinatorV2Interface COORDINATOR;

  // Your subscription ID.
  uint64 s_subscriptionId;

  // Rinkeby coordinator.   跟我们contract使用VRF对接的ChainLink合约方(ChainLink管它叫coordinator)
  // see https://docs.chain.link/docs/vrf-contracts/#configurations
  address vrfCoordinator = 0x6168499c0cFfCaCD319c818142124B7A15E857ab;

  // gas费用和callback等费用的设置,这里就不写了
  bytes32 keyHash = 0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc;
  ..................
  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;    //部署合约的账户,即为Subscription Owner,它也有权修改订阅服务列表

  constructor(uint64 subscriptionId) VRFConsumerBaseV2(vrfCoordinator) {
    COORDINATOR = VRFCoordinatorV2Interface(vrfCoordinator);
    s_owner = msg.sender;
    s_subscriptionId = subscriptionId;
  }

  // Assumes the subscription is funded sufficiently.
  // 调用获取VRF的入口函数
  function requestRandomWords() external onlyOwner {
    // Will revert if subscription is not set and funded.
    s_requestId = COORDINATOR.requestRandomWords(
      keyHash,
      s_subscriptionId,
      requestConfirmations,
      callbackGasLimit,
      numWords
    );
  }

  // 该函数是留给coordinator完成VRF后callback contract的接口
  function fulfillRandomWords(
    uint256, /* requestId */
    uint256[] memory randomWords
  ) internal override {
    s_randomWords = randomWords;
  }

  modifier onlyOwner() {
    require(msg.sender == s_owner);
    _;
  }
}

至于Subscription Owner的角色,基本操作就是创建Subscription account,为Subscription account充值,维护将要使用VRF smart contract的地址列表,我们可以从web页面(Subscription Manager page)上来做,也可以用合约代码来做

  • Change the list of approved subscription consumers with addConsumer(uint64 subId, address consumer) or removeConsumer(uint64 subId, address consumer).
  • Transfer the subscription ownership with requestSubscriptionOwnerTransfer(uint64 subId, address newOwner) and acceptSubscriptionOwnerTransfer(uint64 subId).
  • View the subscription with getSubscription(uint64 subId). Cancel the subscription with cancelSubscription(uint64 subId).
  • To send LINK to the subscription balance, use the LINK token interface with LINKTOKEN.transferAndCall(address(COORDINATOR), amount, abi.encode(subId));. Any wallet can fund a subscription.

总体来说,ChainLink的文档写的很棒

jackalchenxu commented 2 years ago

cannot stand, hurry up to grasp Archor , from now!