transfer erc20 token with blockchain app objective develop a decentralized application dapp to transfer erc20 tokens between ethereum accounts using samsung blockchain platform sdk overview blockchain technology has been creating a significant impact in multiple sectors, such as banking, cybersecurity, and supply chain management it is widely used as a means of secure payment between different parties samsung blockchain platform sdk brings developers and consumers to the blockchain world by providing a complete set of functions that the decentralized app dapp or blockchain app needs ethereum is a decentralized blockchain network where you can perform transactions using its native currency, ether, and token you can interact with the network through simple api calls provided by the sdk for detailed information, see samsung blockchain platform sdk set up your environment you will need the following java se development kit 8 or later android studio latest version recommended samsung galaxy device that supports samsung blockchain sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! token transaction sample code 2 73 mb enable developer mode to activate developer mode on your mobile device, follow the steps below navigate through settings > biometrics and security > samsung blockchain keystore and click about blockchain keystore tap the samsung blockchain keystore app name quickly, ten times or more if succeeded, developer mode will show start your project after downloading the sample code containing the project files, in android studio, click open to open the existing project locate the downloaded android project codelab-send-token-transaction-blank-code from the directory and click ok noteuser interface ui resources are already included in the provided project simply apply the code in the next steps in this code lab moreover, going through the sdk document is recommended initialize the instance since sblockchain is the initial base class of the samsung blockchain platform sdk, the first thing you need to do is create an sblockchain instance along with that, create the following in sendtokenfragment java hardwarewalletmanager for wallet operations accountmanager for account operations ethereumservice for transactions msblockchain = new sblockchain ; try { msblockchain initialize mcontext ; } catch ssdkunsupportedexception e { e printstacktrace ; } maccountmanager = msblockchain getaccountmanager ; mhardwarewalletmanager = msblockchain gethardwarewalletmanager ; mcoinnetworkinfo = new coinnetworkinfo cointype eth, ethereumnetworktype goerli, rpcurl ; mcoinservice = coinservicefactory getcoinservice getcontext , mcoinnetworkinfo ; ethereumservice = ethereumservice mcoinservice; connecttohardwarewallet ; connect to samsung blockchain keystore connect the app to the hardware wallet, which, in this case, is the samsung blockchain keystore you can get a hardware wallet instance from hardwarewalletmanager if you want to reset the wallet, set the second parameter of connect api to true otherwise, set it to false mhardwarewalletmanager connect hardwarewallettype samsung, false setcallback new listenablefuturetask callback<hardwarewallet> { @override public void onsuccess hardwarewallet hardwarewallet { handler post new runnable { @override public void run { log i tag, "hardwarewallet is connected " ; mprogressbar setvisibility view invisible ; getaccount ; } } ; } @override public void onfailure @nonnull executionexception e { mprogressbar setvisibility view invisible ; log e tag, "hardwarewallet connection failed " + e ; } @override public void oncancelled @nonnull interruptedexception e { mprogressbar setvisibility view invisible ; log e tag, "hardwarewallet connection cancelled " + e ; } } ; generate new account samsung blockchain platform sdk manages the address on blockchain as an account it contains the information required for signing and the blockchain address accountmanager provides apis dedicated for fetching, creating, and restoring accounts use generatenewaccount api for the creation of a new ethereum account upon clicking the create account button in the app hardwarewallet connectedhardwarewallet = mhardwarewalletmanager getconnectedhardwarewallet ; maccountmanager generatenewaccount connectedhardwarewallet, mcoinnetworkinfo setcallback new listenablefuturetask callback<account> { @override public void onsuccess account account { mprogressbar setvisibility view invisible ; log i tag, "generate new account successful " + account ; handler post new runnable { @override public void run { getaccount ; } } ; } @override public void onfailure @nonnull executionexception e { mprogressbar setvisibility view invisible ; log e tag, "generate new account failed " + e ; } @override public void oncancelled @nonnull interruptedexception e { mprogressbar setvisibility view invisible ; log e tag, "generate new account cancelled " + e ; } } ; after creating a new account, call the getaccounts api to get the account list if you already have an account, the textview will show the account's address and the create account button will be disabled in the app accounts = maccountmanager getaccounts null, cointype eth, ethereumnetworktype goerli ; if !accounts isempty { methereumaccount = ethereumaccount accounts get accounts size - 1 ; showaccounttextview settext methereumaccount getaddress ; generateaccountbutton setenabled false ; } create a token account to perform a token transaction, add the token address with the corresponding ethereum account and create a token account by calling the addtokenaddress api use the generated token account to perform token-related actions an ethereum account can add one or more tokens ethereumservice addtokenaddress methereumaccount, tokenaddress setcallback new listenablefuturetask callback<ethereumaccount> { @override public void onsuccess ethereumaccount ethereumaccount { mprogressbar setvisibility view invisible ; log i tag, "add token successful " + ethereumaccount ; handler post new runnable { @override public void run { toast maketext getcontext , "add token successful", toast length_short show ; } } ; } @override public void onfailure @nonnull executionexception e { mprogressbar setvisibility view invisible ; log e tag, "add token failed " + e ; } @override public void oncancelled @nonnull interruptedexception e { mprogressbar setvisibility view invisible ; log e tag, "add token cancelled " + e ; } } ; get token balance fetch the balance of the added token using gettokenbalance api ethereumservice gettokenbalance mtokenaccount, ethereumblockparameter latest setcallback new listenablefuturetask callback<biginteger> { @override public void onsuccess biginteger biginteger { mprogressbar setvisibility view invisible ; bigdecimal tokendecimal = bigdecimal ten pow 18 ; bigdecimal balance = new bigdecimal biginteger divide tokendecimal ; log d tag, "gettokenbalance success " + balance ; handler post new runnable { @override public void run { tokenbalance settext balance tostring ; } } ; } @override public void onfailure @nonnull executionexception e { mprogressbar setvisibility view invisible ; log e tag, "gettokenbalance failed " + e ; } @override public void oncancelled @nonnull interruptedexception e { mprogressbar setvisibility view invisible ; log e tag, "gettokenbalance cancelled " + e ; } } ; transfer token to transfer tokens between accounts, set the receiver address and the amount of tokens to send the trusted ui of the samsung blockchain keystore hardware wallet appears upon pressing the send button in the app, showing all the information regarding the transaction for confirmation generate a transaction hash upon confirmation of the transaction ethereumservice sendtokentransaction mhardwarewalletmanager getconnectedhardwarewallet , mtokenaccount, mtoaddress, tokenaddress, maxpriorityfee, methereumfeeinfo getestimatedbasefee add maxpriorityfee , mgaslimit, msendtokenamount, null setcallback new listenablefuturetask callback<transactionresult> { @override public void onsuccess transactionresult transactionresult { mprogressbar setvisibility view invisible ; log i tag, "send token successful " + transactionresult gethash ; handler post new runnable { @override public void run { toast maketext getcontext , "transaction hash " + transactionresult gethash , toast length_short show ; } } ; } @override public void onfailure @nonnull executionexception e { mprogressbar setvisibility view invisible ; log e tag, "send token failed " + e ; } @override public void oncancelled @nonnull interruptedexception e { mprogressbar setvisibility view invisible ; log i tag, "send token cancelled " + e ; } } ; run the app after building the apk, follow the steps below to test the application on a samsung blockchain-compatible device in send token tab, click create account copy the generated account address go to eth faucet tab to open the free faucet site already added to the application paste the account address and press the send me eth button click the hash string under your transactions to open etherscan and wait until the transaction succeeded go to token faucet tab and press get token to add some tokens in the account confirm the transaction go back to send token tab, click add token and press token balance to see added tokens press gas limit and max priority fee lastly, press send to transfer tokens confirm the transaction check the transaction status and details by finding the account address or the generated transaction hash txn hash in goerli testnet explorer you're done! congratulations! you have successfully achieved the goal of this code lab now, you can develop a decentralized application that can transfer erc20 tokens using samsung blockchain platform sdk if you face any trouble, you may download this file token transaction complete code 2 73 mb to learn more about developing apps with samsung blockchain, visit developer samsung com/blockchain