Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solidity receive event sometimes work, sometimes not in no condition change. #5251

Closed
jwbda opened this issue May 18, 2024 · 3 comments
Closed
Assignees

Comments

@jwbda
Copy link
Contributor

jwbda commented May 18, 2024

Version of Hardhat

2.22.3

What happened?

solidity receive event sometimes work, sometimes not in no condition change.

After runing the "## command entry shell code ", the result sometimes is :

Event emitted: undefined

which is failed result.


other time is

>>> aUint -> 7777777n ContractEventPayload {
  filter: 'Message',
  emitter: <ref *1> BaseContract {
    target: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
    interface: Interface {
      fragments: [Array],
      deploy: [ConstructorFragment],
      fallback: null,
      receive: true
    },
    runner: HardhatEthersSigner {
      _gasLimit: 30000000,
      address: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
      provider: [HardhatEthersProvider]
    },
    filters: {},
    fallback: [AsyncFunction: method] {
      _contract: [Circular *1],
      estimateGas: [AsyncFunction: estimateGas],
      populateTransaction: [AsyncFunction: populateTransaction],
      send: [AsyncFunction: send],
      staticCall: [AsyncFunction: staticCall]
    },
    [Symbol(_ethersInternal_contract)]: {}
  },
  log: EventLog {
    provider: HardhatEthersProvider {
      _hardhatProvider: [LazyInitializationProviderAdapter],
      _networkName: 'hardhat',
      _blockListeners: [Array],
      _transactionHashListeners: Map(0) {},
      _eventListeners: [Array],
      _isHardhatNetworkCached: true,
      _latestBlockNumberPolled: 2,
      _blockPollingInterval: Timeout {
        _idleTimeout: 50,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 1475,
        _onTimeout: [AsyncFunction (anonymous)],
        _timerArgs: undefined,
        _repeat: 50,
        _destroyed: false,
        [Symbol(refed)]: true,
        [Symbol(kHasPrimitive)]: false,
        [Symbol(asyncId)]: 302,
        [Symbol(triggerId)]: 0
      }
    },
    transactionHash: '0x57e06f4250ed6c521dcfa404caf3c479601a8fb1e3dd98d42406d44f409da869',
    blockHash: '0xa470beaa31296685146f9d12a6659c269b34a1780a1b0d242006ad53df4d5a01',
    blockNumber: 2,
    removed: false,
    address: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
    data: '0x000000000000000000000000000000000000000000000000000000000076adf1',
    topics: [
      '0xdb5c17b17041a00e40c40ddc3406875c8676803e3a2a50a47f7c56109b884653'
    ],
    index: 0,
    transactionIndex: 0,
    interface: Interface {
      fragments: [Array],
      deploy: [ConstructorFragment],
      fallback: null,
      receive: true
    },
    fragment: EventFragment {
      type: 'event',
      inputs: [Array],
      name: 'Message',
      anonymous: false
    },
    args: Result(1) [ 7777777n ]
  },
  args: Result(1) [ 7777777n ],
  fragment: EventFragment {
    type: 'event',
    inputs: [ [ParamType] ],
    name: 'Message',
    anonymous: false
  }
}
Event emitted: undefined

which is my expect result, where in one or two times in ten of "## command entry shell code"

Minimal reproduction steps

// hardhat.config.ts
const config: HardhatUserConfig = {
  solidity: {
    version: "0.8.24",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200,
      },
    }
  },
};
// solidity code
contract G {
    event Message(uint a);
    constructor() public payable {
        emit Message(111111);
    }
    function a() public payable {}
    receive() external payable {
        emit Message(66666);
    }
    function gb() public {
        // return block.timestamp;
        uint aUint = 7777777;
        emit Message(aUint);
    }
}
// deploy task code 
task("test3", "test3").setAction(async (args, hre: HardhatRuntimeEnvironment) => {
    const amount = hre.ethers.parseEther("1");
    const G = await hre.ethers.getContractFactory("G");
    const g = await G.deploy({ value: amount });
    await g.waitForDeployment();
    g.on("Message", (a, event) => {
        console.log(">>> aUint ->", a, event);
    })
    const tx = await g.gb();
    await tx.wait();

    console.log('Event emitted:', tx.events);
}

command entry shell code

clear && npx hardhat typechain && npx hardhat test3

Search terms

hardhat event not console in terminal, #3935

@jwbda
Copy link
Contributor Author

jwbda commented May 27, 2024

Hi @fvictorio, if I want to solve this problem, what pre knowledge do I need to learn?

@schaable
Copy link
Contributor

You have two issues in your code:

  • Your script is ending before the listener is called, thus the console.log(">>> aUint ->", a, event); is not always outputed. You can fix this by delaying your script execution by a couple milliseconds with something like await new Promise((resolve) => setTimeout(resolve, 50));
  • There's no tx.events so console.log('Event emitted:', tx.events); will always log undefined. If you want to get the logs from the tx, you'll need the transaction receipt and access the logs, then you can filter based on the contract address.

I'm closing this as it is not a hardhat issue.

@schaable schaable closed this as not planned Won't fix, can't repro, duplicate, stale May 27, 2024
@jwbda
Copy link
Contributor Author

jwbda commented May 28, 2024

Amazing, many many thanks to you, @schaable. And I just pray that all token in you wallet to the moon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

2 participants