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/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": [ 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/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 diff --git a/src/TransactionBuilder.php b/src/TransactionBuilder.php index 1403a7c..f1831f6 100644 --- a/src/TransactionBuilder.php +++ b/src/TransactionBuilder.php @@ -283,6 +283,71 @@ public function freezeBalance(float $amount = 0, int $duration = 3, string $reso ]); } + /** + * 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 + * @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, + ], + ); + } + + 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 5ff3d21..ee8ea70 100644 --- a/src/Tron.php +++ b/src/Tron.php @@ -968,6 +968,69 @@ 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']; + } + + $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); + } + + 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.