App Ids are core to the dev experience on Avail DA, and we highly recommend you understand how they work before you start working with them.
You can check out our docs for the same.
Fetch an AppID from Avail DA
On-chain name of method: dataAvailability_appKeys
Parameters
avail-js
| parameter | type | optional | description | 
|---|---|---|---|
| key | string | true | The app_idassociated with this key will be fetched | 
Return value
On failure, a reason for failure is returned. On sucess, the returned JSON object contains:
- Owner: The owner of the- app_id
- appID: The numerical index of the- app_id
Minimal example (Fetch a particular app_id)
- 
You will need to set up the dev environment required to run this example. For instructions, check out our docs here. 
- 
If you’re sending an extrinsic (i.e conducting a transaction) you will need to replace the demo seed phrase with your own seed phrase. The rest of the code should work as is. 
avail-js
- Inside your-file-name.ts, add the following code:
avail-js
import { SDK, Pallets } from 'avail-js-sdk';
 
export async function getAppIdByKey() {
    // Initialize SDK with Turing endpoint
    const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
    
    // Get the current block hash
    const blockHash = await sdk.client.bestBlockHash();
    console.log(`Current block hash: ${blockHash}`);
    
    // Get storage at the current block
    const storageAt = await sdk.client.storageAt(blockHash);
    
    // The key to look up
    const appKey = "my Application Key Name!!!";
    
    // Fetch the app keys entry for this key
    const entry = await Pallets.DataAvailabilityStorage.AppKeys.fetch(storageAt, appKey);
    
    if (entry === null) {
        console.log(`No app ID found for key: "${appKey}"`);
    } else {
        console.log(`App ID: ${entry.value.appId}`);
        console.log(`Owner: ${entry.value.owner.toSS58()}`);
        console.log(`Key: ${appKey}`);
        console.log("App Key fetched successfully");
    }
    
    process.exit(0);
}
 
// Execute the function
getAppIdByKey();- Run the code using:
terminal
ts-node your-file-name.tsAnother example (Fetch all available app_ids registered on Avail DA)
- Think of the dataAvailability_appKeysas a method that returns all theapp_idsregistered on Avail DA as a mapping of their names to their owner and index.
- In most cases a dev will be interested in fetching only a particular app_idand not all of them.
- We are however including both scenarios here.
avail-js
- Inside your-file-name.ts, add the following code:
avail-js
import { SDK, Pallets } from 'avail-js-sdk';
 
export async function getAllAppIds() {
    // Initialize SDK with Turing endpoint
    const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
    
    // Get the current block hash
    const blockHash = await sdk.client.bestBlockHash();
    console.log(`Current block hash: ${blockHash}`);
    
    // Get storage at the current block
    const storageAt = await sdk.client.storageAt(blockHash);
    
    // Fetch all app keys entries
    const entries = await Pallets.DataAvailabilityStorage.AppKeys.fetchAll(storageAt);
    
    console.log(`Total number of app IDs: ${entries.length}`);
    
    // Display all app IDs and their details
    entries.forEach((entry) => {
        console.log(`  App ID: ${entry.value.appId}`);
        console.log(`  Owner: ${entry.value.owner.toSS58()}`);
        
        let keyString = "";
        try {
            keyString = new TextDecoder().decode(entry.key);
            console.log(`  Key (as string): ${keyString}`);
        } catch (e) {
            console.log(`  Key (hex): 0x${Buffer.from(entry.key).toString('hex')}`);
        }
    });
    
    process.exit(0);
}
 
// Execute the function
getAllAppIds();- Run the code using:
terminal
ts-node your-file-name.tsLast updated on