Skip to content

Commit 6a09493

Browse files
authored
feat: add json rpc handling (#114)
1 parent d22afb7 commit 6a09493

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/API/EVM.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArkEcosystem\Client\API;
6+
7+
class EVM extends AbstractAPI
8+
{
9+
/**
10+
* Make an eth_call POST request to retrieve data from a contract.
11+
*
12+
* @param array $payload
13+
*
14+
* @return array|null
15+
*/
16+
public function ethCall(array $payload): ?array
17+
{
18+
$body = [
19+
'jsonrpc' => '2.0',
20+
'method' => 'eth_call',
21+
'params' => [$payload, 'latest'],
22+
'id' => null,
23+
];
24+
25+
$headers = [
26+
'Content-Type' => 'application/json',
27+
];
28+
29+
return $this->withApi('evm')->post('api/', $body, $headers);
30+
}
31+
}

src/ArkClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function getHosts(): array
128128
*/
129129
public function api(string $name): API\AbstractAPI
130130
{
131-
$name = ucfirst($name);
131+
$name = $name === 'evm' ? 'EVM' : ucfirst($name);
132+
132133
$class = "ArkEcosystem\\Client\\API\\{$name}";
133134

134135
if (! class_exists($class)) {

tests/API/EVMTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArkEcosystem\Tests\Client\API;
6+
7+
use ArkEcosystem\Tests\Client\TestCase;
8+
9+
/**
10+
* @covers \ArkEcosystem\Client\API\EVM
11+
*/
12+
class EVMTest extends TestCase
13+
{
14+
/** @test */
15+
public function eth_call_calls_correct_url()
16+
{
17+
$this->assertResponse(
18+
method: 'POST',
19+
path: 'api/',
20+
callback: function ($client) {
21+
return $client->evm()->ethCall([
22+
'from' => '0x1234567890abcdef',
23+
'to' => '0xfedcba0987654321',
24+
'data' => '0xabcdef',
25+
]);
26+
},
27+
expectedApi: 'evm'
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)