Skip to content

Commit

Permalink
Merge pull request #5 from iconation/1.2.0
Browse files Browse the repository at this point in the history
Throws exceptions on RPC errors
  • Loading branch information
mitsosf authored Jul 26, 2020
2 parents 04084c0 + 523f9a2 commit 3eaec84
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.0] - 2020-07-26
### Changed
- IconServiceHelper::sendRequest throws exception on invalid curl request
- TransactionBuilder::send() handles the exception and returns Object or null
- Removed unnecessary TODOs

## [1.1.0] - 2020-06-11
### Added
- README, added requirements and installation instructions
Expand Down
3 changes: 0 additions & 3 deletions src/IconService/IconService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function getBlockByHash($hash)
*
* @return object
*/
//TODO migrate
public function call($score, $params)
{
return $this->transactionBuilder
Expand Down Expand Up @@ -126,7 +125,6 @@ public function getBalance($address)
*
* @return object
*/
//TODO migrate
public function getScoreApi($address)
{
return $this->transactionBuilder
Expand Down Expand Up @@ -160,7 +158,6 @@ public function getTotalSupply()
* @return object
*/

//TODO Keep migrating from here on
public function getTransactionResult($txHash)
{
return $this->transactionBuilder
Expand Down
10 changes: 7 additions & 3 deletions src/Transaction/TransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,14 @@ public function get(): Transaction
return $this->transaction;
}

//TODO add endpoint url in here
public function send(): \stdClass
public function send(): ?\stdClass
{
return $this->iconServiceHelper->sendRequest($this->transaction->getTransactionObject());
try {
return $this->iconServiceHelper->sendRequest($this->transaction->getTransactionObject());
} catch (\Exception $e) {
echo $e->getMessage();
return null;
}
}

public function getTransaction(): Transaction
Expand Down
15 changes: 13 additions & 2 deletions src/Utils/IconServiceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@ public function __construct(IconService $iconService)

/**
* @param $data
* @return object
* @return object|null
* @throws \Exception
*/
public function sendRequest($data)
{
//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->iconService->getIconServiceUrl());
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));

return json_decode(curl_exec($ch));
$result = curl_exec($ch);

if (curl_errno($ch) !== 0 || strpos(strtolower($result), 'jsonrpc') === false) {
curl_close($ch);
throw new \Exception('Curl error' . curl_error($ch) !== '' ? curl_error($ch) : $result );
}

curl_close($ch);
return json_decode($result);

}
}

0 comments on commit 3eaec84

Please sign in to comment.