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.
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
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#,