storage.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { ApiPromise } from "@polkadot/api";
  2. import type { ITuple } from "@polkadot/types/types";
  3. import {
  4. Bag,
  5. BagId,
  6. Cid,
  7. DynamicBagType,
  8. DynamicBagCreationPolicy,
  9. DataObject,
  10. DataObjectId,
  11. StorageBucket,
  12. StorageBucketId,
  13. DistributionBucket,
  14. DistributionBucketIndex,
  15. DistributionBucketFamily,
  16. DistributionBucketFamilyId,
  17. } from "@joystream/types/storage";
  18. import { Hash } from "@polkadot/types/interfaces";
  19. export const uploadingBlocked = (api: ApiPromise): Promise<boolean> =>
  20. api.query.storage.uploadingBlocked() as any;
  21. export const getBlacklist = (api: ApiPromise): Promise<Cid[]> =>
  22. api.query.storage.blacklist() as any;
  23. export const getBlacklistSize = (api: ApiPromise): Promise<number> =>
  24. api.query.storage.currentBlacklistSize() as any;
  25. export const maxStorageBucketsPerBag = (api: ApiPromise): Promise<number> =>
  26. api.query.storage.storageBucketsPerBagLimit() as any;
  27. export const maxDistributionBucketsPerBag = (
  28. api: ApiPromise
  29. ): Promise<number> => api.query.storage.distributionBucketsPerBagLimit() as any;
  30. export const maxStorageBucketObjects = (api: ApiPromise): Promise<number> =>
  31. api.query.storage.voucherMaxObjectsNumberLimit() as any;
  32. export const maxStorageBucketSize = (api: ApiPromise): Promise<number> =>
  33. api.query.storage.voucherMaxObjectsSizeLimit() as any;
  34. export const feePerMegabyte = (api: ApiPromise): Promise<number> =>
  35. api.query.storage.dataObjectPerMegabyteFee() as any;
  36. export const getDynamicBagPolicy = (
  37. api: ApiPromise,
  38. type?: DynamicBagType | "Member" | "Channel" | number
  39. ): Promise<
  40. DynamicBagCreationPolicy | Map<DynamicBagType, DynamicBagCreationPolicy>
  41. > =>
  42. type
  43. ? (api.query.storage.dynamicBagCreationPolicies(type) as any)
  44. : api.query.storage.dynamicBagCreationPolicies.entries();
  45. export const getNextDataObject = (api: ApiPromise): Promise<DataObjectId> =>
  46. api.query.storage.nextDataObjectId() as Promise<DataObjectId>;
  47. export const getNextStorageBucket = (
  48. api: ApiPromise
  49. ): Promise<StorageBucketId> =>
  50. api.query.storage.nextStorageBucketId() as Promise<StorageBucketId>;
  51. export const getNextDistributionFamily = (
  52. api: ApiPromise
  53. ): Promise<DistributionBucketFamilyId> =>
  54. api.query.storage.nextDistributionBucketFamilyId() as Promise<DistributionBucketFamilyId>;
  55. // video + cover are contained in one bag
  56. export const getBag = (
  57. api: ApiPromise,
  58. bagId: BagId | { Static: any } | { Dynamic: any } | string | number
  59. ): Promise<Bag> => api.query.storage.bags(bagId) as any;
  60. export const getBags = async (api: ApiPromise): Promise<Map<BagId, Bag>> =>
  61. api.query.storage.bags.entries() as any;
  62. export const getObject = (
  63. api: ApiPromise,
  64. bagId: BagId | { Static: any } | { Dynamic: any } | string | number,
  65. objectId: DataObjectId | number
  66. ): Promise<Map<DataObjectId, DataObject>> =>
  67. api.query.storage.dataObjectsById(bagId, objectId) as any;
  68. export const getBagObjects = (
  69. api: ApiPromise,
  70. bagId: BagId | { Static: any } | { Dynamic: any } | string | number
  71. ): Promise<Map<[BagId, DataObjectId], DataObject>> =>
  72. api.query.storage.dataObjectsById.entries(bagId) as any;
  73. // storage
  74. export const getStorageBucket = (
  75. api: ApiPromise,
  76. bucketId: StorageBucketId | Hash,
  77. hash?: Hash
  78. ): Promise<StorageBucket> =>
  79. (hash
  80. ? api.query.storage.storageBucketById.at(hash, bucketId)
  81. : api.query.storage.storageBucketById(bucketId)) as Promise<StorageBucket>;
  82. export const getStorageBuckets = (
  83. api: ApiPromise
  84. ): Promise<Map<StorageBucketId, StorageBucket>> =>
  85. api.query.storage.storageBucketById.entries() as any;
  86. // distribution
  87. export const getDistributionFamilyNumber = (api: ApiPromise): Promise<number> =>
  88. api.query.storage.distributionBucketFamilyNumber() as any;
  89. export const getDistributionFamilyBuckets = (
  90. api: ApiPromise,
  91. familyId: DistributionBucketFamilyId | number
  92. ): Promise<Map<DistributionBucketIndex, DistributionBucket>> =>
  93. api.query.storage.distributionBucketByFamilyIdById.entries(familyId) as any;
  94. export const getDistributionFamilyBucket = (
  95. api: ApiPromise,
  96. familyId: DistributionBucketFamilyId | number,
  97. bucketIndex: DistributionBucketIndex | number
  98. ): Promise<DistributionBucket> =>
  99. api.query.storage.distributionBucketByFamilyIdById(
  100. familyId,
  101. bucketIndex
  102. ) as Promise<DistributionBucket>;
  103. export const getDistributionFamilies = (
  104. api: ApiPromise
  105. ): Promise<Map<DistributionBucketFamilyId, DistributionBucketFamily>> =>
  106. api.query.storage.distributionBucketFamilyById.entries() as any;
  107. export const getDistributionFamily = async (
  108. api: ApiPromise,
  109. familyId: DistributionBucketFamilyId | number
  110. ): Promise<DistributionBucketFamily> =>
  111. (await api.query.storage.distributionBucketFamilyById(
  112. familyId
  113. )) as DistributionBucketFamily;