在链链链接VRF中获取历史记录随机数的最佳实践是什么?

发布于 2025-02-08 09:09:07 字数 315 浏览 2 评论 0原文

在项目中,我依靠链链接VRF获取一个随机单词来决定抽奖奖得主,但是我未能在VRF订阅中保留最小链接余额,因此所有请求都在待处理中。 (我认为交易未成功发送,因此我错误地将功能调用)。

将一些链接令牌转移到我的VRF订阅后,将所有随机数发送给我的合同。在这种情况下,我只需要抽奖的这些随机单词中的第一个,但是我不确定现在存储在智能合约中的随机单词现在是第一个。

如果基于随机词的交易由于某种原因没有成功,那么在哪里可以在历史中找到随机词,以便可以重新进行这些交易?

所以我的问题是:

获得链链链接VRF生成的历史随机词的最佳实践是什么?

提前致谢。

In the project, I rely on Chainlink VRF to get a random word to decide a raffle winner, but I failed to keep the minimum LINK balance in my VRF subscription so all requests are pending. (I thought transactions are not sent successfully so I called the function several times by mistake).

After I transfer some Link tokens to my VRF subscription, all the random numbers are sent to my contract. In the scenario, I only need the first one of these random words for the raffle, but I am not sure if the random word stored in the smart contract now is the first one.

If transactions based on random words did not succeed for some reason, where to find random words in history so that these transactions can be re-sent?

So my question is:

What is the best practice to get history random words generated by Chainlink VRF?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪落纷纷 2025-02-15 09:09:07

进行了一些研究之后,我将回答这个问题。

最好的做法是将随机单词保存在智能合约中的数组中,或者您可以在消费者智能合约中添加履行函数中的事件,以记录请求ID和相应的随机单词。

但是,如果没有事件或其他变量可以在智能合约中记录随机单词,则可以执行以下操作:

  1. 从smart Contract vrfcoordinatorv2中获取equestID和随机性andarywordsfulsful ,可以在VRF节点发送的事务中找到。
emit RandomWordsFulfilled(requestId, randomness, payment, success);
  1. vrfcoordinatorv2智能合约中获取event andarwordsrequested的numwords。
    emit RandomWordsRequested(
      keyHash,
      requestId,
      preSeed,
      subId,
      requestConfirmations,
      callbackGasLimit,
      numWords,
      msg.sender
    );
  1. 随机单词是通过以下逻辑中的随机性和数字生成的,该逻辑在函数efferrandomwords中定义的smart Contract vrffoordinatorv2 。您可以使用相同的逻辑来获取历史记录随机单词。
for (uint256 i = 0; i < numWords; i++) {
  randomWords[i] = uint256(keccak256(abi.encode(randomness, i)));
}

After doing some research, I will answer the question.

The best practice is to keep random words in an array in your smart contract or you can add an event in fulfillment function in your consumer smart contract to record requestId and corresponding random words.

But if there is no event or other variables to record the random words in your smart contract, you can do the following:

  1. Get requestId and randomness from event RandomWordsFulfilled in smart contract VRFCoordinatorV2, it can be found in the transaction sent by VRF node.
emit RandomWordsFulfilled(requestId, randomness, payment, success);
  1. Get numWords from event RandomWordsRequested in VRFCoordinatorV2 smart contract.
    emit RandomWordsRequested(
      keyHash,
      requestId,
      preSeed,
      subId,
      requestConfirmations,
      callbackGasLimit,
      numWords,
      msg.sender
    );
  1. Random words are generated by randomness and numWords in the following logic that is defined in function fulfillRandomWords in smart contract VRFCoordinatorV2. You can use the same logic to get the history random words.
for (uint256 i = 0; i < numWords; i++) {
  randomWords[i] = uint256(keccak256(abi.encode(randomness, i)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文