Solidity

如果我用乙太坊價值支付,如何用 Link 代幣支付 Oracle?

  • February 19, 2021

首先,我是這個智能合約世界的新手,所以如果這是一個愚蠢的問題,我想道歉。我正在使用 Remix 和 Solidity 作為程式語言進行程式。我有 2 個文件:

主文件.sol:

[...]
function createSomething(string memory _name, uint _userProvidedSeed) payable public {
       require(msg.value >= productCost);
       //refound user if he/she pays more than the productCost value 
       msg.sender.transfer(msg.value - productCost);
       
       getRandomNumber(_userProvidedSeed);
       _createProduct(_name, randomResult);
   }
[...]

和 RandomNumberConsumer.sol,它是一個用於生成隨機數的預言機:

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.6;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/VRFConsumerBase.sol"; //import of the VRFConsumerBase for generating random data

contract RandomNumberConsumer is VRFConsumerBase {
   
   bytes32 internal keyHash;
   uint256 internal fee;
   
   uint256 internal randomResult;
   
   event RequestRandomness(
       bytes32 indexed requestId,
       bytes32 keyHash,
       uint256 seed
   );

   event RequestRandomnessFulfilled(
       bytes32 indexed requestId,
       uint256 randomness
   );
   
   /**
    * Constructor inherits VRFConsumerBase
    * 
    * Network: Kovan
    * Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
    * LINK token address:                0xa36085F69e2889c224210F603D836748e7dC0088
    * Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
    */
   constructor() 
       VRFConsumerBase(
           0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
           0xa36085F69e2889c224210F603D836748e7dC0088  // LINK Token
       ) public
   {
       keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
       fee = 0.1 * 10 ** 18; // 0.1 LINK
   }
   
   /** 
    * Requests randomness from a user-provided seed
    */
   function getRandomNumber(uint256 userProvidedSeed) internal returns (bytes32 requestId) {
       require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
       uint256 seed = uint256(keccak256(abi.encode(userProvidedSeed, blockhash(block.number)))); // Hash user seed and blockhash
       bytes32 _requestId = requestRandomness(keyHash, fee, seed);
       emit RequestRandomness(_requestId, keyHash, seed);
       return _requestId;
   }

   /**
    * Callback function used by VRF Coordinator
    */
   function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
       emit RequestRandomnessFulfilled(requestId, randomness);
       randomResult = randomness;
   }
}

這裡的問題是,如果我在沒有乙太坊付款的情況下生成我的產品,那麼通過評論“getRandomNumber(_userProvidedSeed);” 它完美地工作並創建了產品,顯然沒有隨機數。當我在沒有註釋上述函式的情況下執行它時,remix transaction 提示會這樣寫:

transact to xxx.xxx errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

在我看來,我的 LINK 餘額可能是 0 存在問題,但生成隨機數需要向預言機支付費用,如 getRandomNumber(..) 函式中所述。有誰能夠幫助我?提前非常感謝:P

看起來您的合約沒有 LINK 資金來執行隨機數生成。請注意函式中的此要求getRandomNumber()

require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");

在此範例的同一文件中,作為初步步驟,解釋瞭如何使用 LINK 為該契約提供資金:

https://docs.chain.link/docs/fund-your-contract

引用自:https://ethereum.stackexchange.com/questions/93656