首页 > 解决方案 > 从智能合约安排通话功能

问题描述

我希望每次从我的智能合约中运行我的农场功能。如何处理每 3 小时运行一次该功能?

标签: cronethereumsolidityschedule

解决方案


该语言没有实现本地方式,因为 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

推荐阅读