.NET Core 波場鏈離線簽名、廣播交易(傳送 TRX和USDT)筆記

lijingkun發表於2023-04-10

Get Started

NuGet

You can run the following command to install the Tron.Wallet.Net in your project.

PM> Install-Package Tron.Wallet.Net

配置

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 = "api key"; //https://trongrid.io 郵箱註冊申請,免費50萬次呼叫
        });
        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);
    }
}

轉出/傳送 TRX

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

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

    return new { Result = returnObj.Result, Message = returnObj.Message, TransactionId = transactionId };
}

轉出/傳送 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);
}

這裡要設定波場手續費上限,現在必須設定30TRX,否則有可能交易失敗,太貴了。其它 trc20 合約也是一樣的操作,替換掉合約地址即可。
注意trc20合約交易的 amount 是 decimal 型別,不需要轉換成 long 型

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

    var privateKey = "D95611A9AF2A2A45359106222ED1AFED48853D9A44DEFF8DC7913F5CBA727366";

    //傳送 trx
    var result = await TrxTransferAsync(privateKey, "TGehVcNhud84JDCGrNHKVz9jEAVKUpbuiv", 10000000L);
    Console.WriteLine(JsonConvert.SerializeObject(result));

    //傳送 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();
}

這個類庫非常的簡單,幾乎不用去關心波場鏈的東西,只需要生成私鑰和地址,就可以輕鬆完成對地址裡面的 trx 和 usdt 等資產進行管理操作。

相關文章