I have used TronNet nudget packages version 0.2.0
but there is always some bug. So I collected the method based on documentation and some other examples.
我使用过TronNet nudget包0.2.0版,但总是有一些错误。因此,我收集了基于文档和其他一些例子的方法。
I have been trying to get simply transfer usdt assets using tron network, it seems packages not working well.Here one of the many transfer trial
我一直试图通过TRON网络简单地转移USDT资产,似乎一揽子计划不起作用。以下是众多转移试验之一
public static async Task TransferTrc20Token(string tokenContractAddress, string walletAddress,
string privateKey, string toAddress, decimal amount)
{
// Create the transfer contract
var transferContract = new TransferContract
{
OwnerAddress = ByteString.CopyFrom(Base58Encoder.DecodeFromBase58Check(walletAddress)),
ToAddress = ByteString.CopyFrom(Base58Encoder.DecodeFromBase58Check(toAddress)),
Amount = (long)(amount * 1000000m)
};
// Create the transaction
var transaction = new Transaction
{
RawData = new Transaction.Types.raw
{
Contract = { new Transaction.Types.Contract
{
Type = Transaction.Types.Contract.Types.ContractType.TransferContract,
Parameter = Google.Protobuf.WellKnownTypes.Any.Pack(transferContract)
}},
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Expiration = DateTimeOffset.Now.AddHours(1).ToUnixTimeMilliseconds()
}
};
// Sign the transaction
transaction = SignTransaction(transaction, privateKey);
// Serialize the transaction
var serializedTransaction = transaction.ToByteArray();
// Convert the serialized transaction to hexadecimal string
var transactionHex = BitConverter.ToString(serializedTransaction).Replace("-", "");
// Create the REST client
var client = new RestClient("https://api.trongrid.io/wallet/easytransfer");
var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\"transaction\":\"" + transactionHex + "\"}", ParameterType.RequestBody);
// Send the request
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful)
{
// Handle successful response
Console.WriteLine("Transfer successful");
}
else
{
// Handle error response
Console.WriteLine("Transfer failed: " + response.ErrorMessage);
}
}
private static Transaction SignTransaction(Transaction transaction, string privateKey)
{
byte[] privateKeyBytes;
if (privateKey.Length == 64)
{
privateKeyBytes = StringToByteArray(privateKey);
}
else
{
// Decode private key from Base58Check format
privateKeyBytes = TronNet.Crypto.Base58Encoder.DecodeFromBase58Check(privateKey);
}
var ecKey = new ECKey(privateKeyBytes, true);
var rawdata = transaction.RawData.ToByteArray();
var hash = rawdata.ToSHA256Hash();
var signature = ecKey.Sign(hash).ToByteArray();
transaction.Signature.Add(ByteString.CopyFrom(signature));
return transaction;
}
private static byte[] StringToByteArray(string hex)
{
int length = hex.Length;
byte[] bytes = new byte[length / 2];
for (int i = 0; i < length; i += 2)
{
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}
To find what is problem? and process transaction in tron network usdt c#,
找出问题出在哪里?并在TRON网络USDT C#中处理交易,
更多回答
优秀答案推荐
It seems you have a couple of problems actually;
看起来你实际上有几个问题;
The code is missing two required attributes in the RawData section, which you can get by fetching the latest block, reference @ v4.7.2
代码在RawData部分中缺少两个必需的属性,您可以通过获取最新的块Reference@v4.7.2来获得这些属性
ref_block_bytes
using the height of the block as BigEndianlong bytes, the 6th to 8th (exclusive).
ref_block_hash
using the 8th to 16th (exclusive) bytes of the reference block hash.
Also I'm not sure which version of Full Node HTTP API of Tron you are using but /wallet/easytransfer
endpoint was deprecated with v4.7.1, and I think you are trying to send a signed transaction hex which would use the endpoint wallet/broadcasthex
. reference @ v4.7.2
此外,我不确定您正在使用Tron的Full Node HTTP API的哪个版本,但是/wallet/easyTransfer端点在v4.7.1中已被弃用,我认为您正在尝试发送一个使用端点wallet/BroadCasthex的签名事务十六进制。Reference@v4.7.2
更多回答
我是一名优秀的程序员,十分优秀!