Skip to content

Commit

Permalink
Refactors helpers, adds bcmath as required extension and bumps php ve…
Browse files Browse the repository at this point in the history
…rsion to v7.2
  • Loading branch information
mitsosf committed Jun 11, 2020
1 parent 8a991c3 commit 04084c0
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 58 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2020-06-11
### Added
- README, added requirements and installation instructions
- composer.json, added ext-bcmath as requirement

### Changed
- Helpers::icxToHex() and Helpers::hexToIcx() refactored using bcmath and gmp
- applied stricter parameter types
- bumped required php version 7.1 -> 7.2

### Removed
- extra unused functions and variable in Transaction

## [1.0.1] - 2020-06-09

### Added

- Changelog
- Security Policy

## [1.0.0] - 2020-06-09

### Added

- Iconservice
- IRC2
- IISS
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
alt="ICONation logo">
</p>

<h1 align="center">ICON SDK for PHP (Unofficial)</h1>
<h1 align="center">ICON SDK for PHP</h1>

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

This is an unofficial SDK to communicate with the ICON JSON-RPC server, built for PHP.
This is an SDK to communicate with the ICON blockchain, built for PHP.

Disclaimer: I cannot guarantee optimal performance of this software.
It is provided as is and without any assurances. Use it at your own risk.
Expand All @@ -18,9 +18,30 @@ Features
--------
Fully or partially supports all Iconservice functions, IRC-2 tokens and IISS calls.

Requirements & Installation
--------
Make sure you're using >=php7.2. Then check if you already have or install the required php extensions:

```shell script
apt install php-curl php-xml php-gmp php-bcmath
```

Require the package in the `composer.json` file in your project:
```shell script
composer require iconation/icon-sdk-php --no-dev
```
Testing
--------

```shell script
apt install php-mbstring
composer install
composer test
```

Usage
--------
####Iconservice:
#### Iconservice:
* icx_getLastBlock
```php
use iconation\IconSDK\IconService\IconService;
Expand Down Expand Up @@ -140,7 +161,7 @@ $message = "Your message goes here"; // = 0.01 ICX

$res = $iconservice->message($from, $to, $private_key, $message);
```
###IRC-2:
### IRC-2:
* name
```php
use iconation\IconSDK\IconService\IconService;
Expand Down Expand Up @@ -216,7 +237,7 @@ $res = $irc2->transfer($from, $to, $value, $privateKey);
```


###IISS:
### IISS:
* setStake
```php
use iconation\IconSDK\IconService\IconService;
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iconation/icon-sdk-php",
"description": "SDK to communicate with the ICON JSON-RPC server",
"description": "SDK to communicate with the ICON blockchain",
"keywords": [
"icon",
"icx"
Expand All @@ -14,10 +14,11 @@
],
"type": "library",
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-curl": "*",
"ext-json": "*",
"ext-gmp": "*",
"ext-bcmath": "*",
"simplito/elliptic-php": "^1.0"
},
"require-dev": {
Expand Down
10 changes: 5 additions & 5 deletions src/IISS/IISS.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,39 @@ public function __construct(IconService $iconService)
$this->transactionBuilder = new TransactionBuilder($iconService);
}

public function setStake($value, $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1')
public function setStake(string $value, string $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1')
{
$methodParams = new \stdClass();
$methodParams->value = Helpers::icxToHex($value);

return $this->sendTransactionToGovernanceContract('setStake', $methodParams, $from, $privateKey, $stepLimit, $nid);
}

public function getStake($address)
public function getStake(string $address)
{
$methodParams = new \stdClass();
$methodParams->address = $address;

return $this->call('getStake', $methodParams);
}

public function setDelegation($delegations, $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1')
public function setDelegation(array $delegations, string $from, string $privateKey, ?string $stepLimit = null, $nid = '0x1')
{
$methodParams = new \stdClass();
$methodParams->delegations = $delegations;

return $this->sendTransactionToGovernanceContract('setDelegation', $methodParams, $from, $privateKey, $stepLimit, $nid);
}

public function getDelegation($address)
public function getDelegation(string $address)
{
$methodParams = new \stdClass();
$methodParams->address = $address;

return $this->call('getDelegation', $methodParams);
}

public function claimIScore($from, string $privateKey, ?string $stepLimit = null, $nid = '0x1'){
public function claimIScore(string $from, string $privateKey, ?string $stepLimit = null, string $nid = '0x1'){
return $this->sendTransactionToGovernanceContract('claimIScore', null, $from, $privateKey, $stepLimit, $nid);
}

Expand Down
2 changes: 1 addition & 1 deletion src/IconService/IconService.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function getStatus($keys)
->send();
}

public function send(string $from, string $to, string $value, string $privateKey, ?string $stepLimit = null, $nid = '0x1')
public function send(string $from, string $to, string $value, string $privateKey, ?string $stepLimit = null, string $nid = '0x1')
{
return $this->transactionBuilder
->method(TransactionTypes::SEND_TRANSACTION)
Expand Down
17 changes: 0 additions & 17 deletions src/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Transaction
private $id;
private $iconService;
private $method;
private $value;
private $params;

public function __construct(IconService $iconService, int $id = 1234)
Expand Down Expand Up @@ -98,22 +97,6 @@ public function setId(int $id): void
$this->id = $id;
}

/**
* @return string
*/
public function getValue()
{
return $this->value;
}

/**
* @param string $value
*/
public function setValue($value): void
{
$this->value = $value;
}

/**
* @return \stdClass
*/
Expand Down
27 changes: 20 additions & 7 deletions src/Utils/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ public static function getBase64TimestampInMilliseconds()
}

/**
* @param $value float|int
* @param string $value
* @param int $decimals
* @return string
*/
public static function icxToHex($value)
public static function icxToHex($value, $decimals = 18)
{
return '0x' . dechex($value * 10 ** 18);
return '0x' . self::bcdechex(bcmul($value, 10**$decimals));
}

/**
* @param $value
* @return float|int
* @param string $value
* @param int $decimals
* @return string
*/
public static function hexToIcx($value)
public static function hexToIcx($value, $decimals = 18)
{
return hexdec($value) / 10 ** 18;
$value = gmp_init($value, '16');
return bcdiv(gmp_strval($value), 10**18, $decimals);
}

public static function isPrivateKey($key)
Expand Down Expand Up @@ -81,4 +84,14 @@ public static function isPublicAddress($address)

return true;
}

public static function bcdechex($dec) {
$hex = '';
do {
$last = bcmod($dec, 16);
$hex = dechex($last).$hex;
$dec = bcdiv(bcsub($dec, $last), 16);
} while($dec>0);
return $hex;
}
}
8 changes: 4 additions & 4 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
class HelpersTest extends TestCase{

public function test_icxToHex(){
$result = '0x2386f26fc10000';
$this->assertTrue(Helpers::icxToHex(0.01) === $result);
$result = '0x8ac7230489e80001';
$this->assertTrue(Helpers::icxToHex('10.000000000000000001') === $result);
unset($var);
}

public function test_hexToIcx(){
$result = 0.01;
$this->assertTrue(Helpers::hexToIcx('0x2386f26fc10000') === $result);
$result = '10.000000000000000001';
$this->assertTrue(Helpers::hexToIcx('0x8ac7230489e80001') === $result);
unset($var);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/IRC2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function testTransfer()
{
$this->assertTrue(!isset($this->irc2->transfer('hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160',
'hxf8689d6c4c8f333651469fdea2ac59a18f6c242d',
'1',
'13.8',
'3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5',
'0x186a00',
null,
'0x3'
)->error));
}
Expand Down
7 changes: 2 additions & 5 deletions tests/IconServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ class IconServiceTest extends TestCase
{
private $iconServiceMainnet;
private $iconServiceYeouido;
private $iconServiceDebugMainnet;

public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->iconServiceMainnet = new IconService('https://ctz.solidwallet.io/api/v3');
$this->iconServiceDebugMainnet = new IconService('https://ctz.solidwallet.io/api/debug/v3');
$this->iconServiceYeouido = new IconService('https://bicon.net.solidwallet.io/api/v3');
}

Expand Down Expand Up @@ -123,18 +121,17 @@ public function test_debug_estimateStep()
{
$from = "hxc4193cda4a75526bf50896ec242d6713bb6b02a3";
$to = "hxaa36c3e67d51f993a900fd5acf8b1eb5029c5dfd";
$timestamp = "0x5c42da6830136";
$value = "0xde0b6b3a7640000";

$this->assertTrue(!isset($this->iconServiceMainnet->debug_estimateStep($from, $to, $timestamp, $value)->error));
$this->assertTrue(!isset($this->iconServiceMainnet->debug_estimateStep($from, $to, $value)->error));
}

public function test_send()
{
$private_key = "3468ea815d8896ef4552f10768caf2660689b965975c3ec2c1f5fe84bc3a77a5"; //Sender's private key
$from = "hx8dc6ae3d93e60a2dddf80bfc5fb1cd16a2bf6160";
$to = "hxf8689d6c4c8f333651469fdea2ac59a18f6c242d";
$value = "0x2386f26fc10000"; // = 0.01 ICX
$value = "0.001"; // = 0.01 ICX
$stepLimit = "0x186a0"; // = 100000 steps
$nid = "0x3"; // YEOUIDO network

Expand Down
8 changes: 0 additions & 8 deletions tests/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public function test_setId()
unset($transaction);
}

public function test_value()
{
$value = '0x123';
$this->transaction->setValue($value);
$this->assertSame($value, $this->transaction->getValue());
unset($transaction);
}

public function test_getTransactionParamsObject_empty_params()
{
$this->assertNull($this->transaction->getTransactionParamsObject());
Expand Down

0 comments on commit 04084c0

Please sign in to comment.