Closed saqibsoftwares closed 3 years ago
TRX balance sample: #11
@saqibsoftwares Hi. I want to get the balance of trc20 token of an trx wallet. please help me why this exception occured as attached
@sinaptech This is how I'm getting TRX balance from wallet.
var protocol = _wallet.GetProtocol();
var addr = _wallet.ParseAddress(accountAddress);
var account = await protocol.GetAccountAsync(new Account
{
Address = addr
});
return Convert.ToDecimal(account.Balance) / 1_000_000L;
"accountAddress" is your account address of which you want to get balance.
"_wallet" is global private variable holding wallet object instance.
private readonly IWalletClient _wallet;
You need to initialize _wallet like this in the constructor.
_record = TronTestServiceExtension.GetTestRecord();
_tronClient = _record.TronClient;
_options = _record.Options;
_contractClientFactory = _record.ContractClientFactory;
_wallet = _record.TronClient.GetWallet();
_transactionClient = _tronClient.GetTransaction();
Let me know if you need further assistance.
@saqibsoftwares I have no problem to get trx wallet balance. I want to get the trc20 token balance in USDT
@sinaptech
public Task<decimal> GetBalanceUSDT(string fromKey)
{
var account = _wallet.GetAccount(fromKey);
var contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
var contractClient = _contractClientFactory.CreateClient(ContractProtocol.TRC20);
return contractClient.BalanceOfAsync(contractAddress, account);
}
contractAddress will be constant as is and fromKey is the key of wallet.
@saqibsoftwares Thanks alot
Hi I'm new to TRON wallet development. I can successfully execute both methods. However, i'm wondering why we need the private key to get the USDT amount of the account ? As in Tron scan, with the PublicAddress we already able to view that information.
Hi, I have tested this library on mainnet as well as testnet and it's working perfectly on both. I'm able to transfer TRX and USDT from one account to another, I have one question though, how can I know the balance of TRX and USDT? couldn't find any example.
Thanks.
UPDATE: I'm able to get USDT account balance using this code
var account = _wallet.GetAccount(fromKey); var contractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; var contractClient = _contractClientFactory.CreateClient(ContractProtocol.TRC20); return contractClient.BalanceOfAsync(contractAddress, account);
Still not able to get TRX balance.