diff --git a/avail-js/docs/advanced_examples/block_abstraction.ts b/avail-js/docs/advanced_examples/block_abstraction.ts deleted file mode 100644 index d792d02aa..000000000 --- a/avail-js/docs/advanced_examples/block_abstraction.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { SDK, Block, sdkBlock, Keyring, Account } from "./../../src/index" - -const main = async () => { - const providerEndpoint = "ws://127.0.0.1:9944" - const sdk = await SDK.New(providerEndpoint) - const account = new Account(sdk, new Keyring({ type: "sr25519" }).addFromUri("//Alice")) - - // Submitting data that we will query later - const tx = await account.submitData("aabbcc") - if (tx.isErr) { - process.exit(0) - } - const { blockHash, txHash, txIndex } = tx - console.log("Reference hex value: " + tx.txData.data) - - /* - Data Submissions from a block can be fetched in two ways: - - Using the Block object - - Using helper functions - */ - - /* - The block abstraction object has helper functions to get data from - submit data transactions. - */ - const block = await Block.New(sdk.api, blockHash) - const r1 = block.getSubmitDataAll() - console.log(r1) - const r2 = block.getSubmitDataHash(txHash) - if (r2.isOk()) { - console.log(r2.value) - } - const r3 = block.getSubmitDataIndex(txIndex) - if (r3.isOk()) { - console.log(r3.value) - } - - /// The Signed block can be accessed if necessary. - const _signedBlock = block.signedBlock - - /* - The same interface can be found as free functions inside - sdkBlock namespace. - - Block can as well be instated if `signedBlock` is available. - const _block = new Block(signedBlock) - */ - const signedBlock = await sdk.api.rpc.chain.getBlock(blockHash) - const r4 = sdkBlock.getSubmitDataAll(signedBlock) - console.log(r4) - const r5 = sdkBlock.getSubmitDataHash(signedBlock, txHash) - if (r5.isOk()) { - console.log(r5.value) - } - const r6 = sdkBlock.getSubmitDataIndex(signedBlock, txIndex) - if (r6.isErr()) { - process.exit(1) - } - const dataSubmission = r6.value - - /* - DataSubmission structure: - { - "txHash": "0x2965fa2a2df6f818f0725be2b92b0dd399f1cf1e1620e1477fcc2b3e9ba9f491", - "txIndex": 1, - "hexData": "616162626363" - "txSigner": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", - } - */ - console.log("Tx Hash: " + dataSubmission.txHash.toString()) - console.log("Tx Index: " + dataSubmission.txIndex) - console.log("Hex data: " + dataSubmission.hexData) - console.log("Tx Signer: " + dataSubmission.txSigner) - - /* - There is a helper function to get the Ascii version of the hex data - */ - console.log("Ascii data: " + dataSubmission.toAscii()) - - process.exit() -} -main() diff --git a/avail-js/docs/advanced_examples/multisig.ts b/avail-js/docs/advanced_examples/multisig.ts index 214e2ccb4..d6c5a49ac 100644 --- a/avail-js/docs/advanced_examples/multisig.ts +++ b/avail-js/docs/advanced_examples/multisig.ts @@ -56,7 +56,7 @@ const main = async () => { async function fundMultisigAccount(sdk: SDK, alice: KeyringPair, multisigAddress: string): Promise { console.log("Funding multisig account...") - const amount = new BN(10).pow(new BN(18)).mul(new BN(100)) // 100 Avail + const amount = SDK.oneAvail().mul(new BN(100)) // 100 Avail const result = await sdk.tx.balances.transferKeepAlive(multisigAddress, amount, WaitFor.BlockInclusion, alice) if (result.isErr) { console.log(result.reason) diff --git a/avail-js/docs/cookbook/.gitignore b/avail-js/docs/cookbook/.gitignore new file mode 100644 index 000000000..7585238ef --- /dev/null +++ b/avail-js/docs/cookbook/.gitignore @@ -0,0 +1 @@ +book diff --git a/avail-js/docs/cookbook/book.toml b/avail-js/docs/cookbook/book.toml new file mode 100644 index 000000000..9a7c6ea7d --- /dev/null +++ b/avail-js/docs/cookbook/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["Marko Petrlic"] +language = "en" +multilingual = false +src = "src" +title = "Avail JS SDK Cookbook" diff --git a/avail-js/docs/cookbook/src/SUMMARY.md b/avail-js/docs/cookbook/src/SUMMARY.md new file mode 100644 index 000000000..25321518c --- /dev/null +++ b/avail-js/docs/cookbook/src/SUMMARY.md @@ -0,0 +1,7 @@ +# Summary + +- [Overview](./overview.md) +- [Blocks](./blocks.md) + - [Block Hashes](./block_hashes.md) + - [Block Abstraction](./block_abstraction.md) +- [Questions](./questions.md) diff --git a/avail-js/docs/cookbook/src/block_abstraction.md b/avail-js/docs/cookbook/src/block_abstraction.md new file mode 100644 index 000000000..eb6a4da77 --- /dev/null +++ b/avail-js/docs/cookbook/src/block_abstraction.md @@ -0,0 +1,93 @@ +# Block Abstraction + +## Transactions + +### Via SDK + +```ts +{{#include ./code/block_abstraction.ts:16:19}} +``` + +```ts +{{#include ./code/block_abstraction.ts:23:25}} +``` + +```ts +{{#include ./code/block_abstraction.ts:27:34}} +``` + +```ts +{{#include ./code/block_abstraction.ts:36:38}} +``` + +```ts +{{#include ./code/block_abstraction.ts:40:42}} +``` + +### Via Free functions + +```ts +{{#include ./code/block_abstraction.ts:46:49}} +``` + +```ts +{{#include ./code/block_abstraction.ts:51:58}} +``` + +```ts +{{#include ./code/block_abstraction.ts:60:62}} +``` + +```ts +{{#include ./code/block_abstraction.ts:64:66}} +``` + +## Data Submissions + +### Via SDK + +```ts +{{#include ./code/block_abstraction.ts:70:72}} +``` + +```ts +{{#include ./code/block_abstraction.ts:74:81}} +``` + +```ts +{{#include ./code/block_abstraction.ts:83:90}} +``` + +```ts +{{#include ./code/block_abstraction.ts:92:94}} +``` + +### Via Free functions + +```ts +{{#include ./code/block_abstraction.ts:98:100}} +``` + +```ts +{{#include ./code/block_abstraction.ts:102:109}} +``` + +```ts +{{#include ./code/block_abstraction.ts:111:118}} +``` + +```ts +{{#include ./code/block_abstraction.ts:120:122}} +``` + +### Data Submission Structure + +```ts +{{#include ./code/block_abstraction.ts:124:144}} +``` + +## Full Example + +```ts +{{#include ./code/block_abstraction.ts}} +``` diff --git a/avail-js/docs/cookbook/src/block_hashes.md b/avail-js/docs/cookbook/src/block_hashes.md new file mode 100644 index 000000000..c28f90627 --- /dev/null +++ b/avail-js/docs/cookbook/src/block_hashes.md @@ -0,0 +1,65 @@ +# Block Hashes + +## Fetching Block Hash + +```ts +{{#include ./code/fetch_block_hash.ts:7:9}} +``` + +```ts +{{#include ./code/fetch_block_hash.ts:11:13}} +``` + +```ts +{{#include ./code/fetch_block_hash.ts:15:19}} +``` + +```ts +{{#include ./code/fetch_block_hash.ts:21:28}} +``` + +### Full Example + +```ts +{{#include ./code/fetch_block_hash.ts}} +``` + +## Conversion + +### From Hex String to H256 (BlockHash) + +```ts +{{#include ./code/block_hash_conversion.ts:7:15}} +``` + +```ts +{{#include ./code/block_hash_conversion.ts:17:19}} +``` + +```ts +{{#include ./code/block_hash_conversion.ts:21:28}} +``` + +```ts +{{#include ./code/block_hash_conversion.ts:30:32}} +``` + +```ts +{{#include ./code/block_hash_conversion.ts:39:44}} +``` + +```ts +{{#include ./code/block_hash_conversion.ts:46:51}} +``` + +### From H256 (BlockHash) to Hex String + +```ts +{{#include ./code/block_hash_conversion.ts:34:37}} +``` + +### Full Example + +```ts +{{#include ./code/block_hash_conversion.ts}} +``` diff --git a/avail-js/docs/cookbook/src/blocks.md b/avail-js/docs/cookbook/src/blocks.md new file mode 100644 index 000000000..cac3609bc --- /dev/null +++ b/avail-js/docs/cookbook/src/blocks.md @@ -0,0 +1,4 @@ +# Blocks + +Make sure that you have already read [Polkadot JS docs](https://polkadot.js.org/docs/api/cookbook/blocks) about Blocks. +Here we will cover only the additional abstraction that was added on top of existing polkadot js objects. diff --git a/avail-js/docs/cookbook/src/code/block_abstraction.ts b/avail-js/docs/cookbook/src/code/block_abstraction.ts new file mode 100644 index 000000000..251b9170b --- /dev/null +++ b/avail-js/docs/cookbook/src/code/block_abstraction.ts @@ -0,0 +1,148 @@ +import { SDK, Block, sdkBlock, Keyring, Account, sdkUtil } from "./../../../../src/index" + +const main = async () => { + const providerEndpoint = "ws://127.0.0.1:9944" + const sdk = await SDK.New(providerEndpoint) + + // Submitting data that we will query later + const alice = new Account(sdk, new Keyring({ type: "sr25519" }).addFromUri("//Alice")) + const tx = await alice.submitData("My Data") + if (tx.isErr) { + process.exit(0) + } + const { blockHash, txHash, txIndex } = tx + console.log("Reference hex value: " + tx.txData.data) + + // Instantiate Block object + const block = await Block.New(sdk.api, blockHash) + // Block is just an wrapper over a SignedBlock object. + const _signedBlock = block.signedBlock + + // Transaction via SDK + + // Getting transaction count + const txCount1 = block.transactionCount() + console.log("Tx Count: " + txCount1) + + // Getting transaction by transaction index + const maybeTx1 = block.transactionByIndex(txIndex) + if (maybeTx1.isErr()) { + console.log(maybeTx1.error) + process.exit(1) + } + const tx1 = maybeTx1.value // GenericExtrinsic + console.log(tx1.hash.toHex()) // `0x2fc9abb314fb77d3fce4bfa019ca1562c23b796c975da38f5779be262ecbd4a8` + + // Getting transaction by hash returns multiple results because transaction hashes are not unique + const txs2 = block.transactionByHash(txHash) // GenericExtrinsic[] + console.log(txs2.length) + + // Getting transaction by transaction signer(author) + const txs3 = block.transactionBySigner(alice.address()) // GenericExtrinsic[] + console.log(txs3.length) + + // Transaction via free functions + + // Getting transaction count + const signedBlock = await sdk.api.rpc.chain.getBlock() + const txCount2 = sdkBlock.transactionCount(signedBlock) + console.log("Tx Count: " + txCount2) + + // Getting transaction by transaction index + const maybeTx4 = sdkBlock.transactionByIndex(signedBlock, txIndex) + if (maybeTx4.isErr()) { + console.log(maybeTx4.error) + process.exit(1) + } + const tx4 = maybeTx4.value // GenericExtrinsic + console.log(tx4.hash.toHex()) // `0x2fc9abb314fb77d3fce4bfa019ca1562c23b796c975da38f5779be262ecbd4a8` + + // Getting transaction by hash returns multiple results because transaction hashes are not unique + const txs5 = sdkBlock.transactionByHash(signedBlock, txHash) // GenericExtrinsic[] + console.log(txs5.length) + + // Getting transaction by transaction signer(author) + const txs6 = sdkBlock.transactionBySigner(signedBlock, alice.address()) // GenericExtrinsic[] + console.log(txs6.length) + + // Data Submission via SDK + + // Getting all data submission transaction data + const dss1 = block.submitDataAll() // DataSubmission[] + console.log(dss1.length) + + // Getting data submission transaction data by transaction index + const maybeDs2 = block.submitDataByIndex(txIndex) + if (maybeDs2.isErr()) { + console.log(maybeDs2.error) + process.exit(1) + } + const ds2 = maybeDs2.value // DataSubmission + console.log(ds2.hexData) + + // Getting data submission transaction data by transaction hash. In most cases transaction hash for data submission transaction is unique inside a block + const maybeDs3 = block.submitDataByHash(txHash) + if (maybeDs3.isErr()) { + console.log(maybeDs3.error) + process.exit(1) + } + const ds3 = maybeDs3.value // DataSubmission + console.log(ds3.hexData) + + // Getting data submission transaction data by transaction signer(author) + const ds4 = block.submitDataBySigner(alice.address()) // DataSubmission[] + console.log(ds4.length) + + // Data Submission via free functions + + // Getting all data submission transaction data + const dss5 = sdkBlock.submitDataAll(signedBlock) // DataSubmission[] + console.log(dss5.length) + + // Getting data submission transaction data by transaction index + const maybeDs6 = sdkBlock.submitDataByIndex(signedBlock, txIndex) + if (maybeDs6.isErr()) { + console.log(maybeDs6.error) + process.exit(1) + } + const ds6 = maybeDs6.value // DataSubmission + console.log(ds6.hexData) + + // Getting data submission transaction data by transaction hash. In most cases transaction hash for data submission transaction is unique inside a block + const maybeDs7 = sdkBlock.submitDataByHash(signedBlock, txHash) + if (maybeDs7.isErr()) { + console.log(maybeDs7.error) + process.exit(1) + } + const ds7 = maybeDs7.value // DataSubmission + console.log(ds7.hexData) + + // Getting data submission transaction data by transaction signer(author) + const ds8 = sdkBlock.submitDataBySigner(signedBlock, alice.address()) // DataSubmission[] + console.log(ds8.length) + + const maybeDs = block.submitDataByIndex(txIndex) + if (maybeDs.isErr()) { + process.exit(1) + } + const dataSubmission = maybeDs.value // DataSubmission + /* + DataSubmission structure: + { + "txHash": "0x2965fa2a2df6f818f0725be2b92b0dd399f1cf1e1620e1477fcc2b3e9ba9f491", + "txIndex": 1, + "hexData": "616162626363" + "txSigner": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", + } + */ + console.log("Tx Hash: " + dataSubmission.txHash.toHex()) + console.log("Tx Index: " + dataSubmission.txIndex) + console.log("Hex data: " + dataSubmission.hexData) + console.log("Tx Signer: " + dataSubmission.txSigner) + + // There is a helper function to get the Ascii version of the hex data + console.log("Ascii data: " + dataSubmission.toAscii()) + + process.exit() +} +main() diff --git a/avail-js/docs/cookbook/src/code/block_hash_conversion.ts b/avail-js/docs/cookbook/src/code/block_hash_conversion.ts new file mode 100644 index 000000000..efd0d8fa9 --- /dev/null +++ b/avail-js/docs/cookbook/src/code/block_hash_conversion.ts @@ -0,0 +1,55 @@ +import { SDK, sdkUtil } from "./../../../../src/index" + +const main = async () => { + const providerEndpoint = "ws://127.0.0.1:9944" + const sdk = await SDK.New(providerEndpoint) + + // Converting hex string to H256 (BlockHash) using `hexStringToHash` interface. + const hexString = "0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3" + const maybeHex1 = sdk.util.hexStringToHash(hexString) + if (maybeHex1.isErr()) { + console.log("Error: " + maybeHex1.error) + process.exit(1) + } + const hex1 = maybeHex1.value + console.log(hex1.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // If we are sure that the hex string is valid. + const hex2 = sdk.util.hexStringToHashUnsafe(hexString) + console.log(hex2.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // Converting hex string to H256 (BlockHash) using `hexStringToHash` free function. + const maybeHex3 = sdkUtil.hexStringToHash(sdk.api, hexString) + if (maybeHex3.isErr()) { + console.log("Error: " + maybeHex3.error) + process.exit(1) + } + const hex3 = maybeHex3.value + console.log(hex3.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // If we are sure that the hex string is valid. + const hex4 = sdkUtil.hexStringToHashUnsafe(sdk.api, hexString) + console.log(hex4.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // Converting from H256 to Hex String + const hex = await sdk.api.rpc.chain.getFinalizedHead() + const hexString2 = hex.toHex() + console.log(hexString2) // `0xb410c0c0b5939567e5a558a4930ae030375894043c2dd5f3c35cea4133470f7f` + + // Error if the hex string doesn't start with 0x + const hexString3 = "4c70c21c8b43d38cdf822d14733151ad2bcf48f9fdfe0d868852c11c9affbc81" + const f1 = sdk.util.hexStringToHash(hexString3) + if (f1.isErr()) { + console.log("Error: " + f1.error) // `Failed to convert hex string to H256. Hash needs to start with 0x` + } + + // Error if the hex string length is not 64 (without `0x`) + const hexString4 = "0x4c70c21c8b43d38cdf822d1473315" + const f2 = sdk.util.hexStringToHash(hexString4) + if (f2.isErr()) { + console.log("Error: " + f2.error) // `Error: Failed to convert hex string to H256. Expected length 64 got 29` + } + + process.exit() +} +main() diff --git a/avail-js/docs/cookbook/src/code/fetch_block_hash.ts b/avail-js/docs/cookbook/src/code/fetch_block_hash.ts new file mode 100644 index 000000000..5f85fca96 --- /dev/null +++ b/avail-js/docs/cookbook/src/code/fetch_block_hash.ts @@ -0,0 +1,32 @@ +import { SDK, Keyring, Account } from "./../../../../src/index" + +const main = async () => { + const providerEndpoint = "ws://127.0.0.1:9944" + const sdk = await SDK.New(providerEndpoint) + + // Getting block hash for the latest (or specific) block + const hash1 = await sdk.api.rpc.chain.getBlockHash() + console.log(hash1.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // Getting block hash for the latest finalized block + const hash2 = await sdk.api.rpc.chain.getFinalizedHead() + console.log(hash2.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // Getting block hash from SignedBlock object + const signedBlock = await sdk.api.rpc.chain.getBlock() + const blockHashHexString = signedBlock.block.header.hash.toHex() + const hash3 = sdk.util.hexStringToHashUnsafe(blockHashHexString) + console.log(hash3.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + // Getting block hash from sdk transaction + const alice = new Account(sdk, new Keyring({ type: "sr25519" }).addFromUri("//Alice")) + const tx = await alice.submitData("My Data") // this is equal to `sdk.tx.dataAvailability.submitData("My Data", WaitFor.BlockInclusion, aliceKeyring, options)` + if (tx.isErr) { + process.exit(1) + } + const hash4 = tx.blockHash + console.log(hash4.toHex()) // `0x4a78c9fd1d88c99fc217eec0ac405307092e53523f6db19fae0242a5af9f4fe3` + + process.exit() +} +main() diff --git a/avail-js/docs/cookbook/src/overview.md b/avail-js/docs/cookbook/src/overview.md new file mode 100644 index 000000000..f3033ca4e --- /dev/null +++ b/avail-js/docs/cookbook/src/overview.md @@ -0,0 +1,3 @@ +# Overview + +SDK Version: 3.0.1 diff --git a/avail-js/docs/cookbook/src/questions.md b/avail-js/docs/cookbook/src/questions.md new file mode 100644 index 000000000..e69de29bb diff --git a/avail-js/docs/extrinsics/README.md b/avail-js/docs/extrinsics/README.md index 38de9127e..0865c4361 100644 --- a/avail-js/docs/extrinsics/README.md +++ b/avail-js/docs/extrinsics/README.md @@ -291,7 +291,7 @@ function setSubmitDataFeeModifier(modifier: DispatchFeeModifier, waitFor: WaitFo ### Minimal Example ```js -import { SDK, WaitFor, Keyring, BN, sdkTransactions } from "avail-js-sdk" +import { SDK, WaitFor, Keyring, sdkTransactions } from "avail-js-sdk" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -300,7 +300,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const modifier = { - weightMaximumFee: new BN("10").pow(new BN("18")), + weightMaximumFee: SDK.oneAvail(), weightFeeDivider: 20, } as sdkTransactions.DispatchFeeModifier @@ -382,7 +382,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const dest = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" // Eve - const amount = new BN(10).pow(new BN(18)) // one Avail + const amount = SDK.oneAvail() const result = await sdk.tx.balances.transferKeepAlive(dest, amount, WaitFor.BlockInclusion, account) if (result.isErr) { @@ -455,7 +455,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const dest = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" // Eve - const amount = new BN(10).pow(new BN(18)) // one Avail + const amount = SDK.oneAvail() const result = await sdk.tx.balances.transferAllowDeath(dest, amount, WaitFor.BlockInclusion, account) if (result.isErr) { @@ -677,7 +677,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice//stash") - const maxAdditional = new BN(10).pow(new BN(18)) // one Avail + const maxAdditional = SDK.oneAvail() const result = await sdk.tx.staking.bondExtra(maxAdditional, WaitFor.BlockInclusion, account) if (result.isErr) { @@ -946,7 +946,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const value = new BN(10).pow(new BN(18)) // one Avail + const value = SDK.oneAvail() const result = await sdk.tx.staking.unbond(value, WaitFor.BlockInclusion, account) if (result.isErr) { @@ -1097,7 +1097,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const root: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice const nominator: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice @@ -1180,7 +1180,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Bob") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const root: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice const nominator: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice @@ -1269,7 +1269,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Bob") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const poolId = 1 const result = await sdk.tx.nominationPools.join(amount, poolId, WaitFor.BlockInclusion, account) @@ -1411,7 +1411,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const bondExtra = { FreeBalance: amount } as BondExtra const result = await sdk.tx.nominationPools.bondExtra(bondExtra, WaitFor.BlockInclusion, account) @@ -1552,7 +1552,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const memberAccount = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice - const unbondingPoints = new BN(10).pow(new BN(18)).mul(new BN(100)) // 100 Avail + const unbondingPoints = SDK.oneAvail().mul(new BN(100)) // 100 Avail const result = await sdk.tx.nominationPools.unbond(memberAccount, unbondingPoints, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/docs/extrinsics/balances_tranfer_allow_death.ts b/avail-js/docs/extrinsics/balances_tranfer_allow_death.ts index 899175982..6ec5f63c3 100644 --- a/avail-js/docs/extrinsics/balances_tranfer_allow_death.ts +++ b/avail-js/docs/extrinsics/balances_tranfer_allow_death.ts @@ -1,4 +1,4 @@ -import { SDK, WaitFor, Keyring, BN } from "avail-js-sdk" +import { SDK, WaitFor, Keyring } from "avail-js-sdk" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -7,7 +7,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const dest = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" // Eve - const amount = new BN(10).pow(new BN(18)) // one Avail + const amount = SDK.oneAvail() const result = await sdk.tx.balances.transferAllowDeath(dest, amount, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/docs/extrinsics/balances_tranfer_keep_alive.ts b/avail-js/docs/extrinsics/balances_tranfer_keep_alive.ts index ef163cddb..c38782d1a 100644 --- a/avail-js/docs/extrinsics/balances_tranfer_keep_alive.ts +++ b/avail-js/docs/extrinsics/balances_tranfer_keep_alive.ts @@ -1,4 +1,4 @@ -import { SDK, WaitFor, Keyring, BN } from "avail-js-sdk" +import { SDK, WaitFor, Keyring } from "../../src/index" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -7,7 +7,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const dest = "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" // Eve - const amount = new BN(10).pow(new BN(18)) // one Avail + const amount = SDK.oneAvail() const result = await sdk.tx.balances.transferKeepAlive(dest, amount, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/docs/extrinsics/da_set_submit_data_fee_modifier.ts b/avail-js/docs/extrinsics/da_set_submit_data_fee_modifier.ts index f018c0623..1571acacc 100644 --- a/avail-js/docs/extrinsics/da_set_submit_data_fee_modifier.ts +++ b/avail-js/docs/extrinsics/da_set_submit_data_fee_modifier.ts @@ -1,4 +1,4 @@ -import { SDK, WaitFor, Keyring, BN, sdkTransactions } from "avail-js-sdk" +import { SDK, WaitFor, Keyring, sdkTransactions } from "../../src/index" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -7,7 +7,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const modifier = { - weightMaximumFee: new BN("10").pow(new BN("18")), + weightMaximumFee: SDK.oneAvail(), weightFeeDivider: 20, } as sdkTransactions.DispatchFeeModifier diff --git a/avail-js/docs/extrinsics/nomination_pools_bond_extra.ts b/avail-js/docs/extrinsics/nomination_pools_bond_extra.ts index 2fb5523d6..12859d84b 100644 --- a/avail-js/docs/extrinsics/nomination_pools_bond_extra.ts +++ b/avail-js/docs/extrinsics/nomination_pools_bond_extra.ts @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const bondExtra = { FreeBalance: amount } as BondExtra const result = await sdk.tx.nominationPools.bondExtra(bondExtra, WaitFor.BlockInclusion, account) diff --git a/avail-js/docs/extrinsics/nomination_pools_create.ts b/avail-js/docs/extrinsics/nomination_pools_create.ts index f1f2fe7c6..7b330d7d7 100644 --- a/avail-js/docs/extrinsics/nomination_pools_create.ts +++ b/avail-js/docs/extrinsics/nomination_pools_create.ts @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const root: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice const nominator: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice diff --git a/avail-js/docs/extrinsics/nomination_pools_create_with_pool_id.ts b/avail-js/docs/extrinsics/nomination_pools_create_with_pool_id.ts index d6953d5c2..889f8dcb8 100644 --- a/avail-js/docs/extrinsics/nomination_pools_create_with_pool_id.ts +++ b/avail-js/docs/extrinsics/nomination_pools_create_with_pool_id.ts @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Bob") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const root: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice const nominator: string = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice diff --git a/avail-js/docs/extrinsics/nomination_pools_join.ts b/avail-js/docs/extrinsics/nomination_pools_join.ts index a65c30d54..31284385b 100644 --- a/avail-js/docs/extrinsics/nomination_pools_join.ts +++ b/avail-js/docs/extrinsics/nomination_pools_join.ts @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Bob") - const amount = new BN(10).pow(new BN(18)).mul(new BN(10000)) // 10_000 Avail + const amount = SDK.oneAvail().mul(new BN(10000)) // 10_000 Avail const poolId = 1 const result = await sdk.tx.nominationPools.join(amount, poolId, WaitFor.BlockInclusion, account) diff --git a/avail-js/docs/extrinsics/nomination_pools_unbond.ts b/avail-js/docs/extrinsics/nomination_pools_unbond.ts index dcb501990..63790a49b 100644 --- a/avail-js/docs/extrinsics/nomination_pools_unbond.ts +++ b/avail-js/docs/extrinsics/nomination_pools_unbond.ts @@ -7,7 +7,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") const memberAccount = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" // Alice - const unbondingPoints = new BN(10).pow(new BN(18)).mul(new BN(100)) // 100 Avail + const unbondingPoints = SDK.oneAvail().mul(new BN(100)) // 100 Avail const result = await sdk.tx.nominationPools.unbond(memberAccount, unbondingPoints, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/docs/extrinsics/staking_bond_extra.ts b/avail-js/docs/extrinsics/staking_bond_extra.ts index 662329e1c..ec58e70b1 100644 --- a/avail-js/docs/extrinsics/staking_bond_extra.ts +++ b/avail-js/docs/extrinsics/staking_bond_extra.ts @@ -1,4 +1,4 @@ -import { SDK, WaitFor, Keyring, BN } from "avail-js-sdk" +import { SDK, WaitFor, Keyring } from "avail-js-sdk" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice//stash") - const maxAdditional = new BN(10).pow(new BN(18)) // one Avail + const maxAdditional = SDK.oneAvail() const result = await sdk.tx.staking.bondExtra(maxAdditional, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/docs/extrinsics/staking_chill.ts b/avail-js/docs/extrinsics/staking_chill.ts index 8a1a171da..e870663ae 100644 --- a/avail-js/docs/extrinsics/staking_chill.ts +++ b/avail-js/docs/extrinsics/staking_chill.ts @@ -1,4 +1,5 @@ import { SDK, WaitFor, Keyring } from "avail-js-sdk" + const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" const sdk = await SDK.New(providerEndpoint) diff --git a/avail-js/docs/extrinsics/staking_chill_other.ts b/avail-js/docs/extrinsics/staking_chill_other.ts index 0bedff0d8..f40dda161 100644 --- a/avail-js/docs/extrinsics/staking_chill_other.ts +++ b/avail-js/docs/extrinsics/staking_chill_other.ts @@ -1,4 +1,5 @@ import { SDK, WaitFor, Keyring } from "avail-js-sdk" + const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" const sdk = await SDK.New(providerEndpoint) diff --git a/avail-js/docs/extrinsics/staking_unbond.ts b/avail-js/docs/extrinsics/staking_unbond.ts index 700f24b8c..9e2ae405d 100644 --- a/avail-js/docs/extrinsics/staking_unbond.ts +++ b/avail-js/docs/extrinsics/staking_unbond.ts @@ -1,4 +1,4 @@ -import { SDK, WaitFor, Keyring, BN } from "avail-js-sdk" +import { SDK, WaitFor, Keyring } from "avail-js-sdk" const main = async () => { const providerEndpoint = "ws://127.0.0.1:9944" @@ -6,7 +6,7 @@ const main = async () => { // Input const account = new Keyring({ type: "sr25519" }).addFromUri("//Alice") - const value = new BN(10).pow(new BN(18)) // one Avail + const value = SDK.oneAvail() const result = await sdk.tx.staking.unbond(value, WaitFor.BlockInclusion, account) if (result.isErr) { diff --git a/avail-js/src/sdk/account.ts b/avail-js/src/sdk/account.ts index 752f86d26..ab22b89f5 100644 --- a/avail-js/src/sdk/account.ts +++ b/avail-js/src/sdk/account.ts @@ -36,6 +36,10 @@ export class Account { this.waitFor = value } + address(): string { + return this.keyring.address + } + async balanceTransfer(dest: string, value: BN): Promise { const options = this.buildOptions() return await this.sdk.tx.balances.transferKeepAlive(dest, value, this.waitFor, this.keyring, options) diff --git a/avail-js/src/sdk/block.ts b/avail-js/src/sdk/block.ts index a0077e3b2..b3c2c00f3 100644 --- a/avail-js/src/sdk/block.ts +++ b/avail-js/src/sdk/block.ts @@ -16,24 +16,44 @@ export class Block { this.signedBlock = block } - getSubmitDataAll(): DataSubmission[] { - return getSubmitDataAll(this.signedBlock) + submitDataAll(): DataSubmission[] { + return submitDataAll(this.signedBlock) } - getSubmitDataHash(txHash: H256): Result { - return getSubmitDataHash(this.signedBlock, txHash) + submitDataBySigner(signer: string): DataSubmission[] { + return submitDataBySigner(this.signedBlock, signer) } - getSubmitDataIndex(txIndex: number): Result { - return getSubmitDataIndex(this.signedBlock, txIndex) + submitDataByHash(txHash: H256): Result { + return submitDataByHash(this.signedBlock, txHash) + } + + submitDataByIndex(txIndex: number): Result { + return submitDataByIndex(this.signedBlock, txIndex) + } + + transactionCount(): number { + return transactionCount(this.signedBlock) + } + + transactionBySigner(signer: string): GenericExtrinsic[] { + return transactionBySigner(this.signedBlock, signer) + } + + transactionByIndex(txIndex: number): Result { + return transactionByIndex(this.signedBlock, txIndex) + } + + transactionByHash(txHash: H256): GenericExtrinsic[] { + return transactionByHash(this.signedBlock, txHash) } } -export function getSubmitDataAll(block: SignedBlock): DataSubmission[] { +export function submitDataAll(block: SignedBlock): DataSubmission[] { const dataSubmissions = [] const txs = block.block.extrinsics for (let i = 0; i < txs.length; i += 1) { - const maybeDataSubmission = getSubmitDataIndex(block, i) + const maybeDataSubmission = submitDataByIndex(block, i) if (maybeDataSubmission.isOk()) { dataSubmissions.push(maybeDataSubmission.value) } @@ -42,7 +62,15 @@ export function getSubmitDataAll(block: SignedBlock): DataSubmission[] { return dataSubmissions } -export function getSubmitDataHash(block: SignedBlock, txHash: H256): Result { +export function submitDataBySigner(block: SignedBlock, signer: string): DataSubmission[] { + const allSubmissions = submitDataAll(block) + + return allSubmissions.filter((sub) => { + return sub.txSigner == signer + }) +} + +export function submitDataByHash(block: SignedBlock, txHash: H256): Result { const index = block.block.extrinsics.findIndex((tx) => { return tx.hash.toHex() == txHash.toHex() }) @@ -50,10 +78,10 @@ export function getSubmitDataHash(block: SignedBlock, txHash: H256): Result { +export function submitDataByIndex(block: SignedBlock, txIndex: number): Result { const transactions = block.block.extrinsics if (transactions.length < txIndex) { return err("Failed to extract data. Transaction has not been found") @@ -70,7 +98,32 @@ export function getSubmitDataIndex(block: SignedBlock, txIndex: number): Result< return ok(new DataSubmission(txHash, txIndex, data, txSigner)) } -function extractDADataFromTx(tx: GenericExtrinsic): Result { +export function transactionCount(block: SignedBlock): number { + return block.block.extrinsics.length +} + +export function transactionBySigner(block: SignedBlock, signer: string): GenericExtrinsic[] { + return block.block.extrinsics.filter((tx) => { + return tx.signer.toString() == signer + }) +} + +export function transactionByIndex(block: SignedBlock, txIndex: number): Result { + const transactions = block.block.extrinsics + if (transactions.length < txIndex) { + return err("Failed to find transaction. Index is out of bounds.") + } + + return ok(transactions[txIndex]) +} + +export function transactionByHash(block: SignedBlock, txHash: H256): GenericExtrinsic[] { + return block.block.extrinsics.filter((tx) => { + return tx.hash.toHex() == txHash.toHex() + }) +} + +export function extractDADataFromTx(tx: GenericExtrinsic): Result { const { method: { args, method, section }, } = tx diff --git a/avail-js/src/sdk/transactions/balances.ts b/avail-js/src/sdk/transactions/balances.ts index d866902d2..b214796c8 100644 --- a/avail-js/src/sdk/transactions/balances.ts +++ b/avail-js/src/sdk/transactions/balances.ts @@ -205,7 +205,7 @@ export namespace Events { constructor( public from: string, public to: string, - public amount: string, + public amount: BN, ) {} static New(events: EventRecord[]): TransferEvent | undefined { const ed: any = events.find((e) => e.event.method == "Transfer")?.event.data @@ -213,7 +213,7 @@ export namespace Events { return undefined } - return new TransferEvent(ed["from"].toString(), ed["to"].toString(), ed["amount"].toString()) + return new TransferEvent(ed["from"].toString(), ed["to"].toString(), ed["amount"]) } } diff --git a/avail-js/src/sdk/utils/index.ts b/avail-js/src/sdk/utils/index.ts index fb87ee98f..a54f35256 100644 --- a/avail-js/src/sdk/utils/index.ts +++ b/avail-js/src/sdk/utils/index.ts @@ -56,6 +56,9 @@ export class Utils { hexStringToHash(value: string): Result { return hexStringToHash(this.api, value) } + hexStringToHashUnsafe(value: string): H256 { + return hexStringToHashUnsafe(this.api, value) + } } export async function parseTransactionResult( @@ -124,12 +127,12 @@ export async function getNonceNode(api: ApiPromise, address: string): Promise { if (!value.startsWith("0x")) { - return err("Hash needs to start with 0x") + return err("Failed to convert hex string to H256. Hash needs to start with 0x") } const hexString = value.slice(2) if (hexString.length != 64) { - return err(`Failed to convert string to H256. Expected 64 bytes got ${hexString.length}.`) + return err(`Failed to convert hex string to H256. Expected length 64 got ${hexString.length}.`) } const u8a = hexToU8a(hexString) @@ -137,6 +140,14 @@ export function hexStringToHash(api: ApiPromise, value: string): Result