functions.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { MemberId, Membership } from "@joystream/types/members";
  2. import { ApiPromise } from "@polkadot/api";
  3. import { Vec } from "@polkadot/types";
  4. import { AccountId } from "@polkadot/types/interfaces";
  5. import { Hash } from "@polkadot/types/interfaces";
  6. import { Participant } from './interfaces';
  7. export async function getParticipant(api: ApiPromise, accountId: AccountId): Promise<Participant> {
  8. let memberId = -1
  9. const isMemberRoot = await api.query.members.memberIdsByRootAccountId(accountId) as Vec<MemberId>
  10. const isMemberCtrl = await api.query.members.memberIdsByControllerAccountId(accountId) as Vec<MemberId>
  11. if (isMemberRoot[0] === isMemberCtrl[0] && isMemberRoot.length == 1 && isMemberCtrl.length == 1) {
  12. console.log("true")
  13. memberId = isMemberRoot[0].toNumber()
  14. const handle = (await api.query.members.membershipById(isMemberRoot[0]) as Membership).handle.toString()
  15. const partipant: Participant = {
  16. memberId,
  17. handle,
  18. accountId:accountId.toString(),
  19. }
  20. return partipant
  21. } else {
  22. const memberIds: number[] = []
  23. const handle: string[] = []
  24. for (let ids of (isMemberRoot && isMemberCtrl)) {
  25. if (!memberIds.includes(ids.toNumber())) {
  26. memberIds.push(ids.toNumber())
  27. handle.push((await api.query.members.membershipById(ids) as Membership).handle.toString())
  28. }
  29. }
  30. if (memberIds.length === 1) {
  31. const partipant: Participant = {
  32. memberId: memberIds[0],
  33. handle,
  34. accountId:accountId.toString(),
  35. }
  36. return partipant
  37. } else {
  38. const partipant: Participant = {
  39. memberId: memberIds,
  40. handle,
  41. accountId:accountId.toString(),
  42. }
  43. return partipant
  44. }
  45. }
  46. }
  47. export async function getParticipantAt(api: ApiPromise, accountId: AccountId, blockHash: Hash): Promise<Participant> {
  48. let memberId = -1
  49. const isMemberRoot = await api.query.members.memberIdsByRootAccountId.at(blockHash,accountId) as Vec<MemberId>
  50. const isMemberCtrl = await api.query.members.memberIdsByControllerAccountId.at(blockHash,accountId) as Vec<MemberId>
  51. if (isMemberRoot[0].toNumber() === isMemberCtrl[0].toNumber() && isMemberRoot.length == 1 && isMemberCtrl.length == 1) {
  52. memberId = isMemberRoot[0].toNumber()
  53. const handle = (await api.query.members.membershipById.at(blockHash,isMemberRoot[0]) as Membership).handle.toString()
  54. const partipant: Participant = {
  55. memberId,
  56. handle,
  57. accountId:accountId.toString(),
  58. }
  59. return partipant
  60. } else {
  61. const memberIds: number[] = []
  62. const handle: string[] = []
  63. for (let ids of (isMemberRoot && isMemberCtrl)) {
  64. if (!memberIds.includes(ids.toNumber())) {
  65. memberIds.push(ids.toNumber())
  66. handle.push((await api.query.members.membershipById.at(blockHash,ids) as Membership).handle.toString())
  67. }
  68. }
  69. if (memberIds.length === 1) {
  70. const partipant: Participant = {
  71. memberId: memberIds[0],
  72. handle : handle[0],
  73. accountId:accountId.toString(),
  74. }
  75. return partipant
  76. } else {
  77. const partipant: Participant = {
  78. memberId: memberIds,
  79. handle,
  80. accountId:accountId.toString(),
  81. }
  82. return partipant
  83. }
  84. }
  85. }