Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
SmartContract.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ethereum;
4 
9 use \Ethereum\EmittedEvent;
10 
17 {
18 
22  private $abi;
23 
27  private $contractAddress;
28 
32  private $eth;
33 
38  protected $events;
39 
40 
56  public function __construct(array $abi, string $contractAddress, Ethereum $eth)
57  {
58  $this->abi = new Abi($abi);
59  $this->eth = $eth;
60  $this->contractAddress = $contractAddress;
61  $this->events = $this->abi->getEventsByTopic();
62  }
63 
79  public function __call(string $method, array $args)
80  {
81  $call = new CallTransaction(
82  new EthD20($this->contractAddress),
83  null,
84  null,
85  null,
86  null,
87  $this->abi->encodeFunction($method, $args)
88  );
89 
90  // @todo Maybe we need a smarter default block param handling here.
91  // Currently we can only call latest Block.
92  $rawReturn = $this->eth->eth_call($call, new EthBlockParam());
93 
94  return $this->abi->decodeMethod($method, $rawReturn);
95  }
96 
97 
104  public function processLog(FilterChange $filterChange) {
105 
106  if ($filterChange->address->hexVal() !== $this->contractAddress) {
107  return null;
108  }
109 
110  if (is_array($filterChange->topics)) {
111  $topic = $filterChange->topics[0]->hexVal();
112  if (isset($this->events[$topic])) {
113  $transaction = $this->eth->eth_getTransactionByHash($filterChange->transactionHash);
114  // We have a relevant event.
115  $event = new EmittedEvent($this->events[$topic], $filterChange, $transaction);
116  // Process onEventName handler.
117  if (method_exists($this, $event->getHandler())) {
118  call_user_func([$this, $event->getHandler()], $event);
119  }
120  return $event;
121  }
122  }
123  return null;
124  }
125 
129  public function getAddress() {
130  return $this->contractAddress;
131  }
132 
133 
155  public static function createFromTruffleBuildDirectory($path, $web3 = null, $networkId = null) {
156  $return = [];
157  foreach (scandir($path) as $fileName) {
158  if (strpos($fileName, '.json') !== false) {
159  $filePath = $path . '/' . $fileName;
160  $file = pathinfo($filePath);
161  if($file['extension'] === 'json') {
162  $contractMeta = self::createMetaFromTruffle($filePath);
163 
164  if ($web3 && $networkId) {
165  $address = $contractMeta->networks->{$networkId}->address;
166  if (!class_exists($file['filename']) && $file['filename'] instanceof SmartContract) {
167  // Class exists?
168  trigger_error('Found a json for ' . $file['filename'] . ', but no corresponding contract class name. Initializing with default contract class.', E_USER_WARNING);
169  $return[$file['filename']] = new SmartContract($contractMeta->abi, $address, $web3);
170  }
171  else {
172  $return[$file['filename']] = new $file['filename']($contractMeta->abi, $address, $web3);
173  }
174  }
175  else {
176  $return[$file['filename']] = self::createMetaFromTruffle($filePath);
177  }
178  }
179  }
180  }
181  return $return;
182  }
183 
184 
189  public static function createMetaFromTruffle($filePath) {
190  return json_decode(file_get_contents($filePath));
191  }
192 
193 
194 }
static createFromTruffleBuildDirectory($path, $web3=null, $networkId=null)
__call(string $method, array $args)
__construct(array $abi, string $contractAddress, Ethereum $eth)
processLog(FilterChange $filterChange)
static createMetaFromTruffle($filePath)
Definition: Abi.php:3