gpt4 book ai didi

ethereum - Solidity 中的事件触发

转载 作者:行者123 更新时间:2023-12-03 16:30:22 25 4
gpt4 key购买 nike

我目前正在开发以太坊平台(node.js 和solidity)。我的问题是如何使用 node.js 在solidity(契约(Contract))中触发事件?

最佳答案

这是智能合约中的示例事件定义:

contract Coin {
//Your smart contract properties...

// Sample event definition: use 'event' keyword and define the parameters
event Sent(address from, address to, uint amount);


function send(address receiver, uint amount) public {
//Some code for your intended logic...

//Call the event that will fire at browser (client-side)
emit Sent(msg.sender, receiver, amount);
}
}

线下事件 Sent(address from, address to, uint amount);声明一个所谓的“ event ” 在函数 send 的最后一行触发.用户界面(当然还有服务器应用程序)可以监听在区块链上触发的事件,而无需花费太多成本。一旦它被触发,监听器也会收到参数 from , toamount ,这使得跟踪交易变得容易。为了监听此事件,您将使用。

将捕获事件并在浏览器控制台中写入一些消息的 Javascript 代码:
Coin.Sent().watch({}, '', function(error, result) {
if (!error) {
console.log("Coin transfer: " + result.args.amount +
" coins were sent from " + result.args.from +
" to " + result.args.to + ".");
console.log("Balances now:\n" +
"Sender: " + Coin.balances.call(result.args.from) +
"Receiver: " + Coin.balances.call(result.args.to));
}
})

引用:
http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html

关于ethereum - Solidity 中的事件触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com