我希望每次从我的智能合约运行我的农场功能。 我如何处理每 3 小时运行一次该功能?
答案 0 :(得分:0)
该语言没有实现本地方式,因为 Solidity 函数是作为事务的结果执行的。但您可以安排每 3 小时发送一次交易。
其中一种方法是使用 Chainlink 作业的 cron 启动器。
包含作业配置示例的文档:
或者在您的服务器上运行一个直接发送交易的脚本(不使用 Chainlink 作为中介)。
const Web3 = require('web3');
const web3 = new Web3(providerUrl);
const myContract = new web3.eth.Contract(jsonAbi, contractAddress);
web3.eth.accounts.wallet.add(senderPrivateKey);
async function sendTransaction() {
myContract.methods.myFunction().send({from: senderAddress});
}
setInterval('sendTransaction', 3 * 60 * 60 * 1000); // 3 hours in milliseconds