diff --git a/README.md b/README.md index 592b896..154dcf7 100644 --- a/README.md +++ b/README.md @@ -16,30 +16,291 @@ It is provided as is and without any assurances. Use it at your own risk. Features -------- -Already fully or partially supports the following JSON-RPC functions: +Fully or partially supports all Iconservice functions, IRC-2 tokens and IISS calls. +Usage +-------- +####Iconservice: * icx_getLastBlock +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$res = $iconservice->getLastBlock(); +``` * icx_getBlockByHeight +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$res = $iconservice->getBlockByHeight('0x3'); +``` * icx_getBlockByHash +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$res = $iconservice->getBlockByHash('0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03'); +``` * icx_call +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; +$params = new stdClass(); +$params->method = "balanceOf"; +$params->params = new stdClass(); +$params->params->_owner = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"; + +$res = $iconservice->call($score, $params); +``` * icx_getBalance +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$res = $iconservice->getBalance('hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd'); +``` * icx_getScoreApi +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; +$res = $iconservice->getBalance($score); +``` * icx_getTotalSupply +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$res = $iconservice->getTotalSupply(); +``` * icx_getTransactionResult +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$txHash = '0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9'; + +$res = $iconservice->getTransactionResult($txHash); +``` * icx_getTransactionByHash +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$txHash = '0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9'; + +$res = $iconservice->getTransactionByHash($txHash); +``` * ise_getStatus -* icx_sendTransaction --Partial support, can only send ICX for now +```php +use iconation\IconSDK\IconService\IconService; -There is also some wallet support, see Wallet.php file for more info. -More detailed documentation coming soon. +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$keys = ['lastBlock']; -TODO --------- +$res = $iconservice->getStatus($keys); +``` +* icx_sendTransaction +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key +$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; +$to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; +$value = "0x2386f26fc10000"; // = 0.01 ICX + +$res = $iconservice->send($from, $to, $value, $private_key); +``` +* message +```php +use iconation\IconSDK\IconService\IconService; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + +$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key +$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; +$to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; +$message = "Your message goes here"; // = 0.01 ICX + +$res = $iconservice->message($from, $to, $private_key, $message); +``` +###IRC-2: +* name +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$res = $irc2->name(); +``` +* symbol +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$res = $irc2->symbol(); +``` +* decimals +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$res = $irc2->decimals(); +``` +* totalSupply +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$res = $irc2->totalSupply(); +``` +* balanceOf +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$account = 'hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160'; + +$res = $irc2->balanceOf($account); +``` +* transfer +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IconService\IRC2; + +$contract = 'cx8901ee4f6df58bd437de0e66c9dd3385ba4c2328'; +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$irc2 = new IRC2($contract, $iconservice); + +$from = 'hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160'; +$to = 'hxf8689d6c4c8f333651469fdea2ac59a18f6c242d'; +$value = '1'; +$privateKey = '3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5'; + +$res = $irc2->transfer($from, $to, $value, $privateKey); +``` + + +###IISS: +* setStake +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); + +$value = 0.5; //Stake 0.5 ICX +$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; +$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key + +$res = $iiss->setStake($value, $from, $private_key); +``` +* getStake +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); + +$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + +$res = $iiss->getStake($address); +``` +* setDelegation +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; +use iconation\IconSDK\IISS\Delegation; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); + +$delegation1 = new Delegation("hxec79e9c1c882632688f8c8f9a07832bcabe8be8f", "0x2c68af0bb140000"); +$delegation1 = $delegation1->getDelegationObject(); + +$delegation2 = new Delegation("hxd3be921dfe193cd49ed7494a53743044e3376cd3", "0x2c68af0bb140000"); +$delegation2 = $delegation2->getDelegationObject(); + +$delegations = array( + $delegation1, + $delegation2 + ); +$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key +$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + +$res = $iiss->setDelegation($delegations, $from, $private_key); +``` +* getDelegation +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); + +$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + +$res = $iiss->getDelegation($address); +``` +* claimIScore +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); + +$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Staker's private key +$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + +$res = $iiss->claimIScore($from, $private_key); +``` +* queryIScore +```php +use iconation\IconSDK\IconService\IconService; +use iconation\IconSDK\IISS\IISS; + +$iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); +$iiss = new IISS($iconservice); +$address = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; -* Usage:.. -* More tests, especially on the wallet part -* Debug and p-rep functions seem not to be working on ICON's part atm. -Will promptly update when they become available. -* Refactoring +$res = $iiss->queryIScore($address); +``` \ No newline at end of file diff --git a/src/IISS/IISS.php b/src/IISS/IISS.php index 38a747e..95c0f6e 100644 --- a/src/IISS/IISS.php +++ b/src/IISS/IISS.php @@ -22,12 +22,12 @@ public function __construct(IconService $iconService) $this->transactionBuilder = new TransactionBuilder($iconService); } - public function setStake($value, $from, $stepLimit, string $privateKey, $nid = '0x1') + public function setStake($value, $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1') { $methodParams = new \stdClass(); $methodParams->value = Helpers::icxToHex($value); - return $this->sendTransactionToGovernanceContract('setStake', $methodParams, $from, $stepLimit, $privateKey, $nid); + return $this->sendTransactionToGovernanceContract('setStake', $methodParams, $from, $privateKey, $stepLimit, $nid); } public function getStake($address) @@ -35,15 +35,15 @@ public function getStake($address) $methodParams = new \stdClass(); $methodParams->address = $address; - return $this->icx_call('getStake', $methodParams); + return $this->call('getStake', $methodParams); } - public function setDelegation($delegations, $from, $stepLimit, string $privateKey, $nid) + public function setDelegation($delegations, $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1') { $methodParams = new \stdClass(); $methodParams->delegations = $delegations; - return $this->sendTransactionToGovernanceContract('setDelegation', $methodParams, $from, $stepLimit, $privateKey, $nid); + return $this->sendTransactionToGovernanceContract('setDelegation', $methodParams, $from, $privateKey, $stepLimit, $nid); } public function getDelegation($address) @@ -51,11 +51,11 @@ public function getDelegation($address) $methodParams = new \stdClass(); $methodParams->address = $address; - return $this->icx_call('getDelegation', $methodParams); + return $this->call('getDelegation', $methodParams); } - public function claimIScore($from, $stepLimit, string $privateKey, $nid){ - return $this->sendTransactionToGovernanceContract('claimIScsore', null, $from, $stepLimit, $privateKey, $nid); + public function claimIScore($from, string $privateKey, ?string $stepLimit = null, $nid = '0x1'){ + return $this->sendTransactionToGovernanceContract('claimIScore', null, $from, $privateKey, $stepLimit, $nid); } public function queryIScore($address) @@ -63,10 +63,10 @@ public function queryIScore($address) $methodParams = new \stdClass(); $methodParams->address = $address; - return $this->icx_call('queryIScore', $methodParams); + return $this->call('queryIScore', $methodParams); } - private function sendTransactionToGovernanceContract($method, $methodParams, $from, $stepLimit, string $privateKey, $nid) + private function sendTransactionToGovernanceContract(string $method, ?\stdClass$methodParams, string $from, string $privateKey, ?string $stepLimit= null, string $nid = '0x1') { $params = new \stdClass(); $params->method = $method; @@ -74,54 +74,29 @@ private function sendTransactionToGovernanceContract($method, $methodParams, $fr $params->params = $methodParams; } - $transaction = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::SEND_TRANSACTION) ->from($from) ->to('cx0000000000000000000000000000000000000000') ->version($this->version) ->nid($nid) - ->stepLimit($stepLimit) ->timestamp() ->call($params) + ->stepLimit($stepLimit) ->sign($privateKey) - ->get(); - - return $this->sendRequest($transaction->getTransactionObject()); + ->send(); } - private function icx_call(string $method, \stdClass $methodParams) + private function call(string $method, ?\stdClass $methodParams) { $params = new \stdClass(); $params->method = $method; $params->params = $methodParams; - $transaction = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to('cx0000000000000000000000000000000000000000') ->call($params) - ->get(); - - $result = $this->sendRequest($transaction->getTransactionObject()); - - return ($result); - } - - /** - * @param $data - * @return bool|string - */ - private function sendRequest($data) - { - //Send request to RPC - $data_string = json_encode($data); - $ch = curl_init($this->iconService->getIconServiceUrl()); - 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 curl_exec($ch); + ->send(); } } diff --git a/src/IconService/IRC2.php b/src/IconService/IRC2.php index 069c9b9..732760b 100644 --- a/src/IconService/IRC2.php +++ b/src/IconService/IRC2.php @@ -28,13 +28,11 @@ public function name() $params = new stdClass(); $params->method = "name"; - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to($this->contract) ->call($params) ->send(); - - return isset($res->result) ? $res->result : $res; } public function symbol() @@ -42,13 +40,11 @@ public function symbol() $params = new stdClass(); $params->method = "symbol"; - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to($this->contract) ->call($params) ->send(); - - return isset($res->result) ? $res->result : $res; } public function decimals() @@ -56,13 +52,11 @@ public function decimals() $params = new stdClass(); $params->method = "decimals"; - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to($this->contract) ->call($params) ->send(); - - return isset($res->result) ? $res->result : $res; } public function totalSupply() @@ -70,13 +64,11 @@ public function totalSupply() $params = new stdClass(); $params->method = "totalSupply"; - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to($this->contract) ->call($params) ->send(); - - return isset($res->result) ? $res->result : $res; } public function balanceOf(string $account) @@ -86,17 +78,14 @@ public function balanceOf(string $account) $params->params = new stdClass(); $params->params->_owner = $account; - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::CALL) ->to($this->contract) ->call($params) ->send(); - - - return isset($res->result) ? $res->result : $res; } - public function transfer(string $from, string $to, string $value, string $privateKey, string $stepLimit, ?string $nid= '0x1', ?string $data = null) + public function transfer(string $from, string $to, string $value, string $privateKey, ?string $stepLimit = null, string $nid= '0x1', ?string $data = null) { $params = new stdClass(); $params->method = "transfer"; @@ -107,20 +96,17 @@ public function transfer(string $from, string $to, string $value, string $privat $params->params->_data = "0x" . bin2hex($data); } - $res = $this->transactionBuilder + return $this->transactionBuilder ->method(TransactionTypes::SEND_TRANSACTION) ->from($from) ->to($this->contract) ->version($this->iconService->getVersion()) ->nid($nid) - ->stepLimit($stepLimit) ->timestamp() ->nonce() ->call($params) + ->stepLimit($stepLimit) ->sign($privateKey) ->send(); - - - return isset($res->result) ? $res->result : $res; } } diff --git a/src/IconService/IconService.php b/src/IconService/IconService.php index 0de111e..94fedcc 100644 --- a/src/IconService/IconService.php +++ b/src/IconService/IconService.php @@ -32,13 +32,13 @@ public function __construct($url) } /** - * icx_getLastBlock + * getLastBlock * * Get the latest block * * @return object */ - public function icx_getLastBlock() + public function getLastBlock() { return $this->transactionBuilder ->method(TransactionTypes::LAST_BLOCK) @@ -46,7 +46,7 @@ public function icx_getLastBlock() } /** - * icx_getBlockByHeight + * getBlockByHeight * * Get the latest block * @@ -55,7 +55,7 @@ public function icx_getLastBlock() * @return object */ - public function icx_getBlockByHeight($height) + public function getBlockByHeight($height) { return $this->transactionBuilder ->method(TransactionTypes::BLOCK_BY_HEIGHT) @@ -64,7 +64,7 @@ public function icx_getBlockByHeight($height) } /** - * icx_getBlockByHash + * getBlockByHash * * Get the latest block * @@ -73,7 +73,7 @@ public function icx_getBlockByHeight($height) * @return object */ - public function icx_getBlockByHash($hash) + public function getBlockByHash($hash) { return $this->transactionBuilder ->method(TransactionTypes::BLOCK_BY_HASH) @@ -82,7 +82,7 @@ public function icx_getBlockByHash($hash) } /** - * icx_call + * call * * @param string $score SCORE we want to interact with eg. cxb0776ee37f5b45bfaea8cff1d8232fbb6122ec32 * @param \stdClass $params Array of SCORE method possible parameters eg. array("address" => "hx1f9a3310f60a03934b917509c86442db703cbd52") @@ -90,7 +90,7 @@ public function icx_getBlockByHash($hash) * @return object */ //TODO migrate - public function icx_call($score, $params) + public function call($score, $params) { return $this->transactionBuilder ->method(TransactionTypes::CALL) @@ -100,7 +100,7 @@ public function icx_call($score, $params) } /** - * icx_getBalance + * getBalance * * Get the balance of an address * @@ -109,7 +109,7 @@ public function icx_call($score, $params) * @return object */ - public function icx_getBalance($address) + public function getBalance($address) { return $this->transactionBuilder ->method(TransactionTypes::BALANCE) @@ -118,7 +118,7 @@ public function icx_getBalance($address) } /** - * icx_getScoreApi + * getScoreApi * * Get a SCORE API * @@ -127,7 +127,7 @@ public function icx_getBalance($address) * @return object */ //TODO migrate - public function icx_getScoreApi($address) + public function getScoreApi($address) { return $this->transactionBuilder ->method(TransactionTypes::BALANCE) @@ -136,14 +136,14 @@ public function icx_getScoreApi($address) } /** - * icx_getTotalSupply + * getTotalSupply * * Get ICX Total Supply * * @return object */ - public function icx_getTotalSupply() + public function getTotalSupply() { return $this->transactionBuilder ->method(TransactionTypes::TOTAL_SUPPLY) @@ -151,7 +151,7 @@ public function icx_getTotalSupply() } /** - * icx_getTransactionResult + * getTransactionResult * * Get transaction result * @@ -161,7 +161,7 @@ public function icx_getTotalSupply() */ //TODO Keep migrating from here on - public function icx_getTransactionResult($txHash) + public function getTransactionResult($txHash) { return $this->transactionBuilder ->method(TransactionTypes::TRANSACTION_RESULT) @@ -170,7 +170,7 @@ public function icx_getTransactionResult($txHash) } /** - * icx_getTransactionByHash + * getTransactionByHash * * Get transaction result * @@ -179,7 +179,7 @@ public function icx_getTransactionResult($txHash) * @return object */ - public function icx_getTransactionByHash($txHash) + public function getTransactionByHash($txHash) { return $this->transactionBuilder ->method(TransactionTypes::TRANSACTION_BY_HASH) @@ -197,7 +197,7 @@ public function icx_getTransactionByHash($txHash) * @return object */ - public function ise_getStatus($keys) + public function getStatus($keys) { return $this->transactionBuilder ->method(TransactionTypes::STATUS) @@ -205,7 +205,7 @@ public function ise_getStatus($keys) ->send(); } - public function send($from, $to, $value, $stepLimit, string $privateKey, $nid = '0x1') + public function send(string $from, string $to, string $value, string $privateKey, ?string $stepLimit = null, $nid = '0x1') { return $this->transactionBuilder ->method(TransactionTypes::SEND_TRANSACTION) @@ -214,9 +214,9 @@ public function send($from, $to, $value, $stepLimit, string $privateKey, $nid = ->value($value) ->version($this->version) ->nid($nid) - ->stepLimit($stepLimit) ->timestamp() ->nonce() + ->stepLimit($stepLimit) ->sign($privateKey) ->send(); } @@ -244,7 +244,7 @@ public function installSCORE($from, $stepLimit, string $privateKey, string $scor //Create transaction table $data = array( "jsonrpc" => "2.0", - "method" => "icx_sendTransaction", + "method" => "sendTransaction", "id" => 1234, "params" => array( "version" => $this->version, @@ -286,7 +286,7 @@ public function updateSCORE($from, $to, $stepLimit, string $privateKey, string $ //Create transaction table $data = array( "jsonrpc" => "2.0", - "method" => "icx_sendTransaction", + "method" => "sendTransaction", "id" => 1234, "params" => array( "version" => $this->version, @@ -305,13 +305,13 @@ public function updateSCORE($from, $to, $stepLimit, string $privateKey, string $ ) ); - //TODO sign($privateKey) + return $this->sendRequest($data); }*/ - public function message($from, $to, $stepLimit, string $privateKey, string $message, string $value = "0x0", $nid = '0x1') + public function message(string $from, string $to, string $privateKey, string $message, ?string $stepLimit = null, string $nid = '0x1') { return $this->transactionBuilder ->method(TransactionTypes::SEND_TRANSACTION) @@ -320,9 +320,9 @@ public function message($from, $to, $stepLimit, string $privateKey, string $mess ->message($message) ->version($this->version) ->nid($nid) - ->stepLimit($stepLimit) ->timestamp() ->nonce() + ->stepLimit($stepLimit) ->sign($privateKey) ->send(); } @@ -344,6 +344,9 @@ public function message($from, $to, $stepLimit, string $privateKey, string $mess //TODO make it work for contracts as well public function debug_estimateStep(string $from, string $to, string $value = "0", string $nid = "0x1") { + $url = $this->iconServiceUrl; + $this->setIconServiceUrl(substr($url, 0,-2).'debug/v3'); + $res = $this->transactionBuilder ->method(TransactionTypes::ESTIMATE_STEP) ->version($this->version) @@ -355,6 +358,8 @@ public function debug_estimateStep(string $from, string $to, string $value = "0" ->nonce() ->send(); + $this->setIconServiceUrl($url); + return $res; } diff --git a/src/Transaction/Transaction.php b/src/Transaction/Transaction.php index d8675c8..6075376 100644 --- a/src/Transaction/Transaction.php +++ b/src/Transaction/Transaction.php @@ -13,9 +13,10 @@ class Transaction private $value; private $params; - public function __construct(int $id = 1234) + public function __construct(IconService $iconService, int $id = 1234) { $this->jsonrpc = '2.0'; + $this->iconService = $iconService; $this->id = $id; } @@ -27,14 +28,6 @@ public function getIconService(): IconService return $this->iconService; } - /** - * @param string $url - */ - public function setIconService(string $url): void - { - $this->iconService = new IconService($url); - } - /** * @return string | null */ diff --git a/src/Transaction/TransactionBuilder.php b/src/Transaction/TransactionBuilder.php index 2448624..2789a33 100644 --- a/src/Transaction/TransactionBuilder.php +++ b/src/Transaction/TransactionBuilder.php @@ -11,13 +11,11 @@ class TransactionBuilder { private $transaction; - private $iconService; private $iconServiceHelper; - public function __construct($iconService) + public function __construct(IconService $iconService) { - $this->transaction = new Transaction(); - $this->iconService = $iconService; + $this->transaction = new Transaction($iconService); $this->iconServiceHelper = new IconServiceHelper($iconService); } @@ -118,10 +116,25 @@ public function nonce($nonce = null): TransactionBuilder return $this; } - public function stepLimit(string $stepLimit = '0x186a0'): TransactionBuilder + public function stepLimit(?string $stepLimit = null): TransactionBuilder { - if (substr($stepLimit, 0, 2) !== '0x') { - $stepLimit = '0x'.dechex($stepLimit); + if (is_null($stepLimit)){ + $url = $this->transaction->getIconService()->getIconServiceUrl(); + $this->transaction->getIconService()->setIconServiceUrl(substr($url, 0, -2).'debug/v3'); + + $method = $this->transaction->getMethod(); + $stepLimit = $this-> + method(TransactionTypes::ESTIMATE_STEP)-> + send(); + $stepLimit = isset($stepLimit) ? (isset($stepLimit->result) ? $stepLimit->result : '0x0') : '0x0'; + + //Revert changes to method and iconservice + $this->transaction->setMethod($method); + $this->transaction->getIconService()->setIconServiceUrl($url); + }else{ + if(substr($stepLimit, 0, 2) !== '0x') { + $stepLimit = '0x'.dechex($stepLimit); + } } $params = [ @@ -162,13 +175,6 @@ public function sign(string $privateKey): TransactionBuilder return $this; } - //Possibly remove from here - public function testnet(string $url): TransactionBuilder - { - $this->transaction->setIconService($url); - return $this; - } - public function blockHeight(string $height): TransactionBuilder { if (substr($height, 0, 2) === '0x') { diff --git a/tests/IISSTest.php b/tests/IISSTest.php index 98948bf..8f84d52 100644 --- a/tests/IISSTest.php +++ b/tests/IISSTest.php @@ -33,7 +33,7 @@ public function test_setStake() $stepLimit = "0x1b580"; //112000 steps $nid = "0x3"; // YEOUIDO network - $this->assertTrue(!isset($this->iiss->setStake($value, $from, $stepLimit, $private_key, $nid)->error)); + $this->assertTrue(!isset($this->iiss->setStake($value, $from, $private_key, $stepLimit, $nid)->error)); } public function test_getStake() @@ -59,7 +59,7 @@ public function test_setDelegation() $stepLimit = "0x7e3a85"; // $nid = "0x3"; // YEOUIDO network - $this->assertTrue(!isset($this->iiss->setDelegation($delegations, $from, $stepLimit, $private_key, $nid)->error)); + $this->assertTrue(!isset($this->iiss->setDelegation($delegations, $from, $private_key, $stepLimit, $nid)->error)); } public function test_getDelegation() @@ -78,7 +78,7 @@ public function test_claimIScore() $stepLimit = "0x7e3a85"; // $nid = "0x3"; // YEOUIDO network - $this->assertTrue(!isset($this->iiss->claimIScore($from, $stepLimit, $private_key, $nid)->error)); + $this->assertTrue(!isset($this->iiss->claimIScore($from, $private_key, $stepLimit, $nid)->error)); } public function test_queryIScore() diff --git a/tests/IRC2Test.php b/tests/IRC2Test.php index 67e20d4..dea52b6 100644 --- a/tests/IRC2Test.php +++ b/tests/IRC2Test.php @@ -31,28 +31,27 @@ public function testIsThereAnySyntaxError() public function testName() { - $this->assertSame('IWinToken', $this->irc2->name()); + $this->assertSame('IWinToken', $this->irc2->name()->result); } public function testSymbol() { - $this->assertSame('IWIN', $this->irc2->symbol()); + $this->assertSame('IWIN', $this->irc2->symbol()->result); } public function testDecimals() { - $this->assertSame('0x12', $this->irc2->decimals()); + $this->assertSame('0x12', $this->irc2->decimals()->result); } public function testTotalSupply() { - $this->assertSame('0x1431e0fae6d7217caa0000000', $this->irc2->totalSupply()); + $this->assertSame('0x1431e0fae6d7217caa0000000', $this->irc2->totalSupply()->result); } public function testBalanceOf() { $this->assertTrue(!isset($this->irc2->balanceOf('hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160')->error)); - //$this->assertSame('0x43c33c1937564800000', $this->irc2->balanceOf('hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160')); } public function testTransfer() diff --git a/tests/IconServiceTest.php b/tests/IconServiceTest.php index 90d8b0f..2e97ed7 100644 --- a/tests/IconServiceTest.php +++ b/tests/IconServiceTest.php @@ -35,35 +35,37 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testIsThereAnySyntaxError() { $this->assertTrue(is_object($this->iconServiceMainnet)); + $this->assertTrue(is_object(new IconService('https://ctz.solidwallet.io/api/v3'))); + } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_getLastBlock() + public function test_getLastBlock() { - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getLastBlock()->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getLastBlock()->error)); } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_getBlockByHeight() + public function test_getBlockByHeight() { $height = "0x3"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getBlockByHeight($height)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getBlockByHeight($height)->error)); } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_getBlockByHash() + public function test_getBlockByHash() { $hash = "0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03"; //Yeouido - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getBlockByHash($hash)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getBlockByHash($hash)->error)); } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_call() + public function test_call() { $score = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; @@ -72,47 +74,47 @@ public function test_icx_call() $params->params = new stdClass(); $params->params->_owner = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_call($score, $params)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->call($score, $params)->error)); } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_getBalance() + public function test_getBalance() { $address = "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getBalance($address)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getBalance($address)->error)); } //Check if error is returned //TODO Check if request is made properly, error doesn't mean that test should fail - public function test_icx_getScoreApi() + public function test_getScoreApi() { $address = "cx9ab3078e72c8d9017194d17b34b1a47b661945ca"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getScoreApi($address)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getScoreApi($address)->error)); } - public function test_icx_getTotalSupply() + public function test_getTotalSupply() { - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getTotalSupply()->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getTotalSupply()->error)); } - public function test_icx_getTransactionResult() + public function test_getTransactionResult() { $txHash = "0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getTransactionResult($txHash)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getTransactionResult($txHash)->error)); } - public function test_icx_getTransactionByHash() + public function test_getTransactionByHash() { $txHash = "0xb89690b7598e07c286db87f05c1ee4cfc1cf915bf061007ac3404a42dc4979e9"; - $this->assertTrue(!isset($this->iconServiceMainnet->icx_getTransactionByHash($txHash)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getTransactionByHash($txHash)->error)); } public function test_ise_getStatus() { $keys = ['lastBlock']; - $this->assertTrue(!isset($this->iconServiceMainnet->ise_getStatus($keys)->error)); + $this->assertTrue(!isset($this->iconServiceMainnet->getStatus($keys)->error)); } //Not working for now @@ -124,8 +126,7 @@ public function test_debug_estimateStep() $timestamp = "0x5c42da6830136"; $value = "0xde0b6b3a7640000"; - $this->assertTrue(!isset($this->iconServiceDebugMainnet->debug_estimateStep($from, $to, $timestamp, $value)->error)); - unset($var); + $this->assertTrue(!isset($this->iconServiceMainnet->debug_estimateStep($from, $to, $timestamp, $value)->error)); } public function test_send() @@ -137,7 +138,7 @@ public function test_send() $stepLimit = "0x186a0"; // = 100000 steps $nid = "0x3"; // YEOUIDO network - $this->assertTrue(!isset($this->iconServiceYeouido->send($from, $to, $value, $stepLimit, $private_key, $nid)->error)); + $this->assertTrue(!isset($this->iconServiceYeouido->send($from, $to, $value, $private_key, $stepLimit, $nid)->error)); } //Commenting out until I find a contract to test against /* public function test_callSCORE() @@ -211,7 +212,7 @@ public function test_message() $stepLimit = "0xfffff"; // = 100000 steps $nid = "0x3"; // YEOUIDO network - $this->assertTrue(!isset($this->iconServiceYeouido->message($from, $to, $stepLimit, $private_key, $message, "0x0", $nid)->error)); + $this->assertTrue(!isset($this->iconServiceYeouido->message($from, $to, $private_key, $message, $stepLimit, $nid)->error)); } public function test_setIconServiceUrl() diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 85f83b5..d6163af 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -51,8 +51,8 @@ public function test_serializer() ->value('0x2386f26fc10000') ->version('0x3') ->nid() - ->stepLimit() ->nonce('0x1') + ->stepLimit('0x186a0') ->get(); $result = \iconation\IconSDK\Utils\Serializer::serialize($transaction); diff --git a/tests/TransactionBuilderTest.php b/tests/TransactionBuilderTest.php index 3ba421d..3dcb24b 100644 --- a/tests/TransactionBuilderTest.php +++ b/tests/TransactionBuilderTest.php @@ -2,6 +2,7 @@ use iconation\IconSDK\IconService\IconService; use iconation\IconSDK\Transaction\TransactionBuilder; +use iconation\IconSDK\Transaction\TransactionTypes; use PHPUnit\Framework\TestCase; @@ -45,12 +46,29 @@ public function test_stepLimit_wrong_prefix(){ unset($var); } - public function test_testnet(){ - $endpoint = 'test.net/endpoint'; - $builder = $this->transactionBuilder; - $result = $builder->testnet($endpoint)->getTransaction()->getIconService()->getIconServiceUrl(); - $this->assertSame($endpoint, $result); - unset($var); + public function test_stepLimit(){ + + $privateKey = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key + $from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160"; + $to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d"; + $value = "0x2386f26fc10000"; // = 0.01 ICX + $nid = "0x3"; // YEOUIDO network + + $transaction = $this->transactionBuilder + ->method(TransactionTypes::SEND_TRANSACTION) + ->from($from) + ->to($to) + ->value($value) + ->version('0x3') + ->nid($nid) + ->timestamp() + ->nonce() + ->stepLimit() + ->get(); + + $expectedStepLimit = '0x186a0'; + $resStepLimit = $transaction->getParams()->stepLimit; + $this->assertSame($expectedStepLimit, $resStepLimit); } public function test_value(){ diff --git a/tests/TransactionTest.php b/tests/TransactionTest.php index 2cc5be9..a2720db 100644 --- a/tests/TransactionTest.php +++ b/tests/TransactionTest.php @@ -1,6 +1,7 @@ iconservice = new IconService('https://ctz.solidwallet.io/api/v3'); + $this->transaction = new Transaction($this->iconservice); + } + /** * Just check if the YourClass has no syntax error * @@ -22,54 +33,52 @@ class TransactionTest extends TestCase public function testIsThereAnySyntaxError() { - $var = new Transaction(); - $this->assertTrue(is_object($var)); + $this->assertTrue(is_object($this->transaction)); unset($var); } public function test_setJsonRpc() { - $transaction = new Transaction(); $jsonrpc = '5.0'; - $transaction->setJsonrpc($jsonrpc); - $this->assertSame($jsonrpc, $transaction->getJsonrpc()); + $this->transaction->setJsonrpc($jsonrpc); + $this->assertSame($jsonrpc, $this->transaction->getJsonrpc()); unset($transaction); } public function test_setId() { - $transaction = new Transaction(); $id = 1234567; - $transaction->setId($id); - $this->assertSame($id, $transaction->getId()); + $this->transaction->setId($id); + $this->assertSame($id, $this->transaction->getId()); unset($transaction); } public function test_value() { - $transaction = new Transaction(); $value = '0x123'; - $transaction->setValue($value); - $this->assertSame($value, $transaction->getValue()); + $this->transaction->setValue($value); + $this->assertSame($value, $this->transaction->getValue()); unset($transaction); } public function test_getTransactionParamsObject_empty_params() { - $transaction = new Transaction(); - $this->assertNull($transaction->getTransactionParamsObject()); + $this->assertNull($this->transaction->getTransactionParamsObject()); unset($transaction); } public function test_getTransactionParamsArray(){ //First check when no param is set - $transaction = new Transaction(); - $this->assertNull($transaction->getTransactionParamsArray()); + $this->assertNull($this->transaction->getTransactionParamsArray()); //Now set an arbitrary param $paramsArray = ['test'=>'test']; - $transaction->setParams($paramsArray); - $this->assertNotNull($transaction->getTransactionParamsArray()); - $this->assertSame($paramsArray, $transaction->getTransactionParamsArray()); + $this->transaction->setParams($paramsArray); + $this->assertNotNull($this->transaction->getTransactionParamsArray()); + $this->assertSame($paramsArray, $this->transaction->getTransactionParamsArray()); + } + + public function test_getIconservice(){ + $this->assertInstanceOf(IconService::class, $this->transaction->getIconService()); } } \ No newline at end of file