-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters