Solidity: Raise events via Assembly using log3

by user1938620   Last Updated July 11, 2019 15:28 PM

I want to raise events from inside a piece of assembly code.

I've extended the answer here with the function AssemblyEvent() which currently causes a runtime error.


Working example:

pragma solidity ^0.4.21;

contract Demo {
    event Deposit(
        address indexed _from,
        bytes32 indexed _id,
        uint256 _value
    );

    function highLevelEvent() public {
        bytes32 _id = hex"420042";
        emit Deposit(msg.sender, _id, 123);
    }

    function lowLevelEvent() public {
        bytes32 _id = hex"420042";
        log3(
            bytes32(uint256(123)),
            bytes32(keccak256("Deposit(address,bytes32,uint256)")),
            bytes32(uint256(uint160(msg.sender))),
            _id
        );
    }
    // ##########################
    // this function doesn't work
    // ##########################

    function AssemblyEvent() public {
        bytes32 _id = hex"420042";
        bytes32 t1 = bytes32(uint256(123));
        bytes32 t2 = bytes32(keccak256("Deposit(address,bytes32,uint256)"));
        bytes32 t3 = bytes32(uint256(uint160(msg.sender)));

        assembly {
            let s := 0x20
            let p := _id
            log3(p, s, t1, t2, t3)
        }
    }
}

What have I done wrong in the above code? Also, as Assembly only has functions up to log4, does this mean that 4 the maximum number of variables I can create logs for?



Related Questions


Updated September 30, 2019 03:28 AM

Updated April 10, 2018 15:28 PM

Updated July 10, 2018 02:28 AM

Updated August 12, 2018 08:28 AM

Updated November 29, 2018 21:28 PM