从安全帽中的交易收据中获取事件

Pat*_*ins 11 javascript solidity ethers.js hardhat

我有一份ethers与之进行交易的合同:

const randomSVG = new ethers.Contract(RandomSVG.address, RandomSVGContract.interface, signer)
let tx = await randomSVG.create()
Run Code Online (Sandbox Code Playgroud)

我有一个与此交易有关的事件:

function create() public returns (bytes32 requestId) {
        requestId = requestRandomness(keyHash, fee);
        emit requestedRandomSVG(requestId);
    }
Run Code Online (Sandbox Code Playgroud)

但是,我看不到交易收据中的日志。]( https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt )

// This returns undefined
console.log(tx.logs)
Run Code Online (Sandbox Code Playgroud)

Jon*_*als 17

当您使用 Ethers.js 创建交易时,您会返回一个可能尚未包含在区块链中的TransactionResponse 。因此它不知道将发出什么日志。

相反,您希望等到交易得到确认并返回TransactionReceipt。此时,交易已包含在块中,您可以看到发出了哪些事件。

const randomSVG = new ethers.Contract(RandomSVG.address, RandomSVGContract.interface, signer)
const tx = await randomSVG.create()
// Wait until the tx has been confirmed (default is 1 confirmation)
const receipt = await tx.wait()
// Receipt should now contain the logs
console.log(receipt.logs)
Run Code Online (Sandbox Code Playgroud)