Best JavaScript code snippet using best
events.ts
Source:events.ts
1import assert from 'assert'2import {Chain, ChainContext, EventContext, Event, Result} from './support'3export class PhalaMiningBenchmarkUpdatedEvent {4 private readonly _chain: Chain5 private readonly event: Event6 constructor(ctx: EventContext)7 constructor(ctx: ChainContext, event: Event)8 constructor(ctx: EventContext, event?: Event) {9 event = event || ctx.event10 assert(event.name === 'PhalaMining.BenchmarkUpdated')11 this._chain = ctx._chain12 this.event = event13 }14 /**15 * Benchmark Updated16 */17 get isV1170(): boolean {18 return this._chain.getEventHash('PhalaMining.BenchmarkUpdated') === '397d392fba3b9ae0effd8a1f1368bb353c9190b5eef023aa903e2c5aaf9c2056'19 }20 /**21 * Benchmark Updated22 */23 get asV1170(): {miner: Uint8Array, pInstant: number} {24 assert(this.isV1170)25 return this._chain.decodeEvent(this.event)26 }27}28export class PhalaMiningCoolDownExpirationChangedEvent {29 private readonly _chain: Chain30 private readonly event: Event31 constructor(ctx: EventContext)32 constructor(ctx: ChainContext, event: Event)33 constructor(ctx: EventContext, event?: Event) {34 event = event || ctx.event35 assert(event.name === 'PhalaMining.CoolDownExpirationChanged')36 this._chain = ctx._chain37 this.event = event38 }39 /**40 * Cool down expiration changed. \[period\]41 */42 get isV1040(): boolean {43 return this._chain.getEventHash('PhalaMining.CoolDownExpirationChanged') === '0e1caef0df80727d2768bc480792261a4e7615b57b3e8182c7f664f06c96a08e'44 }45 /**46 * Cool down expiration changed. \[period\]47 */48 get asV1040(): bigint {49 assert(this.isV1040)50 return this._chain.decodeEvent(this.event)51 }52 /**53 * Cool down expiration changed (in sec).54 * 55 * Indicates a change in [`CoolDownPeriod`].56 */57 get isV1131(): boolean {58 return this._chain.getEventHash('PhalaMining.CoolDownExpirationChanged') === '87f186ff2be08b46b2d8d4513ece9eb5141274a4131e142556a8a421239c7e98'59 }60 /**61 * Cool down expiration changed (in sec).62 * 63 * Indicates a change in [`CoolDownPeriod`].64 */65 get asV1131(): {period: bigint} {66 assert(this.isV1131)67 return this._chain.decodeEvent(this.event)68 }69}70export class PhalaMiningMinerBoundEvent {71 private readonly _chain: Chain72 private readonly event: Event73 constructor(ctx: EventContext)74 constructor(ctx: ChainContext, event: Event)75 constructor(ctx: EventContext, event?: Event) {76 event = event || ctx.event77 assert(event.name === 'PhalaMining.MinerBound')78 this._chain = ctx._chain79 this.event = event80 }81 /**82 * Miner & worker are bound. \[miner, worker\]83 */84 get isV1040(): boolean {85 return this._chain.getEventHash('PhalaMining.MinerBound') === 'e54ae910805a8a9413af1a7f5885a5d0ba5f4e105175cd6b0ce2a8702ddf1861'86 }87 /**88 * Miner & worker are bound. \[miner, worker\]89 */90 get asV1040(): [Uint8Array, Uint8Array] {91 assert(this.isV1040)92 return this._chain.decodeEvent(this.event)93 }94 /**95 * Miner & worker are bound.96 * 97 * Affected states:98 * - [`MinerBindings`] for the miner account is pointed to the worker99 * - [`WorkerBindings`] for the worker is pointed to the miner account100 * - the miner info at [`Miners`] is updated with `Ready` state101 */102 get isV1131(): boolean {103 return this._chain.getEventHash('PhalaMining.MinerBound') === '155e275cc52b608766efe0776ca792908d7669683141051c9b1c5381d14ef1d1'104 }105 /**106 * Miner & worker are bound.107 * 108 * Affected states:109 * - [`MinerBindings`] for the miner account is pointed to the worker110 * - [`WorkerBindings`] for the worker is pointed to the miner account111 * - the miner info at [`Miners`] is updated with `Ready` state112 */113 get asV1131(): {miner: Uint8Array, worker: Uint8Array} {114 assert(this.isV1131)115 return this._chain.decodeEvent(this.event)116 }117}118export class PhalaMiningMinerEnterUnresponsiveEvent {119 private readonly _chain: Chain120 private readonly event: Event121 constructor(ctx: EventContext)122 constructor(ctx: ChainContext, event: Event)123 constructor(ctx: EventContext, event?: Event) {124 event = event || ctx.event125 assert(event.name === 'PhalaMining.MinerEnterUnresponsive')126 this._chain = ctx._chain127 this.event = event128 }129 /**130 * Miner enters unresponsive state. \[miner\]131 */132 get isV1040(): boolean {133 return this._chain.getEventHash('PhalaMining.MinerEnterUnresponsive') === '21ea0c8f2488eafafdea1de92b54cd17d8b1caff525e37616abf0ff93f11531d'134 }135 /**136 * Miner enters unresponsive state. \[miner\]137 */138 get asV1040(): Uint8Array {139 assert(this.isV1040)140 return this._chain.decodeEvent(this.event)141 }142 /**143 * Miner enters unresponsive state.144 * 145 * Affected states:146 * - the miner info at [`Miners`] is updated from `MiningIdle` to `MiningUnresponsive`147 */148 get isV1131(): boolean {149 return this._chain.getEventHash('PhalaMining.MinerEnterUnresponsive') === '5cbbb619afe1395a8eeba5c9c1bc02f2dca2bb47a2f53335232c2d95096bf3fc'150 }151 /**152 * Miner enters unresponsive state.153 * 154 * Affected states:155 * - the miner info at [`Miners`] is updated from `MiningIdle` to `MiningUnresponsive`156 */157 get asV1131(): {miner: Uint8Array} {158 assert(this.isV1131)159 return this._chain.decodeEvent(this.event)160 }161}162export class PhalaMiningMinerExitUnresponsiveEvent {163 private readonly _chain: Chain164 private readonly event: Event165 constructor(ctx: EventContext)166 constructor(ctx: ChainContext, event: Event)167 constructor(ctx: EventContext, event?: Event) {168 event = event || ctx.event169 assert(event.name === 'PhalaMining.MinerExitUnresponsive')170 this._chain = ctx._chain171 this.event = event172 }173 /**174 * Miner returns to responsive state \[miner\]175 */176 get isV1091(): boolean {177 return this._chain.getEventHash('PhalaMining.MinerExitUnresponsive') === '21ea0c8f2488eafafdea1de92b54cd17d8b1caff525e37616abf0ff93f11531d'178 }179 /**180 * Miner returns to responsive state \[miner\]181 */182 get asV1091(): Uint8Array {183 assert(this.isV1091)184 return this._chain.decodeEvent(this.event)185 }186 /**187 * Miner returns to responsive state.188 * 189 * Affected states:190 * - the miner info at [`Miners`] is updated from `MiningUnresponsive` to `MiningIdle`191 */192 get isV1131(): boolean {193 return this._chain.getEventHash('PhalaMining.MinerExitUnresponsive') === '5cbbb619afe1395a8eeba5c9c1bc02f2dca2bb47a2f53335232c2d95096bf3fc'194 }195 /**196 * Miner returns to responsive state.197 * 198 * Affected states:199 * - the miner info at [`Miners`] is updated from `MiningUnresponsive` to `MiningIdle`200 */201 get asV1131(): {miner: Uint8Array} {202 assert(this.isV1131)203 return this._chain.decodeEvent(this.event)204 }205}206export class PhalaMiningMinerReclaimedEvent {207 private readonly _chain: Chain208 private readonly event: Event209 constructor(ctx: EventContext)210 constructor(ctx: ChainContext, event: Event)211 constructor(ctx: EventContext, event?: Event) {212 event = event || ctx.event213 assert(event.name === 'PhalaMining.MinerReclaimed')214 this._chain = ctx._chain215 this.event = event216 }217 /**218 * Miner is reclaimed, with its slash settled. \[miner, original_stake, slashed\]219 */220 get isV1040(): boolean {221 return this._chain.getEventHash('PhalaMining.MinerReclaimed') === '0f263bfdefa394edfb38d20d33662423a2e0902235b599f9b2b0292f157f0902'222 }223 /**224 * Miner is reclaimed, with its slash settled. \[miner, original_stake, slashed\]225 */226 get asV1040(): [Uint8Array, bigint, bigint] {227 assert(this.isV1040)228 return this._chain.decodeEvent(this.event)229 }230 /**231 * Miner is reclaimed, with its slash settled.232 */233 get isV1131(): boolean {234 return this._chain.getEventHash('PhalaMining.MinerReclaimed') === 'b2e0bf41e7f7ee2f60305a2d67d5b94dee97ed25acd0e7b8aad3bbc58848e265'235 }236 /**237 * Miner is reclaimed, with its slash settled.238 */239 get asV1131(): {miner: Uint8Array, originalStake: bigint, slashed: bigint} {240 assert(this.isV1131)241 return this._chain.decodeEvent(this.event)242 }243}244export class PhalaMiningMinerSettledEvent {245 private readonly _chain: Chain246 private readonly event: Event247 constructor(ctx: EventContext)248 constructor(ctx: ChainContext, event: Event)249 constructor(ctx: EventContext, event?: Event) {250 event = event || ctx.event251 assert(event.name === 'PhalaMining.MinerSettled')252 this._chain = ctx._chain253 this.event = event254 }255 /**256 * Miner settled successfully. \[miner, v, payout\]257 */258 get isV1040(): boolean {259 return this._chain.getEventHash('PhalaMining.MinerSettled') === '0f263bfdefa394edfb38d20d33662423a2e0902235b599f9b2b0292f157f0902'260 }261 /**262 * Miner settled successfully. \[miner, v, payout\]263 */264 get asV1040(): [Uint8Array, bigint, bigint] {265 assert(this.isV1040)266 return this._chain.decodeEvent(this.event)267 }268 /**269 * Miner settled successfully.270 * 271 * It results in the v in [`Miners`] being updated. It also indicates the downstream272 * stake pool has received the mining reward (payout), and the treasury has received the273 * tax.274 */275 get isV1131(): boolean {276 return this._chain.getEventHash('PhalaMining.MinerSettled') === '76e2c6595b90cdd18f68df3b37575e908064c95f276f32cb6723c115410570e0'277 }278 /**279 * Miner settled successfully.280 * 281 * It results in the v in [`Miners`] being updated. It also indicates the downstream282 * stake pool has received the mining reward (payout), and the treasury has received the283 * tax.284 */285 get asV1131(): {miner: Uint8Array, vBits: bigint, payoutBits: bigint} {286 assert(this.isV1131)287 return this._chain.decodeEvent(this.event)288 }289}290export class PhalaMiningMinerStartedEvent {291 private readonly _chain: Chain292 private readonly event: Event293 constructor(ctx: EventContext)294 constructor(ctx: ChainContext, event: Event)295 constructor(ctx: EventContext, event?: Event) {296 event = event || ctx.event297 assert(event.name === 'PhalaMining.MinerStarted')298 this._chain = ctx._chain299 this.event = event300 }301 /**302 * Miner starts mining. \[miner\]303 */304 get isV1040(): boolean {305 return this._chain.getEventHash('PhalaMining.MinerStarted') === '21ea0c8f2488eafafdea1de92b54cd17d8b1caff525e37616abf0ff93f11531d'306 }307 /**308 * Miner starts mining. \[miner\]309 */310 get asV1040(): Uint8Array {311 assert(this.isV1040)312 return this._chain.decodeEvent(this.event)313 }314 /**315 * A miner starts mining.316 * 317 * Affected states:318 * - the miner info at [`Miners`] is updated with `MiningIdle` state319 * - [`NextSessionId`] for the miner is incremented320 * - [`Stakes`] for the miner is updated321 * - [`OnlineMiners`] is incremented322 */323 get isV1131(): boolean {324 return this._chain.getEventHash('PhalaMining.MinerStarted') === '5cbbb619afe1395a8eeba5c9c1bc02f2dca2bb47a2f53335232c2d95096bf3fc'325 }326 /**327 * A miner starts mining.328 * 329 * Affected states:330 * - the miner info at [`Miners`] is updated with `MiningIdle` state331 * - [`NextSessionId`] for the miner is incremented332 * - [`Stakes`] for the miner is updated333 * - [`OnlineMiners`] is incremented334 */335 get asV1131(): {miner: Uint8Array} {336 assert(this.isV1131)337 return this._chain.decodeEvent(this.event)338 }339 /**340 * A miner starts mining.341 * 342 * Affected states:343 * - the miner info at [`Miners`] is updated with `MiningIdle` state344 * - [`NextSessionId`] for the miner is incremented345 * - [`Stakes`] for the miner is updated346 * - [`OnlineMiners`] is incremented347 */348 get isV1170(): boolean {349 return this._chain.getEventHash('PhalaMining.MinerStarted') === 'd4b307f4fa783435cb06a98b9e4d163a3444252f78222d8635bac53227a2926e'350 }351 /**352 * A miner starts mining.353 * 354 * Affected states:355 * - the miner info at [`Miners`] is updated with `MiningIdle` state356 * - [`NextSessionId`] for the miner is incremented357 * - [`Stakes`] for the miner is updated358 * - [`OnlineMiners`] is incremented359 */360 get asV1170(): {miner: Uint8Array, initV: bigint, initP: number} {361 assert(this.isV1170)362 return this._chain.decodeEvent(this.event)363 }364}365export class PhalaMiningMinerStoppedEvent {366 private readonly _chain: Chain367 private readonly event: Event368 constructor(ctx: EventContext)369 constructor(ctx: ChainContext, event: Event)370 constructor(ctx: EventContext, event?: Event) {371 event = event || ctx.event372 assert(event.name === 'PhalaMining.MinerStopped')373 this._chain = ctx._chain374 this.event = event375 }376 /**377 * Miner stops mining. \[miner\]378 */379 get isV1040(): boolean {380 return this._chain.getEventHash('PhalaMining.MinerStopped') === '21ea0c8f2488eafafdea1de92b54cd17d8b1caff525e37616abf0ff93f11531d'381 }382 /**383 * Miner stops mining. \[miner\]384 */385 get asV1040(): Uint8Array {386 assert(this.isV1040)387 return this._chain.decodeEvent(this.event)388 }389 /**390 * Miner stops mining.391 * 392 * Affected states:393 * - the miner info at [`Miners`] is updated with `MiningCoolingDown` state394 * - [`OnlineMiners`] is decremented395 */396 get isV1131(): boolean {397 return this._chain.getEventHash('PhalaMining.MinerStopped') === '5cbbb619afe1395a8eeba5c9c1bc02f2dca2bb47a2f53335232c2d95096bf3fc'398 }399 /**400 * Miner stops mining.401 * 402 * Affected states:403 * - the miner info at [`Miners`] is updated with `MiningCoolingDown` state404 * - [`OnlineMiners`] is decremented405 */406 get asV1131(): {miner: Uint8Array} {407 assert(this.isV1131)408 return this._chain.decodeEvent(this.event)409 }410}411export class PhalaMiningMinerUnboundEvent {412 private readonly _chain: Chain413 private readonly event: Event414 constructor(ctx: EventContext)415 constructor(ctx: ChainContext, event: Event)416 constructor(ctx: EventContext, event?: Event) {417 event = event || ctx.event418 assert(event.name === 'PhalaMining.MinerUnbound')419 this._chain = ctx._chain420 this.event = event421 }422 /**423 * Miner & worker are unbound. \[miner, worker\]424 */425 get isV1040(): boolean {426 return this._chain.getEventHash('PhalaMining.MinerUnbound') === 'e54ae910805a8a9413af1a7f5885a5d0ba5f4e105175cd6b0ce2a8702ddf1861'427 }428 /**429 * Miner & worker are unbound. \[miner, worker\]430 */431 get asV1040(): [Uint8Array, Uint8Array] {432 assert(this.isV1040)433 return this._chain.decodeEvent(this.event)434 }435 /**436 * Miner & worker are unbound.437 * 438 * Affected states:439 * - [`MinerBindings`] for the miner account is removed440 * - [`WorkerBindings`] for the worker is removed441 */442 get isV1131(): boolean {443 return this._chain.getEventHash('PhalaMining.MinerUnbound') === '155e275cc52b608766efe0776ca792908d7669683141051c9b1c5381d14ef1d1'444 }445 /**446 * Miner & worker are unbound.447 * 448 * Affected states:449 * - [`MinerBindings`] for the miner account is removed450 * - [`WorkerBindings`] for the worker is removed451 */452 get asV1131(): {miner: Uint8Array, worker: Uint8Array} {453 assert(this.isV1131)454 return this._chain.decodeEvent(this.event)455 }456}457export class PhalaStakePoolPoolCommissionSetEvent {458 private readonly _chain: Chain459 private readonly event: Event460 constructor(ctx: EventContext)461 constructor(ctx: ChainContext, event: Event)462 constructor(ctx: EventContext, event?: Event) {463 event = event || ctx.event464 assert(event.name === 'PhalaStakePool.PoolCommissionSet')465 this._chain = ctx._chain466 this.event = event467 }468 /**469 * The real commission ratio is commission/1_000_000u32. \[pid, commission\]470 */471 get isV1040(): boolean {472 return this._chain.getEventHash('PhalaStakePool.PoolCommissionSet') === 'b57e136df7099b4d95c82d61169a7dca2b6b7da2952d6383cedc8494d541669a'473 }474 /**475 * The real commission ratio is commission/1_000_000u32. \[pid, commission\]476 */477 get asV1040(): [bigint, number] {478 assert(this.isV1040)479 return this._chain.decodeEvent(this.event)480 }481 /**482 * The commission of a pool is updated483 * 484 * The commission ratio is represented by an integer. The real value is485 * `commission / 1_000_000u32`.486 * 487 * Affected states:488 * - the `payout_commission` field in [`StakePools`] is updated489 */490 get isV1131(): boolean {491 return this._chain.getEventHash('PhalaStakePool.PoolCommissionSet') === 'f9fd566d432542f7d455c2c329ace5fed0f06ab260c0f1a71f38b55f59535a53'492 }493 /**494 * The commission of a pool is updated495 * 496 * The commission ratio is represented by an integer. The real value is497 * `commission / 1_000_000u32`.498 * 499 * Affected states:500 * - the `payout_commission` field in [`StakePools`] is updated501 */502 get asV1131(): {pid: bigint, commission: number} {503 assert(this.isV1131)504 return this._chain.decodeEvent(this.event)505 }506}507export class PhalaStakePoolPoolCreatedEvent {508 private readonly _chain: Chain509 private readonly event: Event510 constructor(ctx: EventContext)511 constructor(ctx: ChainContext, event: Event)512 constructor(ctx: EventContext, event?: Event) {513 event = event || ctx.event514 assert(event.name === 'PhalaStakePool.PoolCreated')515 this._chain = ctx._chain516 this.event = event517 }518 /**519 * \[owner, pid\]520 */521 get isV1040(): boolean {522 return this._chain.getEventHash('PhalaStakePool.PoolCreated') === 'fb1b6c83a547837ce9f07d7b623e71a4fec6cea1d51d01009d24c5a20e53d816'523 }524 /**525 * \[owner, pid\]526 */527 get asV1040(): [Uint8Array, bigint] {528 assert(this.isV1040)529 return this._chain.decodeEvent(this.event)530 }531 /**532 * A pool is created under an owner533 * 534 * Affected states:535 * - a new entry in [`StakePools`] with the pid536 */537 get isV1131(): boolean {538 return this._chain.getEventHash('PhalaStakePool.PoolCreated') === '443db31f743a70c8cb7b298e12205d1672956da603edac4d4439cd0cb47151ce'539 }540 /**541 * A pool is created under an owner542 * 543 * Affected states:544 * - a new entry in [`StakePools`] with the pid545 */546 get asV1131(): {owner: Uint8Array, pid: bigint} {547 assert(this.isV1131)548 return this._chain.decodeEvent(this.event)549 }550}551export class PhalaStakePoolPoolWorkerAddedEvent {552 private readonly _chain: Chain553 private readonly event: Event554 constructor(ctx: EventContext)555 constructor(ctx: ChainContext, event: Event)556 constructor(ctx: EventContext, event?: Event) {557 event = event || ctx.event558 assert(event.name === 'PhalaStakePool.PoolWorkerAdded')559 this._chain = ctx._chain560 this.event = event561 }562 /**563 * \[pid, worker\]564 */565 get isV1040(): boolean {566 return this._chain.getEventHash('PhalaStakePool.PoolWorkerAdded') === '62ab179c459e900954ede92a01f149d553e9317efc7d0129525f40d631e8b38f'567 }568 /**569 * \[pid, worker\]570 */571 get asV1040(): [bigint, Uint8Array] {572 assert(this.isV1040)573 return this._chain.decodeEvent(this.event)574 }575 /**576 * A worker is added to the pool577 * 578 * Affected states:579 * - the `worker` is added to the vector `workers` in [`StakePools`]580 * - the worker in the [`WorkerAssignments`] is pointed to `pid`581 * - the worker-miner binding is updated in `mining` pallet ([`WorkerBindings`](mining::pallet::WorkerBindings),582 * [`MinerBindings`](mining::pallet::MinerBindings))583 */584 get isV1131(): boolean {585 return this._chain.getEventHash('PhalaStakePool.PoolWorkerAdded') === '3eddad70bc8df6b13283af1a5095e74f20ea2ecaeb63ab0291ae1f7f937c817b'586 }587 /**588 * A worker is added to the pool589 * 590 * Affected states:591 * - the `worker` is added to the vector `workers` in [`StakePools`]592 * - the worker in the [`WorkerAssignments`] is pointed to `pid`593 * - the worker-miner binding is updated in `mining` pallet ([`WorkerBindings`](mining::pallet::WorkerBindings),594 * [`MinerBindings`](mining::pallet::MinerBindings))595 */596 get asV1131(): {pid: bigint, worker: Uint8Array} {597 assert(this.isV1131)598 return this._chain.decodeEvent(this.event)599 }600 /**601 * A worker is added to the pool602 * 603 * Affected states:604 * - the `worker` is added to the vector `workers` in [`StakePools`]605 * - the worker in the [`WorkerAssignments`] is pointed to `pid`606 * - the worker-miner binding is updated in `mining` pallet ([`WorkerBindings`](mining::pallet::WorkerBindings),607 * [`MinerBindings`](mining::pallet::MinerBindings))608 */609 get isV1170(): boolean {610 return this._chain.getEventHash('PhalaStakePool.PoolWorkerAdded') === '816729d64276f474dc5ebaf960f292a6665bd6bc750eb85436a8e8554ca2e8e6'611 }612 /**613 * A worker is added to the pool614 * 615 * Affected states:616 * - the `worker` is added to the vector `workers` in [`StakePools`]617 * - the worker in the [`WorkerAssignments`] is pointed to `pid`618 * - the worker-miner binding is updated in `mining` pallet ([`WorkerBindings`](mining::pallet::WorkerBindings),619 * [`MinerBindings`](mining::pallet::MinerBindings))620 */621 get asV1170(): {pid: bigint, worker: Uint8Array, miner: Uint8Array} {622 assert(this.isV1170)623 return this._chain.decodeEvent(this.event)624 }625}626export class PhalaStakePoolPoolWorkerRemovedEvent {627 private readonly _chain: Chain628 private readonly event: Event629 constructor(ctx: EventContext)630 constructor(ctx: ChainContext, event: Event)631 constructor(ctx: EventContext, event?: Event) {632 event = event || ctx.event633 assert(event.name === 'PhalaStakePool.PoolWorkerRemoved')634 this._chain = ctx._chain635 this.event = event636 }637 /**638 * A worker is removed from a pool.639 */640 get isV1090(): boolean {641 return this._chain.getEventHash('PhalaStakePool.PoolWorkerRemoved') === '3eddad70bc8df6b13283af1a5095e74f20ea2ecaeb63ab0291ae1f7f937c817b'642 }643 /**644 * A worker is removed from a pool.645 */646 get asV1090(): {pid: bigint, worker: Uint8Array} {647 assert(this.isV1090)648 return this._chain.decodeEvent(this.event)649 }...
processor.ts
Source:processor.ts
1import {lookupArchive} from '@subsquid/archive-registry'2import * as ss58 from '@subsquid/ss58'3import {4 BatchContext,5 BatchProcessorItem,6 SubstrateBatchProcessor,7} from '@subsquid/substrate-processor'8import {Store, TypeormDatabase} from '@subsquid/typeorm-store'9import {toHex} from '@subsquid/util-internal-hex'10import {In} from 'typeorm'11import {readFile} from 'fs/promises'12import {Account, Miner, MinerState, StakePool, Worker} from './model'13import {14 PhalaMiningMinerBoundEvent,15 PhalaMiningMinerUnboundEvent,16 PhalaStakePoolPoolCreatedEvent,17 PhalaStakePoolPoolWorkerAddedEvent,18 PhalaStakePoolPoolWorkerRemovedEvent,19} from './types/events'20import path from 'path'21const START_BLOCK = 243200122const processor = new SubstrateBatchProcessor()23 .setBlockRange({from: START_BLOCK})24 .setBatchSize(500)25 .setDataSource({archive: lookupArchive('khala', {release: 'FireSquid'})})26 .addEvent('PhalaStakePool.PoolCreated', {data: {event: {args: true}}})27 .addEvent('PhalaStakePool.PoolWorkerAdded', {data: {event: {args: true}}})28 .addEvent('PhalaStakePool.PoolWorkerRemoved', {data: {event: {args: true}}})29 .addEvent('PhalaMining.CoolDownExpirationChanged', {30 data: {event: {args: true}},31 })32 .addEvent('PhalaMining.MinerStarted', {data: {event: {args: true}}})33 .addEvent('PhalaMining.MinerStopped', {data: {event: {args: true}}})34 .addEvent('PhalaMining.MinerReclaimed', {data: {event: {args: true}}})35 .addEvent('PhalaMining.MinerBound', {data: {event: {args: true}}})36 .addEvent('PhalaMining.MinerUnbound', {data: {event: {args: true}}})37 .addEvent('PhalaMining.MinerEnterUnresponsive', {data: {event: {args: true}}})38 .addEvent('PhalaMining.MinerExitUnresponsive', {data: {event: {args: true}}})39 .addEvent('PhalaMining.MinerSettled', {data: {event: {args: true}}})40 .addEvent('PhalaMining.BenchmarkUpdated', {data: {event: {args: true}}})41type Item = BatchProcessorItem<typeof processor>42type Ctx = BatchContext<Store, Item>43processor.run(new TypeormDatabase(), async (ctx) => {44 if (ctx.blocks[0].header.height === START_BLOCK) {45 await importDump(ctx)46 }47 await process(ctx)48})49const importDump = async (ctx: Ctx): Promise<void> => {50 const block = START_BLOCK - 151 ctx.log.info('Importing dump...')52 const readJson = async <T>(fileName: string): Promise<T> => {53 const file = await readFile(54 path.join(__dirname, `dump/${fileName}`),55 'utf8'56 )57 return JSON.parse(file).result58 }59 const stakePoolsDump = await readJson<60 Array<{pid: number | string; owner: string; workers: string[]}>61 >(`stake_pools_${block}.json`)62 const minersDump = await readJson<63 Record<64 string,65 {66 state: MinerState67 ve: string | number68 v: string | number69 vUpdatedAt: number70 benchmark: {71 pInit: number72 pInstant: number73 iterations: number74 miningStartTime: number75 challengeTimeLast: number76 }77 coolDownStart: number78 stats: {totalReward: string | number}79 }80 >81 >(`miners_${block}.json`)82 const workersDump = await readJson<83 Array<{84 pubkey: string85 operator: string86 confidenceLevel: number87 initialScore: number88 }>89 >(`workers_${block}.json`)90 const minerBindingsDump = await readJson<Record<string, string>>(91 `miner_bindings_${block}.json`92 )93 const accounts = new Map<string, Account>()94 const stakePools = new Map<string, StakePool>(95 stakePoolsDump.map((s) => [96 String(s.pid),97 new StakePool({98 id: String(s.pid),99 pid: BigInt(s.pid),100 owner: getAccount(accounts, s.owner),101 }),102 ])103 )104 const workers = new Map<string, Worker>(105 workersDump.map((w) => [w.pubkey, new Worker({id: w.pubkey})])106 )107 const miners = new Map<string, Miner>(108 Object.entries(minersDump).map(([id, m]) => [109 id,110 new Miner({111 id,112 isBound: false,113 totalReward: BigInt(m.stats.totalReward),114 }),115 ])116 )117 for (const s of stakePoolsDump) {118 for (const w of s.workers) {119 const worker = workers.get(w)120 if (worker == null) {121 throw new Error(`Worker ${w} not found`)122 }123 worker.stakePool = stakePools.get(String(s.pid))124 }125 }126 await ctx.store.insert(Array.from(accounts.values()))127 await ctx.store.insert(Array.from(stakePools.values()))128 await ctx.store.insert(Array.from(workers.values()))129 const unboundMinerIds: string[] = []130 for (const [id, m] of miners) {131 const workerId = minerBindingsDump[m.id]132 const worker = workers.get(workerId)133 if (worker == null) {134 // throw new Error(`Worker ${workerId} not found`)135 unboundMinerIds.push(id)136 } else {137 worker.miner = m138 m.isBound = true139 m.worker = worker140 }141 }142 for (const id of unboundMinerIds) {143 miners.delete(id)144 }145 await ctx.store.insert(Array.from(miners.values()))146 await ctx.store.save(Array.from(workers.values()))147 ctx.log.info('Dump imported')148}149const process = async (ctx: Ctx): Promise<void> => {150 const stakePoolIds = new Set<string>()151 const workerIds = new Set<string>()152 const minerIds = new Set<string>()153 const events: Array<154 | ['PhalaStakePool.PoolCreated', {id: string; owner: string}]155 | ['PhalaStakePool.PoolWorkerAdded', {id: string; pid: string}]156 | ['PhalaStakePool.PoolWorkerRemoved', {id: string; pid: string}]157 | ['PhalaMining.MinerBound', {id: string; worker: string}]158 | ['PhalaMining.MinerUnbound', {id: string; worker: string}]159 | [160 'PhalaMining.MinerSettled',161 {id: string; payoutBits: bigint; vBits: bigint}162 ]163 > = []164 for (const block of ctx.blocks) {165 for (const item of block.items) {166 if (item.name === 'PhalaStakePool.PoolCreated') {167 const e = new PhalaStakePoolPoolCreatedEvent(ctx, item.event)168 let rec: {owner: Uint8Array; pid: bigint}169 if (e.isV1040) {170 const [owner, pid] = e.asV1040171 rec = {owner, pid}172 } else {173 const {owner, pid} = e.asV1131174 rec = {owner, pid}175 }176 events.push([177 item.name,178 {179 id: rec.pid.toString(),180 owner: ss58.codec('phala').encode(rec.owner),181 },182 ])183 }184 if (item.name === 'PhalaMining.MinerBound') {185 const e = new PhalaMiningMinerBoundEvent(ctx, item.event)186 let rec: {miner: Uint8Array; worker: Uint8Array}187 if (e.isV1040) {188 const [miner, worker] = e.asV1040189 rec = {miner, worker}190 } else {191 const {miner, worker} = e.asV1131192 rec = {miner, worker}193 }194 events.push([195 item.name,196 {197 id: ss58.codec('phala').encode(rec.miner),198 worker: toHex(rec.worker),199 },200 ])201 }202 if (item.name === 'PhalaMining.MinerUnbound') {203 const e = new PhalaMiningMinerUnboundEvent(ctx, item.event)204 let rec: {miner: Uint8Array; worker: Uint8Array}205 if (e.isV1040) {206 const [miner, worker] = e.asV1040207 rec = {miner, worker}208 } else {209 const {miner, worker} = e.asV1131210 rec = {miner, worker}211 }212 events.push([213 item.name,214 {215 id: ss58.codec('phala').encode(rec.miner),216 worker: toHex(rec.worker),217 },218 ])219 }220 // if (item.name === 'PhalaMining.MinerSettled') {221 // const e = new PhalaMiningMinerSettledEvent(ctx, item.event)222 // let rec: {miner: Uint8Array; vBits: bigint; payoutBits: bigint}223 // if (e.isV1040) {224 // const [miner, vBits, payoutBits] = e.asV1040225 // rec = {miner, vBits, payoutBits}226 // } else {227 // const {miner, vBits, payoutBits} = e.asV1131228 // rec = {miner, vBits, payoutBits}229 // }230 // events.push([231 // item.name,232 // {233 // id: ss58.codec('phala').encode(rec.miner),234 // vBits: rec.vBits,235 // payoutBits: rec.payoutBits,236 // },237 // ])238 // }239 if (item.name === 'PhalaStakePool.PoolWorkerAdded') {240 const e = new PhalaStakePoolPoolWorkerAddedEvent(ctx, item.event)241 let rec: {pid: bigint; worker: Uint8Array}242 if (e.isV1040) {243 const [pid, worker] = e.asV1040244 rec = {pid, worker}245 } else if (e.isV1131) {246 const {pid, worker} = e.asV1131247 rec = {pid, worker}248 } else {249 const {pid, worker} = e.asV1170250 rec = {pid, worker}251 }252 events.push([253 item.name,254 {255 id: toHex(rec.worker),256 pid: rec.pid.toString(),257 },258 ])259 }260 if (item.name === 'PhalaStakePool.PoolWorkerRemoved') {261 const e = new PhalaStakePoolPoolWorkerRemovedEvent(ctx, item.event)262 const {pid, worker} = e.asV1090263 const rec: {pid: bigint; worker: Uint8Array} = {pid, worker}264 events.push([265 item.name,266 {267 id: toHex(rec.worker),268 pid: rec.pid.toString(),269 },270 ])271 }272 }273 }274 for (const [name, params] of events) {275 if (276 name === 'PhalaStakePool.PoolWorkerAdded' ||277 name === 'PhalaStakePool.PoolWorkerRemoved'278 ) {279 stakePoolIds.add(params.pid)280 workerIds.add(params.id)281 }282 if (283 name === 'PhalaMining.MinerBound' ||284 name === 'PhalaMining.MinerUnbound'285 ) {286 minerIds.add(params.id)287 workerIds.add(params.worker)288 }289 if (name === 'PhalaMining.MinerSettled') {290 minerIds.add(params.id)291 }292 }293 const accounts = new Map<string, Account>()294 const stakePools = await ctx.store295 .find(StakePool, {296 where: {id: In([...stakePoolIds])},297 relations: {owner: true},298 })299 .then((stakePools) => new Map(stakePools.map((s) => [s.id, s])))300 const workers = await ctx.store301 .find(Worker, {302 where: {id: In([...workerIds])},303 relations: {stakePool: true, miner: true},304 })305 .then((workers) => new Map(workers.map((w) => [w.id, w])))306 const miners = await ctx.store307 .find(Miner, {308 where: {id: In([...minerIds])},309 relations: {worker: true},310 })311 .then((miners) => new Map(miners.map((m) => [m.id, m])))312 for (const [name, params] of events) {313 ctx.log.info(314 `Processing event ${name}, params: ${JSON.stringify(315 params,316 (key, value) => (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged317 )}`318 )319 if (name === 'PhalaStakePool.PoolCreated') {320 const {id, owner} = params321 const stakePool = new StakePool({322 id,323 pid: BigInt(id),324 owner: getAccount(accounts, owner),325 })326 await ctx.store.insert(stakePool)327 stakePools.set(id, stakePool)328 }329 if (name === 'PhalaStakePool.PoolWorkerAdded') {330 const {id, pid} = params331 const worker = workers.get(id)332 const stakePool = stakePools.get(pid)333 if (worker != null) {334 worker.stakePool = stakePool335 } else {336 const worker = new Worker({id, stakePool})337 workers.set(id, worker)338 await ctx.store.insert(worker)339 }340 }341 if (name === 'PhalaStakePool.PoolWorkerRemoved') {342 const {id, pid} = params343 const worker = workers.get(id)344 if (worker != null && worker.stakePool?.id === pid) {345 worker.stakePool = null346 } else {347 throw new Error(`${name}: Remove worker ${id} failed`)348 }349 }350 if (name === 'PhalaMining.MinerBound') {351 const {id, worker: workerId} = params352 let miner = miners.get(id)353 let worker = workers.get(workerId)354 // NOTE: if MinerBound happens before PoolWorkerAdded, the worker will not be in the map355 if (worker == null) {356 worker = new Worker({id: workerId})357 await ctx.store.insert(worker)358 workers.set(workerId, worker)359 }360 if (miner == null) {361 miner = new Miner({362 id,363 isBound: true,364 totalReward: BigInt(0),365 })366 miners.set(id, miner)367 }368 miner.isBound = true369 miner.worker = worker370 worker.miner = miner371 }372 if (name === 'PhalaMining.MinerUnbound') {373 const {id, worker: workerId} = params374 const miner = miners.get(id)375 const worker = workers.get(workerId)376 if (miner != null && worker != null && miner.worker.id === workerId) {377 worker.miner = null378 miner.isBound = false379 } else {380 throw new Error(381 `${name}: Unbind miner ${id} from worker ${workerId} failed`382 )383 }384 }385 if (name === 'PhalaMining.MinerSettled') {386 const {id, vBits, payoutBits} = params387 const miner = miners.get(id)388 if (miner == null) {389 throw new Error(`${name}: Miner ${id} not found`)390 }391 miner.totalReward += payoutBits392 miner.v = vBits393 }394 }395 await ctx.store.save(Array.from(accounts.values()))396 await ctx.store.save(Array.from(stakePools.values()))397 await ctx.store.save(Array.from(miners.values()))398 await ctx.store.save(Array.from(workers.values()))399}400const getAccount = (m: Map<string, Account>, id: string): Account => {401 let acc = m.get(id)402 if (acc == null) {403 acc = new Account()404 acc.id = id405 m.set(id, acc)406 }407 return acc...
menubar.js
Source:menubar.js
...56 items,57 selectedItems,58 };59 }60 benchmarkUpdated(event) {61 const benchmark = event.detail.selectedItems[0];62 store.dispatch(benchmarksChanged(benchmark.id));63 }64 get metricOptions() {65 const items = [66 { id: 'all', title: 'All Metrics' },67 ...this.metricNames.map((name) => ({ id: name, title: name })),68 ];69 const selectedItems = items.filter((item) => item.id === this.viewMetric);70 return {71 multiple: false,72 items,73 selectedItems,74 };...
Using AI Code Generation
1const BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2const bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();3const prices = [7,1,5,3,6,4];4const result = bestTimeToBuyAndSellStock.benchmarkUpdated(prices);5const BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');6const bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();7const prices = [7,1,5,3,6,4];8const result = bestTimeToBuyAndSellStock.benchmarkUpdated(prices);
Using AI Code Generation
1const BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2const stockPrices = [7, 1, 5, 3, 6, 4];3const stockPrices2 = [7, 6, 4, 3, 1];4const stockPrices3 = [2, 4, 1];5const stockPrices4 = [2, 1, 2, 1, 0, 1, 2];6const stockPrices5 = [1, 2, 1, 2];7const stockPrices6 = [1, 2, 3, 4, 5];8const stockPrices7 = [7, 6, 4, 3, 1];9const stockPrices8 = [6, 1, 3, 2, 4, 7];10const stockPrices9 = [2, 4, 1];11const stockPrices10 = [3, 3, 5, 0, 0, 3, 1, 4];12const stockPrices11 = [1, 2];13const stockPrices12 = [2, 4, 1];14const bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();15console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices));16console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices2));17console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices3));18console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices4));19console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices5));20console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices6));21console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices7));22console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices8));23console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices9));24console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices10));25console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices11));26console.log(bestTimeToBuyAndSellStock.benchmarkUpdated(stockPrices12));
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!