Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
contract-example.php
Go to the documentation of this file.
1 <?php
5 
9 define('IS_PUBLIC', TRUE);
10 
11 require_once __DIR__ . '/examples.inc.php';
12 ?>
13 
14 <h3>Example: Register Drupal</h3>
15 <p>
16  <a href="https://github.com/digitaldonkey/register_drupal_ethereum">Smart contract</a>
17  is <a href="https://kovan.etherscan.io/address/0xdacb85f3a6f12ca7893f887f875064880ce14d7d">deployed at</a>
18  Kovan network.</b>
19 </p>
20 <p>
21  It is a simple registry, where user <em>submit</em> a Hash provided by the CMS to a registry.
22  Imagine the registry a a simple array.
23 </p>
24 <p>
25  The <b>validateUserByHash(hash)</b> function returns the submitting user address.
26 </p>
27 
28 <?php
29 
30 // Contract Address on kovan network.
31 $addressAtKovan = '0xdacb85f3a6f12ca7893f887f875064880ce14d7d';
32 
33 // Hash which has been submitted at this contract by the user "0xaec98826319ef42aab9530a23306d5a9b113e23d".
34 $exampleHash = "0x6139633364613535613365333161396433353334353934376261323439353232";
35 
36 // ABI of the Contract
37 //
38 // See also:
39 // Contract on Github: https://github.com/digitaldonkey/register_drupal_ethereum
40 // Deployed contract https://kovan.etherscan.io/address/0xdacb85f3a6f12ca7893f887f875064880ce14d7d
41 //
42 // The ABI of a contract can be gathered using solc (pure javascript compiler),
43 // on Remix http://remix.ethereum.org or with tools like Truffle truffleframework.com
44 $abi = json_decode('[
45  {
46  "inputs": [],
47  "payable": false,
48  "stateMutability": "nonpayable",
49  "type": "constructor"
50  },
51  {
52  "constant": true,
53  "inputs": [
54  {
55  "name": "drupalUserHash",
56  "type": "bytes32"
57  }
58  ],
59  "name": "validateUserByHash",
60  "outputs": [
61  {
62  "name": "result",
63  "type": "address"
64  }
65  ],
66  "payable": false,
67  "stateMutability": "view",
68  "type": "function"
69  },
70  {
71  "anonymous": false,
72  "inputs": [
73  {
74  "indexed": true,
75  "name": "from",
76  "type": "address"
77  },
78  {
79  "indexed": true,
80  "name": "hash",
81  "type": "bytes32"
82  },
83  {
84  "indexed": false,
85  "name": "error",
86  "type": "int256"
87  }
88  ],
89  "name": "AccountCreatedEvent",
90  "type": "event"
91  },
92  {
93  "constant": false,
94  "inputs": [
95  {
96  "name": "from",
97  "type": "address"
98  },
99  {
100  "name": "hash",
101  "type": "bytes32"
102  },
103  {
104  "name": "error",
105  "type": "int256"
106  }
107  ],
108  "name": "accountCreated",
109  "outputs": [],
110  "payable": false,
111  "stateMutability": "nonpayable",
112  "type": "function"
113  },
114  {
115  "constant": false,
116  "inputs": [
117  {
118  "name": "drupalUserHash",
119  "type": "bytes32"
120  }
121  ],
122  "name": "newUser",
123  "outputs": [],
124  "payable": false,
125  "stateMutability": "nonpayable",
126  "type": "function"
127  },
128  {
129  "constant": true,
130  "inputs": [],
131  "name": "contractExists",
132  "outputs": [
133  {
134  "name": "result",
135  "type": "bool"
136  }
137  ],
138  "payable": false,
139  "stateMutability": "pure",
140  "type": "function"
141  },
142  {
143  "constant": false,
144  "inputs": [
145  {
146  "name": "registrationDisabled",
147  "type": "bool"
148  }
149  ],
150  "name": "adminSetRegistrationDisabled",
151  "outputs": [],
152  "payable": false,
153  "stateMutability": "nonpayable",
154  "type": "function"
155  },
156  {
157  "constant": false,
158  "inputs": [
159  {
160  "name": "accountAdmin",
161  "type": "address"
162  }
163  ],
164  "name": "adminSetAccountAdministrator",
165  "outputs": [],
166  "payable": false,
167  "stateMutability": "nonpayable",
168  "type": "function"
169  },
170  {
171  "constant": false,
172  "inputs": [],
173  "name": "adminRetrieveDonations",
174  "outputs": [],
175  "payable": false,
176  "stateMutability": "nonpayable",
177  "type": "function"
178  },
179  {
180  "constant": false,
181  "inputs": [],
182  "name": "adminDeleteRegistry",
183  "outputs": [],
184  "payable": false,
185  "stateMutability": "nonpayable",
186  "type": "function"
187  }
188  ]');
189 
190 try {
191  // Ethereum instance connected to Kovan network.
192  $eth = new Ethereum('https://kovan.infura.io/drupal');
194 
195  // Instanciate Contract.
197 
198  // Call a function.
199  // Note Return type is D20 which is a the same as "address".
200  $test = $register_drupal->validateUserByHash($hash);
201 
202  // Show results.
203  echo "<p style='color: forestgreen;'>The Address submitted this hash is:<br />";
204  echo $test->hexVal()."</p>";
205 
206  }
207 catch (\Exception $exception) {
208  echo "<p style='color: red;'>We have a problem:<br />";
209  echo $exception->getMessage() . "</p>";
210  echo "<pre>" . $exception->getTraceAsString() . "</pre>";
211 }
$exampleHash
$register_drupal
$addressAtKovan
Definition: Abi.php:3