Skip to content

Commit

Permalink
Adds new features and unclatters part of code
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsosf committed Jul 29, 2019
1 parent c1fa22c commit 9ca1aa7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 165 deletions.
198 changes: 33 additions & 165 deletions src/IconService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class IconService
private $version = "0x3";
private $icon_service_URL;

public function __construct($url){
public function __construct($url)
{
$this->icon_service_URL = $url;
}

Expand All @@ -39,16 +40,6 @@ public function icx_getLastBlock()
"method" => "icx_getLastBlock",
"id" => 1234
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand All @@ -75,15 +66,6 @@ public function icx_getBlockByHeight($height)
"height" => $height
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -111,15 +93,6 @@ public function icx_getBlockByHash($hash)
"hash" => $hash
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -156,15 +129,6 @@ public function icx_call($from, $score, $method, $params)
)
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -192,15 +156,6 @@ public function icx_getBalance($address)
"address" => $address
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -228,15 +183,6 @@ public function icx_getScoreApi($address)
"address" => $address
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand All @@ -259,15 +205,6 @@ public function icx_getTotalSupply()
"method" => "icx_getTotalSupply",
"id" => 1234
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -295,15 +232,6 @@ public function icx_getTransactionResult($txHash)
"txHash" => $txHash
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -331,16 +259,6 @@ public function icx_getTransactionByHash($txHash)
"txHash" => $txHash
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand All @@ -367,15 +285,6 @@ public function ise_getStatus($keys)
"filter" => $keys
)
);
$data_string = json_encode($data);

$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

Expand Down Expand Up @@ -439,15 +348,6 @@ public function send($from, $to, $value, $stepLimit, string $privateKey, $nid =
$data["params"]["signature"] = $transaction_signature;

//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand Down Expand Up @@ -536,16 +436,6 @@ public function callSCORE($from, $to, $stepLimit, string $privateKey, string $me
//Add signature to transaction data
$data["params"]["signature"] = $transaction_signature;

//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand Down Expand Up @@ -636,15 +526,6 @@ public function installSCORE($from, $stepLimit, string $privateKey, string $scor
$data["params"]["signature"] = $transaction_signature;

//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand Down Expand Up @@ -735,15 +616,6 @@ public function updateSCORE($from, $to, $stepLimit, string $privateKey, string $
$data["params"]["signature"] = $transaction_signature;

//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
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'
));

$result = $this->sendRequest($data);

//Return as object
Expand Down Expand Up @@ -830,41 +702,33 @@ public function message($from, $to, $stepLimit, string $privateKey, string $mess
* @param string $nonce An arbitrary number used to prevent transaction hash collision eg.0x1
* @return string
*/
/*
//TODO make it work for contracts as well
public function debug_estimateStep($from, $to, $timestamp, $value = "0", $nid = "0x1", $nonce = "0x1")
{
$data = array(
"jsonrpc" => "2.0",
"method" => "debug_estimateStep",
"id" => 1234,
"params" => array(
"version" => $this->version,
"from" => $from,
"to" => $to,
"value" => $value,
"timestamp" => $timestamp,
"nid" => $nid,
"nonce" => $nonce
)
);
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
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'
));
/*
//TODO make it work for contracts as well
public function debug_estimateStep($from, $to, $timestamp, $value = "0", $nid = "0x1", $nonce = "0x1")
{
$data = array(
"jsonrpc" => "2.0",
"method" => "debug_estimateStep",
"id" => 1234,
"params" => array(
"version" => $this->version,
"from" => $from,
"to" => $to,
"value" => $value,
"timestamp" => $timestamp,
"nid" => $nid,
"nonce" => $nonce
)
);
$result = curl_exec($ch);
$result = $this->sendRequest($data);
//Return as object
return json_decode($result);
}*/
//Return as object
return json_decode($result);
}*/

private function sendRequest($data){
private function sendRequest($data)
{
//Send request to RPC
$data_string = json_encode($data);
$ch = curl_init($this->icon_service_URL);
Expand All @@ -886,9 +750,13 @@ private function getBase64TimestampInMilliseconds()
return $milliseconds;
}

public function icxToHex($value){
$dec = $value* 10**18;
return '0x'.dechex($value* 10**18);
public function icxToHex($value)
{
return '0x' . dechex($value * 10 ** 18);
}

public function hexToIcx($value)
{
return hexdec($value) / 10 ** 18;
}
}
7 changes: 7 additions & 0 deletions tests/IconServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,11 @@ public function test_icxToHex(){
$this->assertTrue($var->icxToHex(0.01) === $result);
unset($var);
}

public function test_hexToIcx(){
$var = new mitsosf\IconSDK\IconService($this->icon_service_URL_yeouido);
$result = 0.01;
$this->assertTrue($var->hexToIcx('0x2386f26fc10000') === $result);
unset($var);
}
}

0 comments on commit 9ca1aa7

Please sign in to comment.