Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
EmittedEvent.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ethereum;
6 
7 
8 class EmittedEvent extends Event
9 {
10 
11  protected $data = null;
12  protected $transaction = null;
13  protected $contractAddress;
14  protected $emitterAddress;
15 
16  /* @var $response Optionally pass data from within Contract "onEvent" handler */
17  protected $response;
18 
28  public function __construct($eventOrAbi, FilterChange $filterChange, Transaction $tx)
29  {
30  self::validate($filterChange, $tx);
31 
32  $abi = $eventOrAbi;
33  if (is_a($eventOrAbi, '\Ethereum\Event')) {
34  /* @var \Ethereum\Event $eventOrAbi */
35  $abi = $eventOrAbi->getAbi();
36  }
37  parent::__construct($abi);
38 
39  $this->data = $this->decode($filterChange);
40  $this->transaction = $tx;
41  $this->emitterAddress = $tx->from->hexVal();
42  $this->contractAddress = $filterChange->address->hexVal();
43  }
44 
48  public function getLog() {
49  $txHash = $this->transaction->getProperty('hash', TRUE);
50  return "Transaction: $txHash \n Contract: $this->contractAddress\n Event Emiter: $this->emitterAddress\n Data " . print_r($this->toArray(), TRUE);
51  }
52 
56  public function getContract() {
57  return $this->contractAddress;
58  }
59 
64  public function setResponse($response) {
65  $this->response = $response;
66  }
67 
71  public function getResponse() {
72  return $this->response;
73  }
74 
78  public function getEmitter() {
79  return $this->emitterAddress;
80  }
81 
82 
86  public function hasData() {
87  return is_array($this->data);
88  }
89 
90 
94  public function getData() {
95  return $this->data;
96  }
97 
98 
104  public function getRawData() {
105  $return = [];
106  foreach ($this->getData() as $val) {
107  $return[] = $val->hexVal();
108  }
109  return $return;
110  }
111 
112 
116  public function getTransaction() {
117  return $this->transaction;
118  }
119 
120 
124  public function toArray() {
125  $data = [];
126  foreach ($this->data as $k => $v) {
127  $data[$k] = $v->hexVal();
128  }
129  return $data;
130  }
131 
137  private static function validate(FilterChange $filterChange, Transaction $tx) {
138  if ($filterChange->transactionHash->hexVal() !== $tx->hash->hexVal()) {
139  throw new \Exception('FilterChange and Transaction hash must correspond to create a EmittedEvent.');
140  }
141  if (!isset($tx->from) || is_null($tx->from)) {
142  throw new \Exception('Could not determine Event emitter EmittedEvent.');
143  }
144  }
145 
146 
147 }
Definition: Abi.php:3
__construct($eventOrAbi, FilterChange $filterChange, Transaction $tx)