Tron Service

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().

Get TronService

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.

Simple Payment APIs

The APIs below will be treated at Simple Payment UI separately.

  • createTronPaymentSheetActivityIntent – Creates an Intent that shows up in the Tron Payment Sheet.
  • createTrc10PaymentSheetActivityIntent – Creates an Intent that shows up in the TRC10 token Payment Sheet.
  • createTrc20PaymentSheetActivityIntent – Creates an Intent that shows up in the TRC20 token Payment Sheet.
  • createSmartContractPaymentSheetActivityIntent – Creates an Intent that shows up in the Smart Contract Payment Sheet.

Base CoinService APIs

TronService class inherits the CoinService. Base APIs are implemented through the TronService.

The list below are the APIs in CoinService.

  • getBalance – Get the balance of the input account with CoinNetworkInfo in the 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
        }
    });
  • sendRawTransaction - Sends a raw transaction. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Tron’s own problem.
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
}

Tron specific APIs

  • sendTransaction – Sends a TRX. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Tron’s own problem.
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
}
  • sendTokenTransaction – Sends a Tron Token (TRC10, TRC20 by account type) transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Tron’s own problem.
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
}
  • getTokenBalance – Get Tron Token (TRC10, TRC20 by account type) balance with parameters. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Tron’s own problem.
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
        }
    });
  • getTokenInfo – Retrieves the (TRC10, TRC20) token information.
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
        }
  • sendSmartContractTransaction – Sends a Tron Smart Contract transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Tron’s own problem.
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
        }
  • getTransactionDetail – Gets the detailed information for a single Tron transaction by txId. It will return as a TronTransaction that contains the transaction data like block number, hash, value, and etc.
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
        }
  • addTokenAccount – Adds the token (TRC10, TRC20) address to the TronAccount.
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
        }
    )
});