Skip to content

Commit

Permalink
support stake 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ufado committed May 25, 2024
1 parent f0c5e50 commit 70490e7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
24 changes: 22 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## 概述

波场开发包目前支持波场的 TRX 和 TRC20 中生成地址,发起转账,离线签名等功能。正在持续更新,将会支持更多的功能,已修复[iexbase/tron-api](https://github.com/iexbase/tron-api)中的不少bug,将会持续维护。
波场开发包目前支持波场的 TRX 和 TRC20 中生成地址,发起转账,离线签名,资源代理和收回等功能。正在持续更新,将会支持更多的功能,已修复[iexbase/tron-api](https://github.com/iexbase/tron-api)中的不少bug,将会持续维护。

## 特点

1. 方法调用快捷方便
1. 兼容 TRON 网络中 TRX 货币和 TRC 系列所有通证
1. 支持最新的质押2.0中的资源代理和资源回收
1. 接口可灵活增减
1. 速度迅速 算法经过专门优化
1. 持续更新 始终跟进波场新功能
Expand All @@ -22,6 +23,8 @@
- 查询最新区块 `blockNumber()`
- 根据区块链查询信息 `blockByNumber(int $blockID)`
- 根据交易哈希查询信息 `transactionReceipt(string $txHash)`
- 资源代理`delegate(Address $from, Address $to, float $amount,string $resource = 'ENERGY', $lock=false,$lock_period=0)`
- 资源收回`undelegate(Address $from,Address $to, float $amount,string $resource = 'ENERGY')`

## 快速开始

Expand Down Expand Up @@ -70,11 +73,28 @@ $trx = $trxWallet->balance($fromAddr);//获取trx余额
$transferData = $trxWallet->transfer($fromAddr,$toAddr,1); //转账1trx
```

[Delegate.php](./examples/Delegate.php)

```php
//以下全部为质押2.0接口
$trxWallet->delegate($fromAddr,$toAddr,1);//代理1trx产生的能量
$trxWallet->undelegate($fromAddr,$toAddr,1);//收回1trx产生的能量
$trxWallet->delegate($fromAddr,$toAddr,1,"BANDWITH");//代理1trx产生的带宽
$trxWallet->undelegate($fromAddr,$toAddr,"BANDWITH");//收回1trx产生的带宽
$trxWallet->delegate($fromAddr,$toAddr,1,"ENERGY",true,1200);//代理1trx产生的能量,锁定期1小时,单位为3秒
$trxWallet->tron->getdelegatedresourceaccountindexv2($fromAddr->address);//获取全部已经代理的资源
```



## 感谢

| 开发者名称 | 描述 | 应用场景 |
| :-----| :---- | :---- |
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api |
| [Fenguoz](https://github.com/Fenguoz/) | 波场PHP 实现 | 波场基础Api |
| [Fenguoz](https://github.com/Fenguoz/) | 波场PHP 实现 | 波场基础Api |
| [ufado/tron-api](https://github.com/ufado/tron-api) | 基于iexbase的自行维护扩展包 | 波场基础Api |

## 联系

Https://t.me/ufado_bot
30 changes: 30 additions & 0 deletions examples/Delegate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
require("./vendor/autoload.php");
use GuzzleHttp\Client;
use IEXBase\TronAPI\Exception\ErrorException;
use IEXBase\TronAPI\Exception\TronException;
use Tron\Exceptions\TransactionException;
use Tron\Address;
use Tron\Api;
use Tron\Exceptions\TronErrorException;
use Tron\TRC20;
use Tron\TRX;
$tronApi = new Api(new Client(['base_uri' => 'https://nile.trongrid.io']));
$trxWallet = new TRX($tronApi);

$tronSecret = "0000000";//波场私钥
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
//转换成Address类
$fromAddr = $trc20Wallet->privateKeyToAddress($tronSecret);//发起地址
$toAddr = new Address(
$tronAddress,
'test',
$trc20Wallet->tron->address2HexString($tronAddress)
);//接受地址
//以下全部为质押2.0接口
$trxWallet->delegate($fromAddr,$toAddr,1);//代理1trx产生的能量
$trxWallet->undelegate($fromAddr,$toAddr,1);//收回1trx产生的能量
$trxWallet->delegate($fromAddr,$toAddr,1,"BANDWITH");//代理1trx产生的带宽
$trxWallet->undelegate($fromAddr,$toAddr,"BANDWITH");//收回1trx产生的带宽
$trxWallet->delegate($fromAddr,$toAddr,1,"ENERGY",true,1200);//代理1trx产生的能量,锁定期1小时,单位为3秒
$trxWallet->tron->getdelegatedresourceaccountindexv2($fromAddr->address);//获取全部已经代理的资源
39 changes: 39 additions & 0 deletions src/TRX.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,46 @@ public function balance(Address $address)
$this->tron->setAddress($address->address);
return $this->tron->getBalance(null, true);
}
public function delegate(Address $from, Address $to, float $amount,string $resource = 'ENERGY', $lock=false,$lock_period=0)
{
$this->tron->setAddress($from->address);
$this->tron->setPrivateKey($from->privateKey);
try {
$response = $this->tron->sendDelegate($to->address,$amount,$resource,$lock,$lock_period);
} catch (TronException $e) {
throw new TransactionException($e->getMessage(), $e->getCode());
}

if (isset($response['result']) && $response['result'] == true) {
return new Transaction(
$response['txID'],
$response['raw_data'],
'DelegateResourceContract'
);
} else {
throw new TransactionException(hex2bin($response['message']));
}
}
public function undelegate(Address $from,Address $to, float $amount,string $resource = 'ENERGY')
{
$this->tron->setAddress($from->address);
$this->tron->setPrivateKey($from->privateKey);
try {
$response = $this->tron->sendUnDelegate($to->address,$amount,$resource);
} catch (TronException $e) {
throw new TransactionException($e->getMessage(), $e->getCode());
}

if (isset($response['result']) && $response['result'] == true) {
return new Transaction(
$response['txID'],
$response['raw_data'],
'UnDelegateResourceContract'
);
} else {
throw new TransactionException(hex2bin($response['message']));
}
}
public function transfer(Address $from, Address $to, float $amount): Transaction
{
$this->tron->setAddress($from->address);
Expand Down

0 comments on commit 70490e7

Please sign in to comment.