-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContractStore.php
107 lines (83 loc) · 3.11 KB
/
ContractStore.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
<?php
namespace Enjin\BlockchainTools\Ethereum\ABI;
use Enjin\BlockchainTools\Ethereum\ABI\Exceptions\ContractFileException;
use Enjin\BlockchainTools\HexConverter;
use InvalidArgumentException;
class ContractStore
{
private $contractMeta = [];
private $contracts = [];
public function registerContracts(array $contractMeta = [])
{
foreach ($contractMeta as $meta) {
$this->registerContract($meta['name'], $meta['address'], $meta['jsonFile'], $meta['serializers'] ?? []);
}
}
public function registerContract(string $name, string $address, string $jsonFile, array $config = [])
{
$serializers = [
'default' => $config['default'] ?? Serializer::makeDefault(),
'functions' => $config['functions'] ?? [],
'events' => $config['events'] ?? [],
];
$this->contractMeta[$name] = [
'name' => $name,
'address' => $this->normalizeAddress($address),
'jsonFile' => $jsonFile,
'serializers' => $serializers,
];
}
public function contract(string $name): Contract
{
if (!isset($this->contracts[$name])) {
$this->contracts[$name] = $this->makeContract($name);
}
return $this->contracts[$name];
}
public function contractByAddress(string $address): Contract
{
$name = $this->addressToName($address);
if ($name === null) {
throw new InvalidArgumentException('contract with address not found: ' . $address);
}
return $this->contract($name);
}
public function decodeEvent(string $address, array $topics, string $data): DataBlockDecoder
{
$contract = $this->contractByAddress($address);
$event = $contract->findEventBySignatureTopic($topics[0]);
return $event->decodeInput($topics, $data);
}
protected function makeContract(string $name): Contract
{
if (!isset($this->contractMeta[$name])) {
throw new InvalidArgumentException('contract with name not found: ' . $name);
}
$meta = $this->contractMeta[$name];
$jsonFile = $meta['jsonFile'];
$address = $meta['address'];
$serializers = $meta['serializers'];
if (!file_exists($jsonFile)) {
throw new ContractFileException('Contract file not found: ' . $jsonFile);
}
$contents = file_get_contents($jsonFile);
$json = json_decode($contents, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new ContractFileException('Contract file does not contain valid JSON: ' . $jsonFile);
}
return new Contract($name, $address, $json, $serializers);
}
protected function normalizeAddress(string $address): string
{
return HexConverter::unPrefix(strtolower($address));
}
protected function addressToName(string $address): ?string
{
foreach ($this->contractMeta as $item) {
if ($item['address'] == $this->normalizeAddress($address)) {
return $item['name'];
}
}
return null;
}
}