-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContractFunction.php
251 lines (202 loc) · 6.45 KB
/
ContractFunction.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
<?php
namespace Enjin\BlockchainTools\Ethereum\ABI\Contract;
use Enjin\BlockchainTools\Ethereum\ABI\ContractFunctionDecoder;
use Enjin\BlockchainTools\Ethereum\ABI\ContractFunctionEncoder;
use Enjin\BlockchainTools\Ethereum\ABI\DataBlockDecoder;
use Enjin\BlockchainTools\Ethereum\ABI\DataBlockEncoder;
use Enjin\BlockchainTools\Ethereum\ABI\Serializer;
use InvalidArgumentException;
use kornrunner\Keccak;
class ContractFunction
{
public const MUTABILITY_PURE = 'pure';
public const MUTABILITY_VIEW = 'view';
public const MUTABILITY_NONPAYABLE = 'nonpayable';
public const MUTABILITY_PAYABLE = 'payable';
public const VALID_MUTABILITY_VALUES = [
self::MUTABILITY_PURE,
self::MUTABILITY_VIEW,
self::MUTABILITY_NONPAYABLE,
self::MUTABILITY_PAYABLE,
];
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $stateMutability;
/**
* @var bool
*/
protected $payable;
/**
* @var bool
*/
protected $constant;
/**
* @var array
*/
protected $inputs = [];
/**
* @var array
*/
protected $outputs = [];
/**
* @var string
*/
protected $methodId;
/**
* @var Serializer
*/
protected $defaultSerializer;
/**
* @var Serializer
*/
protected $inputSerializer;
/**
* @var Serializer
*/
protected $outputSerializer;
public function __construct(
array $function,
Serializer $defaultSerializer = null,
Serializer $inputSerializer = null,
Serializer $outputSerializer = null
) {
$this->defaultSerializer = $defaultSerializer ?? Serializer::makeDefault();
$this->inputSerializer = $inputSerializer;
$this->outputSerializer = $outputSerializer;
// backward compatibility with older abi spec
if (!isset($function['stateMutability']) && isset($function['payable'])) {
$stateMutability = $function['payable'] ? 'payable' : 'nonpayable';
} else {
$stateMutability = $function['stateMutability'];
}
$this->name = $function['name'];
$this->payable = $stateMutability === 'payable';
$this->constant = in_array($stateMutability, ['pure', 'view']);
$this->stateMutability = $stateMutability;
$inputs = $function['inputs'] ?? [];
foreach ($inputs as $input) {
$inputName = $input['name'];
$this->inputs[$inputName] = $input;
}
$outputs = $function['outputs'] ?? [];
foreach ($outputs as $output) {
$outputName = $output['name'];
$this->outputs[$outputName] = $output;
}
}
public function name(): string
{
return $this->name;
}
public function stateMutability(): string
{
return $this->stateMutability;
}
public function payable(): bool
{
return $this->payable;
}
public function constant(): bool
{
return $this->constant;
}
public function input(string $name): ContractFunctionValueType
{
if (!array_key_exists($name, $this->inputs)) {
throw new InvalidArgumentException('invalid input name: ' . $name . ' for Contract Function: ' . $this->name());
}
return new ContractFunctionValueType($this->inputs[$name]);
}
public function outputs(): array
{
return array_map(function (array $output) {
return new ContractFunctionValueType($output);
}, $this->outputs);
}
public function output(string $name): ContractFunctionValueType
{
if (!array_key_exists($name, $this->outputs)) {
throw new InvalidArgumentException('invalid output name: ' . $name . ' for Contract Function: ' . $this->name());
}
return new ContractFunctionValueType($this->outputs[$name]);
}
public function inputs(): array
{
return array_map(function (array $input) {
return new ContractFunctionValueType($input);
}, $this->inputs);
}
public function signature(): string
{
$args = [];
/** @var ContractFunctionValueType $input */
foreach ($this->inputs() as $input) {
$args[] = $input->type();
}
return $this->name() . '(' . implode(',', $args) . ')';
}
public function methodId(): string
{
if (!$this->methodId) {
$hash = Keccak::hash($this->signature(), 256);
// first 4 bytes of method signature
$this->methodId = substr($hash, 0, 8);
}
return $this->methodId;
}
public function encodeInput(array $data): DataBlockEncoder
{
$serializer = $this->inputSerializer();
return (new ContractFunctionEncoder($serializer))->encodeInput($this, $data);
}
public function decodeInput(string $data): DataBlockDecoder
{
$serializer = $this->inputSerializer();
return (new ContractFunctionDecoder($serializer))->decodeInput($this, $data);
}
public function encodeOutput(array $data): DataBlockEncoder
{
$serializer = $this->outputSerializer();
return (new ContractFunctionEncoder($serializer))->encodeOutput($this, $data);
}
public function decodeOutput(string $data): DataBlockDecoder
{
$serializer = $this->outputSerializer();
return (new ContractFunctionDecoder($serializer))->decodeOutput($this, $data);
}
public function inputSerializer(): Serializer
{
return $this->inputSerializer ?: $this->defaultSerializer;
}
public function outputSerializer(): Serializer
{
return $this->outputSerializer ?: $this->defaultSerializer;
}
public function toArray(): array
{
return [
'name' => $this->name,
'type' => 'function',
'payable' => $this->payable,
'constant' => $this->constant,
'stateMutability' => $this->stateMutability,
'inputs' => $this->contractValuesToArray($this->inputs()),
'outputs' => $this->contractValuesToArray($this->outputs()),
];
}
protected function contractValuesToArray(array $values): array
{
$mapped = array_map(function (ContractFunctionValueType $item) {
return [
'name' => $item->name(),
'type' => $item->type(),
];
}, $values);
return array_values($mapped);
}
}