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

How to make transfer for USDT TRC20 20 full code

Open MAHMOUDJOR opened this issue 2 years ago • 5 comments

MAHMOUDJOR avatar Mar 25 '23 11:03 MAHMOUDJOR

    $this->app->singleton(Tron::class, function () {
        $headers = [
            'TRON-PRO-API-KEY' => config('tron.grid.api_key'),
            'Content-Type' => 'application/json'
        ];

        $fullNode = new HttpProvider('https://api.trongrid.io', 3000, false, false, $headers);
        $solidityNode = new HttpProvider('https://api.trongrid.io', 3000, false, false, $headers);
        $eventServer = new HttpProvider('https://api.trongrid.io', 3000, false, false, $headers);
        return new Tron($fullNode, $solidityNode, $eventServer);
    });


    $this->app->singleton(WalletData::class, function () {
        return new WalletData(config('tron.wallet'));
    });


    $this->app->singleton(WalletClient::class, function () {
        /** @var Tron $tron */
        $tron = app(Tron::class);
        $wallet = app(WalletData::class);
        $tron->setAddress($wallet->getAddress());
        $tron->setPrivateKey($wallet->getPrivateKey());
        $trc = $tron->contract(Contract::ADDRESS)->setFeeLimit(50); // ADDRESSONLY TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
        return new WalletClient($trc);
    });

JamalAbsalimov avatar Mar 30 '23 23:03 JamalAbsalimov

class WalletClient { public function __construct( protected TRC20Contract $contract ) { }

/**
 * @param  string  $toAddress
 * @param  string  $amount
 * @return void
 * @throws TRC20Exception
 * @throws TronException
 */
public function sendMoney(string $toAddress, string $amount): array
{
    return $this->contract->transfer($toAddress, $amount);
}

}

JamalAbsalimov avatar Mar 30 '23 23:03 JamalAbsalimov

are u able to share ur full class?

shaksi avatar Aug 26 '23 07:08 shaksi

are u able to share ur full class?

`<?php

namespace App\Tron\Wallet;

use App\Tron\Wallet\Response\TransferResponse; use IEXBase\TronAPI\Exception\TRC20Exception; use IEXBase\TronAPI\Exception\TronException; use IEXBase\TronAPI\TRC20Contract; use IEXBase\TronAPI\Tron;

/**

  • Класс который работает с кошельком BtCourse */ class WalletClient { public function __construct( public TRC20Contract $contract, public Tron $tron ) { }

    /**

    • @param string $toAddress

    • @param string $amount

    • @return array

    • @throws TRC20Exception

    • @throws TronException */ public function sendUSDT(string $toAddress, string $amount): array { $response = $this->contract->transfer($toAddress, $amount);

      TransferResponse::check($response);

      return $response; }

    /**

    • @throws TronException */ public function sendTrx(string $toAddress, string $amount): array { return $this->tron->sendTransaction($toAddress, $amount); }

    /**

    • @throws TronException
    • @throws TRC20Exception */ public function balanceUsdt(): string { return $this->contract->balanceOf(); }

    /**

    • @return float
    • @throws TronException */ public function balanceTrx(): float { return $this->tron->getBalance(); } }`

JamalAbsalimov avatar Aug 26 '23 09:08 JamalAbsalimov

`<?php

namespace App\Tron\Wallet\Response;

use Arr; use IEXBase\TronAPI\Exception\TronException;

class TransferResponse {

public const SIGERROR = 'SIGERROR';
public const BANDWITH_ERROR = 'BANDWITH_ERROR';

public const DUP_TRANSACTION_ERROR = 'DUP_TRANSACTION_ERROR';

public const TAPOS_ERROR = 'TAPOS_ERROR';

public const TOO_BIG_TRANSACTION_ERROR = 'TOO_BIG_TRANSACTION_ERROR';

public const TRANSACTION_EXPIRATION_ERROR = 'TRANSACTION_EXPIRATION_ERROR';

public const SERVER_BUSY = 'SERVER_BUSY';
public const NOT_ENOUGH_EFFECTIVE_CONNECTION = 'NOT_ENOUGH_EFFECTIVE_CONNECTION';

public const OTHER_ERROR = 'OTHER_ERROR';

public const CONTRACT_VALIDATE_ERROR = 'CONTRACT_VALIDATE_ERROR';


public const UNKNOWN_ERROR = 'UNKNOWN_ERROR';


private static array $errorCodes = [
    self::CONTRACT_VALIDATE_ERROR,
    self::BANDWITH_ERROR,
    self::OTHER_ERROR,
    self::DUP_TRANSACTION_ERROR,
    self::TOO_BIG_TRANSACTION_ERROR,
    self::NOT_ENOUGH_EFFECTIVE_CONNECTION,
    self::SERVER_BUSY,
    self::SIGERROR,
    self::TRANSACTION_EXPIRATION_ERROR,
    self::TAPOS_ERROR
];


/**
 * @param  array  $response
 * @return void
 * @throws TronException
 */
public static function check(array $response): void
{
    if ((Arr::exists($response, 'code') && !in_array($response['code'], self::$errorCodes, true))) {
        throw new TronException(self::UNKNOWN_ERROR);
    }

    if ((Arr::exists($response, 'code') && in_array($response['code'], self::$errorCodes, true))) {
        throw new TronException($response['code']);
    }
}

}`

JamalAbsalimov avatar Aug 26 '23 09:08 JamalAbsalimov