The Samsung Blockchain Platform (SBP) SDK provides a full set of functionalities for growing decentralized purposes (DApps) that talk with the Ethereum and TRON networks. The SDK features a easy fee UI you can combine into your DApp, enabling customers to simply make TRX, TRC-10, and TRC-20 transactions.

Determine 1: SBP SDK fee sheet

This text describes a pattern utility that demonstrates the way to combine the SBP SDK fee UI right into a TRON DApp. You’ll be able to observe together with the demonstration by downloading the sample application.

The applying connects to a {hardware} pockets, creates a brand new account, and fetches the account stability. Then, it launches the fee sheet UI to make a transaction. Every of those actions has been carried out with a corresponding button that can assist you perceive the sequence of steps and visualize the end result.

Determine 2: Pattern utility overview

Initialize the SBP SDK occasion

To speak with the SBP SDK, you will need to create an occasion of the SBlockchain class.

Earlier than creating the occasion, implement the SBP SDK dependencies and arrange your system atmosphere to make use of the Samsung Blockchain Keystore as a {hardware} pockets. For detailed details about the dependencies and the atmosphere settings, see Getting Started.

To create the SBlockchain class occasion, you should utilize the next code:

strive {
    SBlockchain mSblockchain = new SBlockchain();
    mSblockchain.initialize(context);
} catch (SsdkUnsupportedException e) {
    if (e.getErrorType() == VENDOR_NOT_SUPPORTED){
        Log.e("error", "Platform SDK doesn't assist this system");
    }
}

Hook up with a {hardware} pockets

To handle accounts and create transactions, you will need to hook up with a {hardware} pockets. The SBP SDK helps particular {hardware} wallets, such because the Samsung Blockchain Keystore and Ledger Nano.

The pattern utility helps the Samsung Blockchain Keystore, which is built-in into chosen Samsung gadgets. It’s secured with the Samsung Knox TrustZone and the defense-grade Trusted Execution Atmosphere (TEE). To study extra in regards to the safety features, see Samsung Blockchain Keystore SDK Overview.

Use the join() technique to attach with the {hardware} pockets. To specify Samsung Blockchain Keystore because the {hardware} pockets, set the {hardware} pockets sort to HardwareWalletType.SAMSUNG. For extra info, see Connect With Hardware Wallet.

HardwareWalletType mHardwareWalletType = HardwareWalletType.SAMSUNG;
HardwareWalletManager mHardwareWalletManager = mSblockchain.getHardwareWalletManager();

mHardwareWalletManager.join(mHardwareWalletType, true)
    .setCallback(new ListenableFutureTask.Callback<HardwareWallet>() {
    @Override
    public void onSuccess(HardwareWallet hardwareWallet) {
        mHardwareWallet = hardwareWallet;
        Log.i(LOG_TAG, "Pockets connection profitable.");
    }

    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, "Pockets connection failed.");
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Pockets connection cancelled.");
    }
});

Within the pattern utility, when the pockets is related, its Pockets ID is displayed.

Determine 3: {Hardware} pockets related

Handle accounts

The SBP SDK contains varied account administration options, corresponding to generating, restoring, and retrieving accounts.

To switch TRX, you will need to have a TRON account. A TRON account contains varied attributes, such because the TRX stability, bandwidth, and power. For extra details about the TRON account mannequin, see the official Tron documentation.

The next code snippet reveals the way to retrieve TRON accounts from the pockets:

CoinType coinType = CoinType.TRX;
NetworkType networkType = TronNetworkType.SHASTA;
String rpc = "https://api.shasta.trongrid.io";

mCoinNetworkInfo = new CoinNetworkInfo(coinType, networkType, rpc);

non-public Record<Account> accountList = mSblockchain.getAccountManager()
        .getAccounts(mHardwareWallet.getWalletId(), mCoinNetworkInfo.getCoinType(), mCoinNetworkInfo.getNetworkType());

To create a TRON account, use the generateNewAcount() technique:

mAccountManager.generateNewAccount(mHardwareWallet, mCoinNetworkInfo)
    .setCallback(new ListenableFutureTask.Callback<Account>() {
    @Override
    public void onSuccess(Account account) {
        Log.i(LOG_TAG, "Account created.");
    }
    
    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, "Account creation failed.");
        Log.e(LOG_TAG, "" + e.getMessage());
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Account creation cancelled.");
    }
});

Determine 4: Account retrieved

Fetching the account stability

For the aim of this demonstration, get some TRX for the SHASTA TRON take a look at community from the SHASTA faucet.

To retrieve stability info in your TRON account from the community, use the getBalance() technique:

CoinService mCoinService = CoinServiceFactory.getCoinService(getApplicationContext(), mCoinNetworkInfo);

TronService tronService = (TronService) mCoinService;

tronService.getBalance(firstAccount).setCallback(new ListenableFutureTask.Callback<BigInteger>() {
    @Override
    public void onSuccess(BigInteger bigInteger) {
        amountInTrx = TronUtils.convertSunToTrx(bigInteger);
        Log.i(LOG_TAG, "Steadiness fetched efficiently.");
        Log.i(LOG_TAG, "Steadiness is:" + amountInTrx.toString());
    }

    @Override
    public void onFailure(@NotNull ExecutionException e) {
        Log.e(LOG_TAG, "Steadiness fetching failed.");
        Log.e(LOG_TAG, "" + e.getMessage());
    }

    @Override
    public void onCancelled(@NotNull InterruptedException e) {
        Log.e(LOG_TAG, "Steadiness fetching cancelled.");
    }
});

Determine 5: Account stability retrieved

Launch the fee sheet intent

The easy fee UI included within the SBP SDK reveals all the data you want for the transaction, such because the vacation spot, quantity, and charges.

The createTronPaymentSheetActivityIntent() technique creates an intent of the TRON fee sheet exercise for TRX transactions:

String toAddress = "TMie1hFH6zgxHVNnDWkVNRpHHitGuM7mjN";
BigDecimal sendAmount = new BigDecimal("1.0");
BigInteger convertedSendAmount = TronUtils.convertTrxToSun(sendAmount);

strive {
    Intent intent = tronService.createTronPaymentSheetActivityIntent(
            this,
            mHardwareWallet,
            (TronAccount) firstAccount,
            toAddress,
            convertedSendAmount
    );
    activityResultLauncher.launch(intent);
} catch (Exception e) {
    Log.e(LOG_TAG, "Error in sending: " + e);
}

Determine 6: Cost sheet launched

You may as well use the createTrc10PaymentSheetActivityIntent() and createTrc20PaymentSheetActivityIntent() strategies to create intents for TRC-10 and TRC-20 token fee sheet actions, respectively. A token ID is required to create a TRC-10 fee and a token handle is required to create a TRC-20 fee.

Get the transaction end result

You’ll be able to retrieve the transaction end result utilizing the ActivityResultLauncher class. To get the transaction hash within the acquired intent parameter, use the txid key. To get the error for a failed transaction, use the error key.

ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult end result) {
            change (end result.getResultCode()) {
                case Exercise.RESULT_OK:
                    Log.d(LOG_TAG, "Transaction Hash: " + end result.getData().getStringExtra("txid"));
                    break;

                case Exercise.RESULT_CANCELED:
                    Log.e(LOG_TAG, "Transaction Failed: " + end result.getData().getStringExtra("error"));
                    break;
            }
        }
    }
);

Determine 7: Transaction hash retrieved

This text has demonstrated how one can implement the TRON fee sheet UI offered by the SBP SDK in your DApp. The fee sheet UI simplifies your utility growth and provides your customers a constant person expertise.

Further sources

Further sources on the Samsung Builders web site

The Samsung Builders web site has many sources for builders seeking to construct for and combine with Samsung gadgets and providers. Keep in contact with the newest information by creating a free account and subscribing to our monthly newsletter. Go to the Galaxy Store Games web page for info on bringing your recreation to Galaxy Retailer and go to the Marketing Resources web page for info on selling and distributing your Android apps. Lastly, our Developer Forum is a wonderful solution to keep up-to-date on all issues associated to the Galaxy ecosystem.