[Chainlink] 如何在Polygon上获取随机数

AI数据  收藏
0 / 706

[Chainlink] How To Get A Random Number On Polygon

Breaking News

By Chainlink News 03/30/21 5:49 PM

How to Get a Random Number on Polygon

Polygon (formerly known as Matic Network) is a protocol and a framework for building and connecting Ethereum-compatible blockchain networks. Initially focusing on Ethereum scaling solutions that utilize Proof of Stake (PoS) sidechains and a Plasma framework, in rebranding to Polygon, they’ve also added other major scaling solutions such as zk-Rollups and Optimistic Rollups, as well as inter-chain communication protocols.
Polygon (以前称为Matic Network)是用于建立和连接以太坊兼容区块链网络的协议和框架。 最初侧重于利用权益证明(PoS)侧链和等离子框架的以太坊扩展解决方案,在更名为Polygon之后,他们还添加了其他主要的扩展解决方案,例如zk-Rollups和Optimistic Rollups,以及链间通信协议。

As the most widely adopted decentralized oracle network, Chainlink is the preferred oracle network within Polygon for securely obtaining external data and using tamper-proof data feeds. Recently, Chainlink VRF went live on Polygon mainnet, offering developers the ability to request verifiably random numbers in their smart contracts. In this technical tutorial, we’ll show you how to deploy a smart contract to Polygon and obtain a provably random number using Chainlink VRF.

多边形(以前称为Matic Network)是用于建立和连接以太坊兼容区块链网络的协议和框架。 最初侧重于利用权益证明(PoS)侧链和等离子框架的以太坊扩展解决方案,在更名为Polygon之后,他们还添加了其他主要的扩展解决方案,例如zk-Rollups和Optimistic Rollups,以及链间通信协议。作为最广泛采用的分散式Oracle网络,Chainlink是Polygon中首选的Oracle网络,用于安全地获取外部数据并使用防篡改数据源。 最近,Chainlink VRF在Polygon主网上上线,为开发人员提供了在其智能合约中请求可验证的随机数的功能。 在本技术教程中,我们将向您展示如何将智能合约部署到Polygon并使用Chainlink VRF获得可证明的随机数。

The Need for Verifiable Randomness 为什么需要可以验证的随机性

Chainlink VRF is a provably fair and verifiable source of randomness designed for smart contracts. Solidity developers can use it as a tamper-proof random number generator to build safe and reliable smart contracts for Ethereum applications that rely on unpredictable outcomes. Critically, Chainlink VRF not only provides random numbers, but also cryptographic proof that the number has not been tampered with to generate certain predictable outcomes.

Chainlink VRF是为智能合约设计的可证明公平且可验证的随机性来源。Solidity 开发人员可以将其用作防篡改随机数生成器,为依赖不可预测结果的以太坊应用程序构建安全可靠的智能合约。至关重要的是,Chainlink VRF 不仅提供随机数,而且还提供加密证明该数字未被篡改以产生某些可预测的结果。

Having verifiably random numbers in smart contracts enables developers to build for a wide array of use cases, such as randomness in gaming, random rewards in NFTs, lotteries, and Fair Participant or Random Node Selection.

在智能合约中具有可验证的随机数使开发人员能够构建广泛的用例,例如游戏中的随机性、NFT 中的随机奖励、彩票以及公平参与者或随机节点选择

One noteworthy real-world example is the NFT gaming dApp Aavegotchi, which integrated Chainlink VRF as its source of provably fair randomness to help determine an Aavegotchi’s unique characteristics, generate unpredictable game scenarios, and randomly select DAO jurors. Aavegotchi has launched on Polygon’s layer 2 PoS chain, allowing them to cost-effectively scale to meet user demand thanks to near zero transaction fees and fast settlement times.

一个值得注意的现实例子是NFT游戏 dApp ​​Aavegotchi,它集成了Chainlink VRF作为可证明公平随机性的来源,以帮助确定 Aavegotchi 的独特特征,生成不可预测的游戏场景,并随机选择 DAO 陪审员。Aavegotchi 已在 Polygon 的第 2 层 PoS 链上推出,由于接近零的交易费用和快速的结算时间,使他们能够经济高效地扩展以满足用户需求。

Now that we understand the use cases that benefit from verifiable randomness, we’ll walk through how to obtain a verifiable random number on a smart contract on the Polygon Mumbai Testnet.

现在我们了解了受益于可验证随机性的用例,我们将介绍如何在 Polygon Mumbai 测试网上的智能合约上获取可验证的随机数。

Connecting to Polygon Mumbai Testnet 连接Mumbai 测试网

The first step in using Chainlink VRF on Polygon’s PoS chain is to set up your MetaMask wallet to connect to the Matic Mumbai Testnet. You can do this by selecting ‘Networks’, then choosing ‘Custom RPC’ and entering the following details:

在Polygon的PoS链上使用Chainlink VRF的第一步是设置您的MetaMask钱包,以连接到Matic Mumbai Testnet。 您可以通过选择“网络”,然后选择“自定义RPC”并输入以下详细信息来执行此操作:

How to Get a Random Number on Polygon
Entering the Mumbai Testnet details

MATIC is the native token of the Polygon network and is needed to interact with the network, similar to how ETH is used as gas for Ethereum. To obtain Mumbai Testnet MATIC, head over to the MATIC Faucet, select the MATIC token and the Mumbai Testnet network, enter in your MetaMask Wallet address and press Submit.

MATIC是Polygon网络的原生令牌,与网络交互需要它,类似于ETH 被用作以太坊气体的方式。 要获取孟买Testnet MATIC,请转到MATIC龙头,选择MATIC令牌和Mumbai Testnet网络,输入您的MetaMask电子钱包地址,然后按Submit。

Similar to the MATIC token, the LINK token is required as payment to the VRF coordinator for fulfilling a random number request. To obtain testnet LINK, simply choose the LINK token in the faucet instead of the MATIC token.

与MATIC令牌类似,需要LINK令牌作为向VRF协调器支付的费用,以实现随机数请求。 要获得testnet LINK,只需在水龙头中选择LINK令牌而不是MATIC令牌。

How to Get a Random Number on Polygon

image.png

Obtaining Mumbai Testnet MATIC and LINK

While the process for using the faucet to obtain LINK on the Mumbai Testnet is relatively simple, it’s a little more involved when moving to Polygon’s Mainnet. This is because the LINK provided by the Polygon Bridge is not ERC-677 compatible, so it cannot be used with Chainlink oracles. However, it can be converted to the official LINK token on Polygon using Chainlink’s PegSwap service, essentially creating a wrapped version of the LINK token.

虽然使用水龙头在Mumbai Testnet上获取LINK的过程相对简单,但在移至Polygon的Mainnet时涉及的过程要多一些。 这是因为Polygon Bridge提供的LINK与ERC-677不兼容,因此不能与Chainlink oracles一起使用。 但是,可以使用Chainlink的PegSwap服务将其转换为Polygon上的官方LINK令牌,实质上是创建LINK令牌的包装版本。

https://faucet.polygon.technology/

Building the Smart Contract 建立智能合约

The easiest way to start building a smart contract that uses Chainlink VRF on Polygon is to begin with the standard Chainlink VRFConsumer contract. This is a basic standardized contract for initiating requests for a random number via a Chainlink oracle. So we will open this contract in Remix via the link above and modify it accordingly.

开始在Polygon上使用Chainlink VRF建立智能合约的最简单方法是从标准Chainlink VRFConsumer合约开始。 这是用于通过Chainlink oracle发起对随机数的请求的基本标准化合同。 因此,我们将通过上面的链接在Remix中打开此合同,并进行相应的修改。

  • The VRF Coordinator for Mumbai Testnet is 0x8C7382F9D8f56b33781fE506E897a4F1e2d17255
  • The address of the LINK token on the Mumbai Testnet is 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
  • The KeyHash for Mumbai Testnet is 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4
  • The fee for making a VRF request on the Mumbai Testnet is 100000000000000 (0.0001 LINK)
  • Mumbai测试网的VRF协调器为0x8C7382F9D8f56b33781fE506E897a4F1e2d17255
  • Mumbai测试网上的LINK令牌地址为0x326C977E6efc84E512bB9C30f76E30c160eD06FB
  • Mumbai测试网的KeyHash为0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4
  • 在Mumbai测试网上提出VRF请求的费用为100000000000000(0.0001 LINK)
constructor() 
        VRFConsumerBase(
            0x8C7382F9D8f56b33781fE506E897a4F1e2d17255
            0x326C977E6efc84E512bB9C30f76E30c160eD06FB
        ) public
    {
        keyHash = 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4;
        fee = 100000000000000; // 0.0001 LINK
    }

Now our contract is ready to be compiled and deployed to the Mumbai Testnet.
现在,我们的合同已准备好进行编译,并部署到MumbaiTestnet。

Deploying and Testing the Smart Contract 部署和测试智能合约

Compile the contract in Remix, then on the deployment tab, change the environment to “Injected Web3”, and ensure the wallet address below is the one in your MetaMask wallet that contains your MATIC and LINK tokens, press the deploy button, and follow the steps. The end result is you have your smart contract deployed to the Mumbai Testnet. You should take note of the deployed contract address via the transaction output in the Remix console.

在Remix中编译合同,然后在“部署”选项卡上,将环境更改为“ Injected Web3”,并确保下面的钱包地址是您的MetaMask钱包中包含MATIC和LINK令牌的钱包地址,按部署按钮,然后按照脚步。 最终结果是您将智能合约部署到了孟买Testnet。 您应该通过Remix控制台中的事务输出记录已部署的合同地址。

The next step is to fund the contract with LINK so it can send requests to the VRF Coordinator. To do this, simply take the deployed contract address from the step above, and then send it at least 0.0001 LINK from your MetaMask wallet.

下一步是使用LINK为合同提供资金,以便它可以将请求发送到VRF协调器。 为此,只需从上述步骤中获取已部署的合同地址,然后从您的MetaMask钱包中至少发送0.0001 LINK。

How to Get a Random Number on Polygon

Funding the contract with LINK

Once the contract is deployed and funded with LINK, we can request a random number by executing the getRandomNumber function, passing in a given seed as the parameter. This will send the request along with the seed to the VRF coordinator. Remember it’s extremely important to choose a seed that is difficult to influence or predict.

Entering a seed and sending a request to the VRF CoordinatorOnce this transaction has been processed, we then