Skip to content

Commit

Permalink
Added AppId, Nonce, Blocks and Hex String examples to cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Oct 30, 2024
1 parent 10b5d01 commit a6c33fb
Show file tree
Hide file tree
Showing 31 changed files with 888 additions and 351 deletions.
15 changes: 12 additions & 3 deletions avail-js/docs/cookbook/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# Summary

- [Overview](./overview.md)
- [Blocks](./blocks.md)
- [Block Hashes](./block_hashes.md)
- [Block Abstraction](./block_abstraction.md)
- [App Id](./appid/index.md)
- [Fetching](./appid/fetching.md)
- [Managing](./appid/managing.md)
- [Nonce](./nonce/index.md)
- [Fetching](./nonce/fetching.md)
- [Managing](./nonce/managing.md)
- [Blocks](./blocks/blocks.md)
- [Fetching](./blocks/fetching.md)
- [Managing](./blocks/managing.md)
- [Hex String | Block Hash | H256](./hex_string/index.md)
- [From Hex String to H256](./hex_string/to_h256.md)
- [From H256 to Hex String](./hex_string/from_h256.md)
- [Questions](./questions.md)
61 changes: 61 additions & 0 deletions avail-js/docs/cookbook/src/appid/fetching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Fetching App Id

#### Next App Id chain state query

```ts
{{#include ./fetching.ts:8:11}}
```

#### All App Ids in Use

```ts
{{#include ./fetching.ts:13:20}}
```

#### App Id from Transaction

```ts
{{#include ./fetching.ts:22:27}}
```

#### App Id from Account Instance Transaction

```ts
{{#include ./fetching.ts:29:34}}
```

#### All App Ids owned by an account via Account instance

```ts
{{#include ./fetching.ts:36:38}}
```

#### All App Ids owned by an account via free function

```ts
{{#include ./fetching.ts:40:42}}
```

#### All App Ids from a block via block instance

```ts
{{#include ./fetching.ts:44:46}}
```

#### All App Ids from a block via free function

```ts
{{#include ./fetching.ts:48:50}}
```

#### Manually from Generic Transaction

```ts
{{#include ./fetching.ts:52:55}}
```

## Source Code

```ts
{{#include ./fetching.ts}}
```
59 changes: 59 additions & 0 deletions avail-js/docs/cookbook/src/appid/fetching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { SDK, Keyring, Account, sdkUtil, Block, sdkBlock, WaitFor } from "./../../../../src/index"

const main = async () => {
const providerEndpoint = "ws://127.0.0.1:9944"
const sdk = await SDK.New(providerEndpoint)
const api = sdk.api

// Fetching the next app id via chain state query
const nextAppIdCodec = await api.query.dataAvailability.nextAppId()
const appId = parseInt(nextAppIdCodec.toString())
console.log(appId) // 10

// Fetching all app ids currently in use
const appKeys = await api.query.dataAvailability.appKeys.entries()
appKeys.forEach((entry: any) => {
if (entry[1].isSome) {
const { owner, id } = entry[1].unwrap()
console.log(parseInt(id.toString())) // 1, 2, 0, 4, 3, ...
}
})

// Fetching app id from transaction
const alice = new Keyring({ type: "sr25519" }).addFromUri("//Alice")
const data = "My Data"
const tx = await sdk.tx.dataAvailability.submitData(data, WaitFor.BlockInclusion, alice, { app_id: 1 })
if (tx.isErr) throw Error(tx.reason) // We expect that the call will succeed
console.log(tx.appId) // 1

// Fetching app id from Account instance transaction
const account = new Account(sdk, alice)
account.appId = 1
const tx2 = await account.submitData(data)
if (tx2.isErr) throw Error(tx2.reason) // We expect that the call will succeed
console.log(tx2.appId) // 1

// Fetching all app ids owned by an account via account instance
const appIds = await account.getAppKeys()
console.log(appIds) // [0, 1, 2, 3,...]

// Fetching all app ids owned by an account via free function
const appIds2 = await sdkUtil.getAppKeys(api, alice.address)
console.log(appIds2) // [0, 1, 2, 3,...]

// Fetching app ids from a block via block instance
const block = await Block.New(api, tx.blockHash)
block.submitDataAll().forEach((ds) => console.log(ds.appId)) // 1

// Fetching app ids from a block via free function
const signedBlock = await api.rpc.chain.getBlock(tx.blockHash)
sdkBlock.submitDataAll(signedBlock).forEach((ds) => console.log(ds.appId)) // 1

// Manually from generic transaction
block.transactionAll().forEach((tx) => console.log(sdkBlock.extractAppIdFromTx(tx))) // 0, 1, 0,...
sdkBlock.transactionAll(signedBlock).forEach((tx) => console.log(sdkBlock.extractAppIdFromTx(tx))) // 0, 1, 0,...
signedBlock.block.extrinsics.forEach((tx) => console.log(sdkBlock.extractAppIdFromTx(tx))) // 0, 1, 0,...

process.exit()
}
main()
1 change: 1 addition & 0 deletions avail-js/docs/cookbook/src/appid/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# App Id
35 changes: 35 additions & 0 deletions avail-js/docs/cookbook/src/appid/managing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Managing App Id

## Using App Id in Transactions

To utilize a nonce in a transaction, it should be included in the last argument of the transaction call.

```ts
{{#include ./managing.ts:10:13}}
```

The `Account` instance provides an interface to set the nonce for all subsequent calls.

```ts
{{#include ./managing.ts:15:20}}
```

## Creating App Id

#### Creating App Id via SDK

```ts
{{#include ./managing.ts:22:25}}
```

#### Creating App Id via Account instance

```ts
{{#include ./managing.ts:27:31}}
```

## Source Code

```ts
{{#include ./managing.ts}}
```
35 changes: 35 additions & 0 deletions avail-js/docs/cookbook/src/appid/managing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SDK, Keyring, Account, WaitFor } from "./../../../../src/index"

const main = async () => {
const providerEndpoint = "ws://127.0.0.1:9944"
const sdk = await SDK.New(providerEndpoint)
const alice = new Keyring({ type: "sr25519" }).addFromUri("//Alice")
const data = "My Data"
const appId = 1

// AppId can be passed as part of transaction options instance
const tx = await sdk.tx.dataAvailability.submitData(data, WaitFor.BlockInclusion, alice, { app_id: appId })
if (tx.isErr == true) throw Error() // We expect that the call will succeed
console.log(tx.appId) // 1

// Account instance can be set to use a specific appId
const account = new Account(sdk, alice)
account.setAppId(appId)
const tx2 = await account.submitData(data)
if (tx2.isErr == true) throw Error() // We expect that the call will succeed
console.log(tx.appId) // 1

// Creating App Id via SDK
const tx3 = await sdk.tx.dataAvailability.createApplicationKey("My New Key 1", WaitFor.BlockInclusion, alice)
if (tx3.isErr) throw Error(tx3.reason)
console.log("Generated App Id: " + tx3.event.id) // Generated App Id: 10

// Creating App Id via Account instance
account.setAppId(null)
const tx4 = await account.createApplicationKey("My New Key 2")
if (tx4.isErr) throw Error(tx4.reason)
console.log("Generated App Id: " + tx4.event.id) // Generated App Id: 11

process.exit()
}
main()
93 changes: 0 additions & 93 deletions avail-js/docs/cookbook/src/block_abstraction.md

This file was deleted.

4 changes: 0 additions & 4 deletions avail-js/docs/cookbook/src/blocks.md

This file was deleted.

1 change: 1 addition & 0 deletions avail-js/docs/cookbook/src/blocks/blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Blocks
37 changes: 37 additions & 0 deletions avail-js/docs/cookbook/src/blocks/fetching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Fetching Blocks

#### Fetching block via RPC call

```ts
{{#include ./fetching.ts:9:11}}
```

#### Fetching finalized block (or any specific block)

```ts
{{#include ./fetching.ts:13:16}}
```

#### Fetching block that contains our transaction via SDK

```ts
{{#include ./fetching.ts:18:23}}
```

#### Fetching block that contains our transaction via Account instance

```ts
{{#include ./fetching.ts:25:30}}
```

#### Fetching block that contains our transaction via Account instance

```ts
{{#include ./fetching.ts:32:34}}
```

## Source Code

```ts
{{#include ./fetching.ts}}
```
38 changes: 38 additions & 0 deletions avail-js/docs/cookbook/src/blocks/fetching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SDK, Keyring, Account, Block, WaitFor } from "./../../../../src/index"

const main = async () => {
const providerEndpoint = "ws://127.0.0.1:9944"
const sdk = await SDK.New(providerEndpoint)
const api = sdk.api
const alice = new Keyring({ type: "sr25519" }).addFromUri("//Alice")

// Fetching latest block via rpc call
const block = await api.rpc.chain.getBlock()
console.log(block.block.header.hash.toHex()) // `0xce61bfcb24ae953ec22d810520eb9b0c9d093507621bea74fc759fc981df5dbd`

// Fetching finalized block (or any specific block)
const finalizedHash = await api.rpc.chain.getFinalizedHead()
const block2 = await api.rpc.chain.getBlock(finalizedHash)
console.log(block2.block.header.hash.toHex()) // `0x3d3983fb1d931fb8ec17a623d331fe6a4310f0761c8d6c91da048c1a8eb7aa9f`

// Fetching block that contains our transaction via SDK
const data = "MyData"
const tx = await sdk.tx.dataAvailability.submitData(data, WaitFor.BlockInclusion, alice)
if (tx.isErr) throw Error(tx.reason) // We expect that the call will succeed
const block3 = await api.rpc.chain.getBlock(tx.blockHash)
console.log(block3.block.header.hash.toHex()) // `0xb41d3d37ac7449954956d0a7c5f607a0e10b1a30e280e5a05500b10ded5501ce`

// Fetching block that contains our transaction via Account instance
const account = new Account(sdk, alice)
const tx2 = await account.submitData(data)
if (tx2.isErr) throw Error(tx2.reason) // We expect that the call will succeed
const block4 = await api.rpc.chain.getBlock(tx2.blockHash) // `0xdf8413c48952204bab1c81371d58c1ef17da2fd1680d6d94a0ffab9492b58519`
console.log(block4.block.header.hash.toHex())

// Fetching block that contains our transaction via Block instance
const block5 = await Block.New(api, tx2.blockHash)
console.log(block5.signedBlock.block.header.hash.toHex()) // `0xdf8413c48952204bab1c81371d58c1ef17da2fd1680d6d94a0ffab9492b58519`

process.exit()
}
main()
File renamed without changes.
Loading

0 comments on commit a6c33fb

Please sign in to comment.