授權(Approve)
private static async Task<string> ApproveAsync(string privateKey, string spenderAddress, decimal amount) {
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.ApproveAsync(contractAddress, account, spenderAddress, amount, feeAmount);//廣播交易
}
轉賬(TransferFrom)
private static async Task<string> TransferFromAsync(string privateKey, string fromAddress, 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.TransferFromAsync(contractAddress, account, fromAddress, toAddress, amount, memo, feeAmount);//廣播交易
}
聰明的人可能會聯想到前段時間很熱的0U轉賬的問題,這個操作其實就是省略掉了 Approve 的過程,直接呼叫 TransferFrom 由於轉賬金額是 0,不會發生實際轉賬操作,但是區塊鏈的交易廣播是成功的,所以導致轉賬記錄被記錄在賬本上,然後小白盲目在交易記錄裡面複製到了陷阱地址,這個要是放在之前都不能講,因為很多人導致這個問題轉錯賬,受到不少損失。
但是現在波場基本上修復了這個問題,雖然沒有從合約根本上解決,但這類的交易記錄預設已經沒有辦法從區塊瀏覽器和錢包裡面複製了,雖然治標不治本。
呼叫
var privateKey1 = "";
var spenderAddress = "";
var result = await ApproveAsync(privateKey1, spenderAddress, 100);
Console.WriteLine(JsonConvert.SerializeObject(result));
var privateKey2 = "";
var fromAddress = "";
var toAddress = "";
//fromAddress 是 privateKey1 對應的地址,不是 spenderAddress
//privateKey2 是 spenderAddress 對應的私鑰
var result = await TransferFromAsync(privateKey2, fromAddress, toAddress,100);
Console.WriteLine(JsonConvert.SerializeObject(result));
引入依賴庫
PM> Install-Package Tron.Wallet
配置主網資訊已經 API Key
using Microsoft.Extensions.Options;
namespace ConsoleApp1;
using Microsoft.Extensions.DependencyInjection;
using Tron.Wallet;
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);
}
}