@terradharitri/sdk-dapp-core
v0.0.5
Published
A library to hold core logic for building TypeScript dApps on the DharitrI blockchain
Readme
DharitrI SDK for Front-End DApps
DharitrI Front-End SDK for JavaScript and TypeScript (written in TypeScript).
Introduction
sdk-dapp-core is a library that holds core functional logic that can be used to create a dApp on DharitrI Network.
It is built for applications that use any of the following technologies:
- React
- Angular
- Vue
- Any other JavaScript framework (e.g. Solid.js etc.)
- React Native
- Next.js
GitHub project
The GitHub repository can be found here: https://github.com/TerraDharitri/drt-sdk-dapp-core
Live demo: template-dapp
See Template dApp for live demo or checkout usage in the Github repo
Requirements
- Node.js version 20.13.1+
- Npm version 10.5.2+
Distribution
Installation
The library can be installed via npm or yarn.
npm install @terradharitri/sdk-dapp-coreor
yarn add @terradharitri/sdk-dapp-coreIf you need only the core behaviour, without the additional UI, you can create a project-specific .npmrc file to configure per-package installation behavior. This will skip the installation of @terradharitri/sdk-dapp-core-ui, but keep in mind that you may need to provide the UI components yourself.
Also, make sure you run your app on https, not http, otherwise some providers will not work.
## .npmrc
@terradharitri/sdk-dapp-core:omit-optional=true
## enable the option when needed with:
## @terradharitri/sdk-dapp-core:omit-optional=false
## Run Installation
## When you run npm install, NPM will use the configurations specified in the .npmrc file:
npm installIf you're transitioning from @terradharitri/sdk-dapp, you can check out the Migration guide PR of Template Dapp
Usage
sdk-dapp-core aims to abstract and simplify the process of interacting with users' wallets and with the DharitrI blockchain, allowing developers to easily get started with new applications.
flowchart LR
A["Signing Providers & APIs"] <--> B["sdk-dapp-core"] <--> C["dApp"]The basic concepts you need to understand are configuration, provider interaction, transactions, and presenting data. These are the building blocks of any dApp, and they are abstracted in the sdk-dapp-core library.
Having this knowledge, we can consider several steps needed to put a dApp together:
Table 1. Steps to build a dApp | # | Step | Description | |---|------|-------------| | 1 | Configuration | - storage configuration (e.g. sessionStorage, localStorage etc.)- chain configuration- custom provider configuration (adding / disabling / changing providers) | | 2 | Provider interaction | - logging in and out- signing transactions / messages | | 3 | Presenting data | - get store data (e.g. account balance, account address etc.)- use components to display data (e.g. balance, address, transactions list) | | 4 | Transactions | - sending transactions- tracking transactions |
Each of these steps will be explained in more detail in the following sections.
1. Configuration
Before your application bootstraps, you need to configure the storage, the network, and the signing providers. This is done by calling the initApp method from the core/methods folder.
// index.tsx
import { initApp } from '@terradharitri/sdk-dapp-core/out/core/methods/initApp/initApp';
import type { InitAppType } from '@terradharitri/sdk-dapp-core/out/core/methods/initApp/initApp.types';
import { EnvironmentsEnum } from '@terradharitri/sdk-dapp-core/out/types/enums.types';
import { App } from "./App";
const config: InitAppType = {
storage: { getStorageCallback: () => sessionStorage },
dAppConfig: {
// nativeAuth: true, // optional
environment: EnvironmentsEnum.devnet,
// network: { // optional
// walletAddress: 'https://devnet-wallet.dharitri.org'
// },
successfulToastLifetime: 5000
}
// customProviders: [myCustomProvider] // optional
};
initApp(config).then(() => {
render(() => <App />, root!); // render your app
});2. Provider interaction
Once your dApp has loaded, the first user action is logging in with a chosen provider.
import { ProviderTypeEnum } from '@terradharitri/sdk-dapp-core/out/core/providers/types/providerFactory.types';
import { ProviderFactory } from '@terradharitri/sdk-dapp-core/out/core/providers/ProviderFactory';
const provider = await ProviderFactory.create({
type: ProviderTypeEnum.extension
});
await provider.login();3. Displaying user data
Depending on the framework, you can either use hooks or selectors to get the user details:
React hooks solution:
import { useGetAccount } from '@terradharitri/sdk-dapp-core/out/store/selectors/hooks/account/useGetAccount';
import { useGetNetworkConfig } from '@terradharitri/sdk-dapp-core/out/store/selectors/hooks/network/useGetNetworkConfig';
const account = useGetAccount();
const {
network: { rewaLabel }
} = useGetNetworkConfig();
console.log(account.address);
console.log(`${account.balance} ${rewaLabel}`);Store selector functions:
import { getAccount } from '@terradharitri/sdk-dapp-core/out/core/methods/account/getAccount';
import { getNetworkConfig } from '@terradharitri/sdk-dapp-core/out/core/methods/network/getNetworkConfig';
const account = getAccount();
const { rewaLabel } = getNetworkConfig();4. Transactions
Signing transactions
To sign transactions, you first need to create the Transaction object then pass it to the initialized provider.
import { Transaction, TransactionPayload } from '@terradharitri/sdk-core/out';
import {
GAS_PRICE,
GAS_LIMIT
} from '@terradharitri/sdk-dapp-core/out/constants/drt.constants';
import { getAccountProvider } from '@terradharitri/sdk-dapp-core/out/core/providers/helpers/accountProvider';
import { refreshAccount } from '@terradharitri/sdk-dapp-core/out/utils/account/refreshAccount';
const pongTransaction = new Transaction({
value: '0',
data: new TransactionPayload('pong'),
receiver: contractAddress,
gasLimit: GAS_LIMIT,
gasPrice: GAS_PRICE,
chainID: network.chainId,
nonce: account.nonce,
sender: account.address,
version: 1
});
await refreshAccount(); // optionally, to get the latest nonce
const provider = getAccountProvider();
const signedTransactions = await provider.signTransactions(transactions);Sending and tracking transactions
Then, to send the transactions, you need to use the TransactionManager class and pass in the signedTransactions to the send method. You can optionally track the transactions by using the track method. This will create a toast notification with the transaction hash and its status.
import { TransactionManager } from '@terradharitri/sdk-dapp-core/out/core/managers/TransactionManager';
const txManager = TransactionManager.getInstance();
await txManager.send(signedTransactions);
await txManager.track(signedTransactions);Once the transactions are executed on the blockchain, the flow ends with the user logging out.
import { getAccountProvider } from '@terradharitri/sdk-dapp-core/out/core/providers/helpers/accountProvider';
const provider = getAccountProvider();
await provider.logout();Internal structure
We have seen in the previous chapter what are the minimal steps to get up and running with a blockchain interaction using sdk-dapp-core. Next we will detail each element mentioned above
Table 2. Elements needed to build a dApp | # | Type | Description | |---|------|-------------| | 1 | Network | Chain configuration | | 2 | Provider | The signing provider for logging in and singing transactions | | 3 | Account | Inspecting user address and balance | | 4 | Transactions Manager | Sending and tracking transactions | | 5 | UI Components | Displaying UI information like balance, public keys etc. |
Since these are mixtures of business logic and UI components, the library is split into several folders to make it easier to navigate.
When inspecting the package, there is more content under src, but the main folders of interest are:
src/
├── apiCalls/ ### methods for interacting with the API
├── constants/ ### useful constants from the ecosystem like ledger error codes, default gas limits for transactions etc.
├── controllers/ ### business logic for UI elements like transactions and amount formatting
├── core/ ### hosting the provider class, and all implementations for different signing providers
└── store/ ### store initialization, middleware, slices, selectors and actionsConceptually, these can be split into 3 main parts:
- First is the business logic in
apiCalls,constantsandcore(signing providers). - Then comes the persistence layer hosted in the
storefolder, using Zustand under the hood. - Last are the UI components hosted in @terradharitri/sdk-dapp-core with some components controlled on demand by classes defined in
controlles
Next, we will take the elements from Table 2 and detail them in the following sections.
1. Network
The network configuration is done in the initApp method, where you can make several confgurations like:
- specifying the environment (devnet, testnet, mainnet)
- overriding certain network parameters like wallet address, explorer address etc.
Once the network is configured, the network slice in the store will hold the network configuration.
To query different network parameters, you can use the getNetworkConfig method from the core/methods/network folder.
2. Provider
The provider is the main class that handles the signing of transactions and messages. It is initialized in the initApp method and can be accessed via the getAccountProvider method from the core/providers/helpers folder.
Initialization
It's important to initialize it on app load (this is take care of by initApp), since it restores the session from the store and allows signing transactions without the need to make a new login.
Creating a custom provider
If you need to create a custom signing provider, make sure to extend the IProvider interface and implement all required methods (see example here). Next step would be to include it in the customProviders array in the initApp method or add it to the window object. Last step is to login using the custom provider.
import { ProviderTypeEnum } from '@terradharitri/sdk-dapp-core/out/core/providers/types/providerFactory.types';
const ADDITIONAL_PROVIDERS = {
myCustomProvider: 'myCustomProvider'
} as const;
// do this if you want to reference it later in your code
const ExtendedProviders = {
...ProviderTypeEnum,
...ADDITIONAL_PROVIDERS
} as const;
const provider = await ProviderFactory.create({
type: ExtendedProviders.myCustomProvider // or add a simple string here
});
await provider?.login();Accessing provier methods
Once the provider is initialized, you can get a reference to it using the getAccountProvider method. Then you can call the login, logout, signTransactions, signMessage methods, or other custom methods depending on the intialized provider (see ledger for example).
3. Account
Getting account data
Once the user logs in, a call is made to the API for fetching the account data. This data is persisted in the store and is accessible through helpers found in core/methods/account. These functions are:
Table 3. Getting account data
| # | Helper | Description | React hook equivalent |
|---|------|-------------|----|
| | core/methods/account | path | store/selectors/hooks/account |
| 1 | getAccount() | returns all account data |useGetAccount() |
| 2 | getAddress() | returns just the user's public key | useGetAddress()|
| 3 | getIsLoggedIn() | returns a login status boolean | useGetIsLoggedIn() |
| 4 | getLatestNonce() | returns the account nonce | useGetLatestNonce()
Nonce management
sdk-dapp-core has a mechanism that does its best to manage the account nonce. For example, if the user sends a transaction, the nonce gets incremented on the client so that if he sends a new transaction, it will have the correct increased nonce. If you want to make sure the nonce is in sync with the API account, you can call refreshAccount() as shown above in the Signing transactions section.
4. Transactions Manager
Overview
The TransactionManager is a class that handles sending and tracking transactions in the DharitrI ecosystem. It provides methods to send single and batch transactions while handling tracking, error management, and toasts for user notifications. It is initialized in the initApp method and can be accessed via TransactionManager.getInstance().
Features
- Supports Single and Batch Transactions: Handles individual transactions as well as grouped batch transactions.
- Automatic Tracking: Monitors transaction status and updates accordingly through a webhook or polling fallback mechanism.
- Toast Notifications: Displays status updates for user feedback, with options to disable notifications and customize toast titles.
- Error Handling: Catches and processes errors during transaction submission
Transactions Lifecycle
The transaction lifecycle consists of the following steps:
- Creating a
Transactionobject using the@terradharitri/sdk-core provider - Signing the transaction with the initialized provider and receiving a
SignedTransactionTypeobject - Sending the signed transaction using TransactionManager's
send()function. Signed transactions can be sent in 2 ways:
Table 4. Sending signed transactions
| # | Signature | Method | Description |
|---|------|-------------|-------------|
| 1 | send([tx1, tx2]) | POST to /transactions | Transactions are executed in parallel
| 2 | send([[tx1, tx2], [tx3]]) | POST to /batch | First batch of two transactions is executed, and the second batch of one transaction waits for the finished results, and is then executed
Tracking transactions is made by using
transactionManager.track(). Since thesend()function returns the same arguments it has received, the same array payload can be passed into thetrack()method. Under the hood, status updates are received via a WebSocket or polling mechanism. Once a transaction array is tracked, it gets associated with asessionId, returned by thetrack()method and stored in thetransactionsslice. Depending on the array's type (plain/batch), the session's status varies from initial (pending/invalid/sent) to final (successful/failed/timedOut).User feedback is provided through toast notifications, which are triggered to inform about transactions' progress. Additional tracking details can be optionally displayed in the toast UI. There is an option to add custom toast messages by using the
createCustomToasthelper.
import { createRoot } from 'react-dom/client';
import { createCustomToast } from '@terradharitri/sdk-dapp-core/out/store/actions/toasts/toastsActions';
// by creating a custom toast element containing a component
createCustomToast({
toastId: 'username-toast',
instantiateToastElement: () => {
const toastBody = document.createElement('div');
const root = createRoot(toastBody);
root.render(<ReloadButton />);
return toastBody;
}
});
// or by creating a simple custom toast
createCustomToast({
toastId: 'custom-toast',
icon: 'times',
iconClassName: 'warning',
message: 'This is a custom toast',
title: 'My custom toast'
});
- Error Handling & Recovery is done through a custom toast that prompts the user to take appropriate action.
Methods
- Sending Transactions
In this way, all transactions are sent simultaneously. There is no limit to the number of transactions contained in the array.
const transactionManager = TransactionManager.getInstance();
const parallelTransactions: SigendTransactionType[] = [tx1, tx2, tx3, tx4];
const sentTransactions = await transactionManager.send(parallelTransactions);- Sending Batch Transactions
In this sequential case, each batch waits for the previous one to complete.
const transactionManager = TransactionManager.getInstance();
const batchTransactions: SignedTransactionType[][] = [
[tx1, tx2],
[tx3, tx4]
];
const sentTransactions = await transactionManager.send(batchTransactions);- Tracking Transactions
The basic option is to use the built-in tracking, which displays toast notifications with default messages.
const sessionId = await transactionManager.track(
sentTransactions
// { disableToasts: true } optionally disable toast notifications
);If you want to provide more human-friendly messages to your users, you can enable tracking with custom toast messages:
const sessionId = await transactionManager.track(sentTransactions, {
transactionsDisplayInfo: {
errorMessage: 'Failed adding stake',
successMessage: 'Stake successfully added',
processingMessage: 'Staking in progress'
}
});Tracking transactions without being logged in
If your application needs to track transactions sent by a server and the user does not need to login to see the outcome of these transactions, there are several steps that you need to do to enable this process.
Step 1. Enabling the tracking mechanism
By default the tracking mechanism is enabled only after the user logs in. That is the moment when the WebSocket connection is established. If you want to enable tracking before the user logs in, you need to call the trackTransactions method from the core/methods/trackTransactions folder. This method will enable a polling mechanism.
import { trackTransactions } from '@terradharitri/sdk-dapp-core/out/core/methods/trackTransactions/trackTransactions';
initApp(config).then(async () => {
await trackTransactions(); // enable here since by default tracking will be enabled only after login
render(() => <App />, root!);
});Then, you can track transactions by calling the track method from the TransactionManager class with a plain transaction containing the transaction hash.
import { Transaction, TransactionsConverter } from '@terradharitri/sdk-core/out';
const tManager = TransactionManager.getInstance();
const txConverter = new TransactionsConverter();
const transaction = txConverter.plainObjectToTransaction(signedTx);
const hash = transaction.getHash().toString(); // get the transaction hash
const plainTransaction = { ...transaction.toPlainObject(), hash };
await tManager.track([plainTransaction]);Advanced Usage
If you need to check the status of the signed transactions, you can query the store direclty using the sessionId returned by the track() method.
import { getStore } from '@terradharitri/sdk-dapp-core/out/store/store';
import { transactionsSliceSelector } from '@terradharitri/sdk-dapp-core/out/store/selectors/transactionsSelector';
const state = transactionsSliceSelector(getStore());
Object.entries(state).forEach(([sessionKey, data]) => {
if (sessionKey === sessionId) {
console.log(data.status);
}
});5. UI Components
sdk-dapp-core needs to make use of visual elements for allowing the user to interact with some providers (like the ledger), or to display messages to the user (like idle states or toasts). These visual elements consitst of webcomponents hosted in the @terradharitri/sdk-dapp-core-ui package. Thus, sdk-dapp-core does not hold any UI elements, just business logic that controls external components. We can consider two types of UI components: internal and external. They are differentiated by the way they are controlled: private components are controlled by sdk-dapp-core's signing or logging in flows, while public components can be controlled by the dApp.
Public components
Private components
The way private components are controlled are trough a pub-sub pattern called EventBus. Each webcomponent has a method of exposing its EventBus, thus allowing sdk-dapp-core to get a reference to it and use it for communication.
flowchart LR
A["Controller"] <--> B["Event Bus"] <--> C["webcomponent"]const modalElement = await createUIElement<LedgerConnectModal>(
'ledger-connect-panel'
);
const eventBus = await modalElement.getEventBus();
eventBus.publish('TRANSACTION_TOAST_DATA_UPDATE', someData);If you want to override private components and create your own, you can implement a similar strategy, of course by respecting each webcomponent's API (see an interface example here).
Debugging your dApp
The recommended way to debug your application is by using lerna. Make sure you have the same package version in sdk-daap-core's package.json and in your project's package.json.
If you preffer to use npm link, make sure to use the preserveSymlinks option in the server configuration:
resolve: {
preserveSymlinks: true, // 👈
alias: {
src: "/src",
},
},To build the library, run:
npm run buildTo run the unit tests, run:
npm test