|
| 1 | +--- |
| 2 | +id: interacting-with-Avail |
| 3 | +title: How to interact with Avail |
| 4 | +sidebar_label: Interact |
| 5 | +description: 'Discover how to interact with Avail as a data availability layer.' |
| 6 | +keywords: |
| 7 | + - documentation |
| 8 | + - avail |
| 9 | + - develop |
| 10 | + - build |
| 11 | + - data availability |
| 12 | + - da |
| 13 | +image: https://docs.availproject.org/img/avail/AvailDocs.png |
| 14 | +--- |
| 15 | +import { Callout } from 'nextra/components' |
| 16 | + |
| 17 | +# How to get started interacting with Avail |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +Interaction with Avail Network is the most important thing that developers can start doing to get started in this great journey of modularism. There are multiple ways to start interacting and in this tutorial, we will check out all of them. We will be running a Turing testnet and will be ineracting with tools like Avail-js, Avail-Subxt and similar. |
| 22 | + |
| 23 | +First thing to do is to run an Avail Node. To learn more on how to run a node, check out the documentation [<ins>here</ins>](/docs/operate-a-node/run-a-full-node/full-node). |
| 24 | + |
| 25 | +## Generating an AppId |
| 26 | + |
| 27 | +### What is an AppId? |
| 28 | + |
| 29 | +As a general-purpose base layer, Avail is designed to support many modular chains at the same time, providing consensus and data availability to all of them simultaneously. Avail headers contain an index that allows a given modular chain (or "application" in Avail terminology) to determine and download only the sections of a block that have data for that particular application. |
| 30 | + |
| 31 | +### How to generate a new AppId |
| 32 | + |
| 33 | +1. **Access the Website**: Open your web browser and go to the [<ins>Generator web application</ins>](https://app-id-gen.vercel.app/). |
| 34 | + |
| 35 | +1. **Account Detection / Selection**: |
| 36 | + - The website will automatically detect any accounts linked via browser extensions. |
| 37 | + - Ensure you have the relevant extension installed and are logged in. |
| 38 | + - Select the account you wish to use for the AppId creation. |
| 39 | + |
| 40 | +1. **Input Application Name**: In the provided field, enter the name of your application. Make sure the name is unique and identifies your app. |
| 41 | + |
| 42 | +1. **Send Transaction**: Submit the transaction after entering the application name. This will involve a confirmation step through your browser extension. |
| 43 | + |
| 44 | +1. **Receive Your Application Id:** |
| 45 | + - Upon successful transaction completion, your AppId will be displayed. |
| 46 | + - Note down the Id for future reference. |
| 47 | + |
| 48 | +For more information on AppId, check out the documentation [<ins>here</ins>](/docs/end-user-guide/app-id). |
| 49 | + |
| 50 | +## Tools and Libraries to interact with Avail |
| 51 | + |
| 52 | +There are multiple tools and libraries that can be used to interact with Avail. We will cover each one of them in depth here. |
| 53 | + |
| 54 | +#### Avail-js |
| 55 | + |
| 56 | +Avail-js is a JavaScript library that can be used to interact with Avail. It is a wrapper around the Substrate API and provides a simple interface to interact with Avail. To learn more about Avail-js from the github repo, checkout the repository [<ins>here</ins>](https://github.com/availproject/avail/tree/main/avail-js). We will get into some quick examples and tutorials on how to use Avail-js here. |
| 57 | + |
| 58 | +Pre-requisites for Avail-js: |
| 59 | +[Node.js](https://nodejs.org/en/download/) |
| 60 | + |
| 61 | +Now, you can install the latest stable version of the avail-js library by running the following command: |
| 62 | + |
| 63 | +```bash |
| 64 | +npm install avail-js-sdk |
| 65 | +``` |
| 66 | + |
| 67 | +##### Examples |
| 68 | + |
| 69 | +There are a lot of examples already in the repository. We will be looking at how some of these work. To do so, we will be running a Turing testnet and will be interacting with the network using Avail-js. You can choose to run a local node as well if you wish. |
| 70 | + |
| 71 | +Once you fully run the node, you also need to populate `config.ts` file with seed and endpoint details. You can choose to change other things as well, but we will be focusing on these two for now. |
| 72 | + |
| 73 | +###### How to connect |
| 74 | + |
| 75 | +For the starters, we will run `connect.ts`. This is also located in the [examples](https://github.com/availproject/avail/blob/main/avail-js/examples/node-examples/src/connect.ts) folder. <br /> |
| 76 | + |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { initialize } from "avail-js-sdk" // Global import |
| 80 | +import { isConnected, disconnect } from "avail-js-sdk/chain" // Modular import |
| 81 | +import config from "../../config" |
| 82 | + |
| 83 | +/** |
| 84 | + * Example to connect to a chain and get the ApiPromise. |
| 85 | + */ |
| 86 | +const main = async () => { |
| 87 | + const api = await initialize(config.endpoint) |
| 88 | + const [chain, nodeName, nodeVersion] = await Promise.all([ |
| 89 | + api.rpc.system.chain(), |
| 90 | + api.rpc.system.name(), |
| 91 | + api.rpc.system.version(), |
| 92 | + ]) |
| 93 | + |
| 94 | + console.log( |
| 95 | + `Connected to chain ${chain} using ${nodeName} and node version ${nodeVersion} - is connected: ${isConnected()}`, |
| 96 | + ) |
| 97 | + await disconnect() |
| 98 | + process.exit(0) |
| 99 | +} |
| 100 | +main() |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +###### How to transfer funds |
| 105 | + |
| 106 | +To transfer funds, you can use the following code in TypeScript. Please make sure that the funds that you are sending is specified in the `config.ts` file. |
| 107 | + |
| 108 | +```typescript |
| 109 | +import { getDecimals, initialize, formatNumberToBalance, getKeyringFromSeed, isValidAddress } from "avail-js-sdk" |
| 110 | +import { ISubmittableResult } from "@polkadot/types/types/extrinsic" |
| 111 | +import { H256 } from "@polkadot/types/interfaces/runtime" |
| 112 | + |
| 113 | +import config from "../../config" |
| 114 | + |
| 115 | +const main = async () => { |
| 116 | + try { |
| 117 | + if (!isValidAddress(config.recipient)) throw new Error("Invalid Recipient") |
| 118 | + |
| 119 | + const api = await initialize(config.endpoint) |
| 120 | + const keyring = getKeyringFromSeed(config.seed) |
| 121 | + const options = { app_id: 0, nonce: -1 } |
| 122 | + const decimals = getDecimals(api) |
| 123 | + const amount = formatNumberToBalance(config.amount, decimals) |
| 124 | + |
| 125 | + const oldBalance: any = await api.query.system.account(config.recipient) |
| 126 | + console.log(`Balance before the transfer call: ${oldBalance["data"]["free"].toHuman()}`) |
| 127 | + |
| 128 | + // Transaction call |
| 129 | + const txResult = await new Promise<ISubmittableResult>((res) => { |
| 130 | + api.tx.balances |
| 131 | + .transferKeepAlive(config.recipient, amount) |
| 132 | + .signAndSend(keyring, options, (result: ISubmittableResult) => { |
| 133 | + console.log(`Tx status: ${result.status}`) |
| 134 | + if (result.isFinalized || result.isError) { |
| 135 | + res(result) |
| 136 | + } |
| 137 | + }) |
| 138 | + }) |
| 139 | + console.log(`Tx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}`) |
| 140 | + |
| 141 | + // Error handling |
| 142 | + const error = txResult.dispatchError |
| 143 | + if (txResult.isError) { |
| 144 | + console.log(`Transaction was not executed`) |
| 145 | + } else if (error != undefined) { |
| 146 | + if (error.isModule) { |
| 147 | + const decoded = api.registry.findMetaError(error.asModule) |
| 148 | + const { docs, name, section } = decoded |
| 149 | + console.log(`${section}.${name}: ${docs.join(" ")}`) |
| 150 | + } else { |
| 151 | + console.log(error.toString()) |
| 152 | + } |
| 153 | + process.exit(1) |
| 154 | + } |
| 155 | + |
| 156 | + const newBalance: any = await api.query.system.account(config.recipient) |
| 157 | + console.log(`Balance after the transfer call: ${newBalance["data"]["free"].toHuman()}`) |
| 158 | + |
| 159 | + process.exit(0) |
| 160 | + } catch (err) { |
| 161 | + console.error(err) |
| 162 | + process.exit(1) |
| 163 | + } |
| 164 | +} |
| 165 | +main() |
| 166 | +``` |
| 167 | + |
| 168 | +These two examples are just the starters on what can be achieved by using avail-js. There are a lot of other [examples](https://github.com/availproject/avail/tree/main/avail-js/examples/node-examples) in the repository that you can check out. |
| 169 | + |
| 170 | + |
| 171 | +#### Avail-SubXt |
| 172 | + |
| 173 | +SubXt is a library for interacting with Substrate based nodes in Rust and WebAssembly. We have built Avail-SubXt to interact with Avail nodes. To learn more about Avail-SubXt from the github repo, checkout the repository [<ins>here</ins>](https://github.com/availproject/avail/tree/main/avail-subxt). We will get into some quick examples and tutorials on how to use Avail-SubXt here. |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +import { sortAndDeduplicateDiagnostics } from "typescript" |
| 179 | + |
0 commit comments