diff --git a/src/IconService.php b/src/IconService.php index 672c8bf..d4296dd 100644 --- a/src/IconService.php +++ b/src/IconService.php @@ -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; } @@ -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 @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 @@ -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); @@ -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 @@ -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 @@ -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 @@ -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 @@ -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); @@ -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; + } } diff --git a/tests/IconServiceTest.php b/tests/IconServiceTest.php index be126a0..abde7a4 100644 --- a/tests/IconServiceTest.php +++ b/tests/IconServiceTest.php @@ -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); + } }