Skip to content

Commit

Permalink
unstoppable->papi: code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
josepot committed Jan 18, 2024
1 parent 6867af0 commit 3c4c9b6
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 547 deletions.
17 changes: 16 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validators, x as xPointsRaw, questions } from "./initialData.json"
import type { Pair, ScoredValidator, ValidatorData } from "./types"
import { linearInterpolation } from "./linearInterpolation"
import { getValidators } from "./validators"

type DataPoints = [Array<number>, Array<number>, Array<number>, Array<number>]

Expand Down Expand Up @@ -51,7 +52,21 @@ const sortingDataPromise = import("./sortingData.json").then(
(mod) => mod.default,
) as Promise<Array<DataPoints>>

const validatorsP = import("./validators").then((x) => x.validators)
const pullingValidatorsStart = Date.now()
const validatorsP = getValidators().then(
(validators) => {
console.debug(
`validators loaded in ${(Date.now() - pullingValidatorsStart) / 1_000}s`,
validators,
)
return validators
},
(e) => {
console.log("there was an error loading the validators data")
console.error(e)
return []
},
)

export async function ranking(
questionId: number,
Expand Down
83 changes: 18 additions & 65 deletions src/api/validators/chain/client.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,18 @@
import { createClient, ProviderStatus } from "@unstoppablejs/client"
import { WsProvider } from "@unstoppablejs/ws-provider"
import { storageClient } from "@unstoppablejs/substrate-bindings"

let onMessagePort: (port: MessagePort) => void
const smPortPromise: Promise<MessagePort> = new Promise((res) => {
onMessagePort = res
})

export const setSmPort = (smPort: MessagePort) => {
onMessagePort(smPort)
}

export const client = createClient((onMessage, onStatus) => {
let port: MessagePort | null = null

const onPortMessage = (e: MessageEvent) => {
onMessage(e.data)
}

const onHalt = () => {
onStatus(ProviderStatus.halt)
}

const open = () => {
smPortPromise
.then((_port) => {
if (!(_port instanceof MessagePort)) throw null

port = _port
port.onmessage = onPortMessage
self.addEventListener("message", onHalt, {
once: true,
})
onStatus(ProviderStatus.ready)
})
.catch(() => {
onStatus(ProviderStatus.halt)
})
}

const close = () => {
self.removeEventListener("message", onHalt)
port?.removeEventListener("message", onPortMessage)
port = null
}

const send = (msg: string) => {
port?.postMessage(msg)
}

return { open, close, send }
})
client.connect()

export const { getFromStorage } = storageClient(client)

// We only use the WsProvider for iterating over the keys
// of Stake.Nominators. We can't use smoldot for doing
// that until the following issue gets solved:
// https://github.com/smol-dot/smoldot/issues/692
const wsProvider = WsProvider("wss://rpc.polkadot.io")
const wsClient = createClient(wsProvider)
wsClient.connect()
export const { getKeys } = storageClient(wsClient)
import { createClient } from "@polkadot-api/client"
import { getLegacyProvider } from "@polkadot-api/legacy-polkadot-provider"
import { createScClient, WellKnownChain } from "@substrate/connect"
// @ts-ignore
import ScWorker from "./substrate-worker?worker"
import dot from "./descriptors/dot"
import roc from "./descriptors/roc"

const provider = getLegacyProvider(
createScClient({
embeddedNodeConfig: {
workerFactory: ScWorker,
},
}),
)

const chain = provider.relayChains[WellKnownChain.polkadot]
export const client = createClient(chain.connect, { dot, roc })
128 changes: 0 additions & 128 deletions src/api/validators/chain/generated/storage-identity.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/api/validators/chain/generated/storage-session.ts

This file was deleted.

67 changes: 0 additions & 67 deletions src/api/validators/chain/generated/storage-staking.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/api/validators/chain/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from "./generated/storage-identity"
export * from "./generated/storage-session"
export * from "./generated/storage-staking"
export { client } from "./client"
1 change: 1 addition & 0 deletions src/api/validators/chain/substrate-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@substrate/connect/worker"
Loading

1 comment on commit 3c4c9b6

@Polkadot-Forum
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Polkadot Forum. There might be relevant details there:

https://forum.polkadot.network/t/papi-is-coming-showcasing-the-validator-selector-tool-dapp/5756/1

Please sign in to comment.