membershipModule.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { Api } from '../Api'
  2. import BN from 'bn.js'
  3. import { assert } from 'chai'
  4. import { BaseFixture } from '../Fixture'
  5. import { PaidTermId, MemberId } from '@joystream/types/members'
  6. import { Debugger, extendDebug } from '../Debugger'
  7. export class BuyMembershipHappyCaseFixture extends BaseFixture {
  8. private accounts: string[]
  9. private paidTerms: PaidTermId
  10. private debug: Debugger.Debugger
  11. private memberIds: MemberId[] = []
  12. public constructor(api: Api, accounts: string[], paidTerms: PaidTermId) {
  13. super(api)
  14. this.accounts = accounts
  15. this.paidTerms = paidTerms
  16. this.debug = extendDebug('fixture:BuyMembershipHappyCaseFixture')
  17. }
  18. public getCreatedMembers(): MemberId[] {
  19. return this.memberIds.slice()
  20. }
  21. async execute(): Promise<void> {
  22. // Fee estimation and transfer
  23. const membershipFee: BN = await this.api.getMembershipFee(this.paidTerms)
  24. const membershipTransactionFee: BN = this.api.estimateBuyMembershipFee(
  25. this.accounts[0],
  26. this.paidTerms,
  27. 'member_name_which_is_longer_than_expected'
  28. )
  29. this.api.treasuryTransferBalanceToAccounts(this.accounts, membershipTransactionFee.add(new BN(membershipFee)))
  30. // Note: Member alias is dervied from the account so if it is not unique the member registration
  31. // will fail with HandleAlreadyRegistered error
  32. this.memberIds = (
  33. await Promise.all(
  34. this.accounts.map((account) =>
  35. this.api.buyMembership(account, this.paidTerms, `member${account.substring(0, 14)}`)
  36. )
  37. )
  38. )
  39. .map((r) => this.api.findEvent(r, 'members', 'MemberRegistered')?.data[0])
  40. .filter((id) => id !== undefined) as MemberId[]
  41. this.debug(`Registered ${this.memberIds.length} new members`)
  42. assert.equal(this.memberIds.length, this.accounts.length)
  43. // log the member id and corresponding key id
  44. }
  45. }
  46. export class BuyMembershipWithInsufficienFundsFixture extends BaseFixture {
  47. private account: string
  48. private paidTerms: PaidTermId
  49. public constructor(api: Api, account: string, paidTerms: PaidTermId) {
  50. super(api)
  51. this.account = account
  52. this.paidTerms = paidTerms
  53. }
  54. async execute(): Promise<void> {
  55. let memberId
  56. try {
  57. memberId = await this.api.getMemberId(this.account)
  58. } catch (err) {
  59. // member id not found
  60. }
  61. if (memberId) {
  62. throw new Error('Account must not be associated with a member')
  63. }
  64. // Fee estimation and transfer
  65. const membershipFee: BN = await this.api.getMembershipFee(this.paidTerms)
  66. const membershipTransactionFee: BN = this.api.estimateBuyMembershipFee(
  67. this.account,
  68. this.paidTerms,
  69. 'member_name_which_is_longer_than_expected'
  70. )
  71. // Only provide enough funds for transaction fee but not enough to cover the membership fee
  72. await this.api.treasuryTransferBalance(this.account, membershipTransactionFee)
  73. const balance = await this.api.getBalance(this.account)
  74. assert(
  75. balance.toBn() < membershipFee.add(membershipTransactionFee),
  76. 'Account already has sufficient balance to purchase membership'
  77. )
  78. this.expectDispatchError(
  79. await this.api.buyMembership(this.account, this.paidTerms, `late_member_${this.account.substring(0, 8)}`),
  80. 'Buying membership with insufficient funds should fail.'
  81. )
  82. }
  83. }