membershipModule.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Api } from '../Api'
  2. import BN from 'bn.js'
  3. import { assert } from 'chai'
  4. import { Fixture, BaseFixture } from '../Fixture'
  5. import { PaidTermId, MemberId } from '@joystream/types/members'
  6. import Debugger from 'debug'
  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 = Debugger('fixture:BuyMembershipHappyCaseFixture')
  17. }
  18. public getCreatedMembers(): MemberId[] {
  19. return this.memberIds.slice()
  20. }
  21. public async execute(expectFailure: boolean): Promise<void> {
  22. this.debug(`Registering ${this.accounts.length} new members`)
  23. // Fee estimation and transfer
  24. const membershipFee: BN = await this.api.getMembershipFee(this.paidTerms)
  25. const membershipTransactionFee: BN = this.api.estimateBuyMembershipFee(
  26. this.accounts[0],
  27. this.paidTerms,
  28. 'member_name_which_is_longer_than_expected'
  29. )
  30. this.api.treasuryTransferBalanceToAccounts(this.accounts, membershipTransactionFee.add(new BN(membershipFee)))
  31. this.memberIds = (
  32. await Promise.all(
  33. this.accounts.map((account) =>
  34. this.api.buyMembership(account, this.paidTerms, `member${account.substring(0, 14)}`)
  35. )
  36. )
  37. ).map(({ events }) => this.api.expectMemberRegisteredEvent(events))
  38. this.debug(`New member ids: ${this.memberIds}`)
  39. if (expectFailure) {
  40. throw new Error('Successful fixture run while expecting failure')
  41. }
  42. }
  43. }
  44. export class BuyMembershipWithInsufficienFundsFixture implements Fixture {
  45. private api: Api
  46. private account: string
  47. private paidTerms: PaidTermId
  48. public constructor(api: Api, account: string, paidTerms: PaidTermId) {
  49. this.api = api
  50. this.account = account
  51. this.paidTerms = paidTerms
  52. }
  53. public async runner(expectFailure: boolean) {
  54. // Assertions
  55. this.api.getMemberIds(this.account).then((membership) => assert(membership.length === 0, 'Account A is a member'))
  56. // Fee estimation and transfer
  57. const membershipFee: BN = await this.api.getMembershipFee(this.paidTerms)
  58. const membershipTransactionFee: BN = this.api.estimateBuyMembershipFee(
  59. this.account,
  60. this.paidTerms,
  61. 'member_name_which_is_longer_than_expected'
  62. )
  63. this.api.treasuryTransferBalance(this.account, membershipTransactionFee)
  64. // Balance assertion
  65. await this.api
  66. .getBalance(this.account)
  67. .then((balance) =>
  68. assert(
  69. balance.toBn() < membershipFee.add(membershipTransactionFee),
  70. 'Account A already have sufficient balance to purchase membership'
  71. )
  72. )
  73. // Buying memebership
  74. await this.api.buyMembership(this.account, this.paidTerms, `late_member_${this.account.substring(0, 8)}`, true)
  75. // Assertions
  76. this.api.getMemberIds(this.account).then((membership) => assert(membership.length === 0, 'Account A is a member'))
  77. if (expectFailure) {
  78. throw new Error('Successful fixture run while expecting failure')
  79. }
  80. }
  81. }