10

My goal is to have a PHP script be invoked whenever a transaction is received by a given address.

I know Javascript but this is my first sally into Node.js. I think the right approach is to run a node.js webserver in parallel to Apache, and for that to be running a script which uses web3.eth.filter to trigger a javascript which makes a HTTP request to Apache.

Is that right? If so, I don't know how to use web3.eth.filter, the documentation isn't great.

Some hints, please?

Matthew Schmidt
  • 7,290
  • 1
  • 24
  • 35
spraff
  • 645
  • 1
  • 10
  • 22

2 Answers2

8

If you are not familiar with node.js but with PHP and the ethereum architecture, I recommend you to have a look on the RPC API: https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter

In particular at eth_newFilter and eth_getFilterChanges which you can simply call via rest API

e.g.

# This will install a new filter with your desired address
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_newFilter","params":[{"address": "0x8888f1f195afa192cfee860698584c030f4c9db1"}],"id":73}' <address:port>

# Polling this will return updates - your received transactions 
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getFilterChanges","params":["0x16"],"id":73}' <address:port>
Denis
  • 911
  • 6
  • 8
0

You should check out the Ethereum-php Listener and Indexer

As opposed to above it will also work with clients who don't support EthFilter like Infura.

1) Extend a Ethereum\SmartContract and add event handlers with "onEventName"

class CallableEvents extends SmartContract {
  public function onCalledTrigger1 (EthEvent $event) {
    echo '### ' . substr(__FUNCTION__, 2) . "(\Ethereum\EmittedEvent)\n";
    var_dump($event);
  }
  public function onCalledTrigger2 (EthEvent $event) {
    echo '### ' . substr(__FUNCTION__, 2) . "(\Ethereum\EmittedEvent)\n";
    var_dump($event);
  }
}

2) Initialize your Contracts (in this Example from Truffle builds)

$web3 = new Ethereum('http://192.168.99.100:8545');
$networkId = '5777';

// Contract Classes must have same name as the solidity classes for this to work.
$contracts = SmartContract::createFromTruffleBuildDirectory(
  'YOUR/truffle/build/contracts',
   $web3,
   $networkId
);

3) Create a Event processor

// process any Transaction from current Block to the future.
new ContractEventProcessor(
  $web3,
  $contracts,
  'latest',
  'latest'
);

Code is based on reactPHP.

You'll find more examples for Block or Event processing here.

digitaldonkey
  • 480
  • 4
  • 10