Submit new data to Avail DA
On-chain name of extrinsic: dataAvailability_submitData
Parameters
avail-js
| parameter | type | optional | description | 
|---|---|---|---|
| data | string | false | data to be submitted | 
| waitFor | WaitFor | false | wait for block inclusion or finalization | 
| account | KeyringPair | false | account that will send and sign the transaction | 
| options | SignerOptions | true | used to overwrite existing signer options | 
Returns
On failure, a reason of failure is returned. On Success, DataSubmitted event, transaction data, transaction hash and block hash is returned.
Submit data using a specific 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. 
PLEASE NOTE
- 
You can submit data to Avail DA using a specific app_idthat you created. If you don’t know how, read more in our docs here.
- 
You can submit data to ANY app_id, even if you didn’t create it. This however does not contitute an attack vector since the rollups building on top of Avail DA can always filter out DA submissions.
avail-js
- Inside your-file-name.ts, paste the following code:
import * as dotenv from 'dotenv';
import { Account, SDK, Pallets } from 'avail-js-sdk';
 
dotenv.config();
 
export async function submitData() {
    // Initialize SDK with Turing endpoint
    const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
    
    // Create account from seed in .env file
    const seed = process.env.SEED;
    if (!seed) {
      throw new Error("SEED environment variable is not set");
    }
    
    // Create account from seed
    const account = Account.new(seed);
    console.log("Account Address: ", account.address);
 
    // Replace with your own AppID
    const appId = 89;
    console.log(`Submitting data to App Id: ${appId}`);
    
    // Create data submission transaction
    const data = "My Data Submission";
    const tx = sdk.tx.dataAvailability.submitData(data);
    console.log("Submitting transaction with data...");
    
    // Execute and wait for inclusion with app_id
    const res = await tx.executeWaitForInclusion(account, { app_id: appId });
    
    // Check if transaction was successful
    const isOk = res.isSuccessful();
    if (isOk === undefined) {
      throw new Error("Cannot check if transaction was successful");
    }
    else if (!isOk) {
        throw new Error("Transaction failed");
    }
 
    // Extract event data
    if (res.events === undefined) throw new Error("No events found");
 
    // Transaction Details
    console.log(
      `Block Hash: ${res.blockHash}, Block Number: ${res.blockNumber}, Tx Hash: ${res.txHash}, Tx Index: ${res.txIndex}`
    );
 
    // Find DataSubmitted event
    const event = res.events.findFirst(Pallets.DataAvailabilityEvents.DataSubmitted);
    if (event === undefined) throw new Error("DataSubmitted event not found");
    
    console.log(`Data submitted successfully:`);
    console.log(`Who: ${event.who}`);
    console.log(`DataHash: ${event.dataHash}`);
    
    console.log("Data submission completed successfully");
    process.exit(0);
}
 
// Execute the function
submitData();- Run the code using:
ts-node your-file-name.tsThis will lead to your data being submitted through a specific
app_id, which you can verify by looking up your transaction on the Avail explorer .