Skip to content

Commit

Permalink
Add basic tool classes, add trx and trc20 to obtain block transaction…
Browse files Browse the repository at this point in the history
… records, and optimize some codes
  • Loading branch information
Fenguoz committed Aug 17, 2024
1 parent fff2b43 commit 83bee08
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": ">=8.0",
"fenguoz/tron-api": "~1.1",
"fenguoz/tron-api": "^1.2",
"ionux/phactor": "1.0.8",
"kornrunner/keccak": "~1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(string $address = '', string $privateKey = '', strin

$this->privateKey = $privateKey;
$this->address = $address;
$this->hexAddress = $hexAddress;
$this->hexAddress = $hexAddress ?: Utils::address2HexString($address);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ public function post(string $endpoint, array $data = [], bool $returnAssoc = fal
return $body;
}

/**
* Abstracts some common functionality just to preserve usage like post
*
* @throws TronErrorException
*/
public function get(string $endpoint, array $data = [], bool $returnAssoc = false)
{
if (sizeof($data)) {
$data = ['query' => $data];
}

$stream = (string)$this->getClient()->get($endpoint, $data)->getBody();
$body = json_decode($stream, $returnAssoc);

$this->checkForErrorResponse($returnAssoc, $body);

return $body;
}

/**
* Check if the response has an error and throw it.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/WalletInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public function blockNumber(): Block;
public function blockByNumber(int $blockID): Block;

public function transactionReceipt(string $txHash);

public function walletTransactions(Address $address, int $limit = null): ?array;
}
29 changes: 24 additions & 5 deletions src/TRC20.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@
class TRC20 extends TRX
{
protected $contractAddress;

protected $decimals;
protected $trc20Contract;

public function __construct(Api $_api, array $config)
{
parent::__construct($_api, $config);

$this->contractAddress = new Address(
if (!isset($config['contract_address']) || empty($config['contract_address'])) {
throw new TronErrorException('Missing contract address');
}
if (!isset($config['decimals']) || empty($config['decimals'])) {
throw new TronErrorException('Missing decimals');
}

$this->contractAddress = new Address($config['contract_address']);
$this->decimals = $config['decimals'];
$this->trc20Contract = new \IEXBase\TronAPI\TRC20Contract(
$this->tron,
$config['contract_address'],
'',
$this->tron->address2HexString($config['contract_address'])
(isset($config['abi']) ? $config['abi'] : null)
);
$this->decimals = $config['decimals'];
}

public function balance(Address $address)
Expand Down Expand Up @@ -92,4 +100,15 @@ public function transfer(Address $from, Address $to, float $amount): Transaction
throw new TransactionException(hex2bin($response['result']['message']));
}
}

public function walletTransactions(Address $address, int $limit = null): ?array
{
try {
$ret = $this->trc20Contract->getTransactions($address->address, $limit);
} catch (TronException $e) {
throw new TronErrorException($e->getMessage(), $e->getCode());
}

return $ret['data'];
}
}
17 changes: 14 additions & 3 deletions src/TRX.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TRX implements WalletInterface
protected $_api;

protected $tron;

public function __construct(Api $_api, array $config = [])
{
$this->_api = $_api;
Expand Down Expand Up @@ -138,7 +138,7 @@ public function blockNumber(): Block
try {
$block = $this->tron->getCurrentBlock();
} catch (TronException $e) {
throw new TransactionException($e->getMessage(), $e->getCode());
throw new TronErrorException($e->getMessage(), $e->getCode());
}
$transactions = isset($block['transactions']) ? $block['transactions'] : [];
return new Block($block['blockID'], $block['block_header'], $transactions);
Expand All @@ -149,7 +149,7 @@ public function blockByNumber(int $blockID): Block
try {
$block = $this->tron->getBlockByNumber($blockID);
} catch (TronException $e) {
throw new TransactionException($e->getMessage(), $e->getCode());
throw new TronErrorException($e->getMessage(), $e->getCode());
}

$transactions = isset($block['transactions']) ? $block['transactions'] : [];
Expand All @@ -169,4 +169,15 @@ public function transactionReceipt(string $txHash): Transaction
$detail['ret'][0]['contractRet'] ?? ''
);
}

public function walletTransactions(Address $address, int $limit = null): ?array
{
try {
$ret = $this->tron->getTransactions($address->address, $limit);
} catch (TronException $e) {
throw new TronErrorException($e->getMessage(), $e->getCode());
}

return $ret['data'];
}
}
141 changes: 141 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Tron;

use IEXBase\TronAPI\Support\Base58Check;
use kornrunner\Keccak;
use phpseclib\Math\BigInteger;

class Utils
{
/**
* Convert from Hex
*
* @param $string
* @return string
*/
public static function fromHex($string)
{
if (strlen($string) == 42 && mb_substr($string, 0, 2) === '41') {
return self::hexString2Address($string);
}

return self::hexString2Utf8($string);
}

/**
* Convert to Hex
*
* @param $str
* @return string
*/
public static function toHex($str)
{
if (mb_strlen($str) == 34 && mb_substr($str, 0, 1) === 'T') {
return self::address2HexString($str);
};

return self::stringUtf8toHex($str);
}

/**
* Check the address before converting to Hex
*
* @param $sHexAddress
* @return string
*/
public static function address2HexString($sHexAddress)
{
if (strlen($sHexAddress) == 42 && mb_strpos($sHexAddress, '41') == 0) {
return $sHexAddress;
}
return Base58Check::decode($sHexAddress, 0, 3);
}

/**
* Check Hex address before converting to Base58
*
* @param $sHexString
* @return string
*/
public static function hexString2Address($sHexString)
{
if (!ctype_xdigit($sHexString)) {
return $sHexString;
}

if (strlen($sHexString) < 2 || (strlen($sHexString) & 1) != 0) {
return '';
}

return Base58Check::encode($sHexString, 0, false);
}

/**
* Convert string to hex
*
* @param $sUtf8
* @return string
*/
public static function stringUtf8toHex($sUtf8)
{
return bin2hex($sUtf8);
}

/**
* Convert hex to string
*
* @param $sHexString
* @return string
*/
public static function hexString2Utf8($sHexString)
{
return hex2bin($sHexString);
}

/**
* Convert to great value
*
* @param $str
* @return BigInteger
*/
public static function toBigNumber($str)
{
return new BigInteger($str);
}

/**
* Convert trx to float
*
* @param $amount
* @return float
*/
public static function fromTron($amount): float
{
return (float) bcdiv((string)$amount, (string)1e6, 8);
}

/**
* Convert float to trx format
*
* @param $double
* @return int
*/
public static function toTron($double): int
{
return (int) bcmul((string)$double, (string)1e6, 0);
}

/**
* Convert to SHA3
*
* @param $string
* @param bool $prefix
* @return string
* @throws \Exception
*/
public static function sha3($string, $prefix = true)
{
return ($prefix ? '0x' : '') . Keccak::hash($string, 256);
}
}

0 comments on commit 83bee08

Please sign in to comment.