以太坊闹钟可以在特定时间或区块安排合约函数调用。它目前正在开发主网和测试网。您也可以将其部署在本地网络上。
- 一个便于调度功能调用未来指定区块的以太坊合约。
- 可以安排函数调用针对任何合约执行
- 调度可以由合约或以太坊账户持有人完成。
- 完全包含在以太坊网络中。
例如,以下合同可能会延迟付款。这是来自以太坊闹钟存储库的示例:
pragma solidity 0.4.24;
import "contracts/Interface/SchedulerInterface.sol";
/// Example of using the Scheduler from a smart contract to delay a payment.
contract DelayedPayment {
SchedulerInterface public scheduler;
address recipient;
address owner;
address public payment;
uint lockedUntil;
uint value;
uint twentyGwei = 20000000000 wei;
constructor(
address _scheduler,
uint _numBlocks,
address _recipient,
uint _value
) public payable {
scheduler = SchedulerInterface(_scheduler);
lockedUntil = block.number + _numBlocks;
recipient = _recipient;
owner = msg.sender;
value = _value;
uint endowment = scheduler.computeEndowment(
twentyGwei,
twentyGwei,
200000,
0,
twentyGwei
);
payment = scheduler.schedule.value(endowment)( // 0.1 ether is to pay for gas, bounty and fee
this, // send to self
"", // and trigger fallback function
[
200000, // The amount of gas to be sent with the transaction.
0, // The amount of wei to be sent.
255, // The size of the execution window.
lockedUntil, // The start of the execution window.
twentyGwei, // The gasprice for the transaction (aka 20 gwei)
twentyGwei, // The fee included in the transaction.
twentyGwei, // The bounty that awards the executor of the transaction.
twentyGwei * 2 // The required amount of wei the claimer must send as deposit.
]
);
assert(address(this).balance >= value);
}
function () public payable {
if (msg.value > 0) { //this handles recieving remaining funds sent while scheduling (0.1 ether)
return;
} else if (address(this).balance > 0) {
payout();
} else {
revert();
}
}
function payout()
public returns (bool)
{
require(block.number >= lockedUntil);
recipient.transfer(value);
return true;
}
function collectRemaining()
public returns (bool)
{
owner.transfer(address(this).balance);
}
}
以编程方式安排事务的最简单方法是使用@ethereum-alarm-clock/lib。这是带有 TypeScript 类型的 JS 库(更易于使用)。
这是如何使用这个库的文本教程:https ://github.com/ethereum-alarm-clock/ethereum-alarm-clock/wiki/Integration-using-EAC-JavaScript-library
这是视频教程:https ://youtu.be/DY0QYDQG4lw