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..rnPress any key to exit.");
Console.ReadKey();
}
这个类库非常的简单,几乎不用去关心波场链的东西,只需要生成私钥和地址,就可以轻松完成对地址里面的 trx 和 usdt 等资产进行管理操作。
内容来源于网络如有侵权请私信删除
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!