IncreaseWorkerStakesFixture.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import BN from 'bn.js'
  2. import { assert } from 'chai'
  3. import { Api } from '../../Api'
  4. import { QueryNodeApi } from '../../QueryNodeApi'
  5. import { EventDetails, WorkingGroupModuleName } from '../../types'
  6. import { BaseWorkingGroupFixture } from './BaseWorkingGroupFixture'
  7. import { WorkerId, Worker } from '@joystream/types/working-group'
  8. import { SubmittableExtrinsic } from '@polkadot/api/types'
  9. import { ISubmittableResult } from '@polkadot/types/types/'
  10. import { Utils } from '../../utils'
  11. import { StakeIncreasedEventFieldsFragment, WorkerFieldsFragment } from '../../graphql/generated/queries'
  12. export class IncreaseWorkerStakesFixture extends BaseWorkingGroupFixture {
  13. protected workerIds: WorkerId[]
  14. protected stakeIncreases: BN[]
  15. protected workers: Worker[] = []
  16. protected workerStakes: BN[] = []
  17. public constructor(
  18. api: Api,
  19. query: QueryNodeApi,
  20. group: WorkingGroupModuleName,
  21. workerIds: WorkerId[],
  22. stakeIncreases: BN[]
  23. ) {
  24. super(api, query, group)
  25. this.workerIds = workerIds
  26. this.stakeIncreases = stakeIncreases
  27. }
  28. protected async loadWorkersData(): Promise<void> {
  29. this.workers = await this.api.query[this.group].workerById.multi<Worker>(this.workerIds)
  30. this.workerStakes = await Promise.all(
  31. this.workers.map((w) => this.api.getStakedBalance(w.staking_account_id, this.api.lockIdByGroup(this.group)))
  32. )
  33. }
  34. protected async getSignerAccountOrAccounts(): Promise<string[]> {
  35. await this.loadWorkersData()
  36. return this.workers.map((w) => w.role_account_id.toString())
  37. }
  38. protected async getExtrinsics(): Promise<SubmittableExtrinsic<'promise'>[]> {
  39. return this.workerIds.map((workerId, i) => this.api.tx[this.group].increaseStake(workerId, this.stakeIncreases[i]))
  40. }
  41. protected getEventFromResult(result: ISubmittableResult): Promise<EventDetails> {
  42. return this.api.retrieveWorkingGroupsEventDetails(result, this.group, 'StakeIncreased')
  43. }
  44. protected assertQueryNodeEventIsValid(qEvent: StakeIncreasedEventFieldsFragment, i: number): void {
  45. assert.equal(qEvent.worker.runtimeId, this.workerIds[i].toNumber())
  46. assert.equal(qEvent.group.name, this.group)
  47. assert.equal(qEvent.amount, this.stakeIncreases[i].toString())
  48. }
  49. protected assertQueriedWorkersAreValid(qWorkers: WorkerFieldsFragment[]): void {
  50. this.workerIds.map((workerId, i) => {
  51. const worker = qWorkers.find((w) => w.runtimeId === workerId.toNumber())
  52. Utils.assert(worker, 'Query node: Worker not found!')
  53. assert.equal(worker.stake, this.workerStakes[i].add(this.stakeIncreases[i]).toString())
  54. })
  55. }
  56. async runQueryNodeChecks(): Promise<void> {
  57. await super.runQueryNodeChecks()
  58. // Query and check the events
  59. await this.query.tryQueryWithTimeout(
  60. () => this.query.getStakeIncreasedEvents(this.events),
  61. (qEvents) => this.assertQueryNodeEventsAreValid(qEvents)
  62. )
  63. // Check the workers
  64. const qWorkers = await this.query.getWorkersByIds(this.workerIds, this.group)
  65. this.assertQueriedWorkersAreValid(qWorkers)
  66. }
  67. }