- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 web3 获取 Solidity 合约发出的所有事件,但是 .getPastEvents() 方法用于合约。
这将返回 contractInstance 的所有事件,但是,我的合约调用其他也发出事件的合约。
await contractInstance.getPastEvents("allEvents", {fromBlock: bn, toBlock: bn});
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();
}
}
var contractA = artifacts.require("contractA");
var contractB = artifacts.require("contractB");
module.exports = function(deployer) {
deployer.deploy(contractA);
deployer.deploy(contractB);
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)
最佳答案
通过调用 instanceA.methodA()
触发的事务被称为内部事务,并且当您尝试获取事件时不包括它们的事件。
有一种方法可以从事务中获取所有事件,但是有点麻烦:
1 - 使用 web3.eth.getTransactionReceipt()
获取 TX 收据.这为您提供了包含三个重要字段的事件对象的日志数组:address
, data
和 topics
.
address
,合约地址触发事件。 data
, 事件的非索引参数。 topics
,事件签名的哈希值和索引参数的哈希值(如果有)address
获得契约(Contract) abi。现在你有了这个合约可以触发的所有事件的列表,因为你是 abi。散列所有事件签名并找到与
topics
中的第一项匹配的事件签名。大批。您可以使用
web3.eth.abi.encodeEventSignature()
检查事件签名的哈希值。这样您就可以找到它是哪个事件。还有参数名称。
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'
关于ethereum - 如何获取交易(非合约)的所有事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027386/
当我点击 remix.ethereum.org 下的创建时,我不明白智能合约的实际去向。例如,如果我选择 Injected Web3,这应该会将合约发布到 ropsten 测试网上,对吧?但是我怎样才
我通过下面的推荐安装了ganache-cli npm install -g ganache-cli 我已经下载了文件 ganache-1.1.0-beta.0-x86_64.AppImage 当我点
如果我在 ETH 和 RSK 网络中导入相同的私钥,则帐户相同但大小写不同。为什么是这样?这个可以吗? 最佳答案 大写作为校验和,涉及以下标准 SLIP-0044 EIP-155 EIP-1191 有
我们落后于代理。代理已设置。 早些时候,我们运行以下命令并成功安装了以太坊。 sudo apt-get install software-properties-common sudo add-apt-
请找到我的 golang 规范 GOOS=linux GOARCH=amd64 go build github.com/ethereum/go-ethereum/crypto/secp256k1: b
我正在尝试使用 Puppeth(如 Péter Szilágyi demoed in Ethereum devcon three 2017)设置私有(private)以太坊测试网络。我在 macboo
我是一步一步做的: Git 克隆 go-ethereum 项目到我的本地 PC。它位于D:\GOPATH\src\github.com\ethereum\go-ethereum。这是源代码去以太坊 注
我正在尝试生成 personal_sign在 Golang 中就像在 ethers.js 中实现的那样。 Similar question但最终使用了常规 sign过个人sign_implementa
我正在尝试安装 Istanbul-tools 以运行 IBFT 以太坊网络,如本教程所示 https://medium.com/getamis/istanbul-bft-ibft-c2758b7fe6
我想降低我的 ERC-721A 合约的部署成本。 一般来说; 扁平化智能合约是否有助于降低其成本? 目前我正在使用 ethers.js's contractFactory.deploy method带
我有契约(Contract)要求输入 bytes32,所以我需要将地址转换为 bytes32,但在 web3 中没有看到这个方法。 最佳答案 以太坊地址为 20 个字节,因此您将十六进制地址转换为字节
我想与将在调用时部署其他契约(Contract)的功能签订契约(Contract)。可能吗?有什么想法吗? 最佳答案 这是部署合约的合约示例。 https://ethereum.stackexchan
由于与以太坊创建智能合约涉及以太币用户,因此重新填充所有用户的以太币对公司而言成本高昂。因此,假设一家公司决定通过以太坊网络发行一种代币,而该代币代表一种新货币。每次用户向其他人发送代币时,代币的原始
我想知道提交智能合约调用的确切效果。例如,如果我在 uniswap 上将 USDC 兑换成 ETH。 对于交易,我们有 eth_call,对于合约有类似的东西吗?这需要适用于任意复杂的交互,基本上是我
如果我有这样的功能: function sendToAuthor (uint tokenId) public payable{ //here I want to get some ether fr
我正在试验以太坊。我已经按照网站上的说明成功设置了一个私有(private)测试网。但是,我无法添加来自不同机器的对等点。在我创建的任何节点上,admin.nodeInfo.NodeUrl 参数都是未
我在 Remix 上做了一笔交易,花费了 199093 gas。在撰写本文时,天然气价格为 7.2 Gwei(0.031 美元)from here ethgas station 如果我想计算我应该为这
根据 web3.eth.sendTransaction 的文档和 eth_sendTransaction 的文档: 交易对象可以包含一个可选的data参数,它应该是一个String,包含: 要么是包含
是否有任何方法可以在 Solidity 中实现一个集合,以检查元素是否存在,平均 O(1)?我一直在考虑使用没有值的映射对象,这是否比使用数组并迭代以查找元素更快? 最佳答案 是的,就速度而言,您最好
在OpenZeppelin ERC20实现中,有一个_transfer方法: function _transfer( address sender, address recipient
我是一名优秀的程序员,十分优秀!