Tron System Contract . Transfer Contract


The typical way to transfer TRX from owner address to recipient address.


Generate Contract Serialized Hex:

<?php 

define("TRX_TO_SUN",'1000000');
define("SUN_TO_TRX", '0.000001');

include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");
include_once("tron_utils.php");

//include all php files that generated by protoc
$dir   = new RecursiveDirectoryIterator('protobuf/core/');
$iter  = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($iter, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); // an Iterator, not an array

foreach ( $files as $file ) {
	
	if (is_array($file)) {
		foreach($file as $filename) {
			include $filename;
		}
	} else {
		include $file;
	}
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
		$ownerAddressHex = base58check2HexString($_POST['from']);
		$toAddressHex = base58check2HexString($_POST['to']);
		
		$ownerAddressBin = hex2str($ownerAddressHex);
		$toAddressBin = hex2str($toAddressHex);
		
		$amountInSun = bcmul($_POST['amount'], TRX_TO_SUN);

		$contract = new \Protocol\Transaction\Contract();
		$transferContract = new \Protocol\TransferContract();
		$transferContract->setAmount($amountInSun);
		$transferContract->setOwnerAddress($ownerAddressBin);
		$transferContract->setToAddress($toAddressBin);
		
		$any = new \Google\Protobuf\Any();
		$any->pack($transferContract);
		
		$contract->setParameter( $any );
		$contract->setType( \Protocol\Transaction\Contract\ContractType::TransferContract );
		
    ?>
	<div class="alert alert-success">
		<h6 class="mt-3">Contract Serialized Hex</h6>
		<textarea class="form-control" rows="5" id="comment" readonly><?php echo str2hex($contract->serializeToString());?></textarea>
		
		<h6 class="mt-3">Contract Serialized Json</h6>
		<textarea class="form-control" rows="5" id="comment" readonly><?php echo $contract->serializeToJsonString()?></textarea>
	</div>
<?php 
    } catch (Exception $e) {
        $errmsg .= "Problem found. " . $e->getMessage();

    }
} 

if ($errmsg) {
?>
    <div class="alert alert-danger">
        <strong>Error!</strong> <?php echo $errmsg?>
    </div>
<?php
}
?>
<form action='' method='post'>
    <div class="form-group">
        <label for="from">Owner Address:</label>
        <input class="form-control" type='text' name='from' id='from' value='<?php echo $_POST['from']?>'>
    </div>
	
	<div class="form-group">
        <label for="to">To Address:</label>
        <input class="form-control" type='text' name='to' id='to' value='<?php echo $_POST['to']?>'>
    </div>
	
	<div class="form-group">
		<label for="amount">Amount:</label>
		
		<div class="input-group mb-3">
			<input class="form-control" type='text' name='amount' id='amount' value='<?php echo $_POST['amount']?>'>
			<div class="input-group-append">
			  <span class="input-group-text">TRX</span>
			</div>
		</div>
	</div>
   
    <input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");
message TransferContract {
	
	#The owner of the current account
    bytes owner_address = 1;
	
	#The target address to transfer
    bytes to_address = 2;
	
	#The amount of TRX to transfer
    int64 amount = 3;
}
<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: core/contract/balance_contract.proto

namespace Protocol;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

/**
 * Generated from protobuf message <code>protocol.TransferContract</code>
 */
class TransferContract extends \Google\Protobuf\Internal\Message
{
    /**
     * Generated from protobuf field <code>bytes owner_address = 1;</code>
     */
    protected $owner_address = '';
    /**
     * Generated from protobuf field <code>bytes to_address = 2;</code>
     */
    protected $to_address = '';
    /**
     * Generated from protobuf field <code>int64 amount = 3;</code>
     */
    protected $amount = 0;

    /**
     * Constructor.
     *
     * @param array $data {
     *     Optional. Data for populating the Message object.
     *
     *     @type string $owner_address
     *     @type string $to_address
     *     @type int|string $amount
     * }
     */
    public function __construct($data = NULL) {
        \GPBMetadata\Core\Contract\BalanceContract::initOnce();
        parent::__construct($data);
    }

    /**
     * Generated from protobuf field <code>bytes owner_address = 1;</code>
     * @return string
     */
    public function getOwnerAddress()
    {
        return $this->owner_address;
    }

    /**
     * Generated from protobuf field <code>bytes owner_address = 1;</code>
     * @param string $var
     * @return $this
     */
    public function setOwnerAddress($var)
    {
        GPBUtil::checkString($var, False);
        $this->owner_address = $var;

        return $this;
    }

    /**
     * Generated from protobuf field <code>bytes to_address = 2;</code>
     * @return string
     */
    public function getToAddress()
    {
        return $this->to_address;
    }

    /**
     * Generated from protobuf field <code>bytes to_address = 2;</code>
     * @param string $var
     * @return $this
     */
    public function setToAddress($var)
    {
        GPBUtil::checkString($var, False);
        $this->to_address = $var;

        return $this;
    }

    /**
     * Generated from protobuf field <code>int64 amount = 3;</code>
     * @return int|string
     */
    public function getAmount()
    {
        return $this->amount;
    }

    /**
     * Generated from protobuf field <code>int64 amount = 3;</code>
     * @param int|string $var
     * @return $this
     */
    public function setAmount($var)
    {
        GPBUtil::checkInt64($var);
        $this->amount = $var;

        return $this;
    }

}









Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.