Skip to content

Commit 70490e7

Browse files
committed
support stake 2.0
1 parent f0c5e50 commit 70490e7

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

README.md

100644100755
+22-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## 概述
44

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

77
## 特点
88

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

2629
## 快速开始
2730

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

76+
[Delegate.php](./examples/Delegate.php)
77+
78+
```php
79+
//以下全部为质押2.0接口
80+
$trxWallet->delegate($fromAddr,$toAddr,1);//代理1trx产生的能量
81+
$trxWallet->undelegate($fromAddr,$toAddr,1);//收回1trx产生的能量
82+
$trxWallet->delegate($fromAddr,$toAddr,1,"BANDWITH");//代理1trx产生的带宽
83+
$trxWallet->undelegate($fromAddr,$toAddr,"BANDWITH");//收回1trx产生的带宽
84+
$trxWallet->delegate($fromAddr,$toAddr,1,"ENERGY",true,1200);//代理1trx产生的能量,锁定期1小时,单位为3秒
85+
$trxWallet->tron->getdelegatedresourceaccountindexv2($fromAddr->address);//获取全部已经代理的资源
86+
```
87+
7388

7489

7590
## 感谢
7691

7792
| 开发者名称 | 描述 | 应用场景 |
7893
| :-----| :---- | :---- |
7994
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api |
80-
| [Fenguoz](https://github.com/Fenguoz/) | 波场PHP 实现 | 波场基础Api |
95+
| [Fenguoz](https://github.com/Fenguoz/) | 波场PHP 实现 | 波场基础Api |
96+
| [ufado/tron-api](https://github.com/ufado/tron-api) | 基于iexbase的自行维护扩展包 | 波场基础Api |
97+
98+
## 联系
99+
100+
Https://t.me/ufado_bot

examples/Delegate.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
require("./vendor/autoload.php");
3+
use GuzzleHttp\Client;
4+
use IEXBase\TronAPI\Exception\ErrorException;
5+
use IEXBase\TronAPI\Exception\TronException;
6+
use Tron\Exceptions\TransactionException;
7+
use Tron\Address;
8+
use Tron\Api;
9+
use Tron\Exceptions\TronErrorException;
10+
use Tron\TRC20;
11+
use Tron\TRX;
12+
$tronApi = new Api(new Client(['base_uri' => 'https://nile.trongrid.io']));
13+
$trxWallet = new TRX($tronApi);
14+
15+
$tronSecret = "0000000";//波场私钥
16+
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
17+
//转换成Address类
18+
$fromAddr = $trc20Wallet->privateKeyToAddress($tronSecret);//发起地址
19+
$toAddr = new Address(
20+
$tronAddress,
21+
'test',
22+
$trc20Wallet->tron->address2HexString($tronAddress)
23+
);//接受地址
24+
//以下全部为质押2.0接口
25+
$trxWallet->delegate($fromAddr,$toAddr,1);//代理1trx产生的能量
26+
$trxWallet->undelegate($fromAddr,$toAddr,1);//收回1trx产生的能量
27+
$trxWallet->delegate($fromAddr,$toAddr,1,"BANDWITH");//代理1trx产生的带宽
28+
$trxWallet->undelegate($fromAddr,$toAddr,"BANDWITH");//收回1trx产生的带宽
29+
$trxWallet->delegate($fromAddr,$toAddr,1,"ENERGY",true,1200);//代理1trx产生的能量,锁定期1小时,单位为3秒
30+
$trxWallet->tron->getdelegatedresourceaccountindexv2($fromAddr->address);//获取全部已经代理的资源

src/TRX.php

+39
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,46 @@ public function balance(Address $address)
107107
$this->tron->setAddress($address->address);
108108
return $this->tron->getBalance(null, true);
109109
}
110+
public function delegate(Address $from, Address $to, float $amount,string $resource = 'ENERGY', $lock=false,$lock_period=0)
111+
{
112+
$this->tron->setAddress($from->address);
113+
$this->tron->setPrivateKey($from->privateKey);
114+
try {
115+
$response = $this->tron->sendDelegate($to->address,$amount,$resource,$lock,$lock_period);
116+
} catch (TronException $e) {
117+
throw new TransactionException($e->getMessage(), $e->getCode());
118+
}
110119

120+
if (isset($response['result']) && $response['result'] == true) {
121+
return new Transaction(
122+
$response['txID'],
123+
$response['raw_data'],
124+
'DelegateResourceContract'
125+
);
126+
} else {
127+
throw new TransactionException(hex2bin($response['message']));
128+
}
129+
}
130+
public function undelegate(Address $from,Address $to, float $amount,string $resource = 'ENERGY')
131+
{
132+
$this->tron->setAddress($from->address);
133+
$this->tron->setPrivateKey($from->privateKey);
134+
try {
135+
$response = $this->tron->sendUnDelegate($to->address,$amount,$resource);
136+
} catch (TronException $e) {
137+
throw new TransactionException($e->getMessage(), $e->getCode());
138+
}
139+
140+
if (isset($response['result']) && $response['result'] == true) {
141+
return new Transaction(
142+
$response['txID'],
143+
$response['raw_data'],
144+
'UnDelegateResourceContract'
145+
);
146+
} else {
147+
throw new TransactionException(hex2bin($response['message']));
148+
}
149+
}
111150
public function transfer(Address $from, Address $to, float $amount): Transaction
112151
{
113152
$this->tron->setAddress($from->address);

0 commit comments

Comments
 (0)