Permalink
Browse files

Trigger contract functionality

  • Loading branch information...
1099511627776 committed Jan 3, 2019
1 parent 7f858e4 commit b2d0ccf561cf318dca20e13e4223f8a66c6cde07
Showing with 93 additions and 0 deletions.
  1. +1 −0 composer.json
  2. +92 −0 src/Tron.php
@@ -21,6 +21,7 @@
"require": {
"php": "^7.1",
"guzzlehttp/guzzle": "^6.3",
"sc0vu/web3.php": "^0.1.4",
"simplito/elliptic-php": "^1.0"
},

@@ -20,6 +20,15 @@
use IEXBase\TronAPI\Provider\HttpProviderInterface;
use IEXBase\TronAPI\Exception\TronException;
use Web3\Contracts\Ethabi;
use Web3\Contracts\Types\Address;
use Web3\Contracts\Types\Boolean;
use Web3\Contracts\Types\Bytes;
use Web3\Contracts\Types\DynamicBytes;
use Web3\Contracts\Types\Integer;
use Web3\Contracts\Types\Str;
use Web3\Contracts\Types\Uinteger;
/**
* A PHP API for interacting with the Tron (TRX)
*
@@ -1093,6 +1102,89 @@ public function deployContract($abi, $bytecode, $feeLimit, $address, $callValue
]);
}
/**
* Triggers a contract
*
* @param Mixed $abi
* @param String $contract
* @param String $function
* @param Array $params
* @param integer $feeLimit
* @param String $address
* @param int $callValue
* @param int $bandwidthLimit
*
* @return mixed
* @throws TronException
*/
public function triggerContract($abi, $contract, $function, $params, $feeLimit, $address, $callValue = 0, $bandwidthLimit = 0)
{
$func_abi = [];
foreach($abi as $key =>$item){
if($item['name'] === $function){
$func_abi = $item;
break;
}
}
if(count($func_abi) === 0){
throw new TronException("Function $function not defined in ABI");
}
if(!is_array($params)){
throw new TronException("Function params must be an array");
}
if(count($func_abi['inputs']) !== count($params)){
throw new TronException("Count of params and abi inputs must be identical");
}
if($feeLimit > 1000000000) {
throw new TronException('fee_limit must not be greater than 1000000000');
}
$inputs = array_map(function($item){ return $item['type']; },$func_abi['inputs']);
$signature = $func_abi['name']."(";
if(count($inputs) > 0){
$signature .= implode(',',$inputs);
}
$signature .= ')';
$ethabi = new Ethabi([
'address' => new Address,
'bool' => new Boolean,
'bytes' => new Bytes,
'dynamicBytes' => new DynamicBytes,
'int' => new Integer,
'string' => new Str,
'uint' => new Uinteger,
]);
$parameters = substr($ethabi->encodeParameters($func_abi,$params),2);
$p = [
'contract_address' => $contract,
'function_selector' => $signature,
'parameter' => $parameters,
'owner_address' => $address,
'fee_limit' => $feeLimit,
'call_value' => $callValue,
'consume_user_resource_percent' => $bandwidthLimit,
];
$result = $this->manager->request('wallet/triggersmartcontract', $p);
if(count($func_abi['outputs']) === 0){
if($result['result']['result']){
return $result['transaction'];
}
}
if(!isset($result['constant_result'])){
$message = isset($result['result']['message']) ? $this->hexString2Utf8($result['result']['message']) : '';
throw new TronException('Failed to execute. Error:'.$message);
}
return $ethabi->decodeParameters($func_abi, $result['constant_result'][0]);
}
/**
* Get a list of exchanges
*

0 comments on commit b2d0ccf

Please sign in to comment.