diff --git a/.snippets/code/builders/substrate/interfaces/features/randomness/local-vrf-output.js b/.snippets/code/builders/substrate/interfaces/features/randomness/local-vrf-output.js new file mode 100644 index 000000000..16907a3e8 --- /dev/null +++ b/.snippets/code/builders/substrate/interfaces/features/randomness/local-vrf-output.js @@ -0,0 +1,26 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; + +const main = async () => { + // Initialize the API + const api = await ApiPromise.create({ + provider: new WsProvider('wss://moonbase-alpha.public.blastapi.io') + }); + + try { + // Get the local VRF output from randomness pallet + const localVrf = await api.query.randomness.localVrfOutput(); + + console.log('Local VRF Output:', localVrf.toString()); + + process.exit(0); + } catch (error) { + console.error('Error querying local VRF output:', error); + process.exit(1); + } +}; + +// Execute the script +main().catch(error => { + console.error('Script error:', error); + process.exit(1); +}); \ No newline at end of file diff --git a/.snippets/code/builders/substrate/interfaces/features/randomness/pallet-version.js b/.snippets/code/builders/substrate/interfaces/features/randomness/pallet-version.js new file mode 100644 index 000000000..d790e2210 --- /dev/null +++ b/.snippets/code/builders/substrate/interfaces/features/randomness/pallet-version.js @@ -0,0 +1,26 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; + +const main = async () => { + // Initialize the API + const api = await ApiPromise.create({ + provider: new WsProvider('wss://moonbase-alpha.public.blastapi.io') + }); + + try { + // Get the pallet version from randomness pallet + const version = await api.query.randomness.palletVersion(); + + console.log('Randomness Pallet Version:', version.toString()); + + process.exit(0); + } catch (error) { + console.error('Error querying randomness pallet version:', error); + process.exit(1); + } +}; + +// Execute the script +main().catch(error => { + console.error('Script error:', error); + process.exit(1); +}); \ No newline at end of file diff --git a/.snippets/code/builders/substrate/interfaces/features/randomness/randomness-results.js b/.snippets/code/builders/substrate/interfaces/features/randomness/randomness-results.js new file mode 100644 index 000000000..0f8f77ef7 --- /dev/null +++ b/.snippets/code/builders/substrate/interfaces/features/randomness/randomness-results.js @@ -0,0 +1,40 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; + +const main = async () => { + // Initialize the API + const api = await ApiPromise.create({ + provider: new WsProvider('wss://moonbase-alpha.public.blastapi.io') + }); + + try { + // Query Babe Epoch randomness results + const babeResults = await api.query.randomness.randomnessResults({ BabeEpoch: 0 }); + console.log('\nBabe Epoch Randomness Results:'); + console.log(babeResults.toHuman()); + + // Query Local randomness results + const localResults = await api.query.randomness.randomnessResults({ Local: 0 }); + console.log('\nLocal Randomness Results:'); + console.log(localResults.toHuman()); + + // Get the available keys/entries + console.log('\nAll Available Randomness Results:'); + const entries = await api.query.randomness.randomnessResults.entries(); + entries.forEach(([key, value]) => { + console.log('Key:', key.args.map((k) => k.toHuman())); + console.log('Value:', value.toHuman()); + console.log('---'); + }); + + process.exit(0); + } catch (error) { + console.error('Error querying randomness results:', error); + process.exit(1); + } +}; + +// Execute the script +main().catch(error => { + console.error('Script error:', error); + process.exit(1); +}); \ No newline at end of file diff --git a/builders/substrate/interfaces/features/randomness.md b/builders/substrate/interfaces/features/randomness.md index b292dd089..aa057bd1e 100644 --- a/builders/substrate/interfaces/features/randomness.md +++ b/builders/substrate/interfaces/features/randomness.md @@ -24,48 +24,54 @@ The randomness pallet includes the following read-only storage methods to obtain === "Parameters" - - `` - + None === "Returns" - TODO + `H256` - A 32-byte (256-bit) hex value, starting with "0x"` === "Polkadot.js API Example" ```js - TODO + --8<-- 'code/builders/substrate/interfaces/features/randomness/local-vrf-output.js' ``` ??? function "**palletVersion**() - returns the current pallet version" === "Parameters" - - `` - + None === "Returns" - TODO + `u16` - The pallet version === "Polkadot.js API Example" ```js - TODO + --8<-- 'code/builders/substrate/interfaces/features/randomness/pallet-version.js' ``` ??? function "**randomnessResults**(PalletRandomnessRequestType) - snapshot of randomness to fulfill all requests that are for the same raw randomness" === "Parameters" - - `PalletRandomnessRequestType` - + - `PalletRandomnessRequestType` - You can optionally provide the type of randomness you'd like, e.g. `Local` or `BabeEpoch` Randomness. If you omit this, you'll receive all types of randomness. === "Returns" - TODO + The query returns mappings of request types to their randomness outcomes, where: + + 1. Key: Identifies the source and timing of the randomness request. e.g. `{ Local: '4,619,640' }` indicates this was a Local randomness request from block number 4,619,640. The Local type uses block numbers as identifiers, while BabeEpoch uses epoch numbers. + + 2. Value: Contains two pieces of information, including `randomness`: A 32-byte hex string (0x15b5f6...c816) representing the random value generated and `requestCount`: The number of requests that used this same random value (e.g. '1') + + Multiple requests for randomness at the same block/epoch would share the same random value, which is why there's a requestCount field. === "Polkadot.js API Example" ```js - TODO + --8<-- 'code/builders/substrate/interfaces/features/randomness/randomness-results.js' ``` ??? function "**relayEpoch**() - returns the relay epoch"