Skip to content

Commit

Permalink
start on randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
themacexpert committed Oct 31, 2024
1 parent e0df5f3 commit 5d884ca
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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);
});
24 changes: 15 additions & 9 deletions builders/substrate/interfaces/features/randomness.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5d884ca

Please sign in to comment.