DecreaseWorkerStakesFixture.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 { Worker, WorkerId } 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 { StakeDecreasedEventFieldsFragment, WorkerFieldsFragment } from '../../graphql/generated/queries'
  12. export class DecreaseWorkerStakesFixture extends BaseWorkingGroupFixture {
  13. protected asSudo: boolean
  14. protected workerIds: WorkerId[]
  15. protected amounts: BN[]
  16. protected workers: Worker[] = []
  17. protected workerStakes: BN[] = []
  18. public constructor(
  19. api: Api,
  20. query: QueryNodeApi,
  21. group: WorkingGroupModuleName,
  22. workerIds: WorkerId[],
  23. amounts: BN[],
  24. asSudo = false
  25. ) {
  26. super(api, query, group)
  27. this.workerIds = workerIds
  28. this.amounts = amounts
  29. this.asSudo = asSudo
  30. }
  31. protected async loadWorkersData(): Promise<void> {
  32. this.workers = await this.api.query[this.group].workerById.multi<Worker>(this.workerIds)
  33. this.workerStakes = await Promise.all(
  34. this.workers.map((w) => this.api.getStakedBalance(w.staking_account_id, this.api.lockIdByGroup(this.group)))
  35. )
  36. }
  37. protected async getSignerAccountOrAccounts(): Promise<string> {
  38. return this.asSudo ? (await this.api.query.sudo.key()).toString() : await this.api.getLeadRoleKey(this.group)
  39. }
  40. protected async getExtrinsics(): Promise<SubmittableExtrinsic<'promise'>[]> {
  41. const extrinsics = this.workerIds.map((workerId, i) =>
  42. this.api.tx[this.group].decreaseStake(workerId, this.amounts[i])
  43. )
  44. return this.asSudo ? extrinsics.map((tx) => this.api.tx.sudo.sudo(tx)) : extrinsics
  45. }
  46. protected getEventFromResult(result: ISubmittableResult): Promise<EventDetails> {
  47. return this.api.retrieveWorkingGroupsEventDetails(result, this.group, 'StakeDecreased')
  48. }
  49. public async execute(): Promise<void> {
  50. await this.loadWorkersData()
  51. await super.execute()
  52. }
  53. protected assertQueryNodeEventIsValid(qEvent: StakeDecreasedEventFieldsFragment, i: number): void {
  54. assert.equal(qEvent.worker.runtimeId, this.workerIds[i].toNumber())
  55. assert.equal(qEvent.group.name, this.group)
  56. assert.equal(qEvent.amount, this.amounts[i].toString())
  57. }
  58. protected assertQueriedWorkersAreValid(qWorkers: WorkerFieldsFragment[]): void {
  59. this.workerIds.map((workerId, i) => {
  60. const worker = qWorkers.find((w) => w.runtimeId === workerId.toNumber())
  61. Utils.assert(worker, 'Query node: Worker not found!')
  62. assert.equal(worker.stake, this.workerStakes[i].sub(this.amounts[i]).toString())
  63. })
  64. }
  65. async runQueryNodeChecks(): Promise<void> {
  66. await super.runQueryNodeChecks()
  67. // Query and check events
  68. await this.query.tryQueryWithTimeout(
  69. () => this.query.getStakeDecreasedEvents(this.events),
  70. (qEvents) => this.assertQueryNodeEventsAreValid(qEvents)
  71. )
  72. // Check workers
  73. const qWorkers = await this.query.getWorkersByIds(this.workerIds, this.group)
  74. this.assertQueriedWorkersAreValid(qWorkers)
  75. }
  76. }