tron-api icon indicating copy to clipboard operation
tron-api copied to clipboard

Update Tron.php and TransactoinBuilder.php

Open LuisLACT10 opened this issue 4 years ago • 1 comments

$receiver_address was added to the parameters of freezeBalance and unfreezeBalance. The Tron developers documentation has a section where it explains how to freeze and unfreeze TRX.

The section of FreezeBalance describes that you can freeze an amount of TRX. Will give bandwidth OR Energy and TRON Power (voting rights) to the owner of the frozen tokens. Optionally, can freeze TRX to grant Energy or Bandwidth to other users. Balance amount in the denomination of Sun. The parameter for receiver_address is the address that will receive the resource.

The section of UnfreezeBalance describes TRX that has passed the minimum freeze duration. Unfreezing will remove bandwidth and TRON Power. Returns unfrozen TRX transaction. The parameter for receiver_address is the address that will lose the resource.

Tron.php: /** * Freezes an amount of TRX. * Will give bandwidth OR Energy and TRON Power(voting rights) to the owner of the frozen tokens. * * @param float $amount * @param int $duration * @param string $resource * @param string $owner_address * @param string $receiver_address * @return array * @throws TronException */ public function freezeBalance(float $amount = 0, int $duration = 3, string $resource = 'BANDWIDTH', string $owner_address = null, string $receiver_address = null) { if($owner_address == null) { $owner_address = $this->address['hex']; }

    if($receiver_address == null){
        $receiver_address = "";
    }

    $freeze = $this->transactionBuilder->freezeBalance($amount, $duration, $resource, $owner_address, $receiver_address);
    $signedTransaction = $this->signTransaction($freeze);
    $response = $this->sendRawTransaction($signedTransaction);

    return array_merge($response, $signedTransaction);
}

/**
 * Unfreeze TRX that has passed the minimum freeze duration.
 * Unfreezing will remove bandwidth and TRON Power.
 *
 * @param string $resource
 * @param string $owner_address
 * @param string $receiver_address
 * @return array
 * @throws TronException
 */
public function unfreezeBalance(string $resource = 'BANDWIDTH', string $owner_address = null, string $receiver_address = null)
{
    if($owner_address == null) {
        $owner_address = $this->address['hex'];
    }

    if($receiver_address == null){
        $receiver_address = "";
    }

    $unfreeze = $this->transactionBuilder->unfreezeBalance($resource, $owner_address,  $receiver_address);
    $signedTransaction = $this->signTransaction($unfreeze);
    $response = $this->sendRawTransaction($signedTransaction);

    return array_merge($response, $signedTransaction);
}

TransactionBuilder.php: /** * Freezes an amount of TRX. * Will give bandwidth OR Energy and TRON Power(voting rights) to the owner of the frozen tokens. * * @param float $amount * @param int $duration * @param string $resource * @param string|null $address * @param string|null $receiver_address * @return array * @throws TronException */ public function freezeBalance(float $amount = 0, int $duration = 3, string $resource = 'BANDWIDTH', string $address, string $receiver_address) { if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) { throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"'); }

    if (!is_float($amount)) {
        throw new TronException('Invalid amount provided');
    }

    if(!is_integer($duration) and $duration < 3) {
        throw new TronException('Invalid duration provided, minimum of 3 days');
    }

    

    return $this->tron->getManager()->request('wallet/freezebalance', [
        'owner_address' => $this->tron->address2HexString($address),
        'receiver_address' => ($receiver_address === "")?$receiver_address:$this->tron->address2HexString($receiver_address),
        'frozen_balance' => $this->tron->toTron($amount),
        'frozen_duration' => $duration,
        'resource' => $resource
    ]);
}

/**
 * Unfreeze TRX that has passed the minimum freeze duration.
 * Unfreezing will remove bandwidth and TRON Power.
 *
 * @param string $resource
 * @param string $owner_address
 * @param string $receiver_address
 * @return array
 * @throws TronException
 */
public function unfreezeBalance(string $resource = 'BANDWIDTH', string $owner_address, string $receiver_address)
{
    if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) {
        throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
    }

    return $this->tron->getManager()->request('wallet/unfreezebalance', [
        'owner_address' =>  $this->tron->address2HexString($owner_address),
        'receiver_address' => ($receiver_address === "")?$receiver_address:$this->tron->address2HexString($receiver_address),
        'resource' => $resource
    ]);
}

LuisLACT10 avatar Aug 19 '21 01:08 LuisLACT10

We need this too! Please merge to master

leexin avatar Oct 22 '21 10:10 leexin