iexbase / tron-api

A PHP API for interacting with Tron (TRX)
https://iexbase.github.io/tron-api/
MIT License
405 stars 287 forks source link

How to make transfer for USDT TRC20 20 full code #164

Open MAHMOUDJOR opened 1 year ago

JamalAbsalimov commented 1 year ago
    $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 commented 1 year ago

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);
}

}

shaksi commented 1 year ago

are u able to share ur full class?

JamalAbsalimov commented 1 year ago

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;

/**

JamalAbsalimov commented 1 year ago

`<?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']);
    }
}

}`