Send TRX with the Samsung Blockchain Platform SDK
wallet get accounts each of these actions of the application has been implemented with a corresponding button so that the results of each action can be more visible and clearly illustrated. initialize the object to use the apis of the sdk in an android project, an object of the sdk is required. to create the object, follow the steps below. create a new android project and integrate the sbp sdk with the application. all dependencies need to be mentioned in the build.gradle file. to complete this step follow these instructions. after completing the synchronization of build.gradle with the file system, the project is ready for creating an instance of the sblockchain class. relevant source code can be found here. connect to a hardware wallet a hardware wallet is a storage device with the facility of authentication for securely storing the public-private key pair. there are several hardware wallets in the market, and samsung provides a hardware wallet integrated with select samsung mobile devices, known as the samsung blockchain keystore. sbp provides an interface to connect hardware wallets. alongside the samsung blockchain keystore, sbp currently supports ledger nano x and ledger nano s. connect your application with a hardware wallet using the following code. in this article, the samsung blockchain keystore has been used as the hardware wallet. while running the sample application on your device, please ensure you meet the device restrictions of the samsung blockchain keystore. you can also change the hardware wallet type and test the same application with other supported hardware wallets. get accounts an account is an important protocol of tron. it functions as a gateway to interact with the tron blockchain. your trx balance, token balance, bandwidth, energy, and so on are all attached to your account. trx balance can be sent from one account to another. moreover, an account can issue a smart contract. tron accounts also provide the facility to vote for super representatives. to learn more about super representatives and voting, see the tron documentation. every account consists of a private and public key pair. an account remains deactivated until a trx or token transaction is performed. creating an account consumes frozen bandwidth points. if none are available, then the action burns 0.1 trx instead. if you want to know more about the underlying algorithm and the process of creating an account, see the tron account documentation. the sbp sdk allows you to create a new account or restore existing accounts from the tron network. restoring and creating accounts are network operations. for more information, see the account management guide for the sbp sdk. it discusses generating, restoring and fetching accounts using the sdk. if you have no trx in your account, you can get some by purchasing from the sites listed here or get test trx. trx transaction let's send trx from your account to another account. the sbp sdk provides an api called sendtransaction to perform a transaction between two accounts. this api needs four parameters. wallet: a connected hardware wallet. fromaccount: the sender’s tron account. toaddress: where you want to transfer your trx to, taking the address as a string. value: how much trx you want to transfer. input unit is sun (1 trx= 10^6 sun). the coinservice class called tronservice provides the sendtransaction api for transactions on the tron platform. in addition, a remote procedure call (rpc) is required to communicate with the tron node. we are going to use trongrid for this purpose, which allows developers to access a tron client that is running on the cloud instead of creating one. you can learn more about the trongrid here. before completing the example transaction, some terminology related to transactions, like bandwidth and energy, are discussed. networktype networktype = tronnetworktype.shasta; cointype cointype = cointype.trx; //shasta rpc string rpc = "https://api.shasta.trongrid.io"; // creating coinnetworkinfo coinnetworkinfo mcoinnetworkinfo = new coinnetworkinfo(cointype, networktype, rpc); //creating coinservice coinservice mcoinservice = coinservicefactory.getcoinservice(getapplicationcontext(), mcoinnetworkinfo); //creating tronservice tronservice tronservice = (tronservice) mcoinservice; fetch trx balance trx is the primary currency of tron. it can be used to obtain bandwidth, energy, or power. the sbp sdk provides the getbalance api for fetching the balance of an account. public void onclickgetbalance(view view) { mcoinservice = coinservicefactory.getcoinservice(getapplicationcontext(), mcoinnetworkinfo); textview trxamounttextview = findviewbyid(r.id.amount_tron); mcoinservice.getbalance(mfirstaccount).setcallback(new listenablefuturetask.callback<biginteger>() { @override public void onsuccess(biginteger biginteger) { convertedamount = tronutils.convertsuntotrx(biginteger); log.i(log_tag, "balance has fetched successfully."); log.i(log_tag, "balance is:" + convertedamount.tostring()); runonuithread(() -> { accountinfobutton.setenabled(true); trxamounttextview.settext(convertedamount.tostring()); } ); } @override public void onfailure(@notnull executionexception e) { log.e(log_tag, "fetching balance is failed."); log.e(log_tag, "" + e.getmessage()); } @override public void oncancelled(@notnull interruptedexception e) { log.e(log_tag, "fetching balance is canceled."); } }); } fetch bandwidth and energy tron implements some new and interesting features named freeze, bandwidth, and energy. you can freeze some of your trx balance to gain bandwidth and energy. for every frozen trx, the user receives 1 tron power which is needed to cast a vote on super representatives. after freezing some of your trx, it is not possible to use these trx until they are unfrozen. bandwidth is used as a fee for trx transactions. energy is needed to execute smart contracts. the sbp sdk only provides an api to get a frozen balance amount. you can freeze trx and gain bandwidth and energy using tronscan. tronscan is a blockchain explorer, which allows anyone to explore addresses, transactions, and tokens in the tron blockchain. the sbp sdk provides the getaccountinfo api for fetching bandwidth, energy and power (frozen balance) information. the input parameter is the account address of the sender. in the sample application, after pressing the account info button, the asynchronous task returns this information, which is set on a text view in the ui. public void onclickaccountinfo(view view) { tronservice = (tronservice) mcoinservice; try { tronservice.getaccountinfo(mfirstaccount.getaddress()).setcallback(new listenablefuturetask.callback<tronaccountinfo>() { @override public void onsuccess(tronaccountinfo tronaccountinfo) { log.i(log_tag, "account info is fetched successfully."); log.i(log_tag, "bandwidth is:" + tronaccountinfo.getbandwidth()); log.i(log_tag, "power is:" + tronaccountinfo.getfrozenbalance()); runonuithread(() -> { sendbutton.setenabled(true); bandwidthtextview.settext(tronaccountinfo.getbandwidth().tostring()); powertextview.settext(tronaccountinfo.getfrozenbalance().tostring()); } ); } @override public void onfailure(@notnull executionexception e) { log.e(log_tag, "fetching account info is failed."); log.e(log_tag, "" + e.getmessage()); } @override public void oncancelled(@notnull interruptedexception e) { log.e(log_tag, "fetching account info is canceled."); } }); } catch (exception e) { log.e(log_tag, "error in fetching account info: " + e); } } after fetching bandwidth and energy, we can check them on the sample application ui. transfer trx now we have all the parameters needed for sending trx. the sendtransaction api is used to transfer trx to the recipient. if the transaction is successful, the onsuccess() callback returns the transaction hash. every transaction in the tron blockchain can be found in tronscan. public void onclicksend(view view) { if (toaddress.isempty() || amount.isempty()) { toast.maketext(getapplicationcontext(), "fill toaddress and amount field", toast.length_short).show(); } else if(!tronservice.isvalidaddress(toaddress)){ toast.maketext(getapplicationcontext(), "invalid address.", toast.length_short).show(); } else { bigdecimal sendamount = new bigdecimal(amount); biginteger convertedsendamount = tronutils.converttrxtosun(sendamount); try { tronservice.sendtransaction(mhardwarewallet, (tronaccount) mfirstaccount, toaddress, convertedsendamount).setcallback(new listenablefuturetask.callback<transactionresult>() { @override public void onsuccess(transactionresult transactionresult) { log.d(log_tag, "transaction hash: " + transactionresult.gethash()); runonuithread(() -> toast.maketext(getapplicationcontext(), "transaction hash: " + transactionresult.gethash(), toast.length_short).show() ); } @override public void onfailure(@notnull executionexception e) { log.e(log_tag, "transaction failed."); log.e(log_tag, "" + e.getmessage()); } @override public void oncancelled(@notnull interruptedexception e) { log.e(log_tag, "transaction canceled."); } }); } catch (availabilityexception e) { log.e(log_tag, "error in sending: " + e); } } } now that you can send trx to another account, you can create your wallet application and implement a transfer function using the sbp sdk. keep an eye on the samsung developers site for updates as the samsung blockchain keeps expanding support for new platforms. additional resources: download the sbp sdk transaction sample app more information on the sbp sdk follow up this site has many resources for developers looking to build for and integrate with samsung devices and services. stay in touch with the latest news by creating a free account or by subscribing to our monthly newsletter. visit the marketing resources page for information on promoting and distributing your apps. finally, our developer forum is an excellent way to stay up-to-date on all things related to the galaxy ecosystem.