DEV Community

jared1106
jared1106

Posted on • Updated on

Tron blockchain Signature, Broadcast Transaction

NuGet Import dependency libraries

PM> Install-Package Tron.Wallet
Enter fullscreen mode Exit fullscreen mode

Config Tron RPC network

public record TronRecord(IServiceProvider ServiceProvider, ITronClient? TronClient, IOptions<TronNetOptions>? Options);

public static class TronServiceExtension {
    private static IServiceProvider AddTronNet() {
        IServiceCollection services = new ServiceCollection();
        services.AddTronNet(x =>
        {
            x.Network = TronNetwork.MainNet;
            x.Channel = new GrpcChannelOption { Host = "grpc.trongrid.io", Port = 50051 };
            x.SolidityChannel = new GrpcChannelOption { Host = "grpc.trongrid.io", Port = 50052 };
            x.ApiKey = "80a8b20f-a917-43a9-a2f1-809fe6eec0d6";
        });
        services.AddLogging();
        return services.BuildServiceProvider();
    }

    public static TronRecord GetRecord() {
        var provider = AddTronNet();
        var client = provider.GetService<ITronClient>();
        var options = provider.GetService<IOptions<TronNetOptions>>();

        return new TronRecord(provider, client, options);
    }
}
Enter fullscreen mode Exit fullscreen mode

This class library is very simple, almost no need to care about the TRON chain, just need to generate the private key and address, and then you can easily manage assets such as trx and usdt in the address.

Signature and broadcast the transaction, sending TRX as an example

private static async Task<dynamic> TrxTransferAsync(string privateKey, string to, long amount) {
    var record = TronServiceExtension.GetRecord();
    var transactionClient = record.TronClient?.GetTransaction();

    var account = new TronAccount(privateKey, TronNetwork.MainNet);

    var transactionExtension = await transactionClient?.CreateTransactionAsync(account.Address, to, amount)!;

    var transactionId = transactionExtension.Txid.ToStringUtf8();

    // Signature 
    var transactionSigned = transactionClient.GetTransactionSign(transactionExtension.Transaction, privateKey);
    // Broadcast
    var returnObj = await transactionClient.BroadcastTransactionAsync(transactionSigned);

    return new { Result = returnObj.Result, Message = returnObj.Message, TransactionId = transactionId };
}
Enter fullscreen mode Exit fullscreen mode

Send out USDT

private static async Task<string> EtherTransferAsync(string privateKey, string toAddress, decimal amount, string? memo) {
    const string contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";

    var record = TronServiceExtension.GetRecord();
    var contractClientFactory = record.ServiceProvider.GetService<IContractClientFactory>();
    var contractClient = contractClientFactory?.CreateClient(ContractProtocol.TRC20);

    var account = new TronAccount(privateKey, TronNetwork.MainNet);

    const long feeAmount = 30 * 1000000L;

    return await contractClient.TransferAsync(contractAddress, account, toAddress, amount, memo, feeAmount);
}
Enter fullscreen mode Exit fullscreen mode

Here we need to set the upper limit of TRON’s handling fee, and now we must set 30TRX, otherwise the transaction may fail, which is too expensive. Other trc20 contracts also operate in the same way, just replace the contract address.
Note that the amount of the trc20 contract transaction is a decimal type and does not need to be converted to a long type

Testing

static async Task Main(string[] args) {
    Console.WriteLine("Program begin..");

    var privateKey = "D95611A9AF2A2A45359106222ED1AFED48853D9A44DEFF8DC7913F5CBA727366";

    //Send out TRX
    var result = await TrxTransferAsync(privateKey, "TGehVcNhud84JDCGrNHKVz9jEAVKUpbuiv", 10000000L);
    Console.WriteLine(JsonConvert.SerializeObject(result));

    //Send trc20 token usdt
    var transactionId = await EtherTransferAsync(privateKey, "TGehVcNhud84JDCGrNHKVz9jEAVKUpbuiv", 10, string.Empty);
    Console.WriteLine(transactionId);

    Console.WriteLine("Program end..\r\nPress any key to exit.");
    Console.ReadKey();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)