Transactions
每天一次在事務期間從外部來源獲取數據
代幣合約是否有可能每隔這麼多小時呼叫一次預言機來獲取數據?假設令牌將使用一個隨機數來執行它要做出的某些決定,並且該隨機數需要每天更新一次。呼叫 Chainlink VRF 來獲取隨機數很容易,但有可能每天只呼叫一次嗎?
是的。
你會想要使用來自 chainlink 節點網路的睡眠適配器。這將允許您每 X 分鐘呼叫一次函式。
請注意,在solidity 0.6 版本中,在同一個合約中獲取VRF 和Alarm 有點棘手。因此,一個簡單的解決方法是將它們放在兩個單獨的契約中並讓它們相互呼叫。這是執行此操作的儲存庫的範例。
這是kovan 測試網上remix的一個例子。
/** This example code is designed to quickly deploy an example contract using Remix. * If you have never used Remix, try our example walkthrough: https://docs.chain.link/docs/example-walkthrough * You will need testnet ETH and LINK. * - Kovan ETH faucet: https://faucet.kovan.network/ * - Kovan LINK faucet: https://kovan.chain.link/ */ pragma solidity ^0.6.0; import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol"; contract AlarmClockSample is ChainlinkClient { bool public alarmDone; address private oracle; bytes32 private jobId; uint256 private fee; /** * Network: Kovan * Oracle: Chainlink - 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b * Job ID: Chainlink - 982105d690504c5d9ce374d040c08654 * Fee: 0.1 LINK */ constructor() public { setPublicChainlinkToken(); oracle = 0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b; jobId = "982105d690504c5d9ce374d040c08654"; fee = 0.1 * 10 ** 18; // 0.1 LINK alarmDone = false; } /** * Create a Chainlink request to start an alarm and after * the time in seconds is up, return throught the fulfillAlarm * function */ function requestAlarmClock(uint256 durationInSeconds) public returns (bytes32 requestId) { Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfillAlarm.selector); // This will return in 90 seconds request.addUint("until", block.timestamp + durationInSeconds); return sendChainlinkRequestTo(oracle, request, fee); } /** * Receive the response in the form of uint256 */ function fulfillAlarm(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId) { alarmDone = true; }
您還可以設置一個 cron 作業網路,使用 chainlink cron 啟動器在每 X 小時呼叫一次。