Compare commits

..
90 Commits
Author SHA1 Message Date
Github Actions 7d3464ae2c [CI Skip] 1.9.1
skip-checks: true
2020-04-01 10:26:10 +00:00
Jaco Greeff 295cd2a7e0 1.9 (#2124) 2020-04-01 12:14:00 +02:00
Github Actions 5a66ce5a87 [CI Skip] 1.9.0-beta.27
skip-checks: true
2020-04-01 08:01:14 +00:00
Jaco Greeff d8d008108d CI scripts (#2123) 2020-04-01 09:49:16 +02:00
Github Actions 491518405d [CI Skip] 1.9.0-beta.26
skip-checks: true
2020-03-31 13:18:37 +00:00
Jaco Greeff 7290b3d296 sanitize options (allowNamespaces) (#2122)
* sanitize options (allowNamespaces)

* Pass actual option to test
2020-03-31 15:07:35 +02:00
Github Actions b6162a316f [CI Skip] 1.9.0-beta.25
skip-checks: true
2020-03-31 13:06:05 +00:00
Jaco Greeff 71c1d16dc4 Bump @polkadot/util (#2121) 2020-03-31 14:54:49 +02:00
Github Actions 204f592bea [CI Skip] 1.9.0-beta.24
skip-checks: true
2020-03-31 11:52:59 +00:00
Jaco Greeff 39c44bb5aa Split @polkadot/types/known into @polkadot/types-known (#2120)
* Split @polkadot/types/known into @polkadot/types-known

* Rreadme link

* tsconfig typo

* script aliasses
2020-03-31 13:40:59 +02:00
Github Actions df0de49a67 [CI Skip] 1.9.0-beta.23
skip-checks: true
2020-03-31 07:28:28 +00:00
Jaco Greeff 0bdbf4b7a7 Use querystorageAt for map entries (optional) (#2119) 2020-03-31 09:17:36 +02:00
Github Actions ef9cd6fc4b [CI Skip] 1.9.0-beta.22
skip-checks: true
2020-03-30 16:33:11 +00:00
Jaco Greeff 5ab0bf3998 Aplit derive session.{info, progress} (#2117) 2020-03-30 18:20:34 +02:00
Github Actions e69d97619e [CI Skip] 1.9.0-beta.21
skip-checks: true
2020-03-30 12:04:00 +00:00
Jaco Greeff 6aa0d90fc0 Add childstate_* RPCs (#2065)
* Add childstate_* RPCs

* childstate into rpc-core

* PrefixedStorageKey
2020-03-30 13:53:33 +02:00
Jaco Greeff d338fcb58c Add derive for council voting info (#2115)
* Add derive for council voting info

* Update packages/api-derive/src/council/votes.ts
2020-03-30 13:48:14 +02:00
Github Actions b9a60a2519 [CI Skip] 1.9.0-beta.20
skip-checks: true
2020-03-28 21:06:18 +00:00
Jaco Greeff 7b8b4e8078 BTreeSet only has single sub (#2114) 2020-03-28 21:55:49 +01:00
Github Actions 50b551e4fc [CI Skip] 1.9.0-beta.19
skip-checks: true
2020-03-28 20:06:12 +00:00
Jaco Greeff a4ea4d648a Handle double-encoded Bytes in fallback/constants (#2113)
* Handle double-encoded Bytes in fallback/constants

* Fix import, bypass for tests
2020-03-28 20:55:39 +01:00
Github Actions 146360a454 [CI Skip] 1.9.0-beta.18
skip-checks: true
2020-03-27 20:00:32 +00:00
Jaco Greeff 2e83d92adc Derive for deomcracy nextExternal (#2108) 2020-03-27 20:49:18 +01:00
Github Actions b77e5d079c [CI Skip] 1.9.0-beta.17
skip-checks: true
2020-03-27 17:10:21 +00:00
Jaco Greeff 0d067c9484 Add api.derive.democracy.dispatchQueue (#2107) 2020-03-27 17:59:22 +01:00
Github Actions 88519eff3f [CI Skip] 1.9.0-beta.16
skip-checks: true
2020-03-27 14:44:07 +00:00
Jaco Greeff bb14471e60 Consistency in Derive* type names (#2106) 2020-03-27 15:34:41 +01:00
Github Actions 97ab7fcf2f [CI Skip] 1.9.0-beta.15
skip-checks: true
2020-03-27 13:18:21 +00:00
Jaco Greeff 922cfb8759 FAQ entry for node-template addresses (#2105) 2020-03-27 14:07:51 +01:00
Github Actions 717f33ca02 [CI Skip] 1.9.0-beta.14
skip-checks: true
2020-03-27 12:36:19 +00:00
Jaco Greeff 21a2b5f0b7 signAndSend with finalized hash (depending on lag) (#2104)
* signAndSend with finalized hash (depending on drift)

* Extract lag calc

* mock update

* getSigningHeader

* api.derive.tx (with submittable cleanups)

* Adjust mortal calculations

* Cleanup new tx derives

* Optimize calcs

* Comment cleanups, next-up testing

* Adjust mocks

* Adjust sig (period is now based on metadata)

* Remove extra fn, tested and it works
2020-03-27 13:26:12 +01:00
Github Actions 54efead9d7 [CI Skip] 1.9.0-beta.13
skip-checks: true
2020-03-27 09:29:42 +00:00
Jaco Greeff c472a451a4 Bump dev (#2103)
* Bump dev

* Rewrite yarn.lock
2020-03-27 10:20:02 +01:00
Github Actions 6ed2de43fc [CI Skip] 1.9.0-beta.12
skip-checks: true
2020-03-26 20:20:55 +00:00
Jaco Greeff 8976ded37f Add state_queryStorageAt RPC (#2082)
* Add state_queryStorageAt RPC

* Add queryStorageAt rpc

* Clanup merge
2020-03-26 21:08:34 +01:00
Github Actions 169a374544 [CI Skip] 1.9.0-beta.11
skip-checks: true
2020-03-26 15:52:15 +00:00
Jaco Greeff 6476d88e19 Update .123trigger 2020-03-26 16:40:34 +01:00
Github Actions 75de454619 [CI Skip] 1.9.0-beta.10
skip-checks: true
2020-03-26 15:35:59 +00:00
Jaco Greeff 851428aef9 Merge branch 'master' of github.com:polkadot-js/api 2020-03-26 16:25:50 +01:00
Jaco Greeff dfc51ad300 Adjust toCallsOnly typing 2020-03-26 16:25:45 +01:00
Github Actions e16d288644 [CI Skip] 1.9.0-beta.9
skip-checks: true
2020-03-26 15:18:52 +00:00
Jaco Greeff a19928fef9 Add additional types for offchain Phragmen (#2097)
* Add additional types for offchain Phragmen

* Trim down metadata toCallsOnly
2020-03-26 16:07:54 +01:00
Github Actions 0906c7837a [CI Skip] 1.9.0-beta.8
skip-checks: true
2020-03-26 14:27:21 +00:00
Jaco Greeff cb9831c4f8 Bump metadata (#2096)
* Bump metadata

* Update staking types

* Enum updates
2020-03-26 15:17:20 +01:00
Github Actions 620d3757ba [CI Skip] 1.9.0-beta.7
skip-checks: true
2020-03-25 13:14:24 +00:00
Jaco Greeff 601288b3ec Handle <module>::<Type> in sanitize (#2094)
* Handle <module>::<Type> in santize

* Sanitize all ::'s

* Additional cleanups
2020-03-25 14:01:24 +01:00
Github Actions efbeecc8f6 [CI Skip] 1.9.0-beta.6
skip-checks: true
2020-03-25 10:18:50 +00:00
Jaco Greeff 852e0d4f0b Additional referendum derive cleanups (#2093) 2020-03-25 11:06:16 +01:00
Github Actions 5c7f9f1c8a [CI Skip] 1.9.0-beta.5
skip-checks: true
2020-03-25 08:54:57 +00:00
Jaco Greeff 64f64c1001 Add referensumsActive (#2091) 2020-03-25 09:43:05 +01:00
Github Actions 15c3085511 [CI Skip] 1.9.0-beta.4
skip-checks: true
2020-03-25 08:34:25 +00:00
Jaco Greeff 27cd9b1ca4 Cleanup interfaces/rpc, types to correct locations (#2084)
* Cleanup interfaces/rcp, types to correct locations

* Update import locations
2020-03-25 09:23:03 +01:00
Github Actions 1c2a95f820 [CI Skip] 1.9.0-beta.3
skip-checks: true
2020-03-25 07:52:42 +00:00
Jaco Greeff 8bf5e51725 Return Observable when no referendums (#2090)
* Return Observable when no referendums

* Small ordering cleanups
2020-03-25 08:41:26 +01:00
Github Actions 493225aa9d [CI Skip] 1.9.0-beta.2
skip-checks: true
2020-03-25 07:12:03 +00:00
Jaco Greeff a0979a9ce3 Only format error log, upstream error from core (#2089)
* Only format error log, upstream error from core

* Remove (now unused) wrapping info
2020-03-25 08:00:14 +01:00
Github Actions 02849ae49b [CI Skip] 1.9.0-beta.1
skip-checks: true
2020-03-24 16:50:32 +00:00
Jaco Greeff 602bb1406b derive.referendum extended info (#2086)
* derive.referendum extended info

* update

* util bump
2020-03-24 17:39:37 +01:00
Github Actions d70930587c [CI Skip] 1.9.0-beta.0
skip-checks: true
2020-03-23 10:37:30 +00:00
Jaco Greeff 8c011cdc86 Adjust new vote retrieval with entries (#2080)
* Adjust new vote retrieval with entries

* testing
2020-03-23 11:24:06 +01:00
Github Actions eb3e7a0740 [CI Skip] 1.8.1
skip-checks: true
2020-03-22 17:58:16 +00:00
Jaco Greeff 2071de517d 1.8 (#2079) 2020-03-22 18:47:04 +01:00
Github Actions 205ae40f20 [CI Skip] 1.8.0-beta.12
skip-checks: true
2020-03-22 17:27:07 +00:00
Keith IngramandJaco Greeff aa37e372dd Derived account info flags (#2068)
* Derived account info flags

* Update packages/api-derive/src/accounts/info.ts

Co-Authored-By: Jaco Greeff <jacogr@gmail.com>

* Apply suggestions from code review

* Cleanups

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
2020-03-22 18:16:43 +01:00
Github Actions 8cdac77a8e [CI Skip] 1.8.0-beta.11
skip-checks: true
2020-03-22 10:15:48 +00:00
Jaco Greeff 7a7d6e9b8a New democracy types (#2078)
* New democracy types

* ReferendumInfo

* Adjust democracy derives

* Spring cleaning

* Remove ReferendumInfoExtended
2020-03-22 11:05:57 +01:00
Github Actions 7a1bdb18a8 [CI Skip] 1.8.0-beta.10
skip-checks: true
2020-03-22 06:57:07 +00:00
Jaco Greeff 39f22ca403 Control wasm initialization via initWasm: false flag (#2076) 2020-03-22 07:45:22 +01:00
Github Actions b3c6a5acc5 [CI Skip] 1.8.0-beta.9
skip-checks: true
2020-03-22 06:43:14 +00:00
Jaco Greeff b3a6b3dcb5 Bump dev, arrow functions (#2077) 2020-03-22 07:31:55 +01:00
Github Actions 0c0e4c4d7c [CI Skip] 1.8.0-beta.8
skip-checks: true
2020-03-21 08:51:26 +00:00
Jaco Greeff 9d67d711d4 Bump deps, type import on generated (#2075)
* Bump deps, type import on generated

* type imports in types/types

* Re-export defs

* Split imports/exports

* Additional type import

* Remove import type

* Remove dupes
2020-03-21 09:40:59 +01:00
Github Actions f45d03fa17 [CI Skip] 1.8.0-beta.7
skip-checks: true
2020-03-21 07:00:35 +00:00
Jaco Greeff af11444b72 Update RPCs (babe, contracts) (#2074)
* Update RPCs (babe, contracts)

* Babe in rpc-core
2020-03-21 07:50:56 +01:00
Github Actions f412a5ac86 [CI Skip] 1.8.0-beta.6
skip-checks: true
2020-03-20 16:50:04 +00:00
Jaco Greeff c9dcd51ba6 is isWarning to ISubmittableResult (#2073) 2020-03-20 17:40:09 +01:00
Jaco Greeff 5dca826429 Retracted status to isWarning 2020-03-20 17:27:18 +01:00
Github Actions 8dc53e6339 [CI Skip] 1.8.0-beta.5
skip-checks: true
2020-03-20 15:41:13 +00:00
Jaco Greeff 5867f8ab0c Allow unknown struct fields (i.e. on Map, non-codec) (#2071) 2020-03-20 16:30:29 +01:00
Github Actions 6d3db2fb3f [CI Skip] 1.8.0-beta.4
skip-checks: true
2020-03-20 12:52:46 +00:00
Jaco Greeff 3a1634d67f Add all missing RPC endpoints (#2070)
* Add all missing RPC endpoints

* Rework RPC return types

* rpc-core interface

* Remove crap test, checked anyway
2020-03-20 13:42:18 +01:00
Github Actions 53d18d874d [CI Skip] 1.8.0-beta.3
skip-checks: true
2020-03-19 13:51:15 +00:00
Jaco Greeff 044efbc6de Convert jsonrpc & typings to definitions (#2064)
* Convert jsonrpc & typings to definitions

* Handle empty interface generation (no types)

* Move jsonrpc/create to rpc-core/create

* Create imports

* Remove jsonrpc package, generate rpc-core from defs

* Debug :)

* Generate RPC definitions via interfaces

* Generate md docs

* Remove @polkadot/jsonrpc refs (at least packages)

* No empty types

* Add PAHNTOM "cleanup" gen

* *sigh* regex loop

* Update mock for no jsonrpc

* Add definition export types

* Cleanup linting

* Mocking 1 fix

* Mocking 2 fix

* rpc-core tests updates

* CHANGELOG & docs

* Add jsonrpc (already mapped) to interfaces

* Add aliasses for RPCs

* Fix rpc-core tests
2020-03-19 14:40:54 +01:00
Github Actions 27e3fbfd0f [CI Skip] 1.8.0-beta.2
skip-checks: true
2020-03-18 21:50:49 +00:00
Jaco Greeff b7af4781ea Add Initialization to extrinsic phases (#2063) 2020-03-18 22:40:53 +01:00
Github Actions c51e351117 [CI Skip] 1.8.0-beta.1
skip-checks: true
2020-03-17 23:18:54 +00:00
Jaco Greeff e6afed9bb1 Allow electedInfo commition with/without linked maps (#2060) 2020-03-18 00:08:49 +01:00
Github Actions 4b07d5511e [CI Skip] 1.8.0-beta.0
skip-checks: true
2020-03-17 18:46:06 +00:00
Jaco Greeff a613308bdb Update 2020-03-17 19:34:51 +01:00
Jaco Greeff a863e5eb95 Non-array nominators 2020-03-17 19:22:16 +01:00
296 changed files with 6080 additions and 3805 deletions
+2 -2
View File
@@ -1,3 +1,3 @@
20
21
1.4.2
1.6.2
1.6.2
+1 -1
View File
@@ -10,7 +10,7 @@ jobs:
matrix:
step: ['build:release', 'docs:release']
name: ${{ matrix.step }}
if: "! contains(github.event.head_commit.message, '[CI Skip]')"
if: "! contains(github.event.head_commit.message, '[CI Skip]') && github.repository == 'polkadot-js/api'"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
+28 -1
View File
@@ -1,6 +1,33 @@
# 1.9.1 Apr 1, 2020
**Breaking change** (TypeScript with derives only) The return type names for `api.derive.*` have been adjusted for consistency, all starting with `Derive*` (previously a mixture of `Derived*` and `Derive*`)
**Breaking change** (derives only) Additional cleanups of democracy proposal & referenda derives, including support for new Polkadot/Substrate vote retrievals
- Tx signing via `signAndSend` & `signAsync` now tries to use the last finalized block (depending on finalized drift)
- Add support for `api.rpc.childstate.*` Substrate RPCs, older nodes with still populate `api.rpc.state.*` with the child methods
- Adjust `Error.message` on RPC failures to not include the (already known) method signature
- Split `@polkadot/types/known` into `@polkadot/types-known`, allowing for base API overrides
- Extended `api.derive.democracy` to cater for dispatchQueue and externals
- Extended `api.derive.council` to cater for new phragmen elections
- Use new `queryStorageAt` for query `.entries`, removing unneeded subscriptions (use as available)
- Fix `BTreeSet` initialization when it forms part of query interfaces
- Fix encoding for `Vec<u8>`-related types as part of constants
- Internal adjustment of interface type locations (no impact on imports via `@polkadot/types/interfaces`)
# 1.8.1 Mar 22, 2020
- **Breaking change** The format for any custom RPCs have been changed (alongside API-internal changes) to allow for better RPC management. If you are currently using custom RPCs (or planning to do so), look at the [updated documentation](https://polkadot.js.org/api/start/rpc.custom.html)
- **Breaking change** Alongside API RPC changes, the `@polkadot/jsonrpc` package has been removed. Since it was never documented and only used internally, this should not have adverse impacts. All RPC definitions itself has now been moved to the relevant modules inside `@polkadot/types/interfaces`
- **Important** Substrate has an updated democracy module. If using an older chain add the `ReferendumInfo: 'ReferendumInfoTo239'` type when using referendums
- The `isRetracted` Extrinsic status is now a warning, not a fatal error, correctly aligning with Polkadot/Substrate
- The Substrate extrinsic phase definitions has been expanded with `Initialization` to align with the latest versions
- Add support for all known substrate RPC methods
- Extend account derives to include status flags, e.g. `isCouncil`, `isSudo`, ...
- Adjusted referendum derives to cater for bot new/old democracy. Derived `referendumInfo` now includes `status` field, not `info`
- Add `initWasm` flag to API options. When set to `false` does not init the WASM portion (leaving it to the user elsewhere)
# 1.7.1 Mar 17, 2020
- **Important** Current versions of Polkadot/Substrate have dropped support for linked maps on storage entries. If you are using these queries to retriev all entries, for instance `staking.{nominators,validators}()` and using an upgraded chain, you need to swap to retrieving entries via `.entries()` or `.keys()` for the keys.
- **Important** Current versions of Polkadot/Substrate have dropped support for linked maps on storage entries. If you are using these queries to retrieve all entries, for instance `staking.{nominators,validators}()` and using an upgraded chain, you need to swap to retrieving entries via `.entries()` or `.keys()` for the keys.
- Pull in support and types for latest Polkadot/Substrate
- Add support for the new Substrate `state_getKeysPaged` RPC, including use in storage keys
- Move `derive.staking.controllers` to `derive.staking.stashes`, reflecting actual content
-1
View File
@@ -18,6 +18,5 @@ copy_folder "api-derive"
copy_folder "metadata"
copy_folder "rpc-core"
copy_folder "rpc-provider"
copy_folder "jsonrpc"
copy_folder "types"
copy_folder "typegen"
+1
View File
@@ -43,6 +43,7 @@ module.exports = {
['start/api.tx.wrap.md', 'Complex transactions'],
['start/types.basics.md', 'Type basics'],
['start/types.extend.md', 'Extending types'],
['start/rpc.custom.md', 'Custom RPC'],
['start/types.create.md', 'Creating types'],
['start/typescript.md', 'TypeScript interfaces'],
['start/typescript.user.md', 'TypeScript user generated'],
@@ -24,7 +24,7 @@ async function main () {
if (validators && validators.length > 0) {
// Retrieve the balances for all validators
const validatorBalances = await Promise.all(
validators.map(authorityId =>
validators.map((authorityId) =>
api.query.system.account(authorityId)
)
);
+1 -1
View File
@@ -22,7 +22,7 @@ function main () {
// In the next step, we're checking if the node has active validators.
// If it does, we're making another call to the api to get the balances for all validators
const balances = (validators && validators.length > 0)
? combineLatest(validators.map(authorityId => api.query.system.account(authorityId).pipe(first())))
? combineLatest(validators.map((authorityId) => api.query.system.account(authorityId).pipe(first())))
: of(null);
// We're combining the results together with the emitted value 'validators',
-5
View File
@@ -1,5 +0,0 @@
... somewhere also need a signer injection section, something along the lines of the singlesigner or this https://github.com/polkadot-js/tools/blob/master/packages/signer-cli/src/cmdSubmit.ts#L12
... DoubleMap & LinkedMap examples
... api.derive
+9 -1
View File
@@ -40,7 +40,7 @@ Tuples, as defined in the Polkadot/Substrate types appear as `(TypeA, TypeB)`. F
Polkadot/Substrate supports both immortal and mortal transactions. For immortal, this means that the transaction has an infinite lifetime, for mortals, the transactions expire after a defined period. By default the API sends mortal transactions when no explicit extrinsic era has been specified. This means that all transaction has a limited lifetime and will expire if not included in the period set.
The length for this transaction validity is set to 50 blocks, which translates to 5 minutes assuming a default of 6 second blocktimes. (10 blocks per minute in this default configuration).
The length for this transaction validity is set to 5 minutes, calculated based on the blocktime for the chain. (10 blocks per minute in this default 6s Substrate configuration).
## My chain does not support system.account queries
@@ -49,3 +49,11 @@ The API always tracks the latest Substrate master in terms of examples. This mea
It is possible that you are connecting to an older chain that has not been upgraded yet. For these chains, this storage entry won't be available (yet). To query the nonce on older chains, you can do a query to `api.query.system.accountNonce(<account>)` and balances can be retrieved via `api.query.balances.freeBalance(<account>)`.
Likewise, if your chain has been upgraded recently and you are still using the old `system.accountNonce` or `balances.freeBalance` queries in your code (which is now not available in the chain metadata), you need to update it to query the new location.
## I cannot send transactions from my node-template-based chain
For the API defaults, the API always the type definitions as specified by the Substrate master fully-featured node. This means that any customizations needs to be applied as types should there be differences in specific user-implementations.
The Substrate node-template has added customizations for some types in the default template, specifically around the `Address` and `Lookup` types, removing indices lookups from addresses. This means that the transaction payload saves 2 bytes for a transfer and is an approach followed by other chains as well, notably Polkadot & Kusama.
Due to these customizations and differences that bleed through to the transaction formats, out-of-the-box the node-template will have issues when sending transactions. To fix this, you would need to add [the customized Address types into your API](types.extend.md#impact-on-extrinsics) instances (or UIs), allowing the API to have the information required to adjust the encoding.
+51
View File
@@ -0,0 +1,51 @@
# Custom RPC
In previous sections we looked at the injection of types, as in use and defined in modules from the node. Another area that can be customized is RC definitions, and like with the types, the API provides the capability to add user-defined RPCs (in addition to the Polkadot/Substrate base) to allow use of these RPCs via the API.
## Custom definitions
RPCs are exposed as a method on a specific module, this means that once available you can call any rpc via the `api.rpc.<module>.<method>(...params[])` endpoints. To extend with user-defined RPCs, the injection can happen at the time of API creation with the addition of the `rpc` key (in addition to any other params, such as `types` or `provider`) -
```js
...
const api = await ApiPromise.create({
rpc: {
firstModule: {
testMethod: {
description: 'Just a test method',
params: [
{
name: 'index',
type: 'u64'
}
],
type: 'Balance'
}
}
}
});
```
In the above example we have defined a new method, which is now available on the RPCs as `api.rpc.firstModule.testMethod(u64) => Promise<Balance>`. Do be aware that while defined, the method will only appear if it is in the list as returned by `api.rpc.rpc.methods()`, which is the list of known RPCs the node exposes. When making changes to the node, always ensure that it does expose the RPC method correctly, otherwise it will not be decorated.
## Definition breakdown
While the above example should be self-explanatory, it is important to understand teh structure. The `rpc: { ... }` definitions are keyed first and foremost by the name of the module exposing the RPC. This means that when we have 2 modules `firstModule` & `testModule`, the first-level structure would follow the following pattern,
```js
...
const api = await ApiPromise.create({
rpc: {
firstModule: { ... },
testModule: { ... }
}
});
```
Inside the specific module definitions, the key is the actual expose RPC method. As we have seen in the first example, we expose a `testMethod` on `firstModule`. For the method definitions, apart from the description, we have definitions for the params and return a single result `type`.
Params itself, as an array, contains fields for `name`, `type` and an optional flag `isOptional` that indicates that the field is not required when making the call. (And example of this use would be in cases such as `state.getStorage(key, blockHash?)` where the last param is optional)
## Type creation
While the API always converts all the inputs into the underlying type required by the operation, be it for RPC, query or transaction, in some cases you may want to [create an instance of a type](types.create.md) yourself.
+2 -2
View File
@@ -201,6 +201,6 @@ const api = await ApiPromise.create({
Always look at customization and understand the impacts, replicating these changes between the node and the API. For the above the `Address` type is used in the construction of the `UncheckedExtrinsic` type, while the lookup type is applicable on transactions such as `balances.transfer(to: LookupSource, value: Balance)`
## Type creation
## Custom RPC
While the API always converts all the inputs into the underlying type required by the operation, in some cases you may want to [create an instance of a type](types.create.md) yourself.
In addition to customizing modules and types on your node, you can also add custom RPC definitions. Like the type definitions in ths section, [these can be defined and passed to the API](rpc.custom.md) for decoration.
+3 -13
View File
@@ -7,30 +7,20 @@ module.exports = Object.assign({}, config, {
'@polkadot/api-contract(.*)$': '<rootDir>/packages/api-contract/src/$1',
'@polkadot/api-derive(.*)$': '<rootDir>/packages/api-derive/src/$1',
'@polkadot/api(.*)$': '<rootDir>/packages/api/src/$1',
'@polkadot/jsonrpc(.*)$': '<rootDir>/packages/jsonrpc/src/$1',
'@polkadot/metadata(.*)$': '<rootDir>/packages/metadata/src/$1',
'@polkadot/rpc-(core|provider)(.*)$': '<rootDir>/packages/rpc-$1/src/$2',
'@polkadot/types-known(.*)$': '<rootDir>/packages/types-known/src/$1',
'@polkadot/types(.*)$': '<rootDir>/packages/types/src/$1'
},
modulePathIgnorePatterns: [
'<rootDir>/packages/api/build',
'<rootDir>/packages/api-derive/build',
'<rootDir>/packages/api-contract/build',
'<rootDir>/packages/jsonrpc/build',
'<rootDir>/packages/metadata/build',
'<rootDir>/packages/rpc-core/build',
'<rootDir>/packages/rpc-provider/build',
'<rootDir>/packages/types/build'
'<rootDir>/packages/types/build',
'<rootDir>/packages/types-known/build'
],
globals: {
ws_local: 'ws://127.0.0.1:9944/',
ws_local_substrate_master: 'ws://127.0.0.1:9945/',
ws_local_substrate_1: 'ws://127.0.0.1:9946/',
ws_local_substrate_2: 'ws://127.0.0.1:9947/',
ws_local_polkadot_master: 'ws://127.0.0.1:9948/',
ws_local_polkadot_alex: 'ws://127.0.0.1:9949/',
ws_substrate_1: 'wss://substrate-rpc.parity.io/',
ws_polkadot_alex: 'wss://poc3-rpc.polkadot.io/'
},
resolver: './jest.resolver.js'
});
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "1.7.1"
"version": "1.9.1"
}
+5 -5
View File
@@ -24,11 +24,11 @@
"test:watch": "polkadot-dev-run-test --watch"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/register": "^7.8.6",
"@babel/runtime": "^7.8.7",
"@polkadot/dev": "^0.51.5",
"@polkadot/ts": "^0.3.13",
"@babel/core": "^7.9.0",
"@babel/register": "^7.9.0",
"@babel/runtime": "^7.9.2",
"@polkadot/dev": "^0.51.9",
"@polkadot/ts": "^0.3.14",
"@polkadot/typegen": "workspace:packages/typegen",
"@types/jest": "^25.1.4",
"copyfiles": "^2.2.0"
+6 -6
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-contract",
"version": "1.7.1",
"version": "1.9.1",
"description": "Interfaces for interacting with contracts and contract ABIs",
"main": "index.js",
"keywords": [
@@ -26,11 +26,11 @@
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-contract#readme",
"dependencies": {
"@babel/runtime": "^7.8.7",
"@polkadot/api": "1.7.1",
"@polkadot/rpc-core": "1.7.1",
"@polkadot/types": "1.7.1",
"@polkadot/util": "^2.6.2",
"@babel/runtime": "^7.9.2",
"@polkadot/api": "1.9.1",
"@polkadot/rpc-core": "1.9.1",
"@polkadot/types": "1.9.1",
"@polkadot/util": "^2.7.1",
"bn.js": "^5.1.1",
"rxjs": "^6.5.4"
}
@@ -158,10 +158,10 @@ export default class ContractRegistry extends MetaRegistry {
public convertContract ({ constructors, messages, name, events, ...contract }: ContractABIContractPre): ContractABIContract {
return {
constructors: this.convertConstructors(constructors),
messages: messages.map(message => this.convertMessage(message)),
messages: messages.map((message) => this.convertMessage(message)),
name: this.stringAt(name),
...(events
? { events: events.map(event => this.convertEvent(event)) }
? { events: events.map((event) => this.convertEvent(event)) }
: {}),
...contract
};
+1 -1
View File
@@ -275,7 +275,7 @@ export default class MetaRegistry extends MetadataRegistryLookup {
case 'Result':
return this.typeDefForResult(id, typeIndex);
default: {
const sub = def['enum.variants'].map(variant => this.typeDefForEnumVariant(variant));
const sub = def['enum.variants'].map((variant) => this.typeDefForEnumVariant(variant));
return {
info: TypeDefInfo.Enum,
-1
View File
@@ -7,7 +7,6 @@ import { AccountId, Address, ContractExecResult } from '@polkadot/types/interfac
import { Codec, CodecArg } from '@polkadot/types/types';
import { MetaRegistryJson, StringIndex, TypeIndex, TypeDef } from '@polkadot/types/create/types';
// import { Observable } from 'rxjs';
import { ApiPromise, ApiRx } from '@polkadot/api';
export type ApiObject<ApiType extends ApiTypes> = ApiType extends 'rxjs'
+1 -1
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec, Registry, TypeDefInfo, TypeDef } from '@polkadot/types/types';
import { Codec, Registry, TypeDef, TypeDefInfo } from '@polkadot/types/types';
import { Option, Raw, createClass, createTypeUnsafe } from '@polkadot/types';
+9 -9
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-derive",
"version": "1.7.1",
"version": "1.9.1",
"description": "Common functions used across Polkadot, derived from RPC calls and storage queries.",
"main": "index.js",
"keywords": [
@@ -27,18 +27,18 @@
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
"dependencies": {
"@babel/runtime": "^7.8.7",
"@polkadot/api": "1.7.1",
"@polkadot/rpc-core": "1.7.1",
"@polkadot/rpc-provider": "1.7.1",
"@polkadot/types": "1.7.1",
"@polkadot/util": "^2.6.2",
"@polkadot/util-crypto": "^2.6.2",
"@babel/runtime": "^7.9.2",
"@polkadot/api": "1.9.1",
"@polkadot/rpc-core": "1.9.1",
"@polkadot/rpc-provider": "1.9.1",
"@polkadot/types": "1.9.1",
"@polkadot/util": "^2.7.1",
"@polkadot/util-crypto": "^2.7.1",
"bn.js": "^5.1.1",
"memoizee": "^0.4.14",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.6.2"
"@polkadot/keyring": "^2.7.1"
}
}
+45 -5
View File
@@ -5,11 +5,11 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, AccountIndex, Address, Balance, Registration } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveAccountInfo, DeriveAccountRegistration } from '../types';
import { DeriveAccountFlags, DeriveAccountInfo, DeriveAccountRegistration } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Bytes, Data, Option, u32 } from '@polkadot/types';
import { Bytes, Data, Option, u32, Vec } from '@polkadot/types';
import { u8aToString } from '@polkadot/util';
import { memo } from '../util';
@@ -22,6 +22,12 @@ function dataAsString (data: Data): string | undefined {
: undefined;
}
function includedWrapper (accountId?: AccountId): (_: AccountId) => boolean {
return function (id: AccountId): boolean {
return id.eq(accountId);
};
}
function retrieveNick (api: ApiInterfaceRx, accountId?: AccountId): Observable<string | undefined> {
return ((
accountId && api.query.nicks?.nameOf
@@ -109,6 +115,39 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab
);
}
function retrieveFlags (api: ApiInterfaceRx, accountId?: AccountId): Observable<DeriveAccountFlags> {
const councilSection = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections';
return combineLatest([
api.query[councilSection]?.members
? api.query[councilSection].members<Vec<ITuple<[AccountId, Balance]>>>()
: of(undefined),
api.query.council?.members
? api.query.council.members()
: of([]),
api.query.technicalCommittee?.members
? api.query.technicalCommittee.members()
: of([]),
api.query.society?.members
? api.query.society.members()
: of([]),
api.query.sudo?.key
? api.query.sudo.key()
: of(undefined)
]).pipe(
map(([electionsMembers, councilMembers, technicalCommitteeMembers, societyMembers, sudoKey]): DeriveAccountFlags => {
const checkIncluded = includedWrapper(accountId);
return {
isCouncil: (electionsMembers?.map(([id]) => id) || councilMembers || []).some(checkIncluded),
isTechCommittee: technicalCommitteeMembers.some(checkIncluded),
isSociety: societyMembers.some(checkIncluded),
isSudo: !!sudoKey && sudoKey.eq(accountId)
};
})
);
}
/**
* @name info
* @description Returns aux. info with regards to an account, current that includes the accountId, accountIndex and nickname
@@ -116,15 +155,16 @@ function retrieveIdentity (api: ApiInterfaceRx, accountId?: AccountId): Observab
export function info (api: ApiInterfaceRx): (address?: AccountIndex | AccountId | Address | string | null) => Observable<DeriveAccountInfo> {
return memo((address?: AccountIndex | AccountId | Address | string | null): Observable<DeriveAccountInfo> =>
api.derive.accounts.idAndIndex(address).pipe(
switchMap(([accountId, accountIndex]): Observable<[Partial<DeriveAccountInfo>, DeriveAccountRegistration, string?]> =>
switchMap(([accountId, accountIndex]): Observable<[Partial<DeriveAccountInfo>, DeriveAccountFlags, DeriveAccountRegistration, string?]> =>
combineLatest([
of({ accountId, accountIndex }),
retrieveFlags(api, accountId),
retrieveIdentity(api, accountId),
retrieveNick(api, accountId)
])
),
map(([{ accountId, accountIndex }, identity, nickname]): DeriveAccountInfo => ({
accountId, accountIndex, identity, nickname
map(([{ accountId, accountIndex }, flags, identity, nickname]): DeriveAccountInfo => ({
accountId, accountIndex, ...flags, identity, nickname
}))
));
}
+5 -5
View File
@@ -4,7 +4,7 @@
import { AccountId, AccountData, AccountIndex, AccountInfo, Address, Balance, Index } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DerivedBalancesAccount } from '../types';
import { DeriveBalancesAccount } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -14,7 +14,7 @@ import { memo } from '../util';
type Result = [Balance, Balance, Balance, Balance, Index];
function calcBalances (api: ApiInterfaceRx, [accountId, [freeBalance, reservedBalance, frozenFee, frozenMisc, accountNonce]]: [AccountId, Result]): DerivedBalancesAccount {
function calcBalances (api: ApiInterfaceRx, [accountId, [freeBalance, reservedBalance, frozenFee, frozenMisc, accountNonce]]: [AccountId, Result]): DeriveBalancesAccount {
return {
accountId,
accountNonce,
@@ -79,8 +79,8 @@ function queryCurrent (api: ApiInterfaceRx, accountId: AccountId): Observable<Re
* });
* ```
*/
export function account (api: ApiInterfaceRx): (address: AccountIndex | AccountId | Address | string) => Observable<DerivedBalancesAccount> {
return memo((address: AccountIndex | AccountId | Address | string): Observable<DerivedBalancesAccount> =>
export function account (api: ApiInterfaceRx): (address: AccountIndex | AccountId | Address | string) => Observable<DeriveBalancesAccount> {
return memo((address: AccountIndex | AccountId | Address | string): Observable<DeriveBalancesAccount> =>
api.derive.accounts.info(address).pipe(
switchMap(({ accountId }): Observable<[AccountId, Result]> =>
(accountId
@@ -95,6 +95,6 @@ export function account (api: ApiInterfaceRx): (address: AccountIndex | AccountI
: of([api.registry.createType('AccountId'), [api.registry.createType('Balance'), api.registry.createType('Balance'), api.registry.createType('Balance'), api.registry.createType('Balance'), api.registry.createType('Index')]])
)
),
map((result): DerivedBalancesAccount => calcBalances(api, result))
map((result): DeriveBalancesAccount => calcBalances(api, result))
));
}
+6 -6
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, AccountIndex, Address, Balance, BalanceLock, BalanceLockTo212, BlockNumber, VestingInfo, VestingSchedule } from '@polkadot/types/interfaces';
import { DerivedBalancesAccount, DerivedBalancesAll } from '../types';
import { DeriveBalancesAccount, DeriveBalancesAll } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
@@ -15,9 +15,9 @@ import { bnMax } from '@polkadot/util';
import { memo } from '../util';
type ResultBalance = [VestingInfo | null, (BalanceLock | BalanceLockTo212)[]];
type Result = [DerivedBalancesAccount, BlockNumber, ResultBalance];
type Result = [DeriveBalancesAccount, BlockNumber, ResultBalance];
function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBalance, frozenFee, frozenMisc, reservedBalance, votingBalance }, bestNumber, [vesting, locks]]: Result): DerivedBalancesAll {
function calcBalances (api: ApiInterfaceRx, [{ accountId, accountNonce, freeBalance, frozenFee, frozenMisc, reservedBalance, votingBalance }, bestNumber, [vesting, locks]]: Result): DeriveBalancesAll {
let lockedBalance = api.registry.createType('Balance');
let lockedBreakdown: (BalanceLock | BalanceLockTo212)[] = [];
let allLocked = false;
@@ -127,8 +127,8 @@ function queryCurrent (api: ApiInterfaceRx, accountId: AccountId): Observable<Re
* });
* ```
*/
export function all (api: ApiInterfaceRx): (address: AccountIndex | AccountId | Address | string) => Observable<DerivedBalancesAll> {
return memo((address: AccountIndex | AccountId | Address | string): Observable<DerivedBalancesAll> =>
export function all (api: ApiInterfaceRx): (address: AccountIndex | AccountId | Address | string) => Observable<DeriveBalancesAll> {
return memo((address: AccountIndex | AccountId | Address | string): Observable<DeriveBalancesAll> =>
api.derive.balances.account(address).pipe(
switchMap((account): Observable<Result> =>
(!account.accountId.isEmpty
@@ -142,6 +142,6 @@ export function all (api: ApiInterfaceRx): (address: AccountIndex | AccountId |
: of([account, api.registry.createType('BlockNumber'), [null, api.registry.createType('Vec<BalanceLock>')]])
)
),
map((result): DerivedBalancesAll => calcBalances(api, result))
map((result): DeriveBalancesAll => calcBalances(api, result))
));
}
+4 -4
View File
@@ -4,7 +4,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Balance } from '@polkadot/types/interfaces';
import { DerivedFees } from '../types';
import { DeriveFees } from '../types';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -24,8 +24,8 @@ import { memo } from '../util';
* });
* ```
*/
export function fees (api: ApiInterfaceRx): () => Observable<DerivedFees> {
return memo((): Observable<DerivedFees> =>
export function fees (api: ApiInterfaceRx): () => Observable<DeriveFees> {
return memo((): Observable<DeriveFees> =>
of([
// deprecated - remove
(api.consts.balances?.creationFee as Balance) || api.registry.createType('Balance'),
@@ -36,7 +36,7 @@ export function fees (api: ApiInterfaceRx): () => Observable<DerivedFees> {
(api.consts.transactionPayment?.transactionBaseFee as Balance) || api.registry.createType('Balance'),
(api.consts.transactionPayment?.transactionByteFee as Balance) || api.registry.createType('Balance')
]).pipe(
map(([creationFee, transferFee, existentialDeposit, transactionBaseFee, transactionByteFee]): DerivedFees => ({
map(([creationFee, transferFee, existentialDeposit, transactionBaseFee, transactionByteFee]): DeriveFees => ({
creationFee,
existentialDeposit,
transactionBaseFee,
@@ -3,19 +3,19 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
import { DerivedBalancesAccount } from '../types';
import { DeriveBalancesAccount } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '../util';
export function votingBalances (api: ApiInterfaceRx): (addresses?: (AccountId | AccountIndex | Address | string)[]) => Observable<DerivedBalancesAccount[]> {
return memo((addresses?: (AccountId | AccountIndex | Address | string)[]): Observable<DerivedBalancesAccount[]> =>
export function votingBalances (api: ApiInterfaceRx): (addresses?: (AccountId | AccountIndex | Address | string)[]) => Observable<DeriveBalancesAccount[]> {
return memo((addresses?: (AccountId | AccountIndex | Address | string)[]): Observable<DeriveBalancesAccount[]> =>
!addresses || !addresses.length
? of([] as DerivedBalancesAccount[])
? of([] as DeriveBalancesAccount[])
: combineLatest(
addresses.map((accountId): Observable<DerivedBalancesAccount> =>
addresses.map((accountId): Observable<DeriveBalancesAccount> =>
api.derive.balances.account(accountId)
)
)
@@ -4,7 +4,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Hash, Proposal, Votes } from '@polkadot/types/interfaces';
import { DerivedCollectiveProposal } from '../types';
import { DeriveCollectiveProposal } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -12,9 +12,9 @@ import { Option } from '@polkadot/types';
type Result = [Hash[], Option<Proposal>[], Option<Votes>[]];
function parse ([hashes, proposals, votes]: Result): DerivedCollectiveProposal[] {
function parse ([hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
return proposals
.map((proposalOpt, index): DerivedCollectiveProposal | null =>
.map((proposalOpt, index): DeriveCollectiveProposal | null =>
proposalOpt.isSome
? {
hash: hashes[index],
@@ -23,11 +23,11 @@ function parse ([hashes, proposals, votes]: Result): DerivedCollectiveProposal[]
}
: null
)
.filter((proposal): proposal is DerivedCollectiveProposal => !!proposal);
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
}
export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DerivedCollectiveProposal[]> {
return (): Observable<DerivedCollectiveProposal[]> =>
export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DeriveCollectiveProposal[]> {
return (): Observable<DeriveCollectiveProposal[]> =>
api.query[section]?.proposals
? api.query[section].proposals().pipe(
switchMap((hashes: Hash[]): Observable<Result> =>
@@ -41,5 +41,5 @@ export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalC
),
map(parse)
)
: of([] as DerivedCollectiveProposal[]);
: of([] as DeriveCollectiveProposal[]);
}
+4 -4
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DerivedContractFees } from '../types';
import { DeriveContractFees } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
@@ -44,10 +44,10 @@ function queryConstants (api: ApiInterfaceRx): Observable<ResultV2> {
* });
* ```
*/
export function fees (api: ApiInterfaceRx): () => Observable<DerivedContractFees> {
return memo((): Observable<DerivedContractFees> => {
export function fees (api: ApiInterfaceRx): () => Observable<DeriveContractFees> {
return memo((): Observable<DeriveContractFees> => {
return queryConstants(api).pipe(
map(([creationFee, transferFee, callBaseFee, contractFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee]): DerivedContractFees => ({
map(([creationFee, transferFee, callBaseFee, contractFee, rentByteFee, rentDepositOffset, tombstoneDeposit, transactionBaseFee, transactionByteFee]): DeriveContractFees => ({
callBaseFee,
contractFee,
creationFee,
+2
View File
@@ -3,3 +3,5 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './proposals';
export * from './votes';
export * from './votesOf';
+2 -2
View File
@@ -3,12 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DerivedCollectiveProposals } from '../types';
import { DeriveCollectiveProposals } from '../types';
import { Observable } from 'rxjs';
import { proposals as collectiveProposals } from '../collective';
import { memo } from '../util';
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedCollectiveProposals> {
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> {
return memo(collectiveProposals(api, 'council'));
}
+12
View File
@@ -0,0 +1,12 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance } from '@polkadot/types/interfaces';
export interface DeriveCouncilVote {
stake: Balance;
votes: AccountId[];
}
export type DeriveCouncilVotes = [AccountId, DeriveCouncilVote][];
+88
View File
@@ -0,0 +1,88 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveCouncilVote, DeriveCouncilVotes } from '../types';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { Vec } from '@polkadot/types';
function retrieveStakeOf (api: ApiInterfaceRx): Observable<[AccountId, Balance][]> {
const elections = (api.query.electionsPhragmen || api.query.elections);
return elections.stakeOf.creator.meta.type.asMap.linked.isTrue
? elections.stakeOf<ITuple<[Vec<AccountId>, Vec<Balance>]>>().pipe(
map(([voters, stake]) =>
voters.map((voter, index): [AccountId, Balance] => [voter, stake[index]])
)
)
: elections.stakeOf.entries<Balance>().pipe(
map((entries) =>
entries.map(([key, stake]) => [key.args[0] as AccountId, stake])
)
);
}
function retrieveVoteOf (api: ApiInterfaceRx): Observable<[AccountId, AccountId[]][]> {
const elections = (api.query.electionsPhragmen || api.query.elections);
return elections.votesOf.creator.meta.type.asMap.linked.isTrue
? elections.votesOf<ITuple<[Vec<AccountId>, Vec<Vec<AccountId>>]>>().pipe(
map(([voters, votes]) =>
voters.map((voter, index) => [voter, votes[index]])
)
)
: elections.votesOf.entries<Vec<AccountId>>().pipe(
map((entries) =>
entries.map(([key, votes]) => [key.args[0] as AccountId, votes])
)
);
}
function retrievePrev (api: ApiInterfaceRx): Observable<DeriveCouncilVotes> {
return combineLatest([retrieveStakeOf(api), retrieveVoteOf(api)]).pipe(
map(([stakes, votes]): DeriveCouncilVotes => {
const result: DeriveCouncilVotes = [];
votes.forEach(([voter, votes]): void => {
result.push([voter, { stake: api.registry.createType('Balance'), votes }]);
});
stakes.forEach(([staker, stake]): void => {
const entry = result.find(([voter]) => voter.eq(staker));
if (entry) {
entry[1].stake = stake;
} else {
result.push([staker, { stake, votes: [] }]);
}
});
return result;
})
);
}
function retrieveCurrent (api: ApiInterfaceRx): Observable<DeriveCouncilVotes> {
const elections = (api.query.electionsPhragmen || api.query.elections);
return elections.voting.entries<ITuple<[Balance, Vec<AccountId>]>>().pipe(
map((entries): DeriveCouncilVotes =>
entries.map(([key, [stake, votes]]): [AccountId, DeriveCouncilVote] =>
[key.args[0] as AccountId, { stake, votes }]
)
)
);
}
export function votes (api: ApiInterfaceRx): () => Observable<DeriveCouncilVotes> {
// since we are using entries, we are not adding a memo
return (): Observable<DeriveCouncilVotes> =>
(api.query.electionsPhragmen || api.query.elections).stakeOf
? retrievePrev(api)
: retrieveCurrent(api);
}
@@ -0,0 +1,23 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId } from '@polkadot/types/interfaces';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveCouncilVote } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export function votesOf (api: ApiInterfaceRx): (accountId: string | Uint8Array | AccountId) => Observable<DeriveCouncilVote> {
// no memo, votes actually pulls the entries without a memo (using entries)
return (accountId: string | Uint8Array | AccountId): Observable<DeriveCouncilVote> =>
api.derive.council.votes().pipe(
map((votes): DeriveCouncilVote =>
(
votes.find(([from]) => from.eq(accountId)) ||
[null, { stake: api.registry.createType('Balance'), votes: [] as AccountId[] }]
)[1]
)
);
}
@@ -0,0 +1,32 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ReferendumInfo, ReferendumInfoTo239 } from '@polkadot/types/interfaces';
import { DeriveReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Option } from '@polkadot/types';
import { memo } from '../util';
import { getStatus, parseImage } from './util';
export function _referendumInfo (api: ApiInterfaceRx): (index: BN, info: Option<ReferendumInfo | ReferendumInfoTo239>) => Observable<DeriveReferendum | null> {
return memo((index: BN, info: Option<ReferendumInfo | ReferendumInfoTo239>): Observable<DeriveReferendum | null> => {
const status = getStatus(info);
return status
? api.query.democracy.preimages(status.proposalHash).pipe(
map((preimage): DeriveReferendum => ({
index: api.registry.createType('ReferendumIndex', index),
image: parseImage(api, preimage),
imageHash: status.proposalHash,
status
}))
)
: of(null);
});
}
@@ -0,0 +1,84 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Vote, Voting, VotingDirectVote } from '@polkadot/types/interfaces';
import { DeriveBalancesAccount, DeriveReferendum, DeriveReferendumVote, DeriveReferendumVotes } from '../types';
import BN from 'bn.js';
import { Observable, of, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Vec } from '@polkadot/types';
import { memo } from '../util';
import { calcVotes } from './util';
function votesPrev (api: ApiInterfaceRx, referendumId: BN): Observable<DeriveReferendumVote[]> {
return api.query.democracy.votersFor<Vec<AccountId>>(referendumId).pipe(
switchMap((votersFor): Observable<[Vec<AccountId>, Vote[], DeriveBalancesAccount[]]> =>
combineLatest([
of(votersFor),
votersFor.length
? api.query.democracy.voteOf.multi<Vote>(
votersFor.map((accountId): [BN | number, AccountId] =>
[referendumId, accountId]
)
)
: of([]),
api.derive.balances.votingBalances(votersFor)
])
),
map(([votersFor, votes, balances]): DeriveReferendumVote[] =>
votersFor.map((accountId, index): DeriveReferendumVote => ({
accountId,
balance: balances[index].votingBalance || api.registry.createType('Balance'),
vote: votes[index] || api.registry.createType('Vote')
}))
)
);
}
function votesCurr (api: ApiInterfaceRx, referendumId: BN): Observable<DeriveReferendumVote[]> {
return api.query.democracy.votingOf.entries<Voting>().pipe(
map((allVoting): DeriveReferendumVote[] =>
allVoting
.map(([key, voting]): [AccountId, Voting] => [key.args[0] as AccountId, voting])
// FIXME We are ignoring delegated votes
.filter(([, voting]) => voting.isDirect)
.map(([accountId, voting]): [AccountId, VotingDirectVote[]] => [
accountId,
voting.asDirect.votes.filter(([idx]) => idx.eq(referendumId))
])
.filter(([, directVotes]) => !!directVotes.length)
.reduce((result: DeriveReferendumVote[], [accountId, votes]) =>
// FIXME We are ignoring split votes
votes.reduce((result: DeriveReferendumVote[], [, vote]): DeriveReferendumVote[] => {
if (vote.isStandard) {
result.push({
accountId,
...vote.asStandard
});
}
return result;
}, result), []
)
)
);
}
export function _referendumVotes (api: ApiInterfaceRx): (referendum: DeriveReferendum) => Observable<DeriveReferendumVotes> {
return memo((referendum: DeriveReferendum): Observable<DeriveReferendumVotes> =>
combineLatest([
api.derive.democracy.sqrtElectorate(),
api.query.democracy.votingOf
? votesCurr(api, referendum.index)
: votesPrev(api, referendum.index)
]).pipe(
map(([sqrtElectorate, votes]) =>
calcVotes(sqrtElectorate, referendum, votes)
)
)
);
}
@@ -0,0 +1,22 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveReferendum, DeriveReferendumVotes } from '../types';
import { Observable, of, combineLatest } from 'rxjs';
import { memo } from '../util';
export function _referendumsVotes (api: ApiInterfaceRx): (referendums: DeriveReferendum[]) => Observable<DeriveReferendumVotes[]> {
return memo((referendums: DeriveReferendum[]): Observable<DeriveReferendumVotes[]> =>
referendums.length
? combineLatest(
referendums.map((referendum): Observable<DeriveReferendumVotes> =>
api.derive.democracy._referendumVotes(referendum)
)
)
: of([])
);
}
@@ -0,0 +1,38 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber, Hash, ReferendumIndex } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveDispatch } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Vec } from '@polkadot/types';
import { memo } from '../util';
export function dispatchQueue (api: ApiInterfaceRx): () => Observable<DeriveDispatch[]> {
return memo((): Observable<DeriveDispatch[]> =>
api.query.democracy?.dispatchQueue
? api.query.democracy.dispatchQueue<Vec<ITuple<[BlockNumber, Hash, ReferendumIndex]>>>().pipe(
switchMap((dispatches) =>
combineLatest([
of(dispatches),
api.derive.democracy.preimages(
dispatches.map(([, hash]) => hash))
])
),
map(([dispatches, images]) =>
dispatches.map(([at, imageHash, index], dispatchIndex): DeriveDispatch => ({
at,
index,
image: images[dispatchIndex],
imageHash
}))
)
)
: of([])
);
}
+10 -3
View File
@@ -2,8 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './_referendumsVotes';
export * from './_referendumVotes';
export * from './_referendumInfo';
export * from './dispatchQueue';
export * from './nextExternal';
export * from './preimage';
export * from './preimages';
export * from './proposals';
export * from './referendumInfos';
export * from './referendums';
export * from './referendumVotesFor';
export * from './votes';
export * from './referendumsActive';
export * from './referendumsInfo';
export * from './sqrtElectorate';
@@ -0,0 +1,40 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Hash, VoteThreshold } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveProposalExternal } from '../types';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { memo } from '../util';
function withImage (api: ApiInterfaceRx, nextOpt: Option<ITuple<[Hash, VoteThreshold]>>): Observable<DeriveProposalExternal | null> {
if (nextOpt.isNone) {
return of(null);
}
const [imageHash, threshold] = nextOpt.unwrap();
return api.derive.democracy.preimage(imageHash).pipe(
map((image): DeriveProposalExternal => ({
image,
imageHash,
threshold
}))
);
}
export function nextExternal (api: ApiInterfaceRx): () => Observable<DeriveProposalExternal | null> {
return memo((): Observable<DeriveProposalExternal | null> =>
api.query.democracy?.nextExternal
? api.query.democracy.nextExternal<Option<ITuple<[Hash, VoteThreshold]>>>().pipe(
switchMap((nextOpt) => withImage(api, nextOpt))
)
: of(null)
);
}
@@ -0,0 +1,25 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance, BlockNumber, Hash } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveProposalImage } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Bytes, Option } from '@polkadot/types';
import { memo } from '../util';
import { parseImage } from './util';
type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
export function preimage (api: ApiInterfaceRx): (hash: Hash) => Observable<DeriveProposalImage | undefined> {
return memo((hashes: Hash[]): Observable<DeriveProposalImage | undefined> =>
api.query.democracy.preimages<PreImage>(hashes).pipe(
map((imageOpt) => parseImage(api, imageOpt))
)
);
}
@@ -0,0 +1,29 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance, BlockNumber, Hash } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveProposalImage } from '../types';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Bytes, Option } from '@polkadot/types';
import { memo } from '../util';
import { parseImage } from './util';
type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
export function preimages (api: ApiInterfaceRx): (hashes: Hash[]) => Observable<(DeriveProposalImage | undefined)[]> {
return memo((hashes: Hash[]): Observable<(DeriveProposalImage | undefined)[]> =>
api.query.democracy.preimages.multi<PreImage>(hashes).pipe(
map((images): (DeriveProposalImage | undefined)[] =>
images.map((imageOpt) =>
parseImage(api, imageOpt)
)
)
)
);
}
+14 -42
View File
@@ -2,61 +2,35 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance, BlockNumber, Hash, PropIndex, Proposal } from '@polkadot/types/interfaces';
import { AccountId, Balance, Hash, PropIndex } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveProposal } from '../types';
import { DeriveProposalImage, DeriveProposal } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Option, Vec } from '@polkadot/types';
import { Option, Vec } from '@polkadot/types';
import { memo } from '../util';
export type PreImage = Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>;
type Depositors = Option<ITuple<[Balance, Vec<AccountId>]>>;
type Proposals = Vec<ITuple<[PropIndex, Hash, AccountId]>>;
interface Result {
depositors: Depositors[];
preimages: PreImage[];
proposals: Proposals;
}
function parse (api: ApiInterfaceRx, { depositors, proposals, preimages }: Result): DeriveProposal[] {
function parse ([proposals, images, depositors]: [Proposals, (DeriveProposalImage | undefined)[], Depositors[]]): DeriveProposal[] {
return proposals
.filter(([, , proposer], index): boolean =>
!!(depositors[index]?.isSome) && !proposer.isEmpty
)
.map(([propIndex, hash, proposer], index): DeriveProposal => {
const preimage = preimages[index].unwrapOr(null);
const depositor = depositors[index].unwrap();
let proposal: undefined | Proposal;
// we could end up in a situation where the proposal is non-decodable, e.g. after an upgrade
if (preimage) {
try {
proposal = api.registry.createType('Proposal', preimage[0].toU8a(true));
} catch (error) {
console.error(error);
}
}
.map(([index, imageHash, proposer], proposalIndex): DeriveProposal => {
const [balance, seconds] = depositors[proposalIndex].unwrap();
return {
balance: depositor[0],
hash,
index: propIndex,
preimage: preimage
? {
at: preimage[3],
balance: preimage[2],
proposer: preimage[1]
}
: undefined,
proposal,
balance,
index,
image: images[proposalIndex],
imageHash,
proposer,
seconds: depositor[1]
seconds
};
});
}
@@ -68,16 +42,14 @@ export function proposals (api: ApiInterfaceRx): () => Observable<DeriveProposal
switchMap((proposals) =>
combineLatest([
of(proposals),
api.query.democracy.preimages.multi<PreImage>(
api.derive.democracy.preimages(
proposals.map(([, hash]): Hash => hash)),
api.query.democracy.depositOf.multi<Depositors>(
proposals.map(([index]): PropIndex => index))
])
),
map(([proposals, preimages, depositors]): DeriveProposal[] =>
parse(api, { depositors, proposals, preimages })
)
map(parse)
)
: of([] as DeriveProposal[])
: of([])
);
}
@@ -1,64 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import { PreImage } from './proposals';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { memo } from '../util';
function constructInfo (api: ApiInterfaceRx, index: BN | number, _info: Option<ReferendumInfo>, _preimage?: PreImage): DerivedReferendum | null {
const preImage = _preimage?.isSome
? _preimage.unwrap()
: null;
const info = _info.unwrapOr(null);
if (!info) {
return null;
}
return {
index: api.registry.createType('PropIndex', index),
info,
hash: info.proposalHash,
proposal: preImage
? api.registry.createType('Proposal', preImage[0].toU8a(true))
: undefined,
preimage: preImage
? {
at: preImage[3],
balance: preImage[2],
proposer: preImage[1]
}
: undefined
};
}
export function retrieveInfo (api: ApiInterfaceRx, index: BN | number, info: Option<ReferendumInfo>): Observable<DerivedReferendum | null> {
return ((
info?.isSome
? api.query.democracy.preimages(info.unwrap().proposalHash)
: of(undefined)
) as Observable<PreImage | undefined>).pipe(
map((preimage?: PreImage): DerivedReferendum | null =>
constructInfo(api, index, info, preimage)
)
);
}
export function referendumInfo (api: ApiInterfaceRx): (index: BN | number) => Observable<DerivedReferendum | null> {
return memo((index: BN | number): Observable<DerivedReferendum | null> =>
api.query.democracy.referendumInfoOf<Option<ReferendumInfo>>(index).pipe(
switchMap((info): Observable<DerivedReferendum | null> =>
retrieveInfo(api, index, info)
)
)
);
}
@@ -1,35 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option } from '@polkadot/types';
import { memo } from '../util';
import { retrieveInfo } from './referendumInfo';
export function referendumInfos (api: ApiInterfaceRx): (ids?: (BN | number)[]) => Observable<DerivedReferendum[]> {
return memo((ids: (BN | number)[] = []): Observable<DerivedReferendum[]> =>
(
!ids?.length
? of([] as Option<ReferendumInfo>[])
: api.query.democracy.referendumInfoOf.multi<Option<ReferendumInfo>>(ids)
).pipe(
switchMap((infos): Observable<(DerivedReferendum | null)[]> =>
combineLatest(
ids.map((id, index): Observable<DerivedReferendum | null> =>
retrieveInfo(api, id, infos[index])
)
)
),
map((infos): DerivedReferendum[] =>
infos.filter((referendum): referendum is DerivedReferendum => !!referendum)
)
));
}
@@ -1,34 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Vote } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Vec } from '@polkadot/types';
import { DerivedBalancesAccount, DerivedReferendumVote } from '../types';
import { memo } from '../util';
export function referendumVotesFor (api: ApiInterfaceRx): (referendumId: BN | number) => Observable<DerivedReferendumVote[]> {
return memo((referendumId: BN | number): Observable<DerivedReferendumVote[]> =>
api.query.democracy.votersFor<Vec<AccountId>>(referendumId).pipe(
switchMap((votersFor): Observable<[Vec<AccountId>, Vote[], DerivedBalancesAccount[]]> =>
combineLatest([
of(votersFor),
api.derive.democracy.votes(referendumId, votersFor),
api.derive.balances.votingBalances(votersFor)
])
),
map(([votersFor, votes, balances]): DerivedReferendumVote[] =>
votersFor.map((accountId, index): DerivedReferendumVote => ({
accountId,
balance: balances[index].votingBalance || api.registry.createType('Balance'),
vote: votes[index] || api.registry.createType('Vote')
} as DerivedReferendumVote))
)
));
}
@@ -2,33 +2,29 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex } from '@polkadot/types/interfaces/democracy';
import { DerivedReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DeriveReferendumExt } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { memo } from '../util';
export function referendums (api: ApiInterfaceRx): () => Observable<DerivedReferendum[]> {
return memo((): Observable<DerivedReferendum[]> =>
api.query.democracy?.lowestUnbaked
? api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
api.query.democracy.lowestUnbaked,
api.query.democracy.referendumCount
]).pipe(
switchMap(([earliest, referendumCount]): Observable<DerivedReferendum[]> =>
referendumCount?.gt(earliest) && referendumCount?.gtn(0)
? api.derive.democracy.referendumInfos(
[...Array(referendumCount.sub(earliest).toNumber())].map((_, i): BN =>
earliest.addn(i)
)
)
: of([])
)
export function referendums (api: ApiInterfaceRx): () => Observable<DeriveReferendumExt[]> {
return memo((): Observable<DeriveReferendumExt[]> =>
api.derive.democracy.referendumsActive().pipe(
switchMap((referendums) =>
combineLatest([
of(referendums),
api.derive.democracy._referendumsVotes(referendums)
])
),
map(([referendums, votes]) =>
referendums.map((referendum, index): DeriveReferendumExt => ({
...referendum,
...votes[index]
}))
)
: of([] as DerivedReferendum[])
)
);
}
@@ -0,0 +1,32 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ReferendumIndex } from '@polkadot/types/interfaces';
import { DeriveReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { memo } from '../util';
export function referendumsActive (api: ApiInterfaceRx): () => Observable<DeriveReferendum[]> {
return memo((): Observable<DeriveReferendum[]> =>
api.query.democracy?.lowestUnbaked
? api.queryMulti<[ReferendumIndex, ReferendumIndex]>([
api.query.democracy.lowestUnbaked,
api.query.democracy.referendumCount
]).pipe(
switchMap(([first, total]): Observable<DeriveReferendum[]> =>
total.gt(first)
? api.derive.democracy.referendumsInfo(
[...Array(total.sub(first).toNumber())].map((_, i): BN => first.addn(i))
)
: of([])
)
)
: of([])
);
}
@@ -0,0 +1,33 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ReferendumInfo } from '@polkadot/types/interfaces';
import { DeriveReferendum } from '../types';
import BN from 'bn.js';
import { Observable, of, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Option } from '@polkadot/types';
import { memo } from '../util';
export function referendumsInfo (api: ApiInterfaceRx): (ids: BN[]) => Observable<DeriveReferendum[]> {
return memo((ids: BN[]): Observable<DeriveReferendum[]> =>
ids.length
? api.query.democracy.referendumInfoOf.multi<Option<ReferendumInfo>>(ids).pipe(
switchMap((infos): Observable<(DeriveReferendum | null)[]> =>
combineLatest(
ids.map((id, index): Observable<DeriveReferendum | null> =>
api.derive.democracy._referendumInfo(id, infos[index])
)
)
),
map((infos) =>
infos.filter((referendum): referendum is DeriveReferendum => !!referendum)
)
)
: of([])
);
}
@@ -0,0 +1,22 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import BN from 'bn.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { bnSqrt } from '@polkadot/util';
import { memo } from '../util';
export function sqrtElectorate (api: ApiInterfaceRx): () => Observable<BN> {
return memo((): Observable<BN> =>
api.query.balances.totalIssuance().pipe(
map((totalIssuance) =>
bnSqrt(totalIssuance)
)
)
);
}
@@ -0,0 +1,69 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance, BlockNumber, Hash, Proposal, PropIndex, ReferendumInfoTo239, ReferendumStatus, Vote, ReferendumIndex, VoteThreshold } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { Vec } from '@polkadot/types';
export interface DeriveDispatch {
at: BlockNumber;
index: ReferendumIndex;
imageHash: Hash;
image?: DeriveProposalImage;
}
export interface DeriveProposalImage {
at: BlockNumber;
balance: Balance;
proposal?: Proposal;
proposer: AccountId;
}
export interface DeriveProposal {
balance?: Balance;
index: PropIndex;
image?: DeriveProposalImage;
imageHash: Hash;
proposer: AccountId;
seconds: Vec<AccountId>;
}
export interface DeriveProposalExternal {
image?: DeriveProposalImage;
imageHash: Hash;
threshold: VoteThreshold;
}
export interface DeriveReferendum {
index: ReferendumIndex;
image?: DeriveProposalImage;
imageHash: Hash;
status: ReferendumStatus | ReferendumInfoTo239;
}
export interface DeriveReferendumVote {
accountId: AccountId;
balance: Balance;
vote: Vote;
}
export interface DeriveReferendumVoteState {
allAye: DeriveReferendumVote[];
allNay: DeriveReferendumVote[];
voteCount: number;
voteCountAye: number;
voteCountNay: number;
votedAye: BN;
votedNay: BN;
votedTotal: BN;
}
export interface DeriveReferendumVotes extends DeriveReferendumVoteState {
isPassing: boolean;
votes: DeriveReferendumVote[];
}
export interface DeriveReferendumExt extends DeriveReferendum, DeriveReferendumVotes {
}
+163
View File
@@ -0,0 +1,163 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Balance, BlockNumber, Proposal, ReferendumInfo, ReferendumInfoTo239, ReferendumStatus, Tally, VoteThreshold } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DeriveProposalImage, DeriveReferendum, DeriveReferendumVote, DeriveReferendumVotes, DeriveReferendumVoteState } from '../types';
import BN from 'bn.js';
import { Bytes, Option } from '@polkadot/types';
import { bnSqrt } from '@polkadot/util';
function isOldInfo (info: ReferendumInfo | ReferendumInfoTo239): info is ReferendumInfoTo239 {
return !!(info as ReferendumInfoTo239).proposalHash;
}
function isCurrentStatus (status: ReferendumStatus | ReferendumInfoTo239): status is ReferendumStatus {
return !!(status as ReferendumStatus).tally;
}
export function compareRationals (n1: BN, d1: BN, n2: BN, d2: BN): boolean {
while (true) {
const q1 = n1.div(d1);
const q2 = n2.div(d2);
if (q1.lt(q2)) {
return true;
} else if (q2.lt(q1)) {
return false;
}
const r1 = n1.mod(d1);
const r2 = n2.mod(d2);
if (r2.isZero()) {
return false;
} else if (r1.isZero()) {
return true;
}
n1 = d2;
n2 = d1;
d1 = r2;
d2 = r1;
}
}
function isPassing (threshold: VoteThreshold, sqrtElectorate: BN, { votedAye, votedNay, votedTotal }: DeriveReferendumVoteState): boolean {
const sqrtVoters = bnSqrt(votedTotal);
return sqrtVoters.isZero()
? false
: threshold.isSimplemajority
? votedAye.gt(votedNay)
: threshold.isSupermajorityapproval
? compareRationals(votedNay, sqrtVoters, votedAye, sqrtElectorate)
: compareRationals(votedNay, sqrtElectorate, votedAye, sqrtVoters);
}
function calcVotesPrev (votesFor: DeriveReferendumVote[]): DeriveReferendumVoteState {
return votesFor.reduce((state: DeriveReferendumVoteState, derived): DeriveReferendumVoteState => {
const { balance, vote } = derived;
const isDefault = vote.conviction.index === 0;
const counted = balance
.muln(isDefault ? 1 : vote.conviction.index)
.divn(isDefault ? 10 : 1);
if (vote.isAye) {
state.allAye.push(derived);
state.voteCountAye++;
state.votedAye = state.votedAye.add(counted);
} else {
state.allNay.push(derived);
state.voteCountNay++;
state.votedNay = state.votedNay.add(counted);
}
state.voteCount++;
state.votedTotal = state.votedTotal.add(counted);
return state;
}, {
allAye: [],
allNay: [],
voteCount: 0,
voteCountAye: 0,
voteCountNay: 0,
votedAye: new BN(0),
votedNay: new BN(0),
votedTotal: new BN(0)
});
}
function calcVotesCurrent (tally: Tally, votes: DeriveReferendumVote[]): DeriveReferendumVoteState {
const allAye: DeriveReferendumVote[] = [];
const allNay: DeriveReferendumVote[] = [];
votes.forEach((derived): void => {
if (derived.vote.isAye) {
allAye.push(derived);
} else {
allNay.push(derived);
}
});
return {
allAye,
allNay,
voteCount: allAye.length + allNay.length,
voteCountAye: allAye.length,
voteCountNay: allNay.length,
votedAye: tally.ayes,
votedNay: tally.nays,
votedTotal: tally.turnout
};
}
export function calcVotes (sqrtElectorate: BN, referendum: DeriveReferendum, votes: DeriveReferendumVote[]): DeriveReferendumVotes {
const state = isCurrentStatus(referendum.status)
? calcVotesCurrent(referendum.status.tally, votes)
: calcVotesPrev(votes);
return {
...state,
isPassing: isPassing(referendum.status.threshold, sqrtElectorate, state),
votes
};
}
export function getStatus (info: Option<ReferendumInfo | ReferendumInfoTo239>): ReferendumStatus | ReferendumInfoTo239 | null {
if (info.isNone) {
return null;
}
const unwrapped = info.unwrap();
if (isOldInfo(unwrapped)) {
return unwrapped;
} else if (unwrapped.isOngoing) {
return unwrapped.asOngoing;
}
// done, we don't include it here... only currently active
return null;
}
export function parseImage (api: ApiInterfaceRx, imageOpt: Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>): DeriveProposalImage | undefined {
if (imageOpt.isNone) {
return;
}
let proposal: Proposal | undefined;
const [bytes, proposer, balance, at] = imageOpt.unwrap();
try {
proposal = api.registry.createType('Proposal', bytes.toU8a(true));
} catch (error) {
console.error(error);
}
return { at, balance, proposal, proposer };
}
@@ -1,23 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Vote } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { Observable, of } from 'rxjs';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '../util';
export function votes (api: ApiInterfaceRx): (referendumId: BN | number, accountIds?: AccountId[]) => Observable<Vote[]> {
return memo((referendumId: BN | number, accountIds: AccountId[] = []): Observable<Vote[]> =>
!accountIds || !accountIds.length
? of([] as Vote[])
: api.query.democracy.voteOf.multi<Vote>(
accountIds.map((accountId): [BN | number, AccountId] =>
[referendumId, accountId]
)
)
);
}
+5 -5
View File
@@ -10,14 +10,14 @@ import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Vec, u32 } from '@polkadot/types';
import { DerivedElectionsInfo } from '../types';
import { DeriveElectionsInfo } from '../types';
import { memo } from '../util';
function sortAccounts ([, balanceA]: ITuple<[AccountId, Balance]>, [, balanceB]: ITuple<[AccountId, Balance]>): number {
return balanceB.cmp(balanceA);
}
function queryElections (api: ApiInterfaceRx): Observable<DerivedElectionsInfo> {
function queryElections (api: ApiInterfaceRx): Observable<DeriveElectionsInfo> {
const section = api.query.electionsPhragmen ? 'electionsPhragmen' : 'elections';
return api.queryMulti<[Vec<AccountId>, Vec<AccountId>, Vec<ITuple<[AccountId, Balance]>>, Vec<ITuple<[AccountId, Balance]>>]>([
@@ -26,7 +26,7 @@ function queryElections (api: ApiInterfaceRx): Observable<DerivedElectionsInfo>
api.query[section].members,
api.query[section].runnersUp
]).pipe(
map(([councilMembers, candidates, members, runnersUp]): DerivedElectionsInfo => ({
map(([councilMembers, candidates, members, runnersUp]): DeriveElectionsInfo => ({
candidates,
candidateCount: api.registry.createType('u32', candidates.length),
candidacyBond: api.consts[section].candidacyBond as Balance,
@@ -54,6 +54,6 @@ function queryElections (api: ApiInterfaceRx): Observable<DerivedElectionsInfo>
* });
* ```
*/
export function info (api: ApiInterfaceRx): () => Observable<DerivedElectionsInfo> {
return memo((): Observable<DerivedElectionsInfo> => queryElections(api));
export function info (api: ApiInterfaceRx): () => Observable<DeriveElectionsInfo> {
return memo((): Observable<DeriveElectionsInfo> => queryElections(api));
}
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId } from '@polkadot/types/interfaces';
import { DerivedHeartbeats } from '../types';
import { DeriveHeartbeats } from '../types';
import { Observable, of, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -15,8 +15,8 @@ import { memo } from '../util';
/**
* @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session
*/
export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DerivedHeartbeats> {
return memo((): Observable<DerivedHeartbeats> =>
export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DeriveHeartbeats> {
return memo((): Observable<DeriveHeartbeats> =>
api.query.imOnline?.receivedHeartbeats
? api.derive.staking.overview().pipe(
switchMap(({ currentIndex, validators }): Observable<[AccountId[], Option<Bytes>[], u32[]]> =>
@@ -28,8 +28,8 @@ export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<Deriv
validators.map((address) => [currentIndex, address]))
])
),
map(([validators, heartbeats, numBlocks]): DerivedHeartbeats =>
validators.reduce((result: DerivedHeartbeats, validator, index): DerivedHeartbeats => ({
map(([validators, heartbeats, numBlocks]): DeriveHeartbeats =>
validators.reduce((result: DeriveHeartbeats, validator, index): DeriveHeartbeats => ({
...result,
[validator.toString()]: {
blockCount: numBlocks[index],
+1 -3
View File
@@ -49,10 +49,8 @@ describe('derive', (): void => {
testFunction(api)('chain', 'bestNumber', []);
testFunction(api)('chain', 'bestNumberFinalized', []);
testFunction(api)('democracy', 'referendumInfos', []);
testFunction(api)('democracy', 'proposals', []);
testFunction(api)('democracy', 'referendums', []);
testFunction(api)('democracy', 'referendumVotesFor', []);
testFunction(api)('democracy', 'votes', []);
testFunction(api)('elections', 'info', []);
+2 -1
View File
@@ -21,10 +21,11 @@ import * as society from './society';
import * as staking from './staking';
import * as technicalCommittee from './technicalCommittee';
import * as treasury from './treasury';
import * as tx from './tx';
export * from './type';
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury };
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, parachains, session, society, staking, technicalCommittee, treasury, tx };
type DeriveSection<Section> = {
[Method in keyof Section]: Section[Method] extends AnyFunction
@@ -12,7 +12,7 @@ import { memo } from '../util';
export function eraProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.session.info().pipe(
api.derive.session.progress().pipe(
map((info) => info.eraProgress)
));
}
+1
View File
@@ -6,4 +6,5 @@ export * from './eraLength';
export * from './eraProgress';
export * from './indexes';
export * from './info';
export * from './progress';
export * from './sessionProgress';
+23 -64
View File
@@ -3,110 +3,69 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { SessionIndex } from '@polkadot/types/interfaces';
import { DerivedSessionInfo, DeriveSessionIndexes } from '../types';
import { DeriveSessionInfo, DeriveSessionIndexes } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, u64 } from '@polkadot/types';
import { u64 } from '@polkadot/types';
import { memo } from '../util';
type ResultSlots = [u64, u64, u64, Option<SessionIndex>];
type ResultSlotsFlat = [u64, u64, u64, SessionIndex];
type ResultType = [boolean, u64, SessionIndex];
type Result = [ResultType, DeriveSessionIndexes, ResultSlotsFlat];
function createDerived (api: ApiInterfaceRx, [[hasBabe, epochDuration, sessionsPerEra], { activeEra, activeEraStart, currentEra, currentIndex, validatorCount }, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]]: Result): DerivedSessionInfo {
const epochStartSlot = epochIndex.mul(epochDuration).add(epochOrGenesisStartSlot);
const sessionProgress = currentSlot.sub(epochStartSlot);
const eraProgress = currentIndex.sub(activeEraStartSessionIndex).mul(epochDuration).add(sessionProgress);
type Result = [ResultType, DeriveSessionIndexes];
function createDerive (api: ApiInterfaceRx, [[hasBabe, epochDuration, sessionsPerEra], { activeEra, activeEraStart, currentEra, currentIndex, validatorCount }]: Result): DeriveSessionInfo {
return {
activeEra,
activeEraStart,
currentEra,
currentIndex,
eraLength: api.registry.createType('BlockNumber', sessionsPerEra.mul(epochDuration)),
eraProgress: api.registry.createType('BlockNumber', eraProgress),
isEpoch: hasBabe,
sessionLength: epochDuration,
sessionsPerEra,
sessionProgress: api.registry.createType('BlockNumber', sessionProgress),
validatorCount
};
}
function queryAura (api: ApiInterfaceRx): Observable<DerivedSessionInfo> {
function queryAura (api: ApiInterfaceRx): Observable<DeriveSessionInfo> {
return api.derive.session.indexes().pipe(
map((indexes): DerivedSessionInfo =>
createDerived(api, [
map((indexes): DeriveSessionInfo =>
createDerive(api, [
[
false,
api.registry.createType('u64', 1),
api.consts.staking?.sessionsPerEra || api.registry.createType('SessionIndex', 1)
],
indexes,
[
api.registry.createType('u64', 1),
api.registry.createType('u64', 1),
api.registry.createType('u64', 1),
api.registry.createType('SessionIndex', 1)
]
indexes
])
)
);
}
function queryBabe (api: ApiInterfaceRx): Observable<[DeriveSessionIndexes, ResultSlotsFlat]> {
function queryBabe (api: ApiInterfaceRx): Observable<DeriveSessionInfo> {
return api.derive.session.indexes().pipe(
switchMap((indexes): Observable<[DeriveSessionIndexes, ResultSlots]> =>
combineLatest([
of(indexes),
api.queryMulti<ResultSlots>([
api.query.babe.currentSlot,
api.query.babe.epochIndex,
api.query.babe.genesisSlot,
[api.query.staking.erasStartSessionIndex, indexes.activeEra]
])
map((indexes) =>
createDerive(api, [
[
true,
api.consts.babe.epochDuration,
api.consts.staking.sessionsPerEra
],
indexes
])
),
map(([indexes, [currentSlot, epochIndex, genesisSlot, optStartIndex]]): [DeriveSessionIndexes, ResultSlotsFlat] => [
indexes, [currentSlot, epochIndex, genesisSlot, optStartIndex.unwrapOr(api.registry.createType('SessionIndex', 1))]
])
)
);
}
function queryBabeNoHistory (api: ApiInterfaceRx): Observable<[DeriveSessionIndexes, ResultSlotsFlat]> {
return combineLatest([
api.derive.session.indexes(),
api.queryMulti<ResultSlotsFlat>([
api.query.babe.currentSlot,
api.query.babe.epochIndex,
api.query.babe.genesisSlot,
api.query.staking.currentEraStartSessionIndex
])
]);
}
/**
* @description Retrieves all the session and era query and calculates specific values on it as the length of the session and eras
*/
export function info (api: ApiInterfaceRx): () => Observable<DerivedSessionInfo> {
return memo((): Observable<DerivedSessionInfo> =>
export function info (api: ApiInterfaceRx): () => Observable<DeriveSessionInfo> {
return memo((): Observable<DeriveSessionInfo> =>
api.consts.babe
? (api.query.staking.erasStartSessionIndex
? queryBabe(api) // 2.x with Babe
: queryBabeNoHistory(api)
).pipe(
map(([indexes, slots]: [DeriveSessionIndexes, ResultSlotsFlat]): DerivedSessionInfo =>
createDerived(api, [
[true, api.consts.babe.epochDuration, api.consts.staking.sessionsPerEra],
indexes,
slots
])
)
)
? queryBabe(api)
: queryAura(api)
);
}
@@ -0,0 +1,90 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SessionIndex } from '@polkadot/types/interfaces';
import { DeriveSessionInfo, DeriveSessionProgress } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Option, u64 } from '@polkadot/types';
import { memo } from '../util';
type ResultSlots = [u64, u64, u64, Option<SessionIndex>];
type ResultSlotsFlat = [u64, u64, u64, SessionIndex];
function createDerive (api: ApiInterfaceRx, info: DeriveSessionInfo, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]: ResultSlotsFlat): DeriveSessionProgress {
const epochStartSlot = epochIndex.mul(info.sessionLength).add(epochOrGenesisStartSlot);
const sessionProgress = currentSlot.sub(epochStartSlot);
const eraProgress = info.currentIndex.sub(activeEraStartSessionIndex).mul(info.sessionLength).add(sessionProgress);
return {
...info,
eraProgress: api.registry.createType('BlockNumber', eraProgress),
sessionProgress: api.registry.createType('BlockNumber', sessionProgress)
};
}
function queryAura (api: ApiInterfaceRx): Observable<DeriveSessionProgress> {
return api.derive.session.info().pipe(
map((info): DeriveSessionProgress =>
createDerive(api, info, [
api.registry.createType('u64', 1),
api.registry.createType('u64', 1),
api.registry.createType('u64', 1),
api.registry.createType('SessionIndex', 1)
])
)
);
}
function queryBabe (api: ApiInterfaceRx): Observable<[DeriveSessionInfo, ResultSlotsFlat]> {
return api.derive.session.info().pipe(
switchMap((info): Observable<[DeriveSessionInfo, ResultSlots]> =>
combineLatest([
of(info),
api.queryMulti<ResultSlots>([
api.query.babe.currentSlot,
api.query.babe.epochIndex,
api.query.babe.genesisSlot,
[api.query.staking.erasStartSessionIndex, info.activeEra]
])
])
),
map(([info, [currentSlot, epochIndex, genesisSlot, optStartIndex]]): [DeriveSessionInfo, ResultSlotsFlat] => [
info, [currentSlot, epochIndex, genesisSlot, optStartIndex.unwrapOr(api.registry.createType('SessionIndex', 1))]
])
);
}
function queryBabeNoHistory (api: ApiInterfaceRx): Observable<[DeriveSessionInfo, ResultSlotsFlat]> {
return combineLatest([
api.derive.session.info(),
api.queryMulti<ResultSlotsFlat>([
api.query.babe.currentSlot,
api.query.babe.epochIndex,
api.query.babe.genesisSlot,
api.query.staking.currentEraStartSessionIndex
])
]);
}
/**
* @description Retrieves all the session and era query and calculates specific values on it as the length of the session and eras
*/
export function progress (api: ApiInterfaceRx): () => Observable<DeriveSessionProgress> {
return memo((): Observable<DeriveSessionProgress> =>
api.consts.babe
? (api.query.staking.erasStartSessionIndex
? queryBabe(api) // 2.x with Babe
: queryBabeNoHistory(api)
).pipe(
map(([info, slots]: [DeriveSessionInfo, ResultSlotsFlat]): DeriveSessionProgress =>
createDerive(api, info, slots)
)
)
: queryAura(api)
);
}
@@ -12,7 +12,7 @@ import { memo } from '../util';
export function sessionProgress (api: ApiInterfaceRx): () => Observable<BlockNumber> {
return memo((): Observable<BlockNumber> =>
api.derive.session.info().pipe(
api.derive.session.progress().pipe(
map((info) => info.sessionProgress)
));
}
+5 -2
View File
@@ -14,11 +14,14 @@ export interface DeriveSessionIndexes {
validatorCount: u32;
}
export interface DerivedSessionInfo extends DeriveSessionIndexes {
export interface DeriveSessionInfo extends DeriveSessionIndexes {
eraLength: BlockNumber;
eraProgress: BlockNumber;
isEpoch: boolean;
sessionLength: BlockNumber;
sessionsPerEra: SessionIndex;
}
export interface DeriveSessionProgress extends DeriveSessionInfo {
eraProgress: BlockNumber;
sessionProgress: BlockNumber;
}
+13 -27
View File
@@ -3,8 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Balance, BlockNumber, StakingLedger, UnlockChunk } from '@polkadot/types/interfaces';
import { DerivedSessionInfo, DerivedStakingAccount, DerivedStakingQuery, DerivedUnlocking } from '../types';
import { Balance, StakingLedger, UnlockChunk } from '@polkadot/types/interfaces';
import { DeriveSessionInfo, DeriveStakingAccount, DeriveStakingQuery, DeriveUnlocking } from '../types';
import BN from 'bn.js';
import { Observable, combineLatest } from 'rxjs';
@@ -27,27 +27,13 @@ function groupByEra (list: UnlockChunk[]): Record<string, BN> {
}, {});
}
// calculate the remaining blocks in a specific unlock era
function remainingBlocks (api: ApiInterfaceRx, era: BN, sessionInfo: DerivedSessionInfo): BlockNumber {
const remaining = era.sub(sessionInfo.currentEra);
// on the Rust side the current-era >= era-for-unlock (removal done on >)
return api.registry.createType('BlockNumber', remaining.gtn(0)
? remaining
.subn(1)
.mul(sessionInfo.eraLength)
.add(sessionInfo.eraLength.sub(sessionInfo.eraProgress))
: 0
);
}
function calculateUnlocking (api: ApiInterfaceRx, stakingLedger: StakingLedger | undefined, sessionInfo: DerivedSessionInfo): DerivedUnlocking[] | undefined {
function calculateUnlocking (api: ApiInterfaceRx, stakingLedger: StakingLedger | undefined, sessionInfo: DeriveSessionInfo): DeriveUnlocking[] | undefined {
if (isUndefined(stakingLedger)) {
return undefined;
}
const unlockingChunks = stakingLedger.unlocking.filter(({ era }): boolean =>
remainingBlocks(api, era.unwrap(), sessionInfo).gtn(0)
era.unwrap().sub(sessionInfo.activeEra).gtn(0)
);
if (!unlockingChunks.length) {
@@ -56,27 +42,27 @@ function calculateUnlocking (api: ApiInterfaceRx, stakingLedger: StakingLedger |
// group the unlock chunks that have the same era and sum their values
const groupedResult = groupByEra(unlockingChunks);
const results = Object.entries(groupedResult).map(([eraString, value]): DerivedUnlocking => ({
value: api.registry.createType('Balance', value),
remainingBlocks: remainingBlocks(api, new BN(eraString), sessionInfo)
const results = Object.entries(groupedResult).map(([eraString, value]): DeriveUnlocking => ({
remainingEras: new BN(eraString).sub(sessionInfo.activeEra),
value: api.registry.createType('Balance', value)
}));
return results.length ? results : undefined;
}
function redeemableSum (api: ApiInterfaceRx, stakingLedger: StakingLedger | undefined, sessionInfo: DerivedSessionInfo): Balance {
function redeemableSum (api: ApiInterfaceRx, stakingLedger: StakingLedger | undefined, sessionInfo: DeriveSessionInfo): Balance {
if (isUndefined(stakingLedger)) {
return api.registry.createType('Balance');
}
return api.registry.createType('Balance', stakingLedger.unlocking.reduce((total, { era, value }): BN => {
return remainingBlocks(api, era.unwrap(), sessionInfo).eqn(0)
return sessionInfo.activeEra.gte(era.unwrap())
? total.add(value.unwrap())
: total;
}, new BN(0)));
}
function parseResult (api: ApiInterfaceRx, sessionInfo: DerivedSessionInfo, query: DerivedStakingQuery): DerivedStakingAccount {
function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query: DeriveStakingQuery): DeriveStakingAccount {
return {
...query,
redeemable: redeemableSum(api, query.stakingLedger, sessionInfo),
@@ -87,13 +73,13 @@ function parseResult (api: ApiInterfaceRx, sessionInfo: DerivedSessionInfo, quer
/**
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
*/
export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DerivedStakingAccount> {
return memo((accountId: Uint8Array | string): Observable<DerivedStakingAccount> =>
export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
combineLatest([
api.derive.session.info(),
api.derive.staking.query(accountId)
]).pipe(
map(([sessionInfo, query]: [DerivedSessionInfo, DerivedStakingQuery]) =>
map(([sessionInfo, query]: [DeriveSessionInfo, DeriveStakingQuery]) =>
parseResult(api, sessionInfo, query))
));
}
@@ -4,27 +4,27 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/interfaces';
import { DerivedStakingQuery, DerivedStakingElected } from '../types';
import { DeriveStakingQuery, DeriveStakingElected } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { memo } from '../util';
export function electedInfo (api: ApiInterfaceRx): () => Observable<DerivedStakingElected> {
return memo((): Observable<DerivedStakingElected> =>
export function electedInfo (api: ApiInterfaceRx): () => Observable<DeriveStakingElected> {
return memo((): Observable<DeriveStakingElected> =>
api.derive.staking.validators().pipe(
switchMap(({ nextElected }): Observable<[AccountId[], DerivedStakingQuery[]]> =>
switchMap(({ nextElected }): Observable<[AccountId[], DeriveStakingQuery[]]> =>
combineLatest([
of(nextElected),
combineLatest(
nextElected.map((accountId): Observable<DerivedStakingQuery> =>
nextElected.map((accountId): Observable<DeriveStakingQuery> =>
api.derive.staking.query(accountId)
)
)
])
),
map(([nextElected, info]): DerivedStakingElected => ({
map(([nextElected, info]): DeriveStakingElected => ({
info,
nextElected
}))
+4 -4
View File
@@ -4,7 +4,7 @@
import { AccountId, EraPoints, EraRewardPoints, RewardPoint } from '@polkadot/types/interfaces';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DerivedStakingOverview } from '../types';
import { DeriveStakingOverview } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -29,8 +29,8 @@ function retrievePointsPrev (api: ApiInterfaceRx, currentElected: AccountId[]):
/**
* @description Retrieve the staking overview, including elected and points earned
*/
export function overview (api: ApiInterfaceRx): () => Observable<DerivedStakingOverview> {
return memo((): Observable<DerivedStakingOverview> =>
export function overview (api: ApiInterfaceRx): () => Observable<DeriveStakingOverview> {
return memo((): Observable<DeriveStakingOverview> =>
combineLatest([
api.derive.session.indexes(),
api.derive.staking.validators()
@@ -45,7 +45,7 @@ export function overview (api: ApiInterfaceRx): () => Observable<DerivedStakingO
: of(api.registry.createType('EraRewardPoints'))
])
),
map(([info, eraPoints]): DerivedStakingOverview => ({
map(([info, eraPoints]): DeriveStakingOverview => ({
...info, eraPoints
}))
));
+18 -11
View File
@@ -5,7 +5,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Exposure, Keys, Nominations, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types';
import { DerivedStakingQuery } from '../types';
import { DeriveStakingQuery } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -13,7 +13,7 @@ import { Option, Vec } from '@polkadot/types';
import { memo } from '../util';
type MultiResult = [Option<AccountId>, Option<ITuple<[Nominations]>>, RewardDestination, ITuple<[ValidatorPrefs]>, Option<Keys>, Exposure];
type MultiResult = [Option<AccountId>, Option<ITuple<[Nominations]> | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Option<Keys>, Exposure];
function unwrapSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][], nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; sessionIds: AccountId[] } {
const sessionIds = (queuedKeys.find(([currentId]): boolean =>
@@ -55,20 +55,27 @@ function retrieveCurr (api: ApiInterfaceRx, stashId: AccountId): Observable<Mult
);
}
function retrieveController (api: ApiInterfaceRx, stashId: AccountId, [queuedKeys, [controllerIdOpt, nominatorsOpt, rewardDestination, [validatorPrefs], nextKeys, exposure]]: [Vec<ITuple<[AccountId, Keys]>>, MultiResult]): Observable<DerivedStakingQuery> {
function retrieveController (api: ApiInterfaceRx, stashId: AccountId, [queuedKeys, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]]: [Vec<ITuple<[AccountId, Keys]>>, MultiResult]): Observable<DeriveStakingQuery> {
const controllerId = controllerIdOpt.unwrapOr(null);
const nominators = nominatorsOpt.unwrapOr(null);
return controllerId
? api.query.staking.ledger(controllerId).pipe(
map((stakingLedgerOpt): DerivedStakingQuery => ({
map((stakingLedgerOpt): DeriveStakingQuery => ({
accountId: stashId,
controllerId,
exposure,
nominators: nominatorsOpt.unwrapOr([{ targets: [] }])[0].targets,
nominators: nominators
? Array.isArray(nominators)
? nominators[0].targets
: nominators.targets
: [],
rewardDestination,
stakingLedger: stakingLedgerOpt.unwrapOr(undefined),
stashId,
validatorPrefs,
validatorPrefs: Array.isArray(validatorPrefs)
? validatorPrefs[0]
: validatorPrefs,
...unwrapSessionIds(stashId, queuedKeys, nextKeys)
}))
)
@@ -78,8 +85,8 @@ function retrieveController (api: ApiInterfaceRx, stashId: AccountId, [queuedKey
/**
* @description From a stash, retrieve the controllerId and all relevant details
*/
export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DerivedStakingQuery> {
return memo((accountId: Uint8Array | string): Observable<DerivedStakingQuery> => {
export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingQuery> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingQuery> => {
const stashId = api.registry.createType('AccountId', accountId);
return combineLatest([
@@ -88,15 +95,15 @@ export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) =>
? retrieveCurr(api, stashId)
: retrievePrev(api, stashId)
]).pipe(
switchMap((result): Observable<DerivedStakingQuery> =>
switchMap((result): Observable<DeriveStakingQuery> =>
retrieveController(api, stashId, result)
)
);
});
}
export function queryMulti (api: ApiInterfaceRx): (...accountIds: (Uint8Array | string)[]) => Observable<DerivedStakingQuery[]> {
return memo((...accountIds: (Uint8Array | string)[]): Observable<DerivedStakingQuery[]> =>
export function queryMulti (api: ApiInterfaceRx): (...accountIds: (Uint8Array | string)[]) => Observable<DeriveStakingQuery[]> {
return memo((...accountIds: (Uint8Array | string)[]): Observable<DeriveStakingQuery[]> =>
combineLatest(
accountIds.map((accountId) => api.derive.staking.query(accountId))
)
+12 -10
View File
@@ -2,9 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Balance, BlockNumber, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
import { AccountId, Balance, EraIndex, EraRewardPoints, Exposure, Keys, RewardDestination, RewardPoint, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
import { DeriveSessionIndexes } from '../session/types';
import BN from 'bn.js';
export type DeriveEraValPoints = Record<string, RewardPoint>;
export type DeriveEraValPrefs = Record<string, ValidatorPrefs>;
@@ -79,9 +81,9 @@ export interface DeriveStakerSlashes {
total: Balance;
}
export interface DerivedStakingElected {
export interface DeriveStakingElected {
nextElected: AccountId[];
info: DerivedStakingQuery[];
info: DeriveStakingQuery[];
}
export interface DeriveStakingValidators {
@@ -89,7 +91,7 @@ export interface DeriveStakingValidators {
validators: AccountId[];
}
export interface DerivedStakingStash {
export interface DeriveStakingStash {
controllerId?: AccountId;
exposure?: Exposure;
nominators?: AccountId[];
@@ -100,25 +102,25 @@ export interface DerivedStakingStash {
validatorPrefs?: ValidatorPrefs;
}
export interface DerivedStakingQuery extends DerivedStakingStash {
export interface DeriveStakingQuery extends DeriveStakingStash {
accountId: AccountId;
nextSessionIds: AccountId[];
sessionIds: AccountId[];
stakingLedger?: StakingLedger;
}
export interface DerivedStakingAccount extends DerivedStakingQuery {
export interface DeriveStakingAccount extends DeriveStakingQuery {
redeemable?: Balance;
unlocking?: DerivedUnlocking[];
unlocking?: DeriveUnlocking[];
}
export interface DerivedStakingOverview extends DeriveSessionIndexes {
export interface DeriveStakingOverview extends DeriveSessionIndexes {
eraPoints: EraRewardPoints;
nextElected: AccountId[];
validators: AccountId[];
}
export type DerivedUnlocking = {
remainingBlocks: BlockNumber;
export type DeriveUnlocking = {
remainingEras: BN;
value: Balance;
};
@@ -3,12 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { DerivedCollectiveProposals } from '../types';
import { DeriveCollectiveProposals } from '../types';
import { Observable } from 'rxjs';
import { proposals as collectiveProposals } from '../collective';
import { memo } from '../util';
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedCollectiveProposals> {
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposals> {
return memo(collectiveProposals(api, 'technicalCommittee'));
}
+10 -10
View File
@@ -4,7 +4,7 @@
import { ApiInterfaceRx } from '@polkadot/api/types';
import { ProposalIndex, TreasuryProposal } from '@polkadot/types/interfaces';
import { DerivedCollectiveProposals, DerivedTreasuryProposal, DerivedTreasuryProposals } from '../types';
import { DeriveCollectiveProposals, DeriveTreasuryProposal, DeriveTreasuryProposals } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
@@ -16,13 +16,13 @@ interface Result {
allIds: ProposalIndex[];
allProposals: Option<TreasuryProposal>[];
approvalIds: ProposalIndex[];
councilProposals: DerivedCollectiveProposals;
councilProposals: DeriveCollectiveProposals;
proposalCount: ProposalIndex;
}
function parseResult (_api: ApiInterfaceRx, { allIds, allProposals, approvalIds, councilProposals, proposalCount }: Result): DerivedTreasuryProposals {
const approvals: DerivedTreasuryProposal[] = [];
const proposals: DerivedTreasuryProposal[] = [];
function parseResult (_api: ApiInterfaceRx, { allIds, allProposals, approvalIds, councilProposals, proposalCount }: Result): DeriveTreasuryProposals {
const approvals: DeriveTreasuryProposal[] = [];
const proposals: DeriveTreasuryProposal[] = [];
const councilTreasury = councilProposals.filter(({ proposal: { methodName, sectionName } }): boolean =>
sectionName === 'treasury' &&
['approveProposal', 'rejectProposal'].includes(methodName)
@@ -47,7 +47,7 @@ function parseResult (_api: ApiInterfaceRx, { allIds, allProposals, approvalIds,
return { approvals, proposalCount, proposals };
}
function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, approvalIds: ProposalIndex[]): Observable<DerivedTreasuryProposals> {
function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, approvalIds: ProposalIndex[]): Observable<DeriveTreasuryProposals> {
const proposalIds: ProposalIndex[] = [];
const count = proposalCount.toNumber();
@@ -65,7 +65,7 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
api.query.treasury.proposals.multi<Option<TreasuryProposal>>(allIds),
api.derive.council.proposals()
]).pipe(
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DerivedCollectiveProposals]): DerivedTreasuryProposals =>
map(([allProposals, councilProposals]: [Option<TreasuryProposal>[], DeriveCollectiveProposals]): DeriveTreasuryProposals =>
parseResult(api, { allIds, allProposals, approvalIds, councilProposals, proposalCount })
)
);
@@ -74,8 +74,8 @@ function retrieveProposals (api: ApiInterfaceRx, proposalCount: ProposalIndex, a
/**
* @description Retrieve all active and approved treasury proposals, along with their info
*/
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedTreasuryProposals> {
return memo((): Observable<DerivedTreasuryProposals> =>
export function proposals (api: ApiInterfaceRx): () => Observable<DeriveTreasuryProposals> {
return memo((): Observable<DeriveTreasuryProposals> =>
api.query.treasury
? combineLatest([
api.query.treasury.proposalCount(),
@@ -89,6 +89,6 @@ export function proposals (api: ApiInterfaceRx): () => Observable<DerivedTreasur
approvals: [],
proposalCount: api.registry.createType('ProposalIndex'),
proposals: []
} as DerivedTreasuryProposals)
} as DeriveTreasuryProposals)
);
}
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import BN from 'bn.js';
export const MAX_FINALITY_LAG = new BN(5);
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventRecord, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { memo } from '../util';
interface Result {
block: SignedBlock;
events: EventRecord[];
}
export function events (api: ApiInterfaceRx): (at: Hash) => Observable<Result> {
return memo((at: Hash): Observable<Result> =>
combineLatest([
api.query.system.events.at(at),
api.rpc.chain.getBlock(at)
]).pipe(
map(([events, block]) => ({
block,
events
}))
)
);
}
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export * from './events';
export * from './signingInfo';
+69
View File
@@ -0,0 +1,69 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Header, Index } from '@polkadot/types/interfaces';
import { AnyNumber, Codec, IExtrinsicEra } from '@polkadot/types/types';
import BN from 'bn.js';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { isNumber, isUndefined } from '@polkadot/util';
interface Result {
header: Header | null;
mortalLength: number;
nonce: Index;
}
// default here to 5 min eras, adjusted based on the actual blocktime
const FALLBACK_PERIOD = new BN(6 * 1000);
const MAX_FINALITY_LAG = new BN(5);
const MORTAL_PERIOD = new BN(5 * 60 * 1000);
function latestNonce (api: ApiInterfaceRx, address: string): Observable<Index> {
return api.derive.balances.account(address).pipe(
map(({ accountNonce }): Index => accountNonce)
);
}
function signingHeader (api: ApiInterfaceRx): Observable<Header> {
return combineLatest([
api.rpc.chain.getHeader(),
api.rpc.chain.getFinalizedHead().pipe(
switchMap((hash) => api.rpc.chain.getHeader(hash))
)
]).pipe(
map(([current, finalized]): Header =>
// determine the hash to use, current when lag > max, else finalized
current.number.unwrap().sub(finalized.number.unwrap()).gt(MAX_FINALITY_LAG)
? current
: finalized
)
);
}
export function signingInfo (api: ApiInterfaceRx): (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number) => Observable<Result> {
// no memo, we want to do this fresh on each run
return (address: string, nonce?: AnyNumber | Codec, era?: IExtrinsicEra | number): Observable<Result> =>
combineLatest([
// retrieve nonce if none was specified
isUndefined(nonce)
? latestNonce(api, address)
: of(api.registry.createType('Index', nonce)),
// if no era (create) or era > 0 (mortal), do block retrieval
(isUndefined(era) || (isNumber(era) && era > 0))
? signingHeader(api)
: of(null)
]).pipe(
map(([nonce, header]) => ({
header,
nonce,
mortalLength: MORTAL_PERIOD
.div(api.consts.babe?.expectedBlockTime || api.consts.timestamp?.minimumPeriod.muln(2) || FALLBACK_PERIOD)
.add(MAX_FINALITY_LAG)
.toNumber()
}))
);
}
@@ -1,57 +0,0 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ReferendumIndex, ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { AnyJson, Constructor, Registry } from '@polkadot/types/types';
import BN from 'bn.js';
import democracyTypes from '@polkadot/types/interfaces/democracy/definitions';
import { Struct } from '@polkadot/types';
// We can ignore the properties, added via Struct.with
const _ReferendumInfo: Constructor<ReferendumInfo> = Struct.with(democracyTypes.types.ReferendumInfo as any) as any;
/**
* @name ReferendumInfoExtended
* @description
* A [[ReferendumInfo]] with an additional `index` field
*/
export default class ReferendumInfoExtended extends _ReferendumInfo {
readonly #index: ReferendumIndex;
constructor (registry: Registry, value: ReferendumInfo | ReferendumInfoExtended, index?: BN | number) {
super(registry, value);
this.#index = value instanceof ReferendumInfoExtended
? value.index
: registry.createType('ReferendumIndex', index);
}
/**
* @description Convenience getter, returns the referendumIndex
*/
public get index (): ReferendumIndex {
return this.#index;
}
/**
* @description Creates a human-friendly JSON representation
*/
public toHuman (isExtended?: boolean): AnyJson {
return {
...super.toHuman(isExtended) as { [index: string]: AnyJson },
index: this.index.toHuman(isExtended)
};
}
/**
* @description Creates the JSON representation
*/
public toJSON (): AnyJson {
return {
...super.toJSON() as { [index: string]: AnyJson },
index: this.index.toJSON()
};
}
}
-1
View File
@@ -3,4 +3,3 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export { default as HeaderExtended } from './HeaderExtended';
export { default as ReferendumInfoExtended } from './ReferendumInfoExtended';
+29 -50
View File
@@ -2,11 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Hash, Index, Proposal, PropIndex, ProposalIndex, ReferendumInfo, RegistrationJudgement, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Vote, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces';
import { AccountId, AccountIndex, Balance, BalanceLock, BalanceLockTo212, BalanceOf, Bid, BidKind, BlockNumber, Hash, Index, Proposal, ProposalIndex, RegistrationJudgement, SetIndex, SocietyVote, StrikeCount, TreasuryProposal, Votes, VoteIndex, VouchingStatus } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import { u32, Vec } from '@polkadot/types';
import { u32 } from '@polkadot/types';
export * from './council/types';
export * from './democracy/types';
export * from './parachains/types';
export * from './session/types';
export * from './staking/types';
@@ -28,14 +30,21 @@ export interface DeriveAccountRegistration {
judgements: RegistrationJudgement[];
}
export interface DeriveAccountInfo {
export interface DeriveAccountFlags {
isCouncil: boolean;
isSociety: boolean;
isSudo: boolean;
isTechCommittee: boolean;
}
export interface DeriveAccountInfo extends DeriveAccountFlags {
accountId?: AccountId;
accountIndex?: AccountIndex;
identity: DeriveAccountRegistration;
nickname?: string;
}
export interface DerivedBalancesAccount {
export interface DeriveBalancesAccount {
accountId: AccountId;
accountNonce: Index;
freeBalance: Balance;
@@ -45,7 +54,7 @@ export interface DerivedBalancesAccount {
votingBalance: Balance;
}
export interface DerivedBalancesAll extends DerivedBalancesAccount {
export interface DeriveBalancesAll extends DeriveBalancesAccount {
isVesting: boolean;
lockedBalance: Balance;
lockedBreakdown: (BalanceLock | BalanceLockTo212)[];
@@ -55,9 +64,9 @@ export interface DerivedBalancesAll extends DerivedBalancesAccount {
vestingTotal: Balance;
}
export type DerivedBalancesMap = Record<string, DerivedBalancesAll>;
export type DeriveBalancesMap = Record<string, DeriveBalancesAll>;
export interface DerivedContractFees {
export interface DeriveContractFees {
callBaseFee: BN;
contractFee: BN;
creationFee: BN;
@@ -69,15 +78,15 @@ export interface DerivedContractFees {
transferFee: BN;
}
export interface DerivedCollectiveProposal {
export interface DeriveCollectiveProposal {
hash: Hash;
proposal: Proposal;
votes: Votes | null;
}
export type DerivedCollectiveProposals = DerivedCollectiveProposal[];
export type DeriveCollectiveProposals = DeriveCollectiveProposal[];
export interface DerivedElectionsInfo {
export interface DeriveElectionsInfo {
candidates: AccountId[];
candidateCount: u32;
candidacyBond?: Balance;
@@ -91,7 +100,7 @@ export interface DerivedElectionsInfo {
votingBond?: Balance;
}
export interface DerivedFees {
export interface DeriveFees {
creationFee: Balance;
existentialDeposit: Balance;
transactionBaseFee: Balance;
@@ -99,50 +108,20 @@ export interface DerivedFees {
transferFee: Balance;
}
export interface DerivedHeartbeatAuthor {
export interface DeriveHeartbeatAuthor {
blockCount: u32;
hasMessage: boolean;
isOnline: boolean;
}
export type DerivedHeartbeats = Record<string, DerivedHeartbeatAuthor>;
export type DeriveHeartbeats = Record<string, DeriveHeartbeatAuthor>;
export interface RecentlyOffline {
blockNumber: BlockNumber;
count: BN;
}
export interface DeriveProposalPreImage {
at: BlockNumber;
balance: Balance;
proposer: AccountId;
}
export interface DeriveProposal {
balance?: Balance;
hash: Hash;
index: PropIndex;
preimage?: DeriveProposalPreImage;
proposal?: Proposal;
proposer: AccountId;
seconds: Vec<AccountId>;
}
export interface DerivedReferendum {
hash: Hash;
index: PropIndex;
info: ReferendumInfo;
preimage?: DeriveProposalPreImage;
proposal?: Proposal;
}
export type DerivedRecentlyOffline = Record<string, RecentlyOffline[]>;
export interface DerivedReferendumVote {
accountId: AccountId;
balance: Balance;
vote: Vote;
}
export type DeriveRecentlyOffline = Record<string, RecentlyOffline[]>;
export interface DeriveSociety {
bids: Bid[];
@@ -170,16 +149,16 @@ export interface DeriveSocietyMember {
vouching?: VouchingStatus;
}
export interface DerivedTreasuryProposal {
council: DerivedCollectiveProposal[];
export interface DeriveTreasuryProposal {
council: DeriveCollectiveProposal[];
id: ProposalIndex;
proposal: TreasuryProposal;
}
export interface DerivedTreasuryProposals {
approvals: DerivedTreasuryProposal[];
export interface DeriveTreasuryProposals {
approvals: DeriveTreasuryProposal[];
proposalCount: ProposalIndex;
proposals: DerivedTreasuryProposal[];
proposals: DeriveTreasuryProposal[];
}
export interface VoterPosition {
@@ -188,4 +167,4 @@ export interface VoterPosition {
setIndex: SetIndex;
}
export type DerivedVoterPositions = Record<string, VoterPosition>;
export type DeriveVoterPositions = Record<string, VoterPosition>;
+12 -11
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "1.7.1",
"version": "1.9.1",
"description": "Promise and RxJS wrappers around the Polkadot JS RPC",
"main": "index.js",
"keywords": [
@@ -26,20 +26,21 @@
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
"dependencies": {
"@babel/runtime": "^7.8.7",
"@polkadot/api-derive": "1.7.1",
"@polkadot/keyring": "^2.6.2",
"@polkadot/metadata": "1.7.1",
"@polkadot/rpc-core": "1.7.1",
"@polkadot/rpc-provider": "1.7.1",
"@polkadot/types": "1.7.1",
"@polkadot/util": "^2.6.2",
"@polkadot/util-crypto": "^2.6.2",
"@babel/runtime": "^7.9.2",
"@polkadot/api-derive": "1.9.1",
"@polkadot/keyring": "^2.7.1",
"@polkadot/metadata": "1.9.1",
"@polkadot/rpc-core": "1.9.1",
"@polkadot/rpc-provider": "1.9.1",
"@polkadot/types": "1.9.1",
"@polkadot/types-known": "1.9.1",
"@polkadot/util": "^2.7.1",
"@polkadot/util-crypto": "^2.7.1",
"bn.js": "^5.1.1",
"eventemitter3": "^4.0.0",
"rxjs": "^6.5.4"
},
"devDependencies": {
"@polkadot/keyring": "^2.6.2"
"@polkadot/keyring": "^2.7.1"
}
}
+5 -5
View File
@@ -1,5 +1,5 @@
// Auto-generated via `yarn polkadot-types-from-chain`, do not edit
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable */
import { Codec } from '@polkadot/types/types';
import { u32, u64 } from '@polkadot/types/primitive';
@@ -113,10 +113,6 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
* Period in blocks where an external proposal may not be re-submitted after being vetoed.
**/
cooloffPeriod: AugmentedConst<BlockNumber>;
/**
* Minimum voting period allowed for an emergency referendum.
**/
emergencyVotingPeriod: AugmentedConst<BlockNumber>;
/**
* The minimum period of locking and the period between a proposal being approved and enacted.
* It should generally be a little more than the unstake period to ensure that
@@ -124,6 +120,10 @@ declare module '@polkadot/metadata/Decorated/consts/types' {
* they are on the losing side of a vote.
**/
enactmentPeriod: AugmentedConst<BlockNumber>;
/**
* Minimum voting period allowed for an emergency referendum.
**/
fastTrackVotingPeriod: AugmentedConst<BlockNumber>;
/**
* How often (in blocks) new public referenda are launched.
**/
+60 -41
View File
@@ -1,27 +1,27 @@
// Auto-generated via `yarn polkadot-types-from-chain`, do not edit
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable */
import { AnyNumber, ITuple } from '@polkadot/types/types';
import { Option, U8aFixed, Vec } from '@polkadot/types/codec';
import { Bytes, Data, bool, u32, u64 } from '@polkadot/types/primitive';
import { UncleEntryItem } from '@polkadot/types/interfaces/authorship';
import { BabeAuthorityWeight, MaybeVrf } from '@polkadot/types/interfaces/babe';
import { BabeAuthorityWeight, MaybeVrf, Randomness } from '@polkadot/types/interfaces/babe';
import { AccountData, BalanceLock, ReleasesBalances } from '@polkadot/types/interfaces/balances';
import { ProposalIndex, Votes } from '@polkadot/types/interfaces/collective';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { AuthorityId, RawVRFOutput } from '@polkadot/types/interfaces/consensus';
import { CodeHash, ContractInfo, Gas, PrefabWasmModule, Schedule } from '@polkadot/types/interfaces/contracts';
import { Conviction, PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo } from '@polkadot/types/interfaces/democracy';
import { Vote, VoteThreshold } from '@polkadot/types/interfaces/elections';
import { AuthorityList, SetId, StoredPendingChange, StoredState } from '@polkadot/types/interfaces/grandpa';
import { PropIndex, Proposal, ProxyState, ReferendumIndex, ReferendumInfo, Voting } from '@polkadot/types/interfaces/democracy';
import { VoteThreshold } from '@polkadot/types/interfaces/elections';
import { SetId, StoredPendingChange, StoredState } from '@polkadot/types/interfaces/grandpa';
import { RegistrarInfo, Registration } from '@polkadot/types/interfaces/identity';
import { AuthIndex } from '@polkadot/types/interfaces/imOnline';
import { Kind, OffenceDetails, OpaqueTimeSlot, ReportIdOf } from '@polkadot/types/interfaces/offences';
import { DeferredOffenceOf, Kind, OffenceDetails, OpaqueTimeSlot, ReportIdOf } from '@polkadot/types/interfaces/offences';
import { ActiveRecovery, RecoveryConfig } from '@polkadot/types/interfaces/recovery';
import { AccountId, AccountIndex, Balance, BalanceOf, BlockNumber, Hash, KeyTypeId, Moment, Perbill, ValidatorId, Weight } from '@polkadot/types/interfaces/runtime';
import { Keys, SessionIndex } from '@polkadot/types/interfaces/session';
import { Bid, BidKind, SocietyVote, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
import { ActiveEraInfo, EraIndex, EraRewardPoints, Exposure, Forcing, Nominations, ReleasesStaking, RewardDestination, SlashingSpans, SpanIndex, SpanRecord, StakingLedger, UnappliedSlash, ValidatorPrefs } from '@polkadot/types/interfaces/staking';
import { AccountInfo, DigestOf, EventIndex, EventRecord, LastRuntimeUpgradeInfo } from '@polkadot/types/interfaces/system';
import { ActiveEraInfo, ElectionResult, ElectionStatus, EraIndex, EraRewardPoints, Exposure, Forcing, Nominations, PhragmenScore, ReleasesStaking, RewardDestination, SlashingSpans, SpanIndex, SpanRecord, StakingLedger, UnappliedSlash, ValidatorPrefs } from '@polkadot/types/interfaces/staking';
import { AccountInfo, DigestOf, EventIndex, EventRecord, LastRuntimeUpgradeInfo, Phase } from '@polkadot/types/interfaces/system';
import { OpenTip, TreasuryProposal } from '@polkadot/types/interfaces/treasury';
import { Multiplier } from '@polkadot/types/interfaces/txpayment';
import { Multisig } from '@polkadot/types/interfaces/utility';
@@ -73,7 +73,7 @@ declare module '@polkadot/api/types/storage' {
/**
* Next epoch randomness.
**/
nextRandomness: AugmentedQuery<ApiType, () => Observable<U8aFixed>> & QueryableStorageEntry<ApiType>;
nextRandomness: AugmentedQuery<ApiType, () => Observable<Randomness>> & QueryableStorageEntry<ApiType>;
/**
* The epoch randomness for the *current* epoch.
* # Security
@@ -84,7 +84,7 @@ declare module '@polkadot/api/types/storage' {
* used where a number is needed that cannot have been chosen by an
* adversary, for purposes such as public-coin zero-knowledge proofs.
**/
randomness: AugmentedQuery<ApiType, () => Observable<U8aFixed>> & QueryableStorageEntry<ApiType>;
randomness: AugmentedQuery<ApiType, () => Observable<Randomness>> & QueryableStorageEntry<ApiType>;
/**
* Randomness under construction.
* We make a tradeoff between storage accesses and list length.
@@ -95,7 +95,7 @@ declare module '@polkadot/api/types/storage' {
* epoch.
**/
segmentIndex: AugmentedQuery<ApiType, () => Observable<u32>> & QueryableStorageEntry<ApiType>;
underConstruction: AugmentedQuery<ApiType, (arg: u32 | AnyNumber | Uint8Array) => Observable<Vec<U8aFixed>>> & QueryableStorageEntry<ApiType>;
underConstruction: AugmentedQuery<ApiType, (arg: u32 | AnyNumber | Uint8Array) => Observable<Vec<RawVRFOutput>>> & QueryableStorageEntry<ApiType>;
};
balances: {
[index: string]: QueryableStorageEntry<ApiType>;
@@ -191,10 +191,6 @@ declare module '@polkadot/api/types/storage' {
* Record of all proposals that have been subject to emergency cancellation.
**/
cancellations: AugmentedQuery<ApiType, (arg: Hash | string | Uint8Array) => Observable<bool>> & QueryableStorageEntry<ApiType>;
/**
* Get the account (and lock periods) to which another account is delegating vote.
**/
delegations: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<ITuple<[AccountId, Conviction]>>> & QueryableStorageEntry<ApiType>;
/**
* Those who have locked a deposit.
**/
@@ -252,22 +248,16 @@ declare module '@polkadot/api/types/storage' {
**/
referendumInfoOf: AugmentedQuery<ApiType, (arg: ReferendumIndex | AnyNumber | Uint8Array) => Observable<Option<ReferendumInfo>>> & QueryableStorageEntry<ApiType>;
/**
* Get the vote in a given referendum of a particular voter. The result is meaningful only
* if `voters_for` includes the voter when called with the referendum (you'll get the
* default `Vote` value otherwise). If you don't want to check `voters_for`, then you can
* also check for simple existence with `VoteOf::contains_key` first.
* All votes for a particular voter. We store the balance for the number of votes that we
* have recorded. The second item is the total amount of delegations, that will be added.
**/
voteOf: AugmentedQuery<ApiType, (arg: ITuple<[ReferendumIndex, AccountId]> | [ReferendumIndex | AnyNumber | Uint8Array, AccountId | string | Uint8Array]) => Observable<Vote>> & QueryableStorageEntry<ApiType>;
/**
* Get the voters for the current proposal.
**/
votersFor: AugmentedQuery<ApiType, (arg: ReferendumIndex | AnyNumber | Uint8Array) => Observable<Vec<AccountId>>> & QueryableStorageEntry<ApiType>;
votingOf: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<Voting>> & QueryableStorageEntry<ApiType>;
};
elections: {
[index: string]: QueryableStorageEntry<ApiType>;
/**
* The present candidate list. Sorted based on account-id. A current member or a runner can
* never enter this vector and is always implicitly assumed to be a candidate.
* The present candidate list. Sorted based on account-id. A current member or runner-up
* can never enter this vector and is always implicitly assumed to be a candidate.
**/
candidates: AugmentedQuery<ApiType, () => Observable<Vec<AccountId>>> & QueryableStorageEntry<ApiType>;
/**
@@ -283,22 +273,12 @@ declare module '@polkadot/api/types/storage' {
**/
runnersUp: AugmentedQuery<ApiType, () => Observable<Vec<ITuple<[AccountId, BalanceOf]>>>> & QueryableStorageEntry<ApiType>;
/**
* Locked stake of a voter.
* Votes and locked stake of a particular voter.
**/
stakeOf: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<BalanceOf>> & QueryableStorageEntry<ApiType>;
/**
* Votes of a particular voter, with the round index of the votes.
**/
votesOf: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<Vec<AccountId>>> & QueryableStorageEntry<ApiType>;
voting: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<ITuple<[BalanceOf, Vec<AccountId>]>>> & QueryableStorageEntry<ApiType>;
};
grandpa: {
[index: string]: QueryableStorageEntry<ApiType>;
/**
* DEPRECATED
* This used to store the current authority set, which has been migrated to the well-known
* GRANDPA_AUTHORITIES_KEY unhashed key.
**/
authorities: AugmentedQuery<ApiType, () => Observable<AuthorityList>> & QueryableStorageEntry<ApiType>;
/**
* The number of changes (both in terms of keys and underlying economic responsibilities)
* in the "set" of Grandpa validators from genesis.
@@ -387,6 +367,11 @@ declare module '@polkadot/api/types/storage' {
* A vector of reports of the same kind that happened at the same time slot.
**/
concurrentReportsIndex: AugmentedQueryDoubleMap<ApiType, (key1: Kind | string | Uint8Array, key2: OpaqueTimeSlot | string | Uint8Array) => Observable<Vec<ReportIdOf>>> & QueryableStorageEntry<ApiType>;
/**
* Deferred reports that have been rejected by the offence handler and need to be submitted
* at a later time.
**/
deferredOffences: AugmentedQuery<ApiType, () => Observable<Vec<DeferredOffenceOf>>> & QueryableStorageEntry<ApiType>;
/**
* The primary structure that holds all offence records keyed by report identifiers.
**/
@@ -561,6 +546,11 @@ declare module '@polkadot/api/types/storage' {
* The earliest era for which we have a pending, unapplied slash.
**/
earliestUnappliedSlash: AugmentedQuery<ApiType, () => Observable<Option<EraIndex>>> & QueryableStorageEntry<ApiType>;
/**
* Flag to control the execution of the offchain election. When `Open(_)`, we accept
* solutions to be submitted.
**/
eraElectionStatus: AugmentedQuery<ApiType, () => Observable<ElectionStatus>> & QueryableStorageEntry<ApiType>;
/**
* Rewards for the last `HISTORY_DEPTH` eras.
* If reward hasn't been set or has been removed then 0 reward is returned.
@@ -605,7 +595,7 @@ declare module '@polkadot/api/types/storage' {
**/
erasValidatorReward: AugmentedQuery<ApiType, (arg: EraIndex | AnyNumber | Uint8Array) => Observable<Option<BalanceOf>>> & QueryableStorageEntry<ApiType>;
/**
* True if the next session change will be a new era regardless of index.
* Mode of era forcing.
**/
forceEra: AugmentedQuery<ApiType, () => Observable<Forcing>> & QueryableStorageEntry<ApiType>;
/**
@@ -622,6 +612,10 @@ declare module '@polkadot/api/types/storage' {
* invulnerables) and restricted to testnets.
**/
invulnerables: AugmentedQuery<ApiType, () => Observable<Vec<AccountId>>> & QueryableStorageEntry<ApiType>;
/**
* True if the current planned session is final.
**/
isCurrentSessionFinal: AugmentedQuery<ApiType, () => Observable<bool>> & QueryableStorageEntry<ApiType>;
/**
* Map from all (unlocked) "controller" accounts to the info regarding the staking.
**/
@@ -642,6 +636,16 @@ declare module '@polkadot/api/types/storage' {
* Where the reward payment should be made. Keyed by stash.
**/
payee: AugmentedQuery<ApiType, (arg: AccountId | string | Uint8Array) => Observable<RewardDestination>> & QueryableStorageEntry<ApiType>;
/**
* The next validator set. At the end of an era, if this is available (potentially from the
* result of an offchain worker), it is immediately used. Otherwise, the on-chain election
* is executed.
**/
queuedElected: AugmentedQuery<ApiType, () => Observable<Option<ElectionResult>>> & QueryableStorageEntry<ApiType>;
/**
* The score of the current [`QueuedElected`].
**/
queuedScore: AugmentedQuery<ApiType, () => Observable<Option<PhragmenScore>>> & QueryableStorageEntry<ApiType>;
/**
* Slashing spans for stash accounts.
**/
@@ -651,14 +655,25 @@ declare module '@polkadot/api/types/storage' {
* The rest of the slashed value is handled by the `Slash`.
**/
slashRewardFraction: AugmentedQuery<ApiType, () => Observable<Perbill>> & QueryableStorageEntry<ApiType>;
/**
* Snapshot of nominators at the beginning of the current election window. This should only
* have a value when [`EraElectionStatus`] == `ElectionStatus::Open(_)`.
**/
snapshotNominators: AugmentedQuery<ApiType, () => Observable<Option<Vec<AccountId>>>> & QueryableStorageEntry<ApiType>;
/**
* Snapshot of validators at the beginning of the current election window. This should only
* have a value when [`EraElectionStatus`] == `ElectionStatus::Open(_)`.
**/
snapshotValidators: AugmentedQuery<ApiType, () => Observable<Option<Vec<AccountId>>>> & QueryableStorageEntry<ApiType>;
/**
* Records information about the maximum slash of a stash within a slashing span,
* as well as how much reward has been paid out.
**/
spanSlash: AugmentedQuery<ApiType, (arg: ITuple<[AccountId, SpanIndex]> | [AccountId | string | Uint8Array, SpanIndex | AnyNumber | Uint8Array]) => Observable<SpanRecord>> & QueryableStorageEntry<ApiType>;
/**
* True if network has been upgraded to this version.
* Storage version of the pallet.
* This is set to v2.0.0 for new networks.
* This is set to v3.0.0 for new networks.
**/
storageVersion: AugmentedQuery<ApiType, () => Observable<ReleasesStaking>> & QueryableStorageEntry<ApiType>;
/**
@@ -727,6 +742,10 @@ declare module '@polkadot/api/types/storage' {
* no notification will be triggered thus the event might be lost.
**/
eventTopics: AugmentedQuery<ApiType, (arg: Hash | string | Uint8Array) => Observable<Vec<ITuple<[BlockNumber, EventIndex]>>>> & QueryableStorageEntry<ApiType>;
/**
* The execution phase of the block.
**/
executionPhase: AugmentedQuery<ApiType, () => Observable<Option<Phase>>> & QueryableStorageEntry<ApiType>;
/**
* Total extrinsics count for the current block.
**/
+84 -6
View File
@@ -1,14 +1,24 @@
// Auto-generated via `yarn polkadot-types-from-chain`, do not edit
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable */
import { AnyNumber, Codec, IExtrinsic } from '@polkadot/types/types';
import { Option, Vec } from '@polkadot/types/codec';
import { Bytes, StorageKey, Text, bool, u32, u64 } from '@polkadot/types/primitive';
import { HashMap, Option, Vec } from '@polkadot/types/codec';
import { Bytes, Null, StorageKey, Text, bool, u32, u64 } from '@polkadot/types/primitive';
import { Metadata } from '@polkadot/types';
import { ExtrinsicOrHash, ExtrinsicStatus } from '@polkadot/types/interfaces/author';
import { EpochAuthorship } from '@polkadot/types/interfaces/babe';
import { BlockHash } from '@polkadot/types/interfaces/chain';
import { PrefixedStorageKey } from '@polkadot/types/interfaces/childstate';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { ContractCallRequest, ContractExecResult } from '@polkadot/types/interfaces/contracts';
import { CreatedBlock } from '@polkadot/types/interfaces/engine';
import { Extrinsic } from '@polkadot/types/interfaces/extrinsics';
import { BlockHash, ChainProperties, ExtrinsicOrHash, ExtrinsicStatus, Health, NetworkState, PeerInfo, RpcMethods, RuntimeDispatchInfo, RuntimeVersion } from '@polkadot/types/interfaces/rpc';
import { AccountId, BlockNumber, H256, Hash, Header, Index, SignedBlock, StorageData } from '@polkadot/types/interfaces/runtime';
import { StorageKind } from '@polkadot/types/interfaces/offchain';
import { RuntimeDispatchInfo } from '@polkadot/types/interfaces/payment';
import { RpcMethods } from '@polkadot/types/interfaces/rpc';
import { AccountId, BlockNumber, H256, Hash, Header, Index, Justification, KeyValue, SignedBlock, StorageData } from '@polkadot/types/interfaces/runtime';
import { RuntimeVersion } from '@polkadot/types/interfaces/state';
import { ChainProperties, Health, NetworkState, NodeRole, PeerInfo } from '@polkadot/types/interfaces/system';
import { Observable } from 'rxjs';
declare module '@polkadot/rpc-core/types.jsonrpc' {
@@ -39,7 +49,7 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
/**
* Remove given extrinsic from the pool and temporarily ban it to prevent reimporting
**/
removeExtrinsic: AugmentedRpc<(bytesOrHash: Vec<ExtrinsicOrHash> | (ExtrinsicOrHash | { hash: any } | { extrinsic: any } | string | Uint8Array)[]) => Observable<Vec<Hash>>>;
removeExtrinsic: AugmentedRpc<(bytesOrHash: Vec<ExtrinsicOrHash> | (ExtrinsicOrHash | { Hash: any } | { Extrinsic: any } | string | Uint8Array)[]) => Observable<Vec<Hash>>>;
/**
* Generate new session keys and returns the corresponding public keys
**/
@@ -53,6 +63,12 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
**/
submitExtrinsic: AugmentedRpc<(extrinsic: IExtrinsic) => Observable<Hash>>;
};
babe: {
/**
* Returns data about which slots (primary or secondary) can be claimed in the current epoch with the keys in the keystore
**/
epochAuthorship: AugmentedRpc<() => Observable<HashMap<AuthorityId, EpochAuthorship>>>;
};
chain: {
/**
* Get header and body of a relay chain block
@@ -83,6 +99,24 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
**/
subscribeNewHeads: AugmentedRpc<() => Observable<Header>>;
};
childstate: {
/**
* Returns the keys with prefix from a child storage, leave empty to get all the keys
**/
getKeys: AugmentedRpc<(childKey: PrefixedStorageKey | string | Uint8Array, prefix: StorageKey | string | Uint8Array | any, at?: Hash | string | Uint8Array) => Observable<Vec<StorageKey>>>;
/**
* Returns a child storage entry at a specific block state
**/
getStorage: AugmentedRpc<(childKey: PrefixedStorageKey | string | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: Hash | string | Uint8Array) => Observable<Option<StorageData>>>;
/**
* Returns the hash of a child storage entry at a block state
**/
getStorageHash: AugmentedRpc<(childKey: PrefixedStorageKey | string | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: Hash | string | Uint8Array) => Observable<Option<Hash>>>;
/**
* Returns the size of a child storage entry at a block state
**/
getStorageSize: AugmentedRpc<(childKey: PrefixedStorageKey | string | Uint8Array, key: StorageKey | string | Uint8Array | any, at?: Hash | string | Uint8Array) => Observable<Option<u64>>>;
};
contracts: {
/**
* Executes a call to a contract
@@ -92,6 +126,30 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
* Returns the value under a specified storage key in a contract
**/
getStorage: AugmentedRpc<(address: AccountId | string | Uint8Array, key: H256 | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable<Option<Bytes>>>;
/**
* Returns the projected time a given contract will be able to sustain paying its rent
**/
rentProjection: AugmentedRpc<(address: AccountId | string | Uint8Array, at?: BlockHash | string | Uint8Array) => Observable<Option<BlockNumber>>>;
};
engine: {
/**
* Instructs the manual-seal authorship task to create a new block
**/
createBlock: AugmentedRpc<(createEmpty: bool | boolean | Uint8Array, finalize: bool | boolean | Uint8Array, parentHash?: BlockHash | string | Uint8Array) => Observable<CreatedBlock>>;
/**
* Instructs the manual-seal authorship task to finalize a block
**/
finalizeBlock: AugmentedRpc<(hash: BlockHash | string | Uint8Array, justification?: Justification | string | Uint8Array) => Observable<bool>>;
};
offchain: {
/**
* Get offchain local storage under given key and prefix
**/
localStorageGet: AugmentedRpc<(kind: StorageKind | ('__UNUSED' | 'PERSISTENT' | 'LOCAL') | number | Uint8Array, key: Bytes | string | Uint8Array) => Observable<Option<Bytes>>>;
/**
* Set offchain local storage under given key and prefix
**/
localStorageSet: AugmentedRpc<(kind: StorageKind | ('__UNUSED' | 'PERSISTENT' | 'LOCAL') | number | Uint8Array, key: Bytes | string | Uint8Array, value: Bytes | string | Uint8Array) => Observable<Null>>;
};
payment: {
/**
@@ -138,6 +196,10 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
* Returns the runtime metadata
**/
getMetadata: AugmentedRpc<(at?: BlockHash | string | Uint8Array) => Observable<Metadata>>;
/**
* Returns the keys with prefix, leave empty to get all the keys
**/
getPairs: AugmentedRpc<(prefix: StorageKey | string | Uint8Array | any, at?: BlockHash | string | Uint8Array) => Observable<Vec<KeyValue>>>;
/**
* Get the runtime version
**/
@@ -158,6 +220,10 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
* Query historical storage entries (by key) starting from a start block
**/
queryStorage: AugmentedRpc<<T = Codec[]>(keys: Vec<StorageKey> | (StorageKey | string | Uint8Array | any)[], fromBlock?: Hash | Uint8Array | string, toBlock?: Hash | Uint8Array | string) => Observable<[Hash, T][]>>;
/**
* Query storage entries (by key) starting at block hash given as the second parameter
**/
queryStorageAt: AugmentedRpc<<T = Codec[]>(keys: Vec<StorageKey> | (StorageKey | string | Uint8Array | any)[], at?: Hash | Uint8Array | string) => Observable<T>>;
/**
* Retrieves the runtime version via subscription
**/
@@ -168,6 +234,10 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
subscribeStorage: AugmentedRpc<<T = Codec[]>(keys: Vec<StorageKey> | (StorageKey | string | Uint8Array | any)[]) => Observable<T>>;
};
system: {
/**
* Adds a reserved peer
**/
addReservedPeer: AugmentedRpc<(peer: Text | string) => Observable<Text>>;
/**
* Retrieves the chain
**/
@@ -184,6 +254,10 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
* Returns current state of the network
**/
networkState: AugmentedRpc<() => Observable<NetworkState>>;
/**
* Returns the roles the node is running as
**/
nodeRoles: AugmentedRpc<() => Observable<Vec<NodeRole>>>;
/**
* Returns the currently connected peers
**/
@@ -192,6 +266,10 @@ declare module '@polkadot/rpc-core/types.jsonrpc' {
* Get a custom set of properties as a JSON object, defined in the chain spec
**/
properties: AugmentedRpc<() => Observable<ChainProperties>>;
/**
* Remove a reserved peer
**/
removeReservedPeer: AugmentedRpc<(peerId: Text | string) => Observable<Text>>;
/**
* Retrieves the version of the node
**/
+188 -32
View File
@@ -1,20 +1,19 @@
// Auto-generated via `yarn polkadot-types-from-chain`, do not edit
/* eslint-disable @typescript-eslint/no-empty-interface */
/* eslint-disable */
import { AnyNumber, ITuple } from '@polkadot/types/types';
import { Compact, Option, U8aFixed, Vec } from '@polkadot/types/codec';
import { Bytes, Data, bool, u16, u32, u64 } from '@polkadot/types/primitive';
import { MemberCount, ProposalIndex } from '@polkadot/types/interfaces/collective';
import { CodeHash, Gas, Schedule } from '@polkadot/types/interfaces/contracts';
import { Conviction, PropIndex, Proposal, ReferendumIndex } from '@polkadot/types/interfaces/democracy';
import { Vote } from '@polkadot/types/interfaces/elections';
import { AccountVote, Conviction, PropIndex, Proposal, ReferendumIndex } from '@polkadot/types/interfaces/democracy';
import { Extrinsic, Signature } from '@polkadot/types/interfaces/extrinsics';
import { IdentityFields, IdentityInfo, IdentityJudgement, RegistrarIndex } from '@polkadot/types/interfaces/identity';
import { Heartbeat } from '@polkadot/types/interfaces/imOnline';
import { AccountId, AccountIndex, Address, Balance, BalanceOf, BlockNumber, Call, ChangesTrieConfiguration, Hash, Header, KeyValue, LookupSource, Moment, Perbill } from '@polkadot/types/interfaces/runtime';
import { Keys } from '@polkadot/types/interfaces/session';
import { SocietyJudgement } from '@polkadot/types/interfaces/society';
import { EraIndex, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces/staking';
import { CompactAssignments, EraIndex, PhragmenScore, RewardDestination, ValidatorIndex, ValidatorPrefs } from '@polkadot/types/interfaces/staking';
import { Key } from '@polkadot/types/interfaces/system';
import { Timepoint } from '@polkadot/types/interfaces/utility';
import { VestingInfo } from '@polkadot/types/interfaces/vesting';
@@ -179,6 +178,7 @@ declare module '@polkadot/api/types/submittable' {
* - `which`: The index of the referendum to cancel.
* # <weight>
* - One DB change.
* - O(d) where d is the items in the dispatch queue.
* # </weight>
**/
cancelQueued: AugmentedSubmittable<(which: ReferendumIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
@@ -221,25 +221,30 @@ declare module '@polkadot/api/types/submittable' {
**/
deactivateProxy: AugmentedSubmittable<(proxy: AccountId | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Delegate vote.
* Currency is locked indefinitely for as long as it's delegated.
* The dispatch origin of this call must be _Signed_.
* - `to`: The account to make a delegate of the sender.
* - `conviction`: The conviction that will be attached to the delegated
* votes.
* Delegate the voting power (with some given conviction) of the sending account.
* The balance delegated is locked for as long as it's delegated, and thereafter for the
* time appropriate for the conviction's lock period.
* The dispatch origin of this call must be _Signed_, and the signing account must either:
* - be delegating already; or
* - have no voting activity (if there is, then it will need to be removed/consolidated
* through `reap_vote` or `unvote`).
* - `to`: The account whose voting the `target` account's voting power will follow.
* - `conviction`: The conviction that will be attached to the delegated votes. When the
* account is undelegated, the funds will be locked for the corresponding period.
* - `balance`: The amount of the account's balance to be used in delegating. This must
* not be more than the account's current balance.
* Emits `Delegated`.
* # <weight>
* - One extra DB entry.
* # </weight>
**/
delegate: AugmentedSubmittable<(to: AccountId | string | Uint8Array, conviction: Conviction | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
delegate: AugmentedSubmittable<(to: AccountId | string | Uint8Array, conviction: Conviction | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number | Uint8Array, balance: BalanceOf | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Schedule an emergency cancellation of a referendum. Cannot happen twice to the same
* referendum.
* The dispatch origin of this call must be `CancellationOrigin`.
* -`ref_index`: The index of the referendum to cancel.
* # <weight>
* - Depends on size of storage vec `VotersFor` for this referendum.
* - `O(1)`.
* # </weight>
**/
emergencyCancel: AugmentedSubmittable<(refIndex: ReferendumIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
@@ -287,7 +292,7 @@ declare module '@polkadot/api/types/submittable' {
* The dispatch of this call must be `FastTrackOrigin`.
* - `proposal_hash`: The hash of the current external proposal.
* - `voting_period`: The period that is allowed for voting on this proposal. Increased to
* `EmergencyVotingPeriod` if too low.
* `FastTrackVotingPeriod` if too low.
* - `delay`: The number of block after voting has ended in approval and this should be
* enacted. This doesn't have a minimum amount.
* Emits `Started`.
@@ -305,7 +310,7 @@ declare module '@polkadot/api/types/submittable' {
* - `encoded_proposal`: The preimage of a proposal.
* Emits `PreimageNoted`.
* # <weight>
* - Dependent on the size of `encoded_proposal`.
* - Dependent on the size of `encoded_proposal` and length of dispatch queue.
* # </weight>
**/
noteImminentPreimage: AugmentedSubmittable<(encodedProposal: Bytes | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
@@ -340,11 +345,57 @@ declare module '@polkadot/api/types/submittable' {
* - `value`: The amount of deposit (must be at least `MinimumDeposit`).
* Emits `Proposed`.
* # <weight>
* - `O(1)`.
* - `O(P)`
* - P is the number proposals in the `PublicProps` vec.
* - Two DB changes, one DB entry.
* # </weight>
**/
propose: AugmentedSubmittable<(proposalHash: Hash | string | Uint8Array, value: Compact<BalanceOf> | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Delegate the voting power (with some given conviction) of a proxied account.
* The balance delegated is locked for as long as it's delegated, and thereafter for the
* time appropriate for the conviction's lock period.
* The dispatch origin of this call must be _Signed_, and the signing account must have
* been set as the proxy account for `target`.
* - `target`: The account whole voting power shall be delegated and whose balance locked.
* This account must either:
* - be delegating already; or
* - have no voting activity (if there is, then it will need to be removed/consolidated
* through `reap_vote` or `unvote`).
* - `to`: The account whose voting the `target` account's voting power will follow.
* - `conviction`: The conviction that will be attached to the delegated votes. When the
* account is undelegated, the funds will be locked for the corresponding period.
* - `balance`: The amount of the account's balance to be used in delegating. This must
* not be more than the account's current balance.
* Emits `Delegated`.
* # <weight>
* # </weight>
**/
proxyDelegate: AugmentedSubmittable<(to: AccountId | string | Uint8Array, conviction: Conviction | ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number | Uint8Array, balance: BalanceOf | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Remove a proxied vote for a referendum.
* Exactly equivalent to `remove_vote` except that it operates on the account that the
* sender is a proxy for.
* The dispatch origin of this call must be _Signed_ and the signing account must be a
* proxy for some other account which has a registered vote for the referendum of `index`.
* - `index`: The index of referendum of the vote to be removed.
* # <weight>
* - `O(R + log R)` where R is the number of referenda that `target` has voted on.
* # </weight>
**/
proxyRemoveVote: AugmentedSubmittable<(index: ReferendumIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Undelegate the voting power of a proxied account.
* Tokens may be unlocked following once an amount of time consistent with the lock period
* of the conviction with which the delegation was issued.
* The dispatch origin of this call must be _Signed_ and the signing account must be a
* proxy for some other account which is currently delegating.
* Emits `Undelegated`.
* # <weight>
* - O(1).
* # </weight>
**/
proxyUndelegate: AugmentedSubmittable<() => SubmittableExtrinsic<ApiType>>;
/**
* Vote in a referendum on behalf of a stash. If `vote.is_aye()`, the vote is to enact
* the proposal; otherwise it is a vote to keep the status quo.
@@ -356,7 +407,7 @@ declare module '@polkadot/api/types/submittable' {
* - One DB change, one DB entry.
* # </weight>
**/
proxyVote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
proxyVote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: AccountVote | { Standard: any } | { Split: any } | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Remove an expired proposal preimage and collect the deposit.
* The dispatch origin of this call must be _Signed_.
@@ -370,23 +421,65 @@ declare module '@polkadot/api/types/submittable' {
* # </weight>
**/
reapPreimage: AugmentedSubmittable<(proposalHash: Hash | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Remove a vote for a referendum.
* If the `target` is equal to the signer, then this function is exactly equivalent to
* `remove_vote`. If not equal to the signer, then the vote must have expired,
* either because the referendum was cancelled, because the voter lost the referendum or
* because the conviction period is over.
* The dispatch origin of this call must be _Signed_.
* - `target`: The account of the vote to be removed; this account must have voted for
* referendum `index`.
* - `index`: The index of referendum of the vote to be removed.
* # <weight>
* - `O(R + log R)` where R is the number of referenda that `target` has voted on.
* # </weight>
**/
removeOtherVote: AugmentedSubmittable<(target: AccountId | string | Uint8Array, index: ReferendumIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Remove a vote for a referendum.
* If:
* - the referendum was cancelled, or
* - the referendum is ongoing, or
* - the referendum has ended such that
* - the vote of the account was in opposition to the result; or
* - there was no conviction to the account's vote; or
* - the account made a split vote
* ...then the vote is removed cleanly and a following call to `unlock` may result in more
* funds being available.
* If, however, the referendum has ended and:
* - it finished corresponding to the vote of the account, and
* - the account made a standard vote with conviction, and
* - the lock period of the conviction is not over
* ...then the lock will be aggregated into the overall account's lock, which may involve
* *overlocking* (where the two locks are combined into a single lock that is the maximum
* of both the amount locked and the time is it locked for).
* The dispatch origin of this call must be _Signed_, and the signer must have a vote
* registered for referendum `index`.
* - `index`: The index of referendum of the vote to be removed.
* # <weight>
* - `O(R + log R)` where R is the number of referenda that `target` has voted on.
* # </weight>
**/
removeVote: AugmentedSubmittable<(index: ReferendumIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Signals agreement with a particular proposal.
* The dispatch origin of this call must be _Signed_ and the sender
* must have funds to cover the deposit, equal to the original deposit.
* - `proposal`: The index of the proposal to second.
* # <weight>
* - `O(1)`.
* - `O(S)`.
* - S is the number of seconds a proposal already has.
* - One DB entry.
* # </weight>
**/
second: AugmentedSubmittable<(proposal: Compact<PropIndex> | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Undelegate vote.
* Must be sent from an account that has called delegate previously.
* The tokens will be reduced from an indefinite lock to the maximum
* possible according to the conviction of the prior delegation.
* The dispatch origin of this call must be _Signed_.
* Undelegate the voting power of the sending account.
* Tokens may be unlocked following once an amount of time consistent with the lock period
* of the conviction with which the delegation was issued.
* The dispatch origin of this call must be _Signed_ and the signing account must be
* currently delegating.
* Emits `Undelegated`.
* # <weight>
* - O(1).
@@ -397,7 +490,6 @@ declare module '@polkadot/api/types/submittable' {
* Unlock tokens that have an expired lock.
* The dispatch origin of this call must be _Signed_.
* - `target`: The account to remove the lock on.
* Emits `Unlocked`.
* # <weight>
* - `O(1)`.
* # </weight>
@@ -413,6 +505,7 @@ declare module '@polkadot/api/types/submittable' {
* - One DB clear.
* - Performs a binary search on `existing_vetoers` which should not
* be very large.
* - O(log v), v is number of `existing_vetoers`
* # </weight>
**/
vetoExternal: AugmentedSubmittable<(proposalHash: Hash | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
@@ -423,11 +516,12 @@ declare module '@polkadot/api/types/submittable' {
* - `ref_index`: The index of the referendum to vote for.
* - `vote`: The vote configuration.
* # <weight>
* - `O(1)`.
* - `O(R)`.
* - R is the number of referendums the voter has voted on.
* - One DB change, one DB entry.
* # </weight>
**/
vote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: Vote | { aye: boolean; conviction?: ('None' | 'Locked1x' | 'Locked2x' | 'Locked3x' | 'Locked4x' | 'Locked5x' | 'Locked6x') | number } | boolean | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
vote: AugmentedSubmittable<(refIndex: Compact<ReferendumIndex> | AnyNumber | Uint8Array, vote: AccountVote | { Standard: any } | { Split: any } | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
};
elections: {
[index: string]: SubmittableExtrinsicFunction<ApiType>;
@@ -601,7 +695,7 @@ declare module '@polkadot/api/types/submittable' {
* - One event.
* # </weight>
**/
provideJudgement: AugmentedSubmittable<(regIndex: Compact<RegistrarIndex> | AnyNumber | Uint8Array, target: LookupSource | Address | AccountId | AccountIndex | string | Uint8Array, judgement: IdentityJudgement | { unknown: any } | { feePaid: any } | { reasonable: any } | { knownGood: any } | { outOfDate: any } | { lowQuality: any } | { erroneous: any } | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
provideJudgement: AugmentedSubmittable<(regIndex: Compact<RegistrarIndex> | AnyNumber | Uint8Array, target: LookupSource | Address | AccountId | AccountIndex | string | Uint8Array, judgement: IdentityJudgement | { Unknown: any } | { FeePaid: any } | { Reasonable: any } | { KnownGood: any } | { OutOfDate: any } | { LowQuality: any } | { Erroneous: any } | string | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Request a judgement from a registrar.
* Payment: At most `max_fee` will be reserved for payment to the registrar if judgement
@@ -688,7 +782,7 @@ declare module '@polkadot/api/types/submittable' {
* one storage-exists.
* # </weight>
**/
setSubs: AugmentedSubmittable<(subs: Vec<ITuple<[AccountId, Data]>> | ([AccountId | string | Uint8Array, Data | { none: any } | { raw: any } | { blakeTwo256: any } | { sha256: any } | { keccak256: any } | { shaThree256: any } | string | Uint8Array])[]) => SubmittableExtrinsic<ApiType>>;
setSubs: AugmentedSubmittable<(subs: Vec<ITuple<[AccountId, Data]>> | ([AccountId | string | Uint8Array, Data | { None: any } | { Raw: any } | { BlakeTwo256: any } | { Sha256: any } | { Keccak256: any } | { ShaThree256: any } | string | Uint8Array])[]) => SubmittableExtrinsic<ApiType>>;
};
imOnline: {
[index: string]: SubmittableExtrinsicFunction<ApiType>;
@@ -1220,8 +1314,8 @@ declare module '@polkadot/api/types/submittable' {
* - Independent of the arguments. Moderate complexity.
* - O(1).
* - Three extra DB entries.
* NOTE: Two of the storage writes (`Self::bonded`, `Self::payee`) are _never_ cleaned unless
* the `origin` falls below _existential deposit_ and gets removed as dust.
* NOTE: Two of the storage writes (`Self::bonded`, `Self::payee`) are _never_ cleaned
* unless the `origin` falls below _existential deposit_ and gets removed as dust.
* # </weight>
**/
bond: AugmentedSubmittable<(controller: LookupSource | Address | AccountId | AccountIndex | string | Uint8Array, value: Compact<BalanceOf> | AnyNumber | Uint8Array, payee: RewardDestination | ('Staked' | 'Stash' | 'Controller') | number | Uint8Array) => SubmittableExtrinsic<ApiType>>;
@@ -1292,7 +1386,7 @@ declare module '@polkadot/api/types/submittable' {
* The dispatch origin for this call must be _Signed_ by the controller, not the stash.
* # <weight>
* - The transaction's complexity is proportional to the size of `targets`,
* which is capped at `MAX_NOMINATIONS`.
* which is capped at CompactAssignments::LIMIT.
* - Both the reads and writes follow a similar pattern.
* # </weight>
**/
@@ -1387,6 +1481,69 @@ declare module '@polkadot/api/types/submittable' {
* The ideal number of validators.
**/
setValidatorCount: AugmentedSubmittable<(updated: Compact<u32> | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Submit a phragmen result to the chain. If the solution:
* 1. is valid.
* 2. has a better score than a potentially existing solution on chain.
* then, it will be _put_ on chain.
* A solution consists of two pieces of data:
* 1. `winners`: a flat vector of all the winners of the round.
* 2. `assignments`: the compact version of an assignment vector that encodes the edge
* weights.
* Both of which may be computed using [`phragmen`], or any other algorithm.
* Additionally, the submitter must provide:
* - The `score` that they claim their solution has.
* Both validators and nominators will be represented by indices in the solution. The
* indices should respect the corresponding types ([`ValidatorIndex`] and
* [`NominatorIndex`]). Moreover, they should be valid when used to index into
* [`SnapshotValidators`] and [`SnapshotNominators`]. Any invalid index will cause the
* solution to be rejected. These two storage items are set during the election window and
* may be used to determine the indices.
* A solution is valid if:
* 0. It is submitted when [`EraElectionStatus`] is `Open`.
* 1. Its claimed score is equal to the score computed on-chain.
* 2. Presents the correct number of winners.
* 3. All indexes must be value according to the snapshot vectors. All edge values must
* also be correct and should not overflow the granularity of the ratio type (i.e. 256
* or billion).
* 4. For each edge, all targets are actually nominated by the voter.
* 5. Has correct self-votes.
* A solutions score is consisted of 3 parameters:
* 1. `min { support.total }` for each support of a winner. This value should be maximized.
* 2. `sum { support.total }` for each support of a winner. This value should be minimized.
* 3. `sum { support.total^2 }` for each support of a winner. This value should be
* minimized (to ensure less variance)
* # <weight>
* E: number of edges. m: size of winner committee. n: number of nominators. d: edge degree
* (16 for now) v: number of on-chain validator candidates.
* NOTE: given a solution which is reduced, we can enable a new check the ensure `|E| < n +
* m`. We don't do this _yet_, but our offchain worker code executes it nonetheless.
* major steps (all done in `check_and_replace_solution`):
* - Storage: O(1) read `ElectionStatus`.
* - Storage: O(1) read `PhragmenScore`.
* - Storage: O(1) read `ValidatorCount`.
* - Storage: O(1) length read from `SnapshotValidators`.
* - Storage: O(v) reads of `AccountId` to fetch `snapshot_validators`.
* - Memory: O(m) iterations to map winner index to validator id.
* - Storage: O(n) reads `AccountId` to fetch `snapshot_nominators`.
* - Memory: O(n + m) reads to map index to `AccountId` for un-compact.
* - Storage: O(e) accountid reads from `Nomination` to read correct nominations.
* - Storage: O(e) calls into `slashable_balance_of_extended` to convert ratio to staked.
* - Memory: build_support_map. O(e).
* - Memory: evaluate_support: O(E).
* - Storage: O(e) writes to `QueuedElected`.
* - Storage: O(1) write to `QueuedScore`
* The weight of this call is 1/10th of the blocks total weight.
* # </weight>
**/
submitElectionSolution: AugmentedSubmittable<(winners: Vec<ValidatorIndex> | (ValidatorIndex | AnyNumber | Uint8Array)[], compactAssignments: CompactAssignments | { votes1?: any; votes2?: any; votes3?: any; votes4?: any; votes5?: any; votes6?: any; votes7?: any; votes8?: any; votes9?: any; votes10?: any; votes11?: any; votes12?: any; votes13?: any; votes14?: any; votes15?: any; votes16?: any } | string | Uint8Array, score: PhragmenScore, era: EraIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Unsigned version of `submit_election_solution`.
* Note that this must pass the [`ValidateUnsigned`] check which only allows transactions
* from the local node to be included. In other words, only the block author can include a
* transaction in the block.
**/
submitElectionSolutionUnsigned: AugmentedSubmittable<(winners: Vec<ValidatorIndex> | (ValidatorIndex | AnyNumber | Uint8Array)[], compactAssignments: CompactAssignments | { votes1?: any; votes2?: any; votes3?: any; votes4?: any; votes5?: any; votes6?: any; votes7?: any; votes8?: any; votes9?: any; votes10?: any; votes11?: any; votes12?: any; votes13?: any; votes14?: any; votes15?: any; votes16?: any } | string | Uint8Array, score: PhragmenScore, era: EraIndex | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>>;
/**
* Schedule a portion of the stash to be unlocked ready for transfer out after the bond
* period ends. If this leaves an amount actively bonded less than
@@ -1488,7 +1645,6 @@ declare module '@polkadot/api/types/submittable' {
* Kill some items from storage.
**/
killStorage: AugmentedSubmittable<(keys: Vec<Key> | (Key | string | Uint8Array)[]) => SubmittableExtrinsic<ApiType>>;
migrateAccounts: AugmentedSubmittable<(accounts: Vec<AccountId> | (AccountId | string | Uint8Array)[]) => SubmittableExtrinsic<ApiType>>;
/**
* Make some on-chain remark.
**/
+39 -17
View File
@@ -22,7 +22,7 @@ import { Metadata, Null, Option, Raw, Text, TypeRegistry, u64 } from '@polkadot/
import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage';
import { DEFAULT_VERSION as EXTRINSIC_DEFAULT_VERSION } from '@polkadot/types/extrinsic/constants';
import StorageKey, { StorageEntry, unwrapStorageType } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, isNull, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, compactStripLength, isNull, isUndefined, logger, u8aConcat, u8aToHex } from '@polkadot/util';
import { createSubmittable } from '../submittable';
import augmentObject from '../util/augmentObject';
@@ -41,7 +41,9 @@ interface MetaDecoration {
type LinkageData = ITuple<[Codec, Linkage<Codec>]>;
const PAGE_SIZE_KEYS = 256;
const PAGE_SIZE_VALS = PAGE_SIZE_KEYS / 2;
const PAGE_SIZE_VALS = PAGE_SIZE_KEYS;
const l = logger('api/init');
export default abstract class Decorate<ApiType extends ApiTypes> extends Events {
public readonly registry: Registry;
@@ -213,21 +215,40 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
this.filterRpcMethods(methods);
}
protected filterRpcMethods (methods: string[]): void {
// this is true when the RPC has entries
const hasResults = methods.length !== 0;
protected filterRpcMethods (exposed: string[]): void {
const hasResults = exposed.length !== 0;
const allKnown = [...this._rpcCore.mapping.entries()];
const allKeys = allKnown.reduce((allKeys: string[], [, { alias, method, section, pubsub }]): string[] => {
allKeys.push(`${section}_${method}`);
if (pubsub) {
allKeys.push(`${section}_${pubsub[1]}`);
allKeys.push(`${section}_${pubsub[2]}`);
}
if (alias) {
allKeys.push(...alias);
}
return allKeys;
}, []);
const unknown = exposed.filter((key) => !allKeys.includes(key));
if (unknown.length) {
l.warn(`RPC methods not decorated: ${unknown.join(', ')}}`);
}
// loop through all entries we have (populated in decorate) and filter as required
[...this._rpcCore.mapping.entries()]
.filter(([key, { isOptional }]): boolean =>
// only remove when we have results and method missing, or with no results if optional
hasResults
? !methods.includes(key) && key !== 'rpc_methods' // rpc_methods doesn't appear, v1
: isOptional || key === 'rpc_methods' // we didn't find this one, remove
// only remove when we have results and method missing, or with no results if optional
allKnown
.filter(([key]): boolean => hasResults
? !exposed.includes(key) && key !== 'rpc_methods' // rpc_methods doesn't appear, v1
: key === 'rpc_methods' // we didn't find this one, remove
)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.forEach(([_, { method, section }]): void => {
delete (this._rpc as any)[section][method];
delete (this._rpcCore as any)[section][method];
delete (this._rx.rpc as any)[section][method];
});
}
@@ -482,12 +503,13 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
combineLatest([
of(keys),
from(Array(Math.ceil(keys.length / PAGE_SIZE_VALS)).fill(0)).pipe(
// FIXME New RPC to take care of this in the works...
concatMap((_, index): Observable<Option<Raw>[]> =>
this._rpcCore.state.subscribeStorage<Option<Raw>[]>(
keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS)
).pipe(take(1))
),
concatMap((_, index): Observable<Option<Raw>[]> => {
const keyset = keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS);
return this._rpcCore.state.queryStorageAt
? this._rpcCore.state.queryStorageAt<Option<Raw>[]>(keyset)
: this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keyset).pipe(take(1));
}),
toArray(),
map((valsArr: Option<Raw>[][]): Option<Raw>[] =>
valsArr.reduce((result: Option<Raw>[], vals): Option<Raw>[] =>
+6 -5
View File
@@ -9,7 +9,7 @@ import { Subscription, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Metadata, Text } from '@polkadot/types';
import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/Extrinsic';
import { getMetadataTypes, getSpecTypes, getUserTypes } from '@polkadot/types/known';
import { getMetadataTypes, getSpecTypes } from '@polkadot/types-known';
import { logger } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
@@ -140,7 +140,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
this._runtimeMetadata = metadata;
this._runtimeVersion = version;
this.registerTypes(getSpecTypes(this.registry, this._runtimeChain as Text, version));
this.registerTypes(getSpecTypes(this.registry, this._runtimeChain as Text, version.specName, version.specVersion));
this.injectMetadata(metadata, false);
return true;
@@ -161,8 +161,7 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
// do the setup for the specific chain
this.registry.setChainProperties(chainProps);
this.registerTypes(getSpecTypes(this.registry, chain, runtimeVersion));
this.registerTypes(getUserTypes(this.registry, chain, runtimeVersion));
this.registerTypes(getSpecTypes(this.registry, chain, runtimeVersion.specName, runtimeVersion.specVersion));
this.subscribeUpdates();
// filter the RPC methods (this does an rpc-methods call)
@@ -217,7 +216,9 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
try {
const [hasMeta, cryptoReady] = await Promise.all([
this.loadMeta(),
cryptoWaitReady()
this._options.initWasm === false
? Promise.resolve(true)
: cryptoWaitReady()
]);
if (hasMeta && !this._isReady && cryptoReady) {
+3
View File
@@ -159,6 +159,9 @@ async function tx (api: ApiPromise, keyring: TestKeyringMap): Promise<void> {
second.signAndSend('123', (result): void => {
console.log(result);
});
// it handles enum inputs correctly
await api.tx.democracy.proxyVote(123, { Split: { yay: 123, nay: 456 } }).signAndSend(keyring.alice);
}
async function main (): Promise<void> {
+1 -1
View File
@@ -13,7 +13,7 @@ import { hexToU8a } from '@polkadot/util';
import { SingleAccountSigner } from '../../test/util';
import ApiPromise from './Api';
const TRANSFER_SIG = '0x62bfcd07a9f50ba32ed9e2c41ca11267ac62a24c95949e25efdccc4989d48573f84754dbbaa1d0937ceb888ad8a2cf71daced1545a839d9d6cceac33a5771401';
const TRANSFER_SIG = '0xa1d493ab6b74550da434fbc153a739429be68c9d30e59e631fb5d08f348504deefb5577f4824e138741ad102f479109827e8fa9552812a3e5f505a24d10e820f';
describe('ApiPromise', (): void => {
const registry = new TypeRegistry();
+5 -1
View File
@@ -21,7 +21,7 @@ export default class SubmittableResult implements ISubmittableResult {
}
public get isError (): boolean {
return this.status.isDropped || this.status.isFinalityTimeout || this.status.isInvalid || this.status.isRetracted || this.status.isUsurped;
return this.status.isDropped || this.status.isFinalityTimeout || this.status.isInvalid || this.status.isUsurped;
}
public get isFinalized (): boolean {
@@ -32,6 +32,10 @@ export default class SubmittableResult implements ISubmittableResult {
return this.status.isInBlock;
}
public get isWarning (): boolean {
return this.status.isRetracted;
}
/**
* @description Filters EventRecords for the specified method & section (there could be multiple)
*/
+28 -58
View File
@@ -8,9 +8,9 @@ import { Callback, Codec, Constructor, IKeyringPair, Registry, SignatureOptions,
import { ApiInterfaceRx, ApiTypes, SignerResult } from '../types';
import { AddressOrPair, SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types';
import { Observable, combineLatest, of } from 'rxjs';
import { Observable, of } from 'rxjs';
import { first, map, mapTo, mergeMap, switchMap, tap } from 'rxjs/operators';
import { assert, isBn, isFunction, isNumber, isUndefined } from '@polkadot/util';
import { assert, isBn, isFunction, isNumber } from '@polkadot/util';
import { filterEvents, isKeyringPair } from '../util';
import ApiBase from '../base';
@@ -22,13 +22,6 @@ interface SubmittableOptions<ApiType extends ApiTypes> {
decorateMethod: ApiBase<ApiType>['decorateMethod'];
}
// The default for 6s allowing for 5min eras. When translating this to faster blocks -
// - 4s = (10 / 15) * 5 = 3.33m
// - 2s = (10 / 30) * 5 = 1.66m
const BLOCKTIME = 6;
const ONE_MINUTE = 60 / BLOCKTIME;
const DEFAULT_MORTAL_LENGTH = 5 * ONE_MINUTE;
export default function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
// an instance of the base extrinsic for us to extend
const ExtrinsicBase = api.registry.createClass('Extrinsic');
@@ -49,11 +42,11 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
return decorateMethod(
(): Observable<RuntimeDispatchInfo> =>
this.#getPrelimState(address, allOptions).pipe(
api.derive.tx.signingInfo(address, allOptions.nonce, allOptions.era).pipe(
first(),
switchMap(([nonce, header]): Observable<RuntimeDispatchInfo> => {
switchMap((signingInfo): Observable<RuntimeDispatchInfo> => {
// setup our options (same way as in signAndSend)
const eraOptions = this.#makeEraOptions(allOptions, { header, nonce });
const eraOptions = this.#makeEraOptions(allOptions, signingInfo);
const signOptions = this.#makeSignOptions(eraOptions, {});
// add a fake signature to the extrinsic
@@ -125,23 +118,7 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
)(statusCb);
}
#getPrelimState = (address: string, options: Partial<SignerOptions>): Observable<[Index, Header | null]> => {
return combineLatest([
// if we have a nonce already, don't retrieve the latest, use what is there
isUndefined(options.nonce)
? api.derive.balances.account(address).pipe(
map(({ accountNonce }): Index => accountNonce)
)
: of(this.registry.createType('Index', options.nonce)),
// if we have an era provided already or eraLength is <= 0 (immortal)
// don't get the latest block, just pass null, handle in mergeMap
(isUndefined(options.era) || (isNumber(options.era) && options.era > 0))
? api.rpc.chain.getHeader()
: of(null)
]);
}
#makeEraOptions = (options: Partial<SignerOptions>, { header, nonce }: { header: Header | null; nonce: Index }): SignatureOptions => {
#makeEraOptions = (options: Partial<SignerOptions>, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions => {
if (!header) {
if (isNumber(options.era)) {
// since we have no header, it is immortal, remove any option overrides
@@ -157,7 +134,7 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
blockHash: header.hash,
era: this.registry.createType('ExtrinsicEra', {
current: header.number,
period: options.era || DEFAULT_MORTAL_LENGTH
period: options.era || mortalLength
}),
nonce
});
@@ -166,9 +143,9 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
#makeSignOptions = (options: Partial<SignerOptions>, extras: { blockHash?: Hash; era?: ExtrinsicEra; nonce?: Index }): SignatureOptions => {
return {
blockHash: api.genesisHash,
genesisHash: api.genesisHash,
...options,
...extras,
genesisHash: api.genesisHash,
runtimeVersion: api.runtimeVersion,
version: api.extrinsicType
} as SignatureOptions;
@@ -191,15 +168,15 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
const options = this.#optionsOrNonce(optionsOrNonce);
let updateId: number | undefined;
return this.#getPrelimState(address, options).pipe(
return api.derive.tx.signingInfo(address, options.nonce, options.era).pipe(
first(),
mergeMap(async ([nonce, header]): Promise<void> => {
const eraOptions = this.#makeEraOptions(options, { header, nonce });
mergeMap(async (signingInfo): Promise<void> => {
const eraOptions = this.#makeEraOptions(options, signingInfo);
if (isKeyringPair(account)) {
this.sign(account, eraOptions);
} else {
updateId = await this.#signViaSigner(address, eraOptions, header);
updateId = await this.#signViaSigner(address, eraOptions, signingInfo.header);
}
}),
mapTo(updateId)
@@ -215,13 +192,10 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
? status.asInBlock
: status.asFinalized;
return combineLatest([
api.rpc.chain.getBlock(blockHash),
api.query.system.events.at(blockHash)
]).pipe(
map(([signedBlock, allEvents]): ISubmittableResult =>
return api.derive.tx.events(blockHash).pipe(
map(({ block, events }): ISubmittableResult =>
new SubmittableResult({
events: filterEvents(this.hash, signedBlock, allEvents, status),
events: filterEvents(this.hash, block, events, status),
status
})
)
@@ -229,26 +203,22 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
}
#observeSend = (updateId = -1): Observable<Hash> => {
return api.rpc.author
.submitExtrinsic(this)
.pipe(
tap((hash): void => {
this.#updateSigner(updateId, hash);
})
);
return api.rpc.author.submitExtrinsic(this).pipe(
tap((hash): void => {
this.#updateSigner(updateId, hash);
})
);
}
#observeSubscribe = (updateId = -1): Observable<ISubmittableResult> => {
return api.rpc.author
.submitAndWatchExtrinsic(this)
.pipe(
switchMap((status): Observable<ISubmittableResult> =>
this.#observeStatus(status)
),
tap((status): void => {
this.#updateSigner(updateId, status);
})
);
return api.rpc.author.submitAndWatchExtrinsic(this).pipe(
switchMap((status): Observable<ISubmittableResult> =>
this.#observeStatus(status)
),
tap((status): void => {
this.#updateSigner(updateId, status);
})
);
}
// NOTE here we actually override nonce if it was specified (backwards compat for
+10 -6
View File
@@ -8,18 +8,20 @@ import '@polkadot/api/augment';
import BN from 'bn.js';
import { DeriveCustom, ExactDerive } from '@polkadot/api-derive';
import { Constants } from '@polkadot/metadata/Decorated/types';
import { RpcInterface, UserRpc } from '@polkadot/rpc-core/types';
import { RpcInterface } from '@polkadot/rpc-core/types';
import { Metadata } from '@polkadot/types';
import { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
import { Signer, SignerResult, SignatureOptions, Registry, RegisteredTypes } from '@polkadot/types/types';
import { DefinitionRpc, DefinitionRpcSub, Signer, SignatureOptions, Registry, RegisteredTypes } from '@polkadot/types/types';
import ApiBase from '../base';
import { DeriveAllSections } from '../util/decorate';
import ApiBase from '../base';
import { DecoratedRpc } from './rpc';
import { QueryableStorage, QueryableStorageMulti } from './storage';
import { SubmittableExtrinsics } from './submittable';
import { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
export { Signer, SignerResult } from '@polkadot/types/types';
export { default as ApiBase } from '../base';
export * from '../submittable/types';
export * from './base';
export * from './rpc';
@@ -31,6 +33,10 @@ export interface ApiOptions extends RegisteredTypes {
* @description Add custom derives to be injected
*/
derives?: DeriveCustom;
/**
* @description Control the initialization of the wasm libraries. When not specified, it defaults to `true`, initializing the wasm libraries, set to `false` to not initialize wasm. (No sr25519 support)
*/
initWasm?: boolean;
/**
* @description pre-bundles is a map of 'genesis hash and runtime spec version' as key to a metadata hex string
* if genesis hash and runtime spec version matches, then use metadata, else fetch it from chain
@@ -48,7 +54,7 @@ export interface ApiOptions extends RegisteredTypes {
/**
* @description User-defined RPC methods
*/
rpc?: UserRpc;
rpc?: Record<string, Record<string, DefinitionRpc | DefinitionRpcSub>>;
/**
* @description An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
*/
@@ -83,5 +89,3 @@ export interface SignerOptions extends SignatureOptions {
blockNumber: BN;
genesisHash: Hash;
}
export { ApiBase, Signer, SignerResult };
+1
View File
@@ -4,6 +4,7 @@
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { Type } from '@polkadot/types';
import { assert, isUndefined } from '@polkadot/util';
function sig ({ method, section }: StorageEntry, ...args: Type[]): string {
-24
View File
@@ -1,24 +0,0 @@
# @polkadot/jsonrpc
A definition of all the methods exposed in a general Polkadot client application. These are used not only to provide a comprehensive code-generated document of the available methods, but are also used in the API to auto-generate endpoints with the required type-checking.
For a list of currently exposed methods, see the [method documentation](docs/substrate/rpc.md).
## Usage
Installation -
```
yarn add @polkadot/jsonrpc
```
## Adding methods
As methods are added, simply adding the name, inputs & output will prepare it for use.
- Add the method to the correct file in [src/rpc/](src/rpc/) (Input/Output types as cross-referenced from the canonical implementation and match one-to-one)
- Should a new type be required, add it to the type list, [src/types.js](src/types.js) (Required for TSLint type checking)
## Classes
[Classes](SUMMARY.md)
-34
View File
@@ -1,34 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
// NOTE order here is the same as in Rust, alphabetical below
const nextIndex: RpcMethodOpt = {
description: 'Retrieves the next accountIndex as available on the node',
isOptional: true,
params: [
createParam('accountId', 'AccountId')
],
type: 'Index'
};
const section = 'account';
/**
* @summary Calls to account-specific information.
*/
export default {
isDeprecated: false,
isHidden: false,
description: '(Optional) Methods that retrieves account-specific information',
section,
methods: {
nextIndex: createMethod(section, 'nextIndex', nextIndex)
}
};
-100
View File
@@ -1,100 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
const hasKey: RpcMethodOpt = {
description: 'Returns true if the keystore has private keys for the given public key and key type.',
params: [
createParam('publicKey', 'Bytes'),
createParam('keyType', 'Text')
],
type: 'bool'
};
const hasSessionKeys: RpcMethodOpt = {
description: 'Returns true if the keystore has private keys for the given session public keys.',
params: [
createParam('sessionKeys', 'Bytes')
],
type: 'bool'
};
const removeExtrinsic: RpcMethodOpt = {
description: 'Remove given extrinsic from the pool and temporarily ban it to prevent reimporting',
params: [
createParam('bytesOrHash', 'Vec<ExtrinsicOrHash>')
],
type: 'Vec<Hash>'
};
const insertKey: RpcMethodOpt = {
description: 'Insert a key into the keystore.',
params: [
createParam('keyType', 'Text'),
createParam('suri', 'Text'),
createParam('publicKey', 'Bytes')
],
type: 'Bytes'
};
const rotateKeys: RpcMethodOpt = {
description: 'Generate new session keys and returns the corresponding public keys',
params: [],
type: 'Bytes'
};
const pendingExtrinsics: RpcMethodOpt = {
description: 'Returns all pending extrinsics, potentially grouped by sender',
params: [],
type: 'Vec<Extrinsic>'
};
const submitExtrinsic: RpcMethodOpt = {
isSigned: true,
description: 'Submit a fully formatted extrinsic for block inclusion',
params: [
createParam('extrinsic', 'Extrinsic')
],
type: 'Hash'
};
const submitAndWatchExtrinsic: RpcMethodOpt = {
description: 'Submit and subscribe to watch an extrinsic until unsubscribed',
isSigned: true,
params: [
createParam('extrinsic', 'Extrinsic')
],
pubsub: [
'extrinsicUpdate',
'submitAndWatchExtrinsic',
'unwatchExtrinsic'
],
type: 'ExtrinsicStatus'
};
const section = 'author';
/**
* @summary Calls to work with authors & contributors.
*/
export default {
isDeprecated: false,
isHidden: false,
description: 'Authoring of network items',
section,
methods: {
hasKey: createMethod(section, 'hasKey', hasKey),
hasSessionKeys: createMethod(section, 'hasSessionKeys', hasSessionKeys),
insertKey: createMethod(section, 'insertKey', insertKey),
removeExtrinsic: createMethod(section, 'removeExtrinsic', removeExtrinsic),
rotateKeys: createMethod(section, 'rotateKeys', rotateKeys),
pendingExtrinsics: createMethod(section, 'pendingExtrinsics', pendingExtrinsics),
submitExtrinsic: createMethod(section, 'submitExtrinsic', submitExtrinsic),
submitAndWatchExtrinsic: createMethod(section, 'submitAndWatchExtrinsic', submitAndWatchExtrinsic)
}
};
-95
View File
@@ -1,95 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
// NOTE Order is the same as in Rust
const getHeader: RpcMethodOpt = {
description: 'Retrieves the header for a specific block',
params: [
createParam('hash', 'BlockHash', { isOptional: true })
],
type: 'Header'
};
const getBlock: RpcMethodOpt = {
description: 'Get header and body of a relay chain block',
params: [
createParam('hash', 'BlockHash', { isOptional: true })
],
type: 'SignedBlock'
};
const getBlockHash: RpcMethodOpt = {
description: 'Get the block hash for a specific block',
params: [
createParam('blockNumber', 'BlockNumber', { isOptional: true })
],
type: 'BlockHash'
};
const getFinalizedHead: RpcMethodOpt = {
description: 'Get hash of the last finalized block in the canon chain',
params: [],
type: 'BlockHash'
};
const subscribeNewHeads: RpcMethodOpt = {
description: 'Retrieves the best header via subscription',
params: [],
// NOTE These still has the aliassed version, compatible with 1.x
pubsub: [
'newHead',
'subscribeNewHead',
'unsubscribeNewHead'
],
type: 'Header'
};
const subscribeFinalizedHeads: RpcMethodOpt = {
description: 'Retrieves the best finalized header via subscription',
params: [],
pubsub: [
'finalizedHead',
'subscribeFinalizedHeads',
'unsubscribeFinalizedHeads'
],
type: 'Header'
};
const subscribeAllHeads: RpcMethodOpt = {
description: 'Retrieves the newest header via subscription',
params: [],
pubsub: [
'allHead',
'subscribeAllHeads',
'unsubscribeAllHeads'
],
type: 'Header'
};
const section = 'chain';
/**
* @summary Calls to retrieve chain data.
*/
export default {
isDeprecated: false,
isHidden: false,
description: 'Retrieval of chain data',
section,
methods: {
getBlock: createMethod(section, 'getBlock', getBlock),
getBlockHash: createMethod(section, 'getBlockHash', getBlockHash),
getFinalizedHead: createMethod(section, 'getFinalizedHead', getFinalizedHead),
getHeader: createMethod(section, 'getHeader', getHeader),
subscribeFinalizedHeads: createMethod(section, 'subscribeFinalizedHeads', subscribeFinalizedHeads),
subscribeNewHeads: createMethod(section, 'subscribeNewHeads', subscribeNewHeads),
subscribeAllHeads: createMethod(section, 'subscribeAllHeads', subscribeAllHeads)
}
};
-47
View File
@@ -1,47 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
// NOTE order here is the same as in Rust, alphabetical below
const call: RpcMethodOpt = {
description: 'Executes a call to a contract',
isOptional: true,
params: [
createParam('callRequest', 'ContractCallRequest'),
createParam('at', 'BlockHash', { isOptional: true })
],
type: 'ContractExecResult'
};
const getStorage: RpcMethodOpt = {
description: 'Returns the value under a specified storage key in a contract',
isOptional: true,
params: [
createParam('address', 'AccountId'),
createParam('key', 'H256'),
createParam('at', 'BlockHash', { isOptional: true })
],
type: 'Option<Bytes>'
};
const section = 'contracts';
/**
* @summary Calls to retrieve system info.
*/
export default {
isDeprecated: false,
isHidden: false,
description: '(Optional) Methods that performs actions on contracts',
section,
methods: {
call: createMethod(section, 'call', call),
getStorage: createMethod(section, 'getStorage', getStorage)
}
};
-24
View File
@@ -1,24 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt, RpcMethod } from '../types';
import { isUndefined } from '@polkadot/util';
/** @internal */
export default function createMethod (section: string, method: string, { description, isDeprecated = false, isHidden = false, isOptional = false, isSigned = false, params, pubsub, type }: RpcMethodOpt): RpcMethod {
return {
description,
isDeprecated,
isHidden,
isOptional,
isSigned,
isSubscription: !isUndefined(pubsub),
method,
params,
pubsub: pubsub || ['', '', ''],
section,
type
};
}
-19
View File
@@ -1,19 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { InterfaceTypes } from '@polkadot/types/types';
import { RpcParam } from '../types';
interface RpcParamOptions {
isOptional?: boolean;
}
/** @internal */
export default function createParam (name: string, type: keyof InterfaceTypes, { isOptional = false }: RpcParamOptions = { isOptional: false }): RpcParam {
return {
isOptional,
name,
type
};
}
-43
View File
@@ -1,43 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
const createBlock: RpcMethodOpt = {
description: 'Instructs the manual-seal authorship task to create a new block',
params: [
createParam('createEmpty', 'bool'),
createParam('finalize', 'bool'),
createParam('parentHash', 'Option<BlockHash>')
],
type: 'CreatedBlock'
};
const finalizeBlock: RpcMethodOpt = {
description: 'Instructs the manual-seal authorship task to finalize a block',
params: [
createParam('hash', 'BlockHash'),
createParam('justification', 'Option<Justification>')
],
type: 'bool'
};
const section = 'engine';
/**
* @summary Calls to interact with the block production engine
*/
export default {
isDeprecated: false,
isHidden: false,
description: 'Block production engine',
section,
methods: {
createBlock: createMethod(section, 'createBlock', createBlock),
finalizeBlock: createMethod(section, 'finalizeBlock', finalizeBlock)
}
};
-12
View File
@@ -1,12 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import interfaces from './';
describe('jsonrpc', (): void => {
it('exports the available interfaces', (): void => {
expect(interfaces).toBeDefined();
expect(Object.keys(interfaces).length).toBeGreaterThan(0);
});
});
-27
View File
@@ -1,27 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcSection } from './types';
import account from './account';
import author from './author';
import chain from './chain';
import contracts from './contracts';
import payment from './payment';
import rpc from './rpc';
import state from './state';
import system from './system';
const interfaces: Record<string, RpcSection> = {
account,
author,
chain,
contracts,
payment,
rpc,
state,
system
};
export default interfaces;
-35
View File
@@ -1,35 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
import createParam from './create/param';
// NOTE order here is the same as in Rust, alphabetical below
const queryInfo: RpcMethodOpt = {
description: 'Retrieves the fee information for an encoded extrinsic',
isOptional: true,
params: [
createParam('extrinsic', 'Bytes'),
createParam('at', 'BlockHash', { isOptional: true })
],
type: 'RuntimeDispatchInfo'
};
const section = 'payment';
/**
* @summary Calls to retrieve payment information.
*/
export default {
isDeprecated: false,
isHidden: false,
description: 'Methods that retrieves payment information, e.g. fee calculations',
section,
methods: {
queryInfo: createMethod(section, 'queryInfo', queryInfo)
}
};
-30
View File
@@ -1,30 +0,0 @@
// Copyright 2017-2020 @polkadot/jsonrpc authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcMethodOpt } from './types';
import createMethod from './create/method';
// NOTE order here is the same as in Rust, alphabetical below
const methods: RpcMethodOpt = {
description: 'Retrieves the list of RPC methods that are exposed by the node',
params: [],
type: 'RpcMethods'
};
const section = 'rpc';
/**
* @summary Calls to retrieve system info.
*/
export default {
isDeprecated: false,
isHidden: false,
description: 'Retrieves information about the RPC endpoints',
section,
methods: {
methods: createMethod(section, 'methods', methods)
}
};

Some files were not shown because too many files have changed in this diff Show More