Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delegate resource method #194

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: php

php:
- 7.4
- 8.0
- 8.1

matrix:
fast_finish: true
Expand Down
48 changes: 8 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
```
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
24 changes: 24 additions & 0 deletions examples/delegate-resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
include_once '../vendor/autoload.php';

$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());
}


$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);
2 changes: 1 addition & 1 deletion src/TRC20Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TRC20Contract
*
* @var integer
*/
private int $feeLimit = 10;
private int $feeLimit = 30;

/**
* Base Tron object
Expand Down
65 changes: 65 additions & 0 deletions src/TransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
63 changes: 63 additions & 0 deletions src/Tron.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down