|
@@ -1,11 +1,10 @@
|
|
-import { WsProvider, ApiPromise } from '@polkadot/api'
|
|
|
|
|
|
+import { ApiPromise, WsProvider } from '@polkadot/api'
|
|
import { types } from '@joystream/types'
|
|
import { types } from '@joystream/types'
|
|
import { AccountId, Hash } from '@polkadot/types/interfaces'
|
|
import { AccountId, Hash } from '@polkadot/types/interfaces'
|
|
import { config } from 'dotenv'
|
|
import { config } from 'dotenv'
|
|
import BN from 'bn.js'
|
|
import BN from 'bn.js'
|
|
import { Option, Vec } from '@polkadot/types'
|
|
import { Option, Vec } from '@polkadot/types'
|
|
-import { log } from './debug'
|
|
|
|
-import { ActiveEra } from './Types'
|
|
|
|
|
|
+import { wsLocation } from '../../config'
|
|
|
|
|
|
config()
|
|
config()
|
|
|
|
|
|
@@ -15,13 +14,9 @@ export class JoyApi {
|
|
api!: ApiPromise
|
|
api!: ApiPromise
|
|
|
|
|
|
constructor(endpoint?: string) {
|
|
constructor(endpoint?: string) {
|
|
- const wsEndpoint = endpoint || process.env.REACT_APP_WS_PROVIDER || 'ws://127.0.0.1:9944'
|
|
|
|
- this.endpoint = wsEndpoint
|
|
|
|
- this.isReady = (async () => {
|
|
|
|
- const api = await new ApiPromise({ provider: new WsProvider(wsEndpoint), types })
|
|
|
|
- .isReadyOrError
|
|
|
|
- return api
|
|
|
|
- })()
|
|
|
|
|
|
+ this.endpoint = wsLocation
|
|
|
|
+ this.isReady = (async () =>
|
|
|
|
+ await new ApiPromise({ provider: new WsProvider(wsLocation), types }).isReadyOrError)()
|
|
}
|
|
}
|
|
|
|
|
|
get init(): Promise<JoyApi> {
|
|
get init(): Promise<JoyApi> {
|
|
@@ -31,29 +26,6 @@ export class JoyApi {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- async totalIssuance(blockHash?: Hash) {
|
|
|
|
- const issuance =
|
|
|
|
- blockHash === undefined
|
|
|
|
- ? await this.api.query.balances.totalIssuance()
|
|
|
|
- : await this.api.query.balances.totalIssuance.at(blockHash)
|
|
|
|
-
|
|
|
|
- return issuance.toNumber()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- async systemData() {
|
|
|
|
- const [chain, nodeName, nodeVersion] = await Promise.all([
|
|
|
|
- this.api.rpc.system.chain(),
|
|
|
|
- this.api.rpc.system.name(),
|
|
|
|
- this.api.rpc.system.version(),
|
|
|
|
- ])
|
|
|
|
-
|
|
|
|
- return {
|
|
|
|
- chain: chain.toString(),
|
|
|
|
- nodeName: nodeName.toString(),
|
|
|
|
- nodeVersion: nodeVersion.toString(),
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
async finalizedHash() {
|
|
async finalizedHash() {
|
|
return this.api.rpc.chain.getFinalizedHead()
|
|
return this.api.rpc.chain.getFinalizedHead()
|
|
}
|
|
}
|
|
@@ -64,33 +36,6 @@ export class JoyApi {
|
|
return number.toNumber()
|
|
return number.toNumber()
|
|
}
|
|
}
|
|
|
|
|
|
- async getActiveErasForBlock(address: string, blockStart: number): Promise<ActiveEra[] | undefined> {
|
|
|
|
- const stash = address
|
|
|
|
- const startHash = (await this.api.rpc.chain.getBlockHash(blockStart))
|
|
|
|
- const startEra = (await this.api.query.staking.activeEra.at(startHash)).unwrap().index.toNumber()
|
|
|
|
- const startTimestamp = new Date((await this.api.query.timestamp.now.at(startHash)).toNumber()).toISOString()
|
|
|
|
- const eraPoints = await this.api.query.staking.erasRewardPoints.at(startHash, startEra)
|
|
|
|
- let data = undefined
|
|
|
|
- eraPoints.individual.forEach((points, author) => {
|
|
|
|
- log(`Author Points [${author}]`)
|
|
|
|
- log(`Individual Points [${points}]`)
|
|
|
|
- if (author.toString() === stash) {
|
|
|
|
- const pn = Number(points.toBigInt())
|
|
|
|
- const activeEra: ActiveEra = {
|
|
|
|
- id: blockStart,
|
|
|
|
- era: startEra,
|
|
|
|
- hash: startHash.toString(),
|
|
|
|
- block: blockStart,
|
|
|
|
- date: startTimestamp,
|
|
|
|
- points: pn,
|
|
|
|
- }
|
|
|
|
- log(`Era [${activeEra.era}], Block [${activeEra.block}], Date [${activeEra.date}], Points [${activeEra.points}], Hash [${activeEra.hash}]`)
|
|
|
|
- data = activeEra
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- return data
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
async findActiveValidators(hash: Hash, searchPreviousBlocks: boolean): Promise<AccountId[]> {
|
|
async findActiveValidators(hash: Hash, searchPreviousBlocks: boolean): Promise<AccountId[]> {
|
|
const block = await this.api.rpc.chain.getBlock(hash)
|
|
const block = await this.api.rpc.chain.getBlock(hash)
|
|
|
|
|