web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.16k stars 543 forks source link

Possible to get past events? #93

Open 87vrvk9k opened 6 years ago

87vrvk9k commented 6 years ago

Watching events is obviously not straightforward in PHP, but getting past events seems really useful, for example for viewing transaction history.

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#getpastevents

Is this possible right now or could it be implemented?

sciku1 commented 6 years ago

I think you could get away using some existing functions. Before, web3.js simply polled the getLogs endpoint to get event information. I believe the Keccak256 php comes with Web3.php, a work around is to use Eth->getLogs(['topics' => [ keccakOfEvent ] ]). To get the event data, use the event ABI to get the inputs, should be something like ['string', 'uint256',...]. And call Ethabi->decode($log->data, $inputs); Don't have time to give you a code sample but it should point you in the right direction!

JamesMRM commented 5 years ago

Wanted to share what I came up with using sciku1's pointer - it's working for me.

 function getEventLogged($eventName)
    {
        //use $eventName to get $eventSignature and $eventInputParametersStringArray
        $this->eth->getLogs([
            'topics' => array(Utils::sha3($eventSignature)),
            'address' => $contractAddress
        ],
        function ($err, $result) use (&$output) {
            if($err !== null) {
                echo $err->getMessage();
            }
            $output = $result;
        });
        return $contract->ethabi->decodeParameters($eventInputParametersStringArray, $output[0]->data);
    }

Won't necessarily want $output[0] in most cases, but this will hopefully help anyone else who comes across this. Thanks for the pointer there sciku1!

sciku1 commented 5 years ago

@sc0Vu you think we could add this into the Eth.php?