To use features of Tron like sending of TRX, TRC10, and TRC20 token, you need to use a call method through the TronService class that inherits the CoinService interface. The TronService class is available to get from the CoinServiceFactory.getCoinService().
CoinServiceFactory includes getCoinService, a static method to get the specific inherited CoinService.
CoinNetworkInfo coinNetworkInfo = new CoinNetworkInfo( CoinType.TRX, TronNetworkType.MAINNET, RPC_ENDPOINT // ex) https://api.trongrid.io ); TronService tronService = (TronService) CoinServiceFactory .getCoinService( context, coinNetworkInfo ); // Use account manager AccountManager accountManager; accountManager = sblockchain.getAccountManager(); List<Account> tronAccounts = accountManager.getAccounts(null, CoinType.TRX, TronNetworkType.MAINNET);
If you want to use TronService, a CoinNetworkInfo that assigns CoinType.TRX is required. After creation, reassigning of CoinNetworkInfo is not allowed. If you want to change the NetworkType or RPC_ENDPOINT, you should create a new instance through the CoinServiceFactory.getCoinService().
An account has a TronTokenType which affects sendTokenTransaction and getTokenBalance.
The APIs below will be treated at Simple Payment UI separately.
TronService class inherits the CoinService. Base APIs are implemented through the TronService.
The list below are the APIs in CoinService.
tronService.getBalance(tronAccount).setCallback( new ListenableFutureTask.Callback<BigInteger>() { @Override public void onSuccess(BigInteger result) { //success } @Override public void onFailure(ExecutionException exception) { //failure } @Override public void onCancelled(InterruptedException exception) { //cancelled } });
try { tronService.sendRawTransaction( connectedWallet, trxAccount, rawTx // raw transaction ).setCallback( new ListenableFutureTask.Callback<TransactionResult>() { @Override public void onSuccess(TransactionResult result) { //success } @Override public void onFailure(ExecutionException exception) { //fail } @Override public void onCancelled(InterruptedException exception) { //cancelled } }); } catch (AvailabilityException e) { //handle exception }
The unit of value is sun.
try { tronService .sendTransaction( connectedWallet, tronAccount, toAddress, TronUtils.convertTrxToSun(BigDecimal.ONE) ).setCallback( new ListenableFutureTask.Callback<TransactionResult>() { @Override public void onSuccess(TransactionResult result) { //success } @Override public void onFailure(ExecutionException exception) { //fail } @Override public void onCancelled(InterruptedException exception) { //cancelled } }); } catch (AvailabilityException e) { //handle exception }
try { tronService.sendTokenTransaction( connectedWallet, tronTokenAccount, toAddress, new BigInteger("1000") // value of TRC10 or TRC20 by account ).setCallback( new ListenableFutureTask.Callback<TransactionResult>() { @Override public void onSuccess(TransactionResult result) { //success } @Override public void onFailure(ExecutionException exception) { //failure } @Override public void onCancelled(InterruptedException exception) { //cancelled } }); } catch (AvailabilityException e) { //handle exception }
tronService.getTokenBalance(tronTokenAccount).setCallback( new ListenableFutureTask.Callback<BigInteger>() { @Override public void onSuccess(BigInteger result) { //success } @Override public void onFailure(ExecutionException exception) { //failure } @Override public void onCancelled(InterruptedException exception) { //cancelled } });
String tokenAddrORTokenId = "1000102" tronService.getTokenInfo(TokenAddrORTokenId) .setCallback(new ListenableFutureTask.Callback<TronTokenInfo>() { @Override public void onSuccess(TronTokenInfo result) { //success } @Override public void onFailure(ExecutionException exception) { //failure } @Override public void onCancelled(InterruptedException exception) { //cancelled }
tronService.sendSmartContractTransaction( connectedWallet, tronAccount, contractAddress, encodedFunction, value, tokenId, tokenValue, feeLimit, ).setCallback( new ListenableFutureTask.Callback<TransactionResult>() { @Override public void onSuccess(TransactionResult result) { //success } @Override public void onFailure(ExecutionException exception) { //failure } @Override public void onCancelled(InterruptedException exception) { //cancelled }
tronService.getTransactionDetail(txId) .setCallback( new ListenableFutureTask.Callback<TronTransactionHistoryItem>() { @Override public void onSuccess(TronTransactionHistoryItem result) { // success } @Override public void onFailure(ExecutionException exception) { // failure } @Override public void onCancelled(InterruptedException exception) { // cancel }
String trc10TokenId = "1002000"; tronService.addTokenAccount(tronAccount, null, trc10TokenId) .setCallback(new ListenableFutureTask.Callback<TronAccount>() { @Override public void onSuccess(TronAccount result) { // success } @Override public void onFailure(ExecutionException exception) { // failure } @Override public void onCancelled(InterruptedException exception) { // cancel } ) });