gpt4 book ai didi

ethereum - 如何获取交易(非合约)的所有事件?

转载 作者:行者123 更新时间:2023-12-04 00:25:49 24 4
gpt4 key购买 nike

我想使用 web3 获取 Solidity 合约发出的所有事件,但是 .getPastEvents() 方法用于合约。

这将返回 contractInstance 的所有事件,但是,我的合约调用其他也发出事件的合约。

await contractInstance.getPastEvents("allEvents", {fromBlock: bn, toBlock: bn});

我想从交易中获取所有事件,而不是从契约(Contract)中获取。

或者作为替代,即使是来自一个块的所有事件,然后我可以使用事务哈希对其进行过滤,以获得我想要的。是否有返回块中所有事件的函数?我找过,但我找不到一个。我必须知道链中的每个合约并分别获取事件吗?也许。

我做了一个非常简单的例子来说明。

坚固性代码:
pragma solidity 0.5.8;

contract contractA {
event eventA();
function methodA( address b ) public {
emit eventA();
contractB instanceB = contractB( b );
instanceB.methodB();
}
}

contract contractB {
event eventB();
function methodB() public {
emit eventB();
}
}

我正在使用 Truffle 使其变得简单。这是迁移文件:
var contractA = artifacts.require("contractA");
var contractB = artifacts.require("contractB");

module.exports = function(deployer) {
deployer.deploy(contractA);
deployer.deploy(contractB);

这是调用发出 eventA 的 contractA methodA 并调用发出 eventB 的 contractB methodB 的 truffle javascript 代码:
const contractA = artifacts.require("contractA");
const contractB = artifacts.require("contractB");

contract("contractA", async accounts => {

thisAccount = accounts[0];

it( "Simple test", async () => {

const instanceA = await contractA.deployed();
const instanceB = await contractB.deployed();

const transaction = await instanceA.methodA( instanceB.address, { from: thisAccount } );

const bn = transaction.receipt.blockNumber, txHash = transaction.tx;

const allEventsA = await instanceA.getPastEvents("allEvents", {fromBlock: bn, toBlock: bn});
const allEventsB = await instanceB.getPastEvents("allEvents", {fromBlock: bn, toBlock: bn});

console.log("A");
console.log( allEventsA );

console.log("B");
console.log( allEventsB );

});

});

这是输出:
$ truffle test test.js
Using network 'development'.


Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Contract: contractA
A
[
{
logIndex: 0,
transactionIndex: 0,
transactionHash: '0xe99db12863e5c0a0ae2c9c603d9d29f46a74d45ee9bf9f56d15f6f7bd1888058',
blockHash: '0xfa65496b8cb6ecf5b729892836adf80aa883e6823bbdb2d1b8cdfe61b5c97256',
blockNumber: 1573,
address: '0x97519Ada953F882d61625125D5D68E7932250E9F',
type: 'mined',
id: 'log_d28138a2',
returnValues: Result {},
event: 'eventA',
signature: '0x72f2637d8047e961ba6b558fdf63d428e9734bdf7ee2fb2b114f3b1aa65335c7',
raw: { data: '0x', topics: [Array] },
args: Result { __length__: 0 }
}
]
B
[
{
logIndex: 1,
transactionIndex: 0,
transactionHash: '0xe99db12863e5c0a0ae2c9c603d9d29f46a74d45ee9bf9f56d15f6f7bd1888058',
blockHash: '0xfa65496b8cb6ecf5b729892836adf80aa883e6823bbdb2d1b8cdfe61b5c97256',
blockNumber: 1573,
address: '0x00108B6A5572d95Da87e8b4bbF1A3DcA2a565ff7',
type: 'mined',
id: 'log_da38637d',
returnValues: Result {},
event: 'eventB',
signature: '0x34a286cd617cdbf745989ac7e8dab3f95e8bb2501bcc48d9b6534b73d055a89c',
raw: { data: '0x', topics: [Array] },
args: Result { __length__: 0 }
}
]
✓ Simple test (76ms)

如您所见,我必须独立调用每份契约(Contract)。我想知道是否有一种“事务对象”方法可以在一次调用中获取这两个事件——毕竟它们来自同一个事务。

您可以想象这样一种情况,即同一交易中的许多合约都发出了事件。

也许这是不可能的,但我想我还是会问。

最佳答案

通过调用 instanceA.methodA() 触发的事务被称为内部事务,并且当您尝试获取事件时不包括它们的事件。

有一种方法可以从事务中获取所有事件,但是有点麻烦:

1 - 使用 web3.eth.getTransactionReceipt() 获取 TX 收据.这为您提供了包含三个重要字段的事件对象的日志数组:address , datatopics .

  • address ,合约地址触发事件。
  • data , 事件的非索引参数。
  • topics ,事件签名的哈希值和索引参数的哈希值(如果有)

  • 2 - 拥有此信息,请使用 address获得契约(Contract) abi。现在你有了这个合约可以触发的所有事件的列表,因为你是 abi。散列所有事件签名并找到与 topics 中的第一项匹配的事件签名。大批。您可以使用 web3.eth.abi.encodeEventSignature()检查事件签名的哈希值。这样您就可以找到它是哪个事件。还有参数名称。

    3 - 使用事件签名解码 abi web3.eth.abi.decodeLog(inputs, hexString, topics)
    示例: web3.eth.abi.decodeLog([{
    type: 'string',
    name: 'myString'
    },{
    type: 'uint256',
    name: 'myNumber',
    indexed: true
    },{
    type: 'uint8',
    name: 'mySmallNumber',
    indexed: true
    }],
    '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000748656c6c6f252100000000000000000000000000000000000000000000000000',
    ['0x000000000000000000000000000000000000000000000000000000000000f310', '0x0000000000000000000000000000000000000000000000000000000000000010']);

    你会得到:

    Result { '0': 'Hello%!', '1': '62224', '2': '16', myString: 'Hello%!', myNumber: '62224', mySmallNumber: '16'



    这里也有解释: https://codeburst.io/deep-dive-into-ethereum-logs-a8d2047c7371

    关于ethereum - 如何获取交易(非合约)的所有事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027386/

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