波场(Tron)离线签名、广播交易、设置多签笔记
NuGet 引入依赖库
PM> Install-Package TronNet.Wallet -Version 1.0.1
配置波场RPC网络
using Microsoft.Extensions.DependencyInjection;
using Tron;
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);
}
}
这个类库非常的简单,几乎不用去关心波场链的东西,只需要生成私钥和地址,就可以轻松完成对地址里面的 trx 和 usdt 等资产进行管理操作。
签名并广播交易,以发送 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 = 60* 1000000L;
return await contractClient.TransferAsync(contractAddress, account, toAddress, amount, memo, feeAmount);
}
这里要设置波场手续费上限,现在最少设置30TRX,否则有可能交易失败,太贵了。其它 trc20 合约也是一样的操作,替换掉合约地址即可。
注意trc20合约交易的 amount 是 decimal 类型,不需要转换成 long 型
设置多签
private static async Task<dynamic> AccountPermissionUpdateAsync(string privateKey, string ownerAddress) {
var record = TronServiceExtension.GetRecord();
var transactionClient = record.TronClient?.GetTransaction();
var account = new TronAccount(privateKey, TronNetwork.MainNet);
var transactionExtention = await transactionClient?.CreateAccountPermissionUpdateTransactionAsync(account.Address, ownerAddress)!;
var transactionSigned = transactionClient.GetTransactionSign(transactionExtention.Transaction, privateKey);
var returnObj = await transactionClient.BroadcastTransactionAsync(transactionSigned);
return new { Result = returnObj.Result, Message = returnObj.Message, TransactionId = transactionExtention.Transaction.GetTxid() };
}
设置多签地址最少需要 100 TRX 多签费和 0.1 网络交易费,否则会交易失败
static async Task Main(string[] args) {
Console.WriteLine("Program begin..");
var privateKey = "";
var toAddress = "";
//发送 trx
var result = await TrxTransferAsync(privateKey, toAddress, 10000000L);
Console.WriteLine(JsonConvert.SerializeObject(result));
//发送 trc20 token usdt
var transactionId = await EtherTransferAsync(privateKey, toAddress, 10, string.Empty);
Console.WriteLine(transactionId);
//多签
var ownerAddress = "";
var result = await AccountPermissionUpdateAsync(privateKey, ownerAddress);
Console.WriteLine(JsonConvert.SerializeObject(result));
Console.WriteLine("Program end..\r\nPress any key to exit.");
Console.ReadKey();
}
其他
波场(Tron)钱包设置多签
波场(Tron)网页版(本地)钱包开源
波场(Tron)项目常用工具分享
波场(Tron)离线签名、广播交易笔记
波场(Tron)离线生成私钥和地址笔记