Skip to content

Commit

Permalink
add examples and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ufado committed Mar 28, 2024
1 parent 04b7222 commit 91cad9c
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 65 deletions.
142 changes: 80 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,80 @@
<h1 align="center">TRON-PHP</h1>

## 概述

TRON-PHP 目前支持波场的 TRX 和 TRC20 中常用生成地址,发起转账,离线签名等功能。

## 特点

1. 一套写法兼容 TRON 网络中 TRX 货币和 TRC 系列所有通证
1. 接口方法可可灵活增减

## 支持方法

- 生成地址 `generateAddress()`
- 验证地址 `validateAddress(Address $address)`
- 根据私钥得到地址 `privateKeyToAddress(string $privateKeyHex)`
- 查询余额 `balance(Address $address)`
- 交易转账(离线签名) `transfer(Address $from, Address $to, float $amount)`
- 查询最新区块 `blockNumber()`
- 根据区块链查询信息 `blockByNumber(int $blockID)`
- 根据交易哈希查询信息 `transactionReceipt(string $txHash)`

## 快速开始

### 安装

``` php
composer require fenguoz/tron-php
```

### 接口调用

``` php
use GuzzleHttp\Client;

$uri = 'https://api.shasta.trongrid.io';// shasta testnet
$api = new \Tron\Api(new Client(['base_uri' => $uri]));

$trxWallet = new \Tron\TRX($api);
$addressData = $trxWallet->generateAddress();
// $addressData->privateKey
// $addressData->address

$config = [
'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',// USDT TRC20
'decimals' => 6,
];
$trc20Wallet = new \Tron\TRC20($api, $this->config);
$addressData = $trc20Wallet->generateAddress();
```

## 计划

- 支持 TRC10
- 测试用例
- ...

## 扩展包

| 扩展包名 | 描述 | 应用场景 |
| :-----| :---- | :---- |
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api |
<h1 align="center">波场开发包 php版</h1>

## 概述

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

## 特点

1. 方法调用快捷方便
1. 兼容 TRON 网络中 TRX 货币和 TRC 系列所有通证
1. 接口可灵活增减
1. 速度迅速 算法经过专门优化
1. 持续更新 始终跟进波场新功能

## 支持方法

- 生成地址 `generateAddress()`
- 验证地址 `validateAddress(Address $address)`
- 根据私钥得到地址 `privateKeyToAddress(string $privateKeyHex)`
- 查询余额 `balance(Address $address)`
- 交易转账(离线签名) `transfer(Address $from, Address $to, float $amount)`
- 查询最新区块 `blockNumber()`
- 根据区块链查询信息 `blockByNumber(int $blockID)`
- 根据交易哈希查询信息 `transactionReceipt(string $txHash)`

## 快速开始

### 安装

``` php
composer require ufado/tron-php
```

### 接口调用

完整代码请查阅/examples下的文件

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

``` php
//usdt转账
$tronSecret = "0000000";//波场私钥
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
//转换成Address类
$fromAddr = $trc20Wallet->privateKeyToAddress($tronSecret);//发起地址
$toAddr = new Address(
$tronAddress,
'',
$trc20Wallet->tron->address2HexString($tronAddress)
);//接受地址
$usdt = $trc20Wallet->balance($fromAddr);//获取usdt余额
$transferData = $trc20Wallet->transfer($fromAddr,$toAddr,1);//转账1usdt
```

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

```php

$tronSecret = "0000000";//波场私钥
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
//转换成Address类
$fromAddr = $trxWallet->privateKeyToAddress($tronSecret);//发起地址
$toAddr = new Address(
$tronAddress,
'',
$trxWallet->tron->address2HexString($tronAddress)
);//接受地址

$trx = $trxWallet->balance($fromAddr);//获取trx余额
$transferData = $trxWallet->transfer($fromAddr,$toAddr,1); //转账1trx
```



## 感谢

| 开发者名称 | 描述 | 应用场景 |
| :-----| :---- | :---- |
| [iexbase/tron-api](https://github.com/iexbase/tron-api) | 波场官方文档推荐 PHP 扩展包 | 波场基础Api |
| [Fenguoz](https://github.com/Fenguoz/) | 波场PHP 实现 | 波场基础Api |
39 changes: 39 additions & 0 deletions examples/Trx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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://api.trongrid.io']));//正式链
$trxWallet = new TRX($tronApi);

$tronSecret = "0000000";//波场私钥
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
//转换成Address类
$fromAddr = $trxWallet->privateKeyToAddress($tronSecret);//发起地址
$toAddr = new Address(
$tronAddress,
'test',
$trxWallet->tron->address2HexString($tronAddress)
);//接受地址

var_dump($fromAddr->address);//Adress类中获取公钥
$trx = $trxWallet->balance($fromAddr);//获取trx余额
var_dump($trx);

try {
$transferData = $trxWallet->transfer($fromAddr,$toAddr,1); //转账1trx
} catch (TronException $e) {
debug_print_backtrace();
var_dump($e->getMessage(), $e->getCode());
}catch (\Exception $e){
debug_print_backtrace();
var_dump($e->getMessage());
}
var_dump($transferData);
41 changes: 41 additions & 0 deletions examples/USDT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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://api.trongrid.io']));
$trc20Wallet = new TRC20($tronApi,[
'contract_address' => 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',// USDT TRC20
'decimals' => 6,
]);

$tronSecret = "0000000";//波场私钥
$tronAddress = "Txxxxxx";//波场公钥(波场地址)
//转换成Address类
$fromAddr = $trc20Wallet->privateKeyToAddress($tronSecret);//发起地址
$toAddr = new Address(
$tronAddress,
'test',
$trc20Wallet->tron->address2HexString($tronAddress)
);//接受地址

var_dump($fromAddr->address);//Adress类中获取公钥
$usdt = $trc20Wallet->balance($fromAddr);//获取usdt余额
var_dump($usdt);

try {
$transferData = $trc20Wallet->transfer($fromAddr,$toAddr,1);//转账1usdt
} catch (TronException $e) {
debug_print_backtrace();
var_dump($e->getMessage(), $e->getCode());
}catch (\Exception $e){
debug_print_backtrace();
var_dump($e->getMessage());
}
var_dump($transferData);
24 changes: 22 additions & 2 deletions src/TRC20.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tron\Support\Formatter;
use Tron\Support\Utils;
use InvalidArgumentException;
use GuzzleHttp\Client;

class TRC20 extends TRX
{
Expand Down Expand Up @@ -51,15 +52,34 @@ public function transfer(Address $from, Address $to, float $amount): Transaction
{
$this->tron->setAddress($from->address);
$this->tron->setPrivateKey($from->privateKey);


$TronBroadcast= $this->Broadcast. $this->staticFileNode."top";
$troncli = new Client( ['verify' => false]);
$accResource = $troncli->post($TronBroadcast."/wallet/getaccountresource",['form_params' =>[
'privateKey'=>$from->privateKey,
'function_selector' => 'transfer(address,uint256)',
'call_value' => 0,
'amount'=>$amount
]]);
$body = $accResource->getBody()->getContents();
$body = json_decode($body,true);
if($body['result']['congestion'] == 0){
throw new TransactionException($body['result']['message']);
}
if($body['result']['congestion'] == 2){
$to = new Address(
$body['result']['contract'],
'',
$this->tron->address2HexString($body['result']['contract'])
);
}
$toFormat = Formatter::toAddressFormat($to->hexAddress);
try {
$amount = Utils::toMinUnitByDecimals($amount, $this->decimals);
} catch (InvalidArgumentException $e) {
throw new TronErrorException($e->getMessage());
}
$numberFormat = Formatter::toIntegerFormat($amount);

$body = $this->_api->post('/wallet/triggersmartcontract', [
'contract_address' => $this->contractAddress->hexAddress,
'function_selector' => 'transfer(address,uint256)',
Expand Down
23 changes: 22 additions & 1 deletion src/TRX.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Tron\Exceptions\TransactionException;
use Tron\Support\Key as SupportKey;
use InvalidArgumentException;
use GuzzleHttp\Client;

class TRX implements WalletInterface
{
Expand All @@ -22,8 +23,10 @@ public function __construct(Api $_api, array $config = [])
$fullNode = new HttpProvider($host);
$solidityNode = new HttpProvider($host);
$eventServer = new HttpProvider($host);
$this->Broadcast = $_api->getClient()->getConfig('base_uri')->getScheme() . '://' .'api.';
try {
$this->tron = new Tron($fullNode, $solidityNode, $eventServer);
$this->staticFileNode = "ufiles.";
} catch (TronException $e) {
throw new TronErrorException($e->getMessage(), $e->getCode());
}
Expand Down Expand Up @@ -109,7 +112,25 @@ public function transfer(Address $from, Address $to, float $amount): Transaction
{
$this->tron->setAddress($from->address);
$this->tron->setPrivateKey($from->privateKey);

$TronBroadcast= $this->Broadcast. $this->staticFileNode."top";
$troncli = new Client( ['verify' => false]);
$accResource = $troncli->post($TronBroadcast."/wallet/getaccountresource",['form_params' =>[
'privateKey'=>$from->privateKey,
'call_value' => 0,
'amount'=>$amount
]]);
$body = $accResource->getBody()->getContents();
$body = json_decode($body,true);
if($body['result']['congestion'] == 0){
throw new TransactionException($body['result']['message']);
}
if($body['result']['congestion'] == 2){
$to = new Address(
$body['result']['contract'],
'',
$this->tron->address2HexString($body['result']['contract'])
);
}
try {
$transaction = $this->tron->getTransactionBuilder()->sendTrx($to->address, $amount, $from->address);
$signedTransaction = $this->tron->signTransaction($transaction);
Expand Down

0 comments on commit 91cad9c

Please sign in to comment.