In this short tutorial you'll learn how a distributed App (dApp) can save and load settings directly to the Radix distributed ledger using our JavaScript library.
Tip: If you're new to our JavaScript library, we recommend reading this guide.
Loading and saving settings are fundamental activities in the workflow of any software application. When we are building a dApp, we can take advantage of the distributed ledger and store those settings directly on it. In this section we present basic JavaScript examples to Save and Load settings to the Radix ledger.
Before we start, your application needs to have an identity in the ledger. Let's assume that your dApp has an identity appIdentity
already available, and that we have the following settings to save:
const mySettings = {user: 'user@example.com',name: 'John Connor',verified: true,version: 12345}
Note: check our code examples articles to learn how to create identities.
To save the application settings to the ledger, you have to...
open a node connection to the network,
prepare the payload (appSettings
) by encoding mySettings
,
build a transaction using RadixTransactionBuilder
,
sign and submit the transaction with the appIdentity
identity.
Note: you can also subscribe to get transaction status updates and validate if settings were saved correctly.
// appIdentity is the application Identityconst myAccount = appIdentity.accountmyAccount.openNodeConnection()// Wait for the account to sync data from the ledgerconst applicationId = 'my-test-app.settings'const appSettings = JSON.stringify(mySettings)const transactionStatus = RadixTransactionBuilder.createPayloadAtom([myAccount], applicationId, appSettings).signAndSubmit(appIdentity)transactionStatus.subscribe({complete: () => { console.log('Settings saved successfully.') },error: error => { console.error('Error saving settings!', error) }})
As in the previous example, let's assume that your dApp has an identity appIdentity
already available. Loading settings from the ledger is simple and straight forward. To load the application settings from the ledger, you have to...
open a node connection to the network,
get the address of the application account
subscribe to the account data system to get all the messages signed by the application
myAccount.openNodeConnection()// Wait for the account to sync data from the ledgerconst myAppAddress = myAccount.getAddress()// Subscribe for all previous messages as well as new ones signed by a specific addressmyAccount.dataSystem.getApplicationData('my-test-app.settings', [myAppAddress]).subscribe(appData => { console.log(appData.data.payload)})
To wrap up this tutorial, we bring the saving and loading pieces together and present this example dApp that saves user settings to the ledger.
import {radixUniverse, RadixUniverse} from 'radixdlt'import {RadixIdentityManager, RadixTransactionBuilder} from 'radixdlt'radixUniverse.bootstrap(RadixUniverse.LOCALHOST_SINGLENODE)const identityManager = new RadixIdentityManager()const appIdentity = identityManager.generateSimpleIdentity()const myAccount = appIdentity.accountmyAccount.openNodeConnection()// Wait for the account to sync data from the ledgerconst applicationId = 'my-test-app.settings'const appSettings = JSON.stringify({user: 'user@example.com',name: 'John Connor',verified: true,version: 12345})const transactionStatus = RadixTransactionBuilder.createPayloadAtom([myAccount], applicationId, appSettings).signAndSubmit(appIdentity)transactionStatus.subscribe({complete: () => { console.log('Settings saved successfully.') },error: error => { console.error('Error saving settings!', error) }})const myAppAddress = myAccount.getAddress()// Subscribe for all previous messages as well as new ones signed by a specific addressmyAccount.dataSystem.getApplicationData('my-test-app.settings', [myAppAddress]).subscribe(appData => { console.log(appData.data.payload)})