I want all transaction of the tokens send to my database through smart contract.

For example, A send 10Token to B, it will show on blockchain, but I also want a function which smart contract will let my server save and record this transactions.

I am using php for this record.


Not a PHP guru here but reversing the flow control might help you google up the details.

Contracts don't interact with other systems in either direction. Ever.

The most interaction that can take place is

  • Alice signed a transaction with data and/or ether and sent it to the contract, or
  • Alice listens to events emitted by the contract.

A suitable event log is part of the ERC20 token standard, so you can "listen" to the contract and catch the events. The most common way to accomplish that is with the "Web3" Javascript library.

In JavaScript, one one would use a callback to execute whenever a transfer "event" is observed in the "log". That could, for example, trigger a database insertion or inform a PHP system by other means.

There are some experimental PHP Web3 implementations.

Main takeaway - the contract just does its thing. The server can react by observing it (Pull) but never by anything on the Ethereum side interacting with the server (Push).

Hope it helps.

| improve this answer | |
  • The whole purpose of a PHP implementation is mainly, that it has been a good practice to verify any user user input. Payments or other events can be integrated in a safer way. So IMHO the flow might be required in cases where PHP is set. – digitaldonkey May 21 '19 at 22:48
  • I remember me strictly opposing the idea of PHP generated transactions, because of "own your key". But in UX reality you use a multisig / change ownership setup it might be suitable for web based user on boarding in some cases. Web3p might support signing transactions, but Ethereum-PHP doesn't. – digitaldonkey May 21 '19 at 22:49
  • The solution will depend on where you are trying to place the trust. If it's trust in the contract, then even listeners are the way the server will be informed. It's worth noting that many implementations include servers than present UI layers and assist with the client-side creation of a valid transaction that gets submitted to the chain. Or, the user signs a transaction and hands the signed transaction over to a server that can forward it to the chain. It will help to think of chain/contract, server and client as a triangular topology. Each node can talk to the other two. – Rob Hitchens May 21 '19 at 23:34

You might set up a listener process in PHP to tackle this. Asuming you have a contract like:

contract CallableEvents {
  event CalledTrigger1 (address indexed from);
  // ...

You need to extend the \Ethereum\SmartContract object with a Event handler. Make sure the class name is the same as your contract class name.

class CallableEvents extends SmartContract {
  public function onCalledTrigger1 (EthEvent $event) {
    echo '### ' . substr(__FUNCTION__, 2) . "(\Ethereum\EmittedEvent)\n";
    // Add your database code here

// Initialize Web3 and Network ID as deployed with 'truffle migrate'
$web3 = new Ethereum('');
$networkId = '5777';

// Initialize the ABI from the truffle generated JSON 
$contracts = SmartContract::createFromTruffleBuildDirectory(

// Initialize to process any transaction from current Block to the future.
new ContractEventProcessor(

If you now run the script you can act on every event triggered by contract interaction on chain

php path_toScript_above.php


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.