From a75dc7ab2137da4ff6ea32b52dbde396fa206508 Mon Sep 17 00:00:00 2001 From: wawilow Date: Fri, 8 Mar 2024 08:09:38 -0500 Subject: [PATCH 1/7] delegateResource function --- src/TransactionBuilder.php | 41 ++++++++++++++++++++++++++++++++++++++ src/Tron.php | 26 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/TransactionBuilder.php b/src/TransactionBuilder.php index 1403a7c..9c3adb5 100644 --- a/src/TransactionBuilder.php +++ b/src/TransactionBuilder.php @@ -283,6 +283,47 @@ public function freezeBalance(float $amount = 0, int $duration = 3, string $reso ]); } + /** + * Freezes an amount of TRX. + * Will give bandwidth OR Energy and TRON Power(voting rights) to the owner of the frozen tokens. + * + * @param string $owner_address + * @param string $resource + * @param string $receiver_address + * @param int $balance + * @param bool $lock + * @param int $lock_period + * @return array + * @throws TronException + */ + public function delegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0, bool $lock = false, int $lock_period = 0) + { + if(empty($owner_address) or empty($receiver_address)) { + throw new TronException('Address not specified'); + } + if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) { + throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"'); + } + if (!is_int($balance) or !is_int($lock_period)) { + throw new TronException('Invalid balance or lock_period provided'); + } + if(!is_bool($lock)) { + throw new TronException('Invalid lock value provided'); + } + + return $this->tron->getManager()->request( + 'wallet/delegateresource', + [ + 'owner_address' => $this->tron->address2HexString($owner_address), + 'resource' => $resource, + 'receiver_address' => $this->tron->address2HexString($receiver_address), + 'balance' => $this->tron->toTron($balance), + 'lock' => $lock, + 'lock_period' => $lock_period, + ], + ); + } + /** * Unfreeze TRX that has passed the minimum freeze duration. * Unfreezing will remove bandwidth and TRON Power. diff --git a/src/Tron.php b/src/Tron.php index 5ff3d21..225ead4 100644 --- a/src/Tron.php +++ b/src/Tron.php @@ -968,6 +968,32 @@ public function freezeBalance(float $amount = 0, int $duration = 3, string $reso return array_merge($response, $signedTransaction); } + /** + * Delegate bandwidth or energy resources to other accounts in Stake2.0. + * Will delegate bandwidth OR Energy resources to other accounts. + * + * @param string|null $owner_address + * @param string $resource + * @param string|null $receiver_address + * @param int $balance + * @param bool $lock + * @param int $lock_period + * @return array + * @throws TronException + */ + public function delegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0, bool $lock = false, int $lock_period = 0) + { + if($owner_address == null) { + $owner_address = $this->address['hex']; + } + + $freeze = $this->transactionBuilder->delegateResource($owner_address, $resource, $receiver_address, $balance, $lock, $lock_period); + $signedTransaction = $this->signTransaction($freeze); + $response = $this->sendRawTransaction($signedTransaction); + + return array_merge($response, $signedTransaction); + } + /** * Unfreeze TRX that has passed the minimum freeze duration. * Unfreezing will remove bandwidth and TRON Power. From 22640e46b6fb7b1fd8f0be1927a1ef29f0b9c966 Mon Sep 17 00:00:00 2001 From: wawilow Date: Fri, 8 Mar 2024 08:11:17 -0500 Subject: [PATCH 2/7] Docs update --- src/TransactionBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TransactionBuilder.php b/src/TransactionBuilder.php index 9c3adb5..a404491 100644 --- a/src/TransactionBuilder.php +++ b/src/TransactionBuilder.php @@ -284,8 +284,8 @@ public function freezeBalance(float $amount = 0, int $duration = 3, string $reso } /** - * Freezes an amount of TRX. - * Will give bandwidth OR Energy and TRON Power(voting rights) to the owner of the frozen tokens. + * Delegate bandwidth or energy resources to other accounts in Stake2.0. + * Will delegate bandwidth OR Energy resources to other accounts. * * @param string $owner_address * @param string $resource From ef98b247f818a6dfe264ba852d7730c957ca725c Mon Sep 17 00:00:00 2001 From: wawilow Date: Sun, 10 Mar 2024 01:50:55 -0500 Subject: [PATCH 3/7] Tests and refactoring --- examples/delegate-resource.php | 24 ++++++++++++++++++++++++ src/Tron.php | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 examples/delegate-resource.php diff --git a/examples/delegate-resource.php b/examples/delegate-resource.php new file mode 100644 index 0000000..da86e36 --- /dev/null +++ b/examples/delegate-resource.php @@ -0,0 +1,24 @@ +getMessage()); +} + + +$tron->setAddress('public_key'); +$tron->setPrivateKey('private_key'); + +try { + $transfer = $tron->delegateResource( null, 'BANDWIDTH', 'receiver_public_key', 1, false, 0); +} catch (\IEXBase\TronAPI\Exception\TronException $e) { + die($e->getMessage()); +} + +var_dump($transfer); \ No newline at end of file diff --git a/src/Tron.php b/src/Tron.php index 225ead4..a94d32f 100644 --- a/src/Tron.php +++ b/src/Tron.php @@ -987,8 +987,8 @@ public function delegateResource(string $owner_address = null, string $resource $owner_address = $this->address['hex']; } - $freeze = $this->transactionBuilder->delegateResource($owner_address, $resource, $receiver_address, $balance, $lock, $lock_period); - $signedTransaction = $this->signTransaction($freeze); + $delegate = $this->transactionBuilder->delegateResource($owner_address, $resource, $receiver_address, $balance, $lock, $lock_period); + $signedTransaction = $this->signTransaction($delegate); $response = $this->sendRawTransaction($signedTransaction); return array_merge($response, $signedTransaction); From b3efdbd1f18ffe79487eab1f1ddef0ec1019b107 Mon Sep 17 00:00:00 2001 From: wawilow Date: Sun, 10 Mar 2024 03:17:35 -0400 Subject: [PATCH 4/7] Increase fee limit to 30, add allow php 8 and 8.1 --- .travis.yml | 2 ++ src/TRC20Contract.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 727cfa2..be39aaa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,8 @@ language: php php: - 7.4 + - 8.0 + - 8.1 matrix: fast_finish: true diff --git a/src/TRC20Contract.php b/src/TRC20Contract.php index 708772f..c5d2c3e 100644 --- a/src/TRC20Contract.php +++ b/src/TRC20Contract.php @@ -68,7 +68,7 @@ class TRC20Contract * * @var integer */ - private int $feeLimit = 10; + private int $feeLimit = 30; /** * Base Tron object From aae3ec938eb204becb72a286be1eb8904e100bc5 Mon Sep 17 00:00:00 2001 From: wawilow Date: Sun, 10 Mar 2024 03:30:45 -0400 Subject: [PATCH 5/7] Edit composer package to publish --- README.md | 48 ++++++++---------------------------------------- composer.json | 8 ++++++-- 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index a6b9894..6025b1b 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ A PHP API for interacting with the Tron Protocol ## Install +```bash +> composer require wawilow/tron-api --ignore-platform-reqs +``` + +PS Install original library ```bash > composer require iexbase/tron-api --ignore-platform-reqs ``` @@ -18,48 +23,11 @@ The following versions of PHP are supported by this version. * PHP 7.4 -## Example Usage - -```php -use IEXBase\TronAPI\Tron; - -$fullNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io'); -$solidityNode = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io'); -$eventServer = new \IEXBase\TronAPI\Provider\HttpProvider('https://api.trongrid.io'); - -try { - $tron = new \IEXBase\TronAPI\Tron($fullNode, $solidityNode, $eventServer); -} catch (\IEXBase\TronAPI\Exception\TronException $e) { - exit($e->getMessage()); -} - - -$this->setAddress('..'); -//Balance -$tron->getBalance(null, true); - -// Transfer Trx -var_dump($tron->send('to', 1.5)); - -//Generate Address -var_dump($tron->createAccount()); - -//Get Last Blocks -var_dump($tron->getLatestBlocks(2)); - -//Change account name (only once) -var_dump($tron->changeAccountName('address', 'NewName')); - - -// Contract -$tron->contract('Contract Address'); - - - -``` - ## Testing +```bash +> composer install --ignore-platform-reqs +``` ``` bash $ vendor/bin/phpunit ``` diff --git a/composer.json b/composer.json index df49f38..346073f 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,17 @@ { - "name": "iexbase/tron-api", + "name": "wawilow/tron-api", "description": "A PHP API for interacting with Tron (Trx)", "license": "MIT", "type": "library", - "homepage": "https://github.com/iexbase/tron-api", + "homepage": "https://github.com/wawilow/tron-api", "authors": [ { "name": "Shamsudin Serderov", "email": "steein.shamsudin@gmail.com" + }, + { + "name": "George Wawilow", + "email": "wawilow@protonmail.com" } ], "keywords": [ From 2694c2158c49e1f355e3b9f309c40c5e9cbebeef Mon Sep 17 00:00:00 2001 From: Shaksi Date: Sun, 11 Aug 2024 20:25:45 +0300 Subject: [PATCH 6/7] adding delegate and undelegate (#1) adding undelegate and GetDelegatedResourceV2 --- composer.json | 100 +++++++++++++++++-------------------- src/TransactionBuilder.php | 24 +++++++++ src/Tron.php | 37 ++++++++++++++ 3 files changed, 106 insertions(+), 55 deletions(-) diff --git a/composer.json b/composer.json index 346073f..818cef5 100644 --- a/composer.json +++ b/composer.json @@ -1,57 +1,47 @@ { - "name": "wawilow/tron-api", - "description": "A PHP API for interacting with Tron (Trx)", - "license": "MIT", - "type": "library", - "homepage": "https://github.com/wawilow/tron-api", - "authors": [ - { - "name": "Shamsudin Serderov", - "email": "steein.shamsudin@gmail.com" - }, - { - "name": "George Wawilow", - "email": "wawilow@protonmail.com" - } - ], - "keywords": [ - "iexbase", - "tron-lib", - "tron-php", - "tron-api", - "tron-rest-api" - ], - - "require": { - "php": ">=8.0", - "comely-io/data-types": "^1.0", - "guzzlehttp/guzzle": "^7.2", - "iexbase/web3.php": "^2.0.1", - "kornrunner/secp256k1": "^0.2", - "simplito/elliptic-php": "^1.0", - "ext-json": "*", - "ext-bcmath": "*" - }, - - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "autoload": { - "psr-4": { - "IEXBase\\TronAPI\\": "src" - } - }, - - "autoload-dev": { - "psr-4": { - "IEXBase\\TronAPI\\Test\\": "tests" - } - }, - - "config": { - "sort-packages": true - }, - - "minimum-stability": "dev", - "prefer-stable": true + "name": "shaksi/tron-sdk", + "description": "A PHP API for interacting with Tron (Trx)", + "license": "MIT", + "type": "library", + "homepage": "https://github.com/iexbase/tron-api", + "authors": [ + { + "name": "Shamsudin Serderov", + "email": "steein.shamsudin@gmail.com" + } + ], + "keywords": [ + "iexbase", + "tron-lib", + "tron-php", + "tron-api", + "tron-rest-api" + ], + "require": { + "php": ">=8.0", + "guzzlehttp/guzzle": "^7.2", + "tronapi/data-types": "^1.0.35", + "ffsoft-foss/web3.php": "^2.1", + "kornrunner/secp256k1": "^0.2", + "simplito/elliptic-php": "^1.0", + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "autoload": { + "psr-4": { + "IEXBase\\TronAPI\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "IEXBase\\TronAPI\\Test\\": "tests" + } + }, + "config": { + "sort-packages": true + }, + "minimum-stability": "stable", + "prefer-stable": true } diff --git a/src/TransactionBuilder.php b/src/TransactionBuilder.php index a404491..f1831f6 100644 --- a/src/TransactionBuilder.php +++ b/src/TransactionBuilder.php @@ -324,6 +324,30 @@ public function delegateResource(string $owner_address = null, string $resource ); } + public function undelegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0) + { + if(empty($owner_address) or empty($receiver_address)) { + throw new TronException('Address not specified'); + } + if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) { + throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"'); + } + if (!is_int($balance)) { + throw new TronException('Invalid balance'); + } + + + return $this->tron->getManager()->request( + 'wallet/undelegateresource', + [ + 'owner_address' => $this->tron->address2HexString($owner_address), + 'resource' => $resource, + 'receiver_address' => $this->tron->address2HexString($receiver_address), + 'balance' => $this->tron->toTron($balance), + ], + ); + } + /** * Unfreeze TRX that has passed the minimum freeze duration. * Unfreezing will remove bandwidth and TRON Power. diff --git a/src/Tron.php b/src/Tron.php index a94d32f..ee8ea70 100644 --- a/src/Tron.php +++ b/src/Tron.php @@ -994,6 +994,43 @@ public function delegateResource(string $owner_address = null, string $resource return array_merge($response, $signedTransaction); } + public function UndelegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0, bool $visible = true): array + { + if($owner_address == null) { + $owner_address = $this->address['hex']; + } + + $undelegate = $this->transactionBuilder->UndelegateResource($owner_address, $resource, $receiver_address, $balance); + $signedTransaction = $this->signTransaction($undelegate); + $response = $this->sendRawTransaction($signedTransaction); + + return array_merge($response, $signedTransaction); + } + + /** + * Query bandwidth information. + * + * @param $address + * @return array + * @throws TronException + */ + public function getdelegatedresourcev2(string $to,string $from=null) + { + $from = (!is_null($from) ? $this->toHex($from) : $this->address['hex']); + $to = $this->toHex($to); + return $this->manager->request('wallet/getdelegatedresourcev2', [ + 'fromAddress' => $from, + 'toAddress'=>$to + ]); + } + public function getdelegatedresourceaccountindexv2(string $from=null) + { + $from = (!is_null($from) ? $this->toHex($from) : $this->address['hex']); + return $this->manager->request('wallet/getdelegatedresourceaccountindexv2', [ + 'value' => $from, + ]); + } + /** * Unfreeze TRX that has passed the minimum freeze duration. * Unfreezing will remove bandwidth and TRON Power. From baaa5e2e755bf7bca8a375cfa3ddab73e1a486ab Mon Sep 17 00:00:00 2001 From: George Wawilow Date: Sun, 11 Aug 2024 13:26:11 -0400 Subject: [PATCH 7/7] Get back composer.json --- composer.json | 100 +++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index 818cef5..346073f 100644 --- a/composer.json +++ b/composer.json @@ -1,47 +1,57 @@ { - "name": "shaksi/tron-sdk", - "description": "A PHP API for interacting with Tron (Trx)", - "license": "MIT", - "type": "library", - "homepage": "https://github.com/iexbase/tron-api", - "authors": [ - { - "name": "Shamsudin Serderov", - "email": "steein.shamsudin@gmail.com" - } - ], - "keywords": [ - "iexbase", - "tron-lib", - "tron-php", - "tron-api", - "tron-rest-api" - ], - "require": { - "php": ">=8.0", - "guzzlehttp/guzzle": "^7.2", - "tronapi/data-types": "^1.0.35", - "ffsoft-foss/web3.php": "^2.1", - "kornrunner/secp256k1": "^0.2", - "simplito/elliptic-php": "^1.0", - "ext-json": "*" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "autoload": { - "psr-4": { - "IEXBase\\TronAPI\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "IEXBase\\TronAPI\\Test\\": "tests" - } - }, - "config": { - "sort-packages": true - }, - "minimum-stability": "stable", - "prefer-stable": true + "name": "wawilow/tron-api", + "description": "A PHP API for interacting with Tron (Trx)", + "license": "MIT", + "type": "library", + "homepage": "https://github.com/wawilow/tron-api", + "authors": [ + { + "name": "Shamsudin Serderov", + "email": "steein.shamsudin@gmail.com" + }, + { + "name": "George Wawilow", + "email": "wawilow@protonmail.com" + } + ], + "keywords": [ + "iexbase", + "tron-lib", + "tron-php", + "tron-api", + "tron-rest-api" + ], + + "require": { + "php": ">=8.0", + "comely-io/data-types": "^1.0", + "guzzlehttp/guzzle": "^7.2", + "iexbase/web3.php": "^2.0.1", + "kornrunner/secp256k1": "^0.2", + "simplito/elliptic-php": "^1.0", + "ext-json": "*", + "ext-bcmath": "*" + }, + + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "autoload": { + "psr-4": { + "IEXBase\\TronAPI\\": "src" + } + }, + + "autoload-dev": { + "psr-4": { + "IEXBase\\TronAPI\\Test\\": "tests" + } + }, + + "config": { + "sort-packages": true + }, + + "minimum-stability": "dev", + "prefer-stable": true }