事件触发可靠



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

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

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);
    }
}

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

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

事件从函数内部触发。因此,您可以通过调用调用事件的函数来触发一个。 以下是更多信息:固体事件文档。

所以基本上你不会在整个节点.js代码中直接触发事件。
假设你有坚固合约,看起来像这样:

contract MyContract {
    event Deposit(address indexed _from, uint256 _value);
    
    function deposit(uint256 value) public {
        ...
        emit Deposit(msg.sender, value);
        ...
    }
}

为了触发事件,您必须调用 deposit(uint256) 函数,该函数如下所示:

const myContract = new web3.eth.Contract(contract_abi, contract_address);
myContract.deposit("1000").send({ from: "0x..." }) // function call

只有当从函数调用生成的事务成功并且您已经订阅了此类事件时,您才能看到发出的事件。


关于如何订阅活动

当您通过web3调用智能合约函数时会触发事件。您可以只观察节点中的事件.js以了解链上发生了什么。

你可以参考这个

 // SPDX-License-Identifier: MIT
 pragma solidity >=0.4.22 <0.8.0;
 contract EventExample {
   event DataStored(uint256 val);
   uint256 val;
   function storeData(uint256 _val) external {
         val = _val;
         emit DataStored(val);
   }
 }
以下是

在 Solidity 中触发事件的一些步骤。我将使用简单的交易示例来帮助您理解。

  1. 定义事件。例:
event NewTransaction(uint256 indexed id, address from, address to, uint256 amount);
  1. 在合约中发出事件。以下是您可以做到这一点的方法:
function transfer(address _to, uint256 _amount) public {
    // Transfer logic here
    emit NewTransaction(txId, msg.sender, _to, _amount);
}
  1. 使用web3.js与 Solidity 合约进行交互。以下是执行此操作的方法:
// Import the web3.js library
const Web3 = require('web3');
// Connect to the local Ethereum node using HTTP
const web3 = new Web3('http://localhost:8545');
// Define the contract address and ABI
const contractAddress = '0x123...'; // replace with your contract address
const abi = [...]; // replace with your contract ABI
// Create an instance of the contract using web3.js
const contract = new web3.eth.Contract(abi, contractAddress);
// Listen for the NewTransaction event using web3.js
const transferEvent = contract.events.NewTransaction({ fromBlock: 0, toBlock: 'latest' });
// Log the event data when the event is triggered
transferEvent.on('data', (event) => {
    console.log(event.returnValues);
});

代码说明:

  • 合约地址和 ABI 用于与 Solidity 合约进行交互。
  • web3.eth.Contract函数用于创建合约的实例。
  • 我们正在定义contract.events.NewTransaction事件侦听器来帮助侦听NewTransaction事件。
  • 最后,我们在触发事件时在控制台中记录了事件数据。
事件允许方便地使用 EVM 日志记录工具,

而 EVM 日志记录工具又可用于在 dapp 的用户界面中"调用"JavaScript 回调,这些回调侦听这些事件,您可以在此处查看详细信息

将事件发出添加到函数,然后调用该函数。您也可以使用模拟合约(仅在必要时),以防您只使用事件进行调试并且不需要合约本身中的事件。在这种情况下,从合约函数获取到模拟函数的返回,然后使用该返回值在那里触发事件。在JS中,你只需要调用mock的函数,然后读取一个事件。

您必须在智能合约中定义事件,并使其从智能合约中的函数触发。要通过节点触发它,您必须通过 web3 调用智能合约中的函数。

最新更新