我正在尝试发送一个交易并让它在某个块上执行。根据JS API,这似乎是可能的:
https://github.com/ethereum/wiki/wiki/JavaScript-API web3ethsendtransaction
参见参数#2,除非我误解了它。
但是每次我尝试这样做时,它都会以"invalid address"失败:
incrementer.increment.sendTransaction({from:eth.coinbase}, 28410, function(err, address) {
if (!err)
console.log("no err " + address);
else
console.log("err " + address);
});
…而删除块参数28410…
incrementer.increment.sendTransaction({from:eth.coinbase}, function(err, address) {
if (!err)
console.log("no err " + address);
else
console.log("err " + address);
});
…成功得很好。
有人知道这是怎么回事吗?我想做的事有可能吗?web3.eth.sendTransaction(transactionObject [,callback])
函数确实只有2个参数。
(见这里:https://github.com/ethereum/web3.js/blob/master/lib/web3/methods/eth.js#L177,可选的Callback是隐式的)。
维基中的文本可能是副本&过去的错误。我现在修复了这个问题,所以请不要责怪我没有阅读文档:)
NB。我不明白你为什么要针对一个特殊的区块来包含交易。你永远无法确定你的交易是否包含在一个区块中,因为这是由矿工决定的,而不是交易提交者。如果你想延迟执行,你需要使用契约。
编辑:在下面的评论中添加回复,因为这是一般信息。
"交易"one_answers"合同"是两个不同层次的东西。当谈到"合约"时,人们通常(在以太坊的上下文中)谈论应用程序代码,该代码定义了一个完全执行或根本不执行的逻辑(由区块链保证,因此不需要第三方可信方,因此"智能合约")。这段代码"存在"在区块链上,即。它的代码存储在那里,它的状态/内存也存储在那里。
交易是你在区块链上"做"的事情。当你想要部署一个合约时,你把它(代码)放在一个交易对象中,然后发送它,没有目的地地址(可以这么说,发送到区块链)。部署由矿工执行,合约被插入区块链。部署操作是一个事务。
执行以太币转移也是一项交易(基本上调用一个简单的内部价值转移合约)。调用和执行复杂的"用户"合约是一项交易,该交易也由矿工执行,结果/结果存储在区块链上(作为当前开采区块的一部分)。基本的交易执行是有成本的(发送价值、部署),执行复杂的合同也是有成本的(比如Gas等)。
用两个字来解释这一切有点困难。每次,我重读课文,我添加新的句子;)以太坊闹钟可以在特定时间或块安排合约函数调用。它目前正在主网和测试网上工作。您也可以将其部署在本地网络。
例如,下面的合同可以延迟付款。这是来自以太坊闹钟存储库的示例:
- 一种以太坊合约,便于将来对指定块的调度函数调用。
- 函数调用可以被安排在任何合约上执行
- 调度可以通过合约或以太坊账户持有人完成。
- 完全包含在以太坊网络中。
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