-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContract.php
274 lines (220 loc) · 7.2 KB
/
Contract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
namespace Enjin\BlockchainTools\Ethereum\ABI;
use Enjin\BlockchainTools\Ethereum\ABI\Contract\ContractEvent;
use Enjin\BlockchainTools\Ethereum\ABI\Contract\ContractFunction;
use Enjin\BlockchainTools\HexConverter;
use InvalidArgumentException;
class Contract
{
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $address;
/**
* @var array
*/
protected $json;
protected $functionsMeta = [];
protected $functions = [];
protected $eventsMeta = [];
protected $events = [];
protected $functionSerializers = [];
protected $eventInputSerializers = [];
/**
* @var Serializer
*/
protected $defaultSerializer;
public function __construct(
string $name,
string $address,
array $json,
array $serializers = []
) {
$serializer = $serializers['default'] ?? Serializer::makeDefault();
$this->setDefaultSerializer($serializer);
$this->name = $name;
$this->address = HexConverter::prefix(strtolower($address));
$this->json = $json;
foreach ($json as $item) {
$type = $item['type'] ?? 'function';
if (!in_array($type, ['function', 'event'])) {
continue;
}
$name = $item['name'];
if ($type === 'function') {
$this->functionsMeta[$name] = $item;
} elseif ($type === 'event') {
$this->eventsMeta[$name] = $item;
}
}
foreach ($serializers['functions'] ?? [] as $name => $config) {
$this->registerFunctionSerializers(
$name,
$config['default'] ?? null,
$config['input'] ?? null,
$config['output'] ?? null
);
}
foreach ($serializers['events'] ?? [] as $name => $serializer) {
$this->registerEventSerializer($name, $serializer);
}
}
public function name(): string
{
return $this->name;
}
public function address(): string
{
return $this->address;
}
public function addressUnPrefixed(): string
{
return HexConverter::unPrefix($this->address);
}
public function functions(): array
{
$names = array_keys($this->functionsMeta);
return array_map(function ($name) {
return $this->function($name);
}, $names);
}
public function function (string $name): ContractFunction
{
$this->validateFunctionName($name);
$isInitialized = array_key_exists($name, $this->functions);
if (!$isInitialized) {
$this->functions[$name] = $this->makeFunction($name);
}
return $this->functions[$name];
}
public function events(): array
{
$names = array_keys($this->eventsMeta);
return array_map(function ($name) {
return $this->event($name);
}, $names);
}
public function event(string $name): ContractEvent
{
$this->validateEventName($name);
$isInitialized = array_key_exists($name, $this->events);
if (!$isInitialized) {
$this->events[$name] = $this->makeEvent($name);
}
return $this->events[$name];
}
public function decodeEventInput(array $topics, string $data)
{
$signatureTopic = $topics[0];
$event = $this->findEventBySignatureTopic($signatureTopic);
if (!$event) {
throw new InvalidArgumentException('event with matching topic not found: ' . $signatureTopic);
}
return $event->decodeInput($topics, $data);
}
public function findEventBySignatureTopic(string $topic0): ?ContractEvent
{
foreach ($this->events() as $event) {
if ($event->signatureTopic() == $topic0) {
return $event;
}
}
return null;
}
public function findFunctionByMethodId(string $methodId): ?ContractFunction
{
$methodId = HexConverter::unPrefix($methodId);
foreach ($this->functions() as $function) {
if ($function->methodId() == $methodId) {
return $function;
}
}
return null;
}
public function decodeFunctionInput(string $data)
{
$methodId = $this->getMethodIdFromData($data);
$function = $this->findFunctionByMethodId($methodId);
if (!$function) {
throw new InvalidArgumentException('function with matching methodId not found: ' . $methodId);
}
return $function->decodeInput($data);
}
public function decodeFunctionOutput(string $data)
{
$methodId = $this->getMethodIdFromData($data);
$function = $this->findFunctionByMethodId($methodId);
if (!$function) {
throw new InvalidArgumentException('function with matching methodId not found: ' . $methodId);
}
return $function->decodeOutput($data);
}
protected function getMethodIdFromData(string $data): string
{
$data = HexConverter::unPrefix($data);
return substr($data, 0, 8);
}
protected function validateEventName(string $name)
{
$isValid = array_key_exists($name, $this->eventsMeta);
if (!$isValid) {
throw new InvalidArgumentException('event name not found: ' . $name . ' for Contract: ' . $this->name());
}
}
protected function validateFunctionName(string $name): void
{
$isValid = array_key_exists($name, $this->functionsMeta);
if (!$isValid) {
$message = 'method name not found: ' . $name . ' for Contract: ' . $this->name();
throw new InvalidArgumentException($message);
}
}
protected function registerFunctionSerializers(
string $name,
Serializer $default = null,
Serializer $input = null,
Serializer $output = null
) {
$this->validateFunctionName($name);
$this->functionSerializers[$name] = [
'default' => $default,
'input' => $input,
'output' => $output,
];
}
protected function registerEventSerializer(string $name, Serializer $serializer)
{
$this->validateEventName($name);
$this->eventInputSerializers[$name] = $serializer;
}
protected function setDefaultSerializer(Serializer $defaultSerializer)
{
$this->defaultSerializer = $defaultSerializer;
}
protected function makeFunction(string $name): ContractFunction
{
$meta = $this->functionsMeta[$name];
$default = $this->functionSerializers[$name]['default'] ?? $this->defaultSerializer;
$input = $this->functionSerializers[$name]['input'] ?? null;
$output = $this->functionSerializers[$name]['output'] ?? null;
return new ContractFunction(
$meta,
$default,
$input,
$output
);
}
protected function makeEvent(string $name): ContractEvent
{
$meta = $this->eventsMeta[$name];
$serializer = $this->eventInputSerializers[$name] ?? $this->defaultSerializer;
return new ContractEvent(
$meta,
$serializer
);
}
}